(redisplay_window): Check return value of
[emacs.git] / src / print.c
bloba474fa31e818b1ea0c4a00a217309e84a3239b03
1 /* Lisp object printing and output streams.
2 Copyright (C) 1985, 1986, 1988, 1993, 1994, 1995, 1997,
3 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
6 This file is part of GNU Emacs.
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, or (at your option)
11 any later version.
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.
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs; see the file COPYING. If not, write to
20 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
24 #include <config.h>
25 #include <stdio.h>
26 #include "lisp.h"
27 #include "buffer.h"
28 #include "charset.h"
29 #include "keyboard.h"
30 #include "frame.h"
31 #include "window.h"
32 #include "process.h"
33 #include "dispextern.h"
34 #include "termchar.h"
35 #include "intervals.h"
36 #include "blockinput.h"
38 Lisp_Object Vstandard_output, Qstandard_output;
40 Lisp_Object Qtemp_buffer_setup_hook;
42 /* These are used to print like we read. */
43 extern Lisp_Object Qbackquote, Qcomma, Qcomma_at, Qcomma_dot, Qfunction;
45 Lisp_Object Vfloat_output_format, Qfloat_output_format;
47 /* Work around a problem that happens because math.h on hpux 7
48 defines two static variables--which, in Emacs, are not really static,
49 because `static' is defined as nothing. The problem is that they are
50 defined both here and in lread.c.
51 These macros prevent the name conflict. */
52 #if defined (HPUX) && !defined (HPUX8)
53 #define _MAXLDBL print_maxldbl
54 #define _NMAXLDBL print_nmaxldbl
55 #endif
57 #include <math.h>
59 #if STDC_HEADERS
60 #include <float.h>
61 #endif
63 /* Default to values appropriate for IEEE floating point. */
64 #ifndef FLT_RADIX
65 #define FLT_RADIX 2
66 #endif
67 #ifndef DBL_MANT_DIG
68 #define DBL_MANT_DIG 53
69 #endif
70 #ifndef DBL_DIG
71 #define DBL_DIG 15
72 #endif
73 #ifndef DBL_MIN
74 #define DBL_MIN 2.2250738585072014e-308
75 #endif
77 #ifdef DBL_MIN_REPLACEMENT
78 #undef DBL_MIN
79 #define DBL_MIN DBL_MIN_REPLACEMENT
80 #endif
82 /* Define DOUBLE_DIGITS_BOUND, an upper bound on the number of decimal digits
83 needed to express a float without losing information.
84 The general-case formula is valid for the usual case, IEEE floating point,
85 but many compilers can't optimize the formula to an integer constant,
86 so make a special case for it. */
87 #if FLT_RADIX == 2 && DBL_MANT_DIG == 53
88 #define DOUBLE_DIGITS_BOUND 17 /* IEEE floating point */
89 #else
90 #define DOUBLE_DIGITS_BOUND ((int) ceil (log10 (pow (FLT_RADIX, DBL_MANT_DIG))))
91 #endif
93 /* Avoid actual stack overflow in print. */
94 int print_depth;
96 /* Level of nesting inside outputting backquote in new style. */
97 int new_backquote_output;
99 /* Detect most circularities to print finite output. */
100 #define PRINT_CIRCLE 200
101 Lisp_Object being_printed[PRINT_CIRCLE];
103 /* When printing into a buffer, first we put the text in this
104 block, then insert it all at once. */
105 char *print_buffer;
107 /* Size allocated in print_buffer. */
108 int print_buffer_size;
109 /* Chars stored in print_buffer. */
110 int print_buffer_pos;
111 /* Bytes stored in print_buffer. */
112 int print_buffer_pos_byte;
114 /* Maximum length of list to print in full; noninteger means
115 effectively infinity */
117 Lisp_Object Vprint_length;
119 /* Maximum depth of list to print in full; noninteger means
120 effectively infinity. */
122 Lisp_Object Vprint_level;
124 /* Nonzero means print newlines in strings as \n. */
126 int print_escape_newlines;
128 /* Nonzero means to print single-byte non-ascii characters in strings as
129 octal escapes. */
131 int print_escape_nonascii;
133 /* Nonzero means to print multibyte characters in strings as hex escapes. */
135 int print_escape_multibyte;
137 Lisp_Object Qprint_escape_newlines;
138 Lisp_Object Qprint_escape_multibyte, Qprint_escape_nonascii;
140 /* Nonzero means print (quote foo) forms as 'foo, etc. */
142 int print_quoted;
144 /* Non-nil means print #: before uninterned symbols. */
146 Lisp_Object Vprint_gensym;
148 /* Non-nil means print recursive structures using #n= and #n# syntax. */
150 Lisp_Object Vprint_circle;
152 /* Non-nil means keep continuous number for #n= and #n# syntax
153 between several print functions. */
155 Lisp_Object Vprint_continuous_numbering;
157 /* Vprint_number_table is a vector like [OBJ1 STAT1 OBJ2 STAT2 ...],
158 where OBJn are objects going to be printed, and STATn are their status,
159 which may be different meanings during process. See the comments of
160 the functions print and print_preprocess for details.
161 print_number_index keeps the last position the next object should be added,
162 twice of which is the actual vector position in Vprint_number_table. */
163 int print_number_index;
164 Lisp_Object Vprint_number_table;
166 /* PRINT_NUMBER_OBJECT returns the I'th object in Vprint_number_table TABLE.
167 PRINT_NUMBER_STATUS returns the status of the I'th object in TABLE.
168 See the comment of the variable Vprint_number_table. */
169 #define PRINT_NUMBER_OBJECT(table,i) XVECTOR ((table))->contents[(i) * 2]
170 #define PRINT_NUMBER_STATUS(table,i) XVECTOR ((table))->contents[(i) * 2 + 1]
172 /* Nonzero means print newline to stdout before next minibuffer message.
173 Defined in xdisp.c */
175 extern int noninteractive_need_newline;
177 extern int minibuffer_auto_raise;
179 #ifdef MAX_PRINT_CHARS
180 static int print_chars;
181 static int max_print;
182 #endif /* MAX_PRINT_CHARS */
184 void print_interval ();
186 /* GDB resets this to zero on W32 to disable OutputDebugString calls. */
187 int print_output_debug_flag = 1;
190 /* Low level output routines for characters and strings */
192 /* Lisp functions to do output using a stream
193 must have the stream in a variable called printcharfun
194 and must start with PRINTPREPARE, end with PRINTFINISH,
195 and use PRINTDECLARE to declare common variables.
196 Use PRINTCHAR to output one character,
197 or call strout to output a block of characters. */
199 #define PRINTDECLARE \
200 struct buffer *old = current_buffer; \
201 int old_point = -1, start_point = -1; \
202 int old_point_byte = -1, start_point_byte = -1; \
203 int specpdl_count = SPECPDL_INDEX (); \
204 int free_print_buffer = 0; \
205 int multibyte = !NILP (current_buffer->enable_multibyte_characters); \
206 Lisp_Object original
208 #define PRINTPREPARE \
209 original = printcharfun; \
210 if (NILP (printcharfun)) printcharfun = Qt; \
211 if (BUFFERP (printcharfun)) \
213 if (XBUFFER (printcharfun) != current_buffer) \
214 Fset_buffer (printcharfun); \
215 printcharfun = Qnil; \
217 if (MARKERP (printcharfun)) \
219 EMACS_INT marker_pos; \
220 if (! XMARKER (printcharfun)->buffer) \
221 error ("Marker does not point anywhere"); \
222 if (XMARKER (printcharfun)->buffer != current_buffer) \
223 set_buffer_internal (XMARKER (printcharfun)->buffer); \
224 marker_pos = marker_position (printcharfun); \
225 if (marker_pos < BEGV || marker_pos > ZV) \
226 error ("Marker is outside the accessible part of the buffer"); \
227 old_point = PT; \
228 old_point_byte = PT_BYTE; \
229 SET_PT_BOTH (marker_pos, \
230 marker_byte_position (printcharfun)); \
231 start_point = PT; \
232 start_point_byte = PT_BYTE; \
233 printcharfun = Qnil; \
235 if (NILP (printcharfun)) \
237 Lisp_Object string; \
238 if (NILP (current_buffer->enable_multibyte_characters) \
239 && ! print_escape_multibyte) \
240 specbind (Qprint_escape_multibyte, Qt); \
241 if (! NILP (current_buffer->enable_multibyte_characters) \
242 && ! print_escape_nonascii) \
243 specbind (Qprint_escape_nonascii, Qt); \
244 if (print_buffer != 0) \
246 string = make_string_from_bytes (print_buffer, \
247 print_buffer_pos, \
248 print_buffer_pos_byte); \
249 record_unwind_protect (print_unwind, string); \
251 else \
253 print_buffer_size = 1000; \
254 print_buffer = (char *) xmalloc (print_buffer_size); \
255 free_print_buffer = 1; \
257 print_buffer_pos = 0; \
258 print_buffer_pos_byte = 0; \
260 if (EQ (printcharfun, Qt) && ! noninteractive) \
261 setup_echo_area_for_printing (multibyte);
263 #define PRINTFINISH \
264 if (NILP (printcharfun)) \
266 if (print_buffer_pos != print_buffer_pos_byte \
267 && NILP (current_buffer->enable_multibyte_characters)) \
269 unsigned char *temp \
270 = (unsigned char *) alloca (print_buffer_pos + 1); \
271 copy_text (print_buffer, temp, print_buffer_pos_byte, \
272 1, 0); \
273 insert_1_both (temp, print_buffer_pos, \
274 print_buffer_pos, 0, 1, 0); \
276 else \
277 insert_1_both (print_buffer, print_buffer_pos, \
278 print_buffer_pos_byte, 0, 1, 0); \
279 signal_after_change (PT - print_buffer_pos, 0, print_buffer_pos);\
281 if (free_print_buffer) \
283 xfree (print_buffer); \
284 print_buffer = 0; \
286 unbind_to (specpdl_count, Qnil); \
287 if (MARKERP (original)) \
288 set_marker_both (original, Qnil, PT, PT_BYTE); \
289 if (old_point >= 0) \
290 SET_PT_BOTH (old_point + (old_point >= start_point \
291 ? PT - start_point : 0), \
292 old_point_byte + (old_point_byte >= start_point_byte \
293 ? PT_BYTE - start_point_byte : 0)); \
294 if (old != current_buffer) \
295 set_buffer_internal (old);
297 #define PRINTCHAR(ch) printchar (ch, printcharfun)
299 /* This is used to restore the saved contents of print_buffer
300 when there is a recursive call to print. */
302 static Lisp_Object
303 print_unwind (saved_text)
304 Lisp_Object saved_text;
306 bcopy (SDATA (saved_text), print_buffer, SCHARS (saved_text));
307 return Qnil;
311 /* Print character CH using method FUN. FUN nil means print to
312 print_buffer. FUN t means print to echo area or stdout if
313 non-interactive. If FUN is neither nil nor t, call FUN with CH as
314 argument. */
316 static void
317 printchar (ch, fun)
318 unsigned int ch;
319 Lisp_Object fun;
321 #ifdef MAX_PRINT_CHARS
322 if (max_print)
323 print_chars++;
324 #endif /* MAX_PRINT_CHARS */
326 if (!NILP (fun) && !EQ (fun, Qt))
327 call1 (fun, make_number (ch));
328 else
330 unsigned char str[MAX_MULTIBYTE_LENGTH];
331 int len = CHAR_STRING (ch, str);
333 QUIT;
335 if (NILP (fun))
337 if (print_buffer_pos_byte + len >= print_buffer_size)
338 print_buffer = (char *) xrealloc (print_buffer,
339 print_buffer_size *= 2);
340 bcopy (str, print_buffer + print_buffer_pos_byte, len);
341 print_buffer_pos += 1;
342 print_buffer_pos_byte += len;
344 else if (noninteractive)
346 fwrite (str, 1, len, stdout);
347 noninteractive_need_newline = 1;
349 else
351 int multibyte_p
352 = !NILP (current_buffer->enable_multibyte_characters);
354 setup_echo_area_for_printing (multibyte_p);
355 insert_char (ch);
356 message_dolog (str, len, 0, multibyte_p);
362 /* Output SIZE characters, SIZE_BYTE bytes from string PTR using
363 method PRINTCHARFUN. If SIZE < 0, use the string length of PTR for
364 both SIZE and SIZE_BYTE. PRINTCHARFUN nil means output to
365 print_buffer. PRINTCHARFUN t means output to the echo area or to
366 stdout if non-interactive. If neither nil nor t, call Lisp
367 function PRINTCHARFUN for each character printed. MULTIBYTE
368 non-zero means PTR contains multibyte characters.
370 In the case where PRINTCHARFUN is nil, it is safe for PTR to point
371 to data in a Lisp string. Otherwise that is not safe. */
373 static void
374 strout (ptr, size, size_byte, printcharfun, multibyte)
375 char *ptr;
376 int size, size_byte;
377 Lisp_Object printcharfun;
378 int multibyte;
380 if (size < 0)
381 size_byte = size = strlen (ptr);
383 if (NILP (printcharfun))
385 if (print_buffer_pos_byte + size_byte > print_buffer_size)
387 print_buffer_size = print_buffer_size * 2 + size_byte;
388 print_buffer = (char *) xrealloc (print_buffer,
389 print_buffer_size);
391 bcopy (ptr, print_buffer + print_buffer_pos_byte, size_byte);
392 print_buffer_pos += size;
393 print_buffer_pos_byte += size_byte;
395 #ifdef MAX_PRINT_CHARS
396 if (max_print)
397 print_chars += size;
398 #endif /* MAX_PRINT_CHARS */
400 else if (noninteractive && EQ (printcharfun, Qt))
402 fwrite (ptr, 1, size_byte, stdout);
403 noninteractive_need_newline = 1;
405 else if (EQ (printcharfun, Qt))
407 /* Output to echo area. We're trying to avoid a little overhead
408 here, that's the reason we don't call printchar to do the
409 job. */
410 int i;
411 int multibyte_p
412 = !NILP (current_buffer->enable_multibyte_characters);
414 setup_echo_area_for_printing (multibyte_p);
415 message_dolog (ptr, size_byte, 0, multibyte_p);
417 if (size == size_byte)
419 for (i = 0; i < size; ++i)
420 insert_char ((unsigned char) *ptr++);
422 else
424 int len;
425 for (i = 0; i < size_byte; i += len)
427 int ch = STRING_CHAR_AND_LENGTH (ptr + i, size_byte - i, len);
428 insert_char (ch);
432 #ifdef MAX_PRINT_CHARS
433 if (max_print)
434 print_chars += size;
435 #endif /* MAX_PRINT_CHARS */
437 else
439 /* PRINTCHARFUN is a Lisp function. */
440 int i = 0;
442 if (size == size_byte)
444 while (i < size_byte)
446 int ch = ptr[i++];
447 PRINTCHAR (ch);
450 else
452 while (i < size_byte)
454 /* Here, we must convert each multi-byte form to the
455 corresponding character code before handing it to
456 PRINTCHAR. */
457 int len;
458 int ch = STRING_CHAR_AND_LENGTH (ptr + i, size_byte - i, len);
459 PRINTCHAR (ch);
460 i += len;
466 /* Print the contents of a string STRING using PRINTCHARFUN.
467 It isn't safe to use strout in many cases,
468 because printing one char can relocate. */
470 static void
471 print_string (string, printcharfun)
472 Lisp_Object string;
473 Lisp_Object printcharfun;
475 if (EQ (printcharfun, Qt) || NILP (printcharfun))
477 int chars;
479 if (STRING_MULTIBYTE (string))
480 chars = SCHARS (string);
481 else if (EQ (printcharfun, Qt)
482 ? ! NILP (buffer_defaults.enable_multibyte_characters)
483 : ! NILP (current_buffer->enable_multibyte_characters))
485 /* If unibyte string STRING contains 8-bit codes, we must
486 convert STRING to a multibyte string containing the same
487 character codes. */
488 Lisp_Object newstr;
489 int bytes;
491 chars = SBYTES (string);
492 bytes = parse_str_to_multibyte (SDATA (string), chars);
493 if (chars < bytes)
495 newstr = make_uninit_multibyte_string (chars, bytes);
496 bcopy (SDATA (string), SDATA (newstr), chars);
497 str_to_multibyte (SDATA (newstr), bytes, chars);
498 string = newstr;
501 else
502 chars = SBYTES (string);
504 if (EQ (printcharfun, Qt))
506 /* Output to echo area. */
507 int nbytes = SBYTES (string);
508 char *buffer;
510 /* Copy the string contents so that relocation of STRING by
511 GC does not cause trouble. */
512 USE_SAFE_ALLOCA;
514 SAFE_ALLOCA (buffer, char *, nbytes);
515 bcopy (SDATA (string), buffer, nbytes);
517 strout (buffer, chars, SBYTES (string),
518 printcharfun, STRING_MULTIBYTE (string));
520 SAFE_FREE ();
522 else
523 /* No need to copy, since output to print_buffer can't GC. */
524 strout (SDATA (string),
525 chars, SBYTES (string),
526 printcharfun, STRING_MULTIBYTE (string));
528 else
530 /* Otherwise, string may be relocated by printing one char.
531 So re-fetch the string address for each character. */
532 int i;
533 int size = SCHARS (string);
534 int size_byte = SBYTES (string);
535 struct gcpro gcpro1;
536 GCPRO1 (string);
537 if (size == size_byte)
538 for (i = 0; i < size; i++)
539 PRINTCHAR (SREF (string, i));
540 else
541 for (i = 0; i < size_byte; )
543 /* Here, we must convert each multi-byte form to the
544 corresponding character code before handing it to PRINTCHAR. */
545 int len;
546 int ch = STRING_CHAR_AND_LENGTH (SDATA (string) + i,
547 size_byte - i, len);
548 if (!CHAR_VALID_P (ch, 0))
550 ch = SREF (string, i);
551 len = 1;
553 PRINTCHAR (ch);
554 i += len;
556 UNGCPRO;
560 DEFUN ("write-char", Fwrite_char, Swrite_char, 1, 2, 0,
561 doc: /* Output character CHARACTER to stream PRINTCHARFUN.
562 PRINTCHARFUN defaults to the value of `standard-output' (which see). */)
563 (character, printcharfun)
564 Lisp_Object character, printcharfun;
566 PRINTDECLARE;
568 if (NILP (printcharfun))
569 printcharfun = Vstandard_output;
570 CHECK_NUMBER (character);
571 PRINTPREPARE;
572 PRINTCHAR (XINT (character));
573 PRINTFINISH;
574 return character;
577 /* Used from outside of print.c to print a block of SIZE
578 single-byte chars at DATA on the default output stream.
579 Do not use this on the contents of a Lisp string. */
581 void
582 write_string (data, size)
583 char *data;
584 int size;
586 PRINTDECLARE;
587 Lisp_Object printcharfun;
589 printcharfun = Vstandard_output;
591 PRINTPREPARE;
592 strout (data, size, size, printcharfun, 0);
593 PRINTFINISH;
596 /* Used from outside of print.c to print a block of SIZE
597 single-byte chars at DATA on a specified stream PRINTCHARFUN.
598 Do not use this on the contents of a Lisp string. */
600 void
601 write_string_1 (data, size, printcharfun)
602 char *data;
603 int size;
604 Lisp_Object printcharfun;
606 PRINTDECLARE;
608 PRINTPREPARE;
609 strout (data, size, size, printcharfun, 0);
610 PRINTFINISH;
614 void
615 temp_output_buffer_setup (bufname)
616 const char *bufname;
618 int count = SPECPDL_INDEX ();
619 register struct buffer *old = current_buffer;
620 register Lisp_Object buf;
622 record_unwind_protect (set_buffer_if_live, Fcurrent_buffer ());
624 Fset_buffer (Fget_buffer_create (build_string (bufname)));
626 Fkill_all_local_variables ();
627 delete_all_overlays (current_buffer);
628 current_buffer->directory = old->directory;
629 current_buffer->read_only = Qnil;
630 current_buffer->filename = Qnil;
631 current_buffer->undo_list = Qt;
632 eassert (current_buffer->overlays_before == NULL);
633 eassert (current_buffer->overlays_after == NULL);
634 current_buffer->enable_multibyte_characters
635 = buffer_defaults.enable_multibyte_characters;
636 specbind (Qinhibit_read_only, Qt);
637 specbind (Qinhibit_modification_hooks, Qt);
638 Ferase_buffer ();
639 XSETBUFFER (buf, current_buffer);
641 Frun_hooks (1, &Qtemp_buffer_setup_hook);
643 unbind_to (count, Qnil);
645 specbind (Qstandard_output, buf);
648 Lisp_Object
649 internal_with_output_to_temp_buffer (bufname, function, args)
650 const char *bufname;
651 Lisp_Object (*function) P_ ((Lisp_Object));
652 Lisp_Object args;
654 int count = SPECPDL_INDEX ();
655 Lisp_Object buf, val;
656 struct gcpro gcpro1;
658 GCPRO1 (args);
659 record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
660 temp_output_buffer_setup (bufname);
661 buf = Vstandard_output;
662 UNGCPRO;
664 val = (*function) (args);
666 GCPRO1 (val);
667 temp_output_buffer_show (buf);
668 UNGCPRO;
670 return unbind_to (count, val);
673 DEFUN ("with-output-to-temp-buffer",
674 Fwith_output_to_temp_buffer, Swith_output_to_temp_buffer,
675 1, UNEVALLED, 0,
676 doc: /* Bind `standard-output' to buffer BUFNAME, eval BODY, then show that buffer.
678 This construct makes buffer BUFNAME empty before running BODY.
679 It does not make the buffer current for BODY.
680 Instead it binds `standard-output' to that buffer, so that output
681 generated with `prin1' and similar functions in BODY goes into
682 the buffer.
684 At the end of BODY, this marks buffer BUFNAME unmodifed and displays
685 it in a window, but does not select it. The normal way to do this is
686 by calling `display-buffer', then running `temp-buffer-show-hook'.
687 However, if `temp-buffer-show-function' is non-nil, it calls that
688 function instead (and does not run `temp-buffer-show-hook'). The
689 function gets one argument, the buffer to display.
691 The return value of `with-output-to-temp-buffer' is the value of the
692 last form in BODY. If BODY does not finish normally, the buffer
693 BUFNAME is not displayed.
695 This runs the hook `temp-buffer-setup-hook' before BODY,
696 with the buffer BUFNAME temporarily current. It runs the hook
697 `temp-buffer-show-hook' after displaying buffer BUFNAME, with that
698 buffer temporarily current, and the window that was used to display it
699 temporarily selected. But it doesn't run `temp-buffer-show-hook'
700 if it uses `temp-buffer-show-function'.
702 usage: (with-output-to-temp-buffer BUFNAME BODY...) */)
703 (args)
704 Lisp_Object args;
706 struct gcpro gcpro1;
707 Lisp_Object name;
708 int count = SPECPDL_INDEX ();
709 Lisp_Object buf, val;
711 GCPRO1(args);
712 name = Feval (Fcar (args));
713 CHECK_STRING (name);
714 temp_output_buffer_setup (SDATA (name));
715 buf = Vstandard_output;
716 UNGCPRO;
718 val = Fprogn (XCDR (args));
720 GCPRO1 (val);
721 temp_output_buffer_show (buf);
722 UNGCPRO;
724 return unbind_to (count, val);
728 static void print ();
729 static void print_preprocess ();
730 static void print_preprocess_string ();
731 static void print_object ();
733 DEFUN ("terpri", Fterpri, Sterpri, 0, 1, 0,
734 doc: /* Output a newline to stream PRINTCHARFUN.
735 If PRINTCHARFUN is omitted or nil, the value of `standard-output' is used. */)
736 (printcharfun)
737 Lisp_Object printcharfun;
739 PRINTDECLARE;
741 if (NILP (printcharfun))
742 printcharfun = Vstandard_output;
743 PRINTPREPARE;
744 PRINTCHAR ('\n');
745 PRINTFINISH;
746 return Qt;
749 DEFUN ("prin1", Fprin1, Sprin1, 1, 2, 0,
750 doc: /* Output the printed representation of OBJECT, any Lisp object.
751 Quoting characters are printed when needed to make output that `read'
752 can handle, whenever this is possible. For complex objects, the behavior
753 is controlled by `print-level' and `print-length', which see.
755 OBJECT is any of the Lisp data types: a number, a string, a symbol,
756 a list, a buffer, a window, a frame, etc.
758 A printed representation of an object is text which describes that object.
760 Optional argument PRINTCHARFUN is the output stream, which can be one
761 of these:
763 - a buffer, in which case output is inserted into that buffer at point;
764 - a marker, in which case output is inserted at marker's position;
765 - a function, in which case that function is called once for each
766 character of OBJECT's printed representation;
767 - a symbol, in which case that symbol's function definition is called; or
768 - t, in which case the output is displayed in the echo area.
770 If PRINTCHARFUN is omitted, the value of `standard-output' (which see)
771 is used instead. */)
772 (object, printcharfun)
773 Lisp_Object object, printcharfun;
775 PRINTDECLARE;
777 #ifdef MAX_PRINT_CHARS
778 max_print = 0;
779 #endif /* MAX_PRINT_CHARS */
780 if (NILP (printcharfun))
781 printcharfun = Vstandard_output;
782 PRINTPREPARE;
783 print (object, printcharfun, 1);
784 PRINTFINISH;
785 return object;
788 /* a buffer which is used to hold output being built by prin1-to-string */
789 Lisp_Object Vprin1_to_string_buffer;
791 DEFUN ("prin1-to-string", Fprin1_to_string, Sprin1_to_string, 1, 2, 0,
792 doc: /* Return a string containing the printed representation of OBJECT.
793 OBJECT can be any Lisp object. This function outputs quoting characters
794 when necessary to make output that `read' can handle, whenever possible,
795 unless the optional second argument NOESCAPE is non-nil. For complex objects,
796 the behavior is controlled by `print-level' and `print-length', which see.
798 OBJECT is any of the Lisp data types: a number, a string, a symbol,
799 a list, a buffer, a window, a frame, etc.
801 A printed representation of an object is text which describes that object. */)
802 (object, noescape)
803 Lisp_Object object, noescape;
805 Lisp_Object printcharfun;
806 /* struct gcpro gcpro1, gcpro2; */
807 Lisp_Object save_deactivate_mark;
808 int count = SPECPDL_INDEX ();
809 struct buffer *previous;
811 specbind (Qinhibit_modification_hooks, Qt);
814 PRINTDECLARE;
816 /* Save and restore this--we are altering a buffer
817 but we don't want to deactivate the mark just for that.
818 No need for specbind, since errors deactivate the mark. */
819 save_deactivate_mark = Vdeactivate_mark;
820 /* GCPRO2 (object, save_deactivate_mark); */
821 abort_on_gc++;
823 printcharfun = Vprin1_to_string_buffer;
824 PRINTPREPARE;
825 print (object, printcharfun, NILP (noescape));
826 /* Make Vprin1_to_string_buffer be the default buffer after PRINTFINSH */
827 PRINTFINISH;
830 previous = current_buffer;
831 set_buffer_internal (XBUFFER (Vprin1_to_string_buffer));
832 object = Fbuffer_string ();
833 if (SBYTES (object) == SCHARS (object))
834 STRING_SET_UNIBYTE (object);
836 /* Note that this won't make prepare_to_modify_buffer call
837 ask-user-about-supersession-threat because this buffer
838 does not visit a file. */
839 Ferase_buffer ();
840 set_buffer_internal (previous);
842 Vdeactivate_mark = save_deactivate_mark;
843 /* UNGCPRO; */
845 abort_on_gc--;
846 return unbind_to (count, object);
849 DEFUN ("princ", Fprinc, Sprinc, 1, 2, 0,
850 doc: /* Output the printed representation of OBJECT, any Lisp object.
851 No quoting characters are used; no delimiters are printed around
852 the contents of strings.
854 OBJECT is any of the Lisp data types: a number, a string, a symbol,
855 a list, a buffer, a window, a frame, etc.
857 A printed representation of an object is text which describes that object.
859 Optional argument PRINTCHARFUN is the output stream, which can be one
860 of these:
862 - a buffer, in which case output is inserted into that buffer at point;
863 - a marker, in which case output is inserted at marker's position;
864 - a function, in which case that function is called once for each
865 character of OBJECT's printed representation;
866 - a symbol, in which case that symbol's function definition is called; or
867 - t, in which case the output is displayed in the echo area.
869 If PRINTCHARFUN is omitted, the value of `standard-output' (which see)
870 is used instead. */)
871 (object, printcharfun)
872 Lisp_Object object, printcharfun;
874 PRINTDECLARE;
876 if (NILP (printcharfun))
877 printcharfun = Vstandard_output;
878 PRINTPREPARE;
879 print (object, printcharfun, 0);
880 PRINTFINISH;
881 return object;
884 DEFUN ("print", Fprint, Sprint, 1, 2, 0,
885 doc: /* Output the printed representation of OBJECT, with newlines around it.
886 Quoting characters are printed when needed to make output that `read'
887 can handle, whenever this is possible. For complex objects, the behavior
888 is controlled by `print-level' and `print-length', which see.
890 OBJECT is any of the Lisp data types: a number, a string, a symbol,
891 a list, a buffer, a window, a frame, etc.
893 A printed representation of an object is text which describes that object.
895 Optional argument PRINTCHARFUN is the output stream, which can be one
896 of these:
898 - a buffer, in which case output is inserted into that buffer at point;
899 - a marker, in which case output is inserted at marker's position;
900 - a function, in which case that function is called once for each
901 character of OBJECT's printed representation;
902 - a symbol, in which case that symbol's function definition is called; or
903 - t, in which case the output is displayed in the echo area.
905 If PRINTCHARFUN is omitted, the value of `standard-output' (which see)
906 is used instead. */)
907 (object, printcharfun)
908 Lisp_Object object, printcharfun;
910 PRINTDECLARE;
911 struct gcpro gcpro1;
913 #ifdef MAX_PRINT_CHARS
914 print_chars = 0;
915 max_print = MAX_PRINT_CHARS;
916 #endif /* MAX_PRINT_CHARS */
917 if (NILP (printcharfun))
918 printcharfun = Vstandard_output;
919 GCPRO1 (object);
920 PRINTPREPARE;
921 PRINTCHAR ('\n');
922 print (object, printcharfun, 1);
923 PRINTCHAR ('\n');
924 PRINTFINISH;
925 #ifdef MAX_PRINT_CHARS
926 max_print = 0;
927 print_chars = 0;
928 #endif /* MAX_PRINT_CHARS */
929 UNGCPRO;
930 return object;
933 /* The subroutine object for external-debugging-output is kept here
934 for the convenience of the debugger. */
935 Lisp_Object Qexternal_debugging_output;
937 DEFUN ("external-debugging-output", Fexternal_debugging_output, Sexternal_debugging_output, 1, 1, 0,
938 doc: /* Write CHARACTER to stderr.
939 You can call print while debugging emacs, and pass it this function
940 to make it write to the debugging output. */)
941 (character)
942 Lisp_Object character;
944 CHECK_NUMBER (character);
945 putc (XINT (character), stderr);
947 #ifdef WINDOWSNT
948 /* Send the output to a debugger (nothing happens if there isn't one). */
949 if (print_output_debug_flag)
951 char buf[2] = {(char) XINT (character), '\0'};
952 OutputDebugString (buf);
954 #endif
956 return character;
959 /* This function is never called. Its purpose is to prevent
960 print_output_debug_flag from being optimized away. */
962 void
963 debug_output_compilation_hack (x)
964 int x;
966 print_output_debug_flag = x;
969 #if defined (GNU_LINUX)
971 /* This functionality is not vitally important in general, so we rely on
972 non-portable ability to use stderr as lvalue. */
974 #define WITH_REDIRECT_DEBUGGING_OUTPUT 1
976 FILE *initial_stderr_stream = NULL;
978 DEFUN ("redirect-debugging-output", Fredirect_debugging_output, Sredirect_debugging_output,
979 1, 2,
980 "FDebug output file: \nP",
981 doc: /* Redirect debugging output (stderr stream) to file FILE.
982 If FILE is nil, reset target to the initial stderr stream.
983 Optional arg APPEND non-nil (interactively, with prefix arg) means
984 append to existing target file. */)
985 (file, append)
986 Lisp_Object file, append;
988 if (initial_stderr_stream != NULL)
990 BLOCK_INPUT;
991 fclose (stderr);
992 UNBLOCK_INPUT;
994 stderr = initial_stderr_stream;
995 initial_stderr_stream = NULL;
997 if (STRINGP (file))
999 file = Fexpand_file_name (file, Qnil);
1000 initial_stderr_stream = stderr;
1001 stderr = fopen (SDATA (file), NILP (append) ? "w" : "a");
1002 if (stderr == NULL)
1004 stderr = initial_stderr_stream;
1005 initial_stderr_stream = NULL;
1006 report_file_error ("Cannot open debugging output stream",
1007 Fcons (file, Qnil));
1010 return Qnil;
1012 #endif /* GNU_LINUX */
1015 /* This is the interface for debugging printing. */
1017 void
1018 debug_print (arg)
1019 Lisp_Object arg;
1021 Fprin1 (arg, Qexternal_debugging_output);
1022 fprintf (stderr, "\r\n");
1025 void
1026 safe_debug_print (arg)
1027 Lisp_Object arg;
1029 int valid = valid_lisp_object_p (arg);
1031 if (valid > 0)
1032 debug_print (arg);
1033 else
1034 fprintf (stderr, "#<%s_LISP_OBJECT 0x%08lx>\r\n",
1035 !valid ? "INVALID" : "SOME",
1036 #ifdef NO_UNION_TYPE
1037 (unsigned long) arg
1038 #else
1039 (unsigned long) arg.i
1040 #endif
1045 DEFUN ("error-message-string", Ferror_message_string, Serror_message_string,
1046 1, 1, 0,
1047 doc: /* Convert an error value (ERROR-SYMBOL . DATA) to an error message.
1048 See Info anchor `(elisp)Definition of signal' for some details on how this
1049 error message is constructed. */)
1050 (obj)
1051 Lisp_Object obj;
1053 struct buffer *old = current_buffer;
1054 Lisp_Object value;
1055 struct gcpro gcpro1;
1057 /* If OBJ is (error STRING), just return STRING.
1058 That is not only faster, it also avoids the need to allocate
1059 space here when the error is due to memory full. */
1060 if (CONSP (obj) && EQ (XCAR (obj), Qerror)
1061 && CONSP (XCDR (obj))
1062 && STRINGP (XCAR (XCDR (obj)))
1063 && NILP (XCDR (XCDR (obj))))
1064 return XCAR (XCDR (obj));
1066 print_error_message (obj, Vprin1_to_string_buffer, 0, Qnil);
1068 set_buffer_internal (XBUFFER (Vprin1_to_string_buffer));
1069 value = Fbuffer_string ();
1071 GCPRO1 (value);
1072 Ferase_buffer ();
1073 set_buffer_internal (old);
1074 UNGCPRO;
1076 return value;
1079 /* Print an error message for the error DATA onto Lisp output stream
1080 STREAM (suitable for the print functions).
1081 CONTEXT is a C string describing the context of the error.
1082 CALLER is the Lisp function inside which the error was signaled. */
1084 void
1085 print_error_message (data, stream, context, caller)
1086 Lisp_Object data, stream;
1087 char *context;
1088 Lisp_Object caller;
1090 Lisp_Object errname, errmsg, file_error, tail;
1091 struct gcpro gcpro1;
1092 int i;
1094 if (context != 0)
1095 write_string_1 (context, -1, stream);
1097 /* If we know from where the error was signaled, show it in
1098 *Messages*. */
1099 if (!NILP (caller) && SYMBOLP (caller))
1101 Lisp_Object cname = SYMBOL_NAME (caller);
1102 char *name = alloca (SBYTES (cname));
1103 bcopy (SDATA (cname), name, SBYTES (cname));
1104 message_dolog (name, SBYTES (cname), 0, 0);
1105 message_dolog (": ", 2, 0, 0);
1108 errname = Fcar (data);
1110 if (EQ (errname, Qerror))
1112 data = Fcdr (data);
1113 if (!CONSP (data))
1114 data = Qnil;
1115 errmsg = Fcar (data);
1116 file_error = Qnil;
1118 else
1120 Lisp_Object error_conditions;
1121 errmsg = Fget (errname, Qerror_message);
1122 error_conditions = Fget (errname, Qerror_conditions);
1123 file_error = Fmemq (Qfile_error, error_conditions);
1126 /* Print an error message including the data items. */
1128 tail = Fcdr_safe (data);
1129 GCPRO1 (tail);
1131 /* For file-error, make error message by concatenating
1132 all the data items. They are all strings. */
1133 if (!NILP (file_error) && CONSP (tail))
1134 errmsg = XCAR (tail), tail = XCDR (tail);
1136 if (STRINGP (errmsg))
1137 Fprinc (errmsg, stream);
1138 else
1139 write_string_1 ("peculiar error", -1, stream);
1141 for (i = 0; CONSP (tail); tail = XCDR (tail), i++)
1143 Lisp_Object obj;
1145 write_string_1 (i ? ", " : ": ", 2, stream);
1146 obj = XCAR (tail);
1147 if (!NILP (file_error) || EQ (errname, Qend_of_file))
1148 Fprinc (obj, stream);
1149 else
1150 Fprin1 (obj, stream);
1153 UNGCPRO;
1159 * The buffer should be at least as large as the max string size of the
1160 * largest float, printed in the biggest notation. This is undoubtedly
1161 * 20d float_output_format, with the negative of the C-constant "HUGE"
1162 * from <math.h>.
1164 * On the vax the worst case is -1e38 in 20d format which takes 61 bytes.
1166 * I assume that IEEE-754 format numbers can take 329 bytes for the worst
1167 * case of -1e307 in 20d float_output_format. What is one to do (short of
1168 * re-writing _doprnt to be more sane)?
1169 * -wsr
1172 void
1173 float_to_string (buf, data)
1174 unsigned char *buf;
1175 double data;
1177 unsigned char *cp;
1178 int width;
1180 /* Check for plus infinity in a way that won't lose
1181 if there is no plus infinity. */
1182 if (data == data / 2 && data > 1.0)
1184 strcpy (buf, "1.0e+INF");
1185 return;
1187 /* Likewise for minus infinity. */
1188 if (data == data / 2 && data < -1.0)
1190 strcpy (buf, "-1.0e+INF");
1191 return;
1193 /* Check for NaN in a way that won't fail if there are no NaNs. */
1194 if (! (data * 0.0 >= 0.0))
1196 /* Prepend "-" if the NaN's sign bit is negative.
1197 The sign bit of a double is the bit that is 1 in -0.0. */
1198 int i;
1199 union { double d; char c[sizeof (double)]; } u_data, u_minus_zero;
1200 u_data.d = data;
1201 u_minus_zero.d = - 0.0;
1202 for (i = 0; i < sizeof (double); i++)
1203 if (u_data.c[i] & u_minus_zero.c[i])
1205 *buf++ = '-';
1206 break;
1209 strcpy (buf, "0.0e+NaN");
1210 return;
1213 if (NILP (Vfloat_output_format)
1214 || !STRINGP (Vfloat_output_format))
1215 lose:
1217 /* Generate the fewest number of digits that represent the
1218 floating point value without losing information.
1219 The following method is simple but a bit slow.
1220 For ideas about speeding things up, please see:
1222 Guy L Steele Jr & Jon L White, How to print floating-point numbers
1223 accurately. SIGPLAN notices 25, 6 (June 1990), 112-126.
1225 Robert G Burger & R Kent Dybvig, Printing floating point numbers
1226 quickly and accurately, SIGPLAN notices 31, 5 (May 1996), 108-116. */
1228 width = fabs (data) < DBL_MIN ? 1 : DBL_DIG;
1230 sprintf (buf, "%.*g", width, data);
1231 while (width++ < DOUBLE_DIGITS_BOUND && atof (buf) != data);
1233 else /* oink oink */
1235 /* Check that the spec we have is fully valid.
1236 This means not only valid for printf,
1237 but meant for floats, and reasonable. */
1238 cp = SDATA (Vfloat_output_format);
1240 if (cp[0] != '%')
1241 goto lose;
1242 if (cp[1] != '.')
1243 goto lose;
1245 cp += 2;
1247 /* Check the width specification. */
1248 width = -1;
1249 if ('0' <= *cp && *cp <= '9')
1251 width = 0;
1253 width = (width * 10) + (*cp++ - '0');
1254 while (*cp >= '0' && *cp <= '9');
1256 /* A precision of zero is valid only for %f. */
1257 if (width > DBL_DIG
1258 || (width == 0 && *cp != 'f'))
1259 goto lose;
1262 if (*cp != 'e' && *cp != 'f' && *cp != 'g')
1263 goto lose;
1265 if (cp[1] != 0)
1266 goto lose;
1268 sprintf (buf, SDATA (Vfloat_output_format), data);
1271 /* Make sure there is a decimal point with digit after, or an
1272 exponent, so that the value is readable as a float. But don't do
1273 this with "%.0f"; it's valid for that not to produce a decimal
1274 point. Note that width can be 0 only for %.0f. */
1275 if (width != 0)
1277 for (cp = buf; *cp; cp++)
1278 if ((*cp < '0' || *cp > '9') && *cp != '-')
1279 break;
1281 if (*cp == '.' && cp[1] == 0)
1283 cp[1] = '0';
1284 cp[2] = 0;
1287 if (*cp == 0)
1289 *cp++ = '.';
1290 *cp++ = '0';
1291 *cp++ = 0;
1297 static void
1298 print (obj, printcharfun, escapeflag)
1299 Lisp_Object obj;
1300 register Lisp_Object printcharfun;
1301 int escapeflag;
1303 new_backquote_output = 0;
1305 /* Reset print_number_index and Vprint_number_table only when
1306 the variable Vprint_continuous_numbering is nil. Otherwise,
1307 the values of these variables will be kept between several
1308 print functions. */
1309 if (NILP (Vprint_continuous_numbering)
1310 || NILP (Vprint_number_table))
1312 print_number_index = 0;
1313 Vprint_number_table = Qnil;
1316 /* Construct Vprint_number_table for print-gensym and print-circle. */
1317 if (!NILP (Vprint_gensym) || !NILP (Vprint_circle))
1319 int i, start, index;
1320 start = index = print_number_index;
1321 /* Construct Vprint_number_table.
1322 This increments print_number_index for the objects added. */
1323 print_depth = 0;
1324 print_preprocess (obj);
1326 /* Remove unnecessary objects, which appear only once in OBJ;
1327 that is, whose status is Qnil. Compactify the necessary objects. */
1328 for (i = start; i < print_number_index; i++)
1329 if (!NILP (PRINT_NUMBER_STATUS (Vprint_number_table, i)))
1331 PRINT_NUMBER_OBJECT (Vprint_number_table, index)
1332 = PRINT_NUMBER_OBJECT (Vprint_number_table, i);
1333 index++;
1336 /* Clear out objects outside the active part of the table. */
1337 for (i = index; i < print_number_index; i++)
1338 PRINT_NUMBER_OBJECT (Vprint_number_table, i) = Qnil;
1340 /* Reset the status field for the next print step. Now this
1341 field means whether the object has already been printed. */
1342 for (i = start; i < print_number_index; i++)
1343 PRINT_NUMBER_STATUS (Vprint_number_table, i) = Qnil;
1345 print_number_index = index;
1348 print_depth = 0;
1349 print_object (obj, printcharfun, escapeflag);
1352 /* Construct Vprint_number_table according to the structure of OBJ.
1353 OBJ itself and all its elements will be added to Vprint_number_table
1354 recursively if it is a list, vector, compiled function, char-table,
1355 string (its text properties will be traced), or a symbol that has
1356 no obarray (this is for the print-gensym feature).
1357 The status fields of Vprint_number_table mean whether each object appears
1358 more than once in OBJ: Qnil at the first time, and Qt after that . */
1359 static void
1360 print_preprocess (obj)
1361 Lisp_Object obj;
1363 int i;
1364 EMACS_INT size;
1365 int loop_count = 0;
1366 Lisp_Object halftail;
1368 /* Give up if we go so deep that print_object will get an error. */
1369 /* See similar code in print_object. */
1370 if (print_depth >= PRINT_CIRCLE)
1371 error ("Apparently circular structure being printed");
1373 /* Avoid infinite recursion for circular nested structure
1374 in the case where Vprint_circle is nil. */
1375 if (NILP (Vprint_circle))
1377 for (i = 0; i < print_depth; i++)
1378 if (EQ (obj, being_printed[i]))
1379 return;
1380 being_printed[print_depth] = obj;
1383 print_depth++;
1384 halftail = obj;
1386 loop:
1387 if (STRINGP (obj) || CONSP (obj) || VECTORP (obj)
1388 || COMPILEDP (obj) || CHAR_TABLE_P (obj)
1389 || (! NILP (Vprint_gensym)
1390 && SYMBOLP (obj)
1391 && !SYMBOL_INTERNED_P (obj)))
1393 /* In case print-circle is nil and print-gensym is t,
1394 add OBJ to Vprint_number_table only when OBJ is a symbol. */
1395 if (! NILP (Vprint_circle) || SYMBOLP (obj))
1397 for (i = 0; i < print_number_index; i++)
1398 if (EQ (PRINT_NUMBER_OBJECT (Vprint_number_table, i), obj))
1400 /* OBJ appears more than once. Let's remember that. */
1401 PRINT_NUMBER_STATUS (Vprint_number_table, i) = Qt;
1402 print_depth--;
1403 return;
1406 /* OBJ is not yet recorded. Let's add to the table. */
1407 if (print_number_index == 0)
1409 /* Initialize the table. */
1410 Vprint_number_table = Fmake_vector (make_number (40), Qnil);
1412 else if (XVECTOR (Vprint_number_table)->size == print_number_index * 2)
1414 /* Reallocate the table. */
1415 int i = print_number_index * 4;
1416 Lisp_Object old_table = Vprint_number_table;
1417 Vprint_number_table = Fmake_vector (make_number (i), Qnil);
1418 for (i = 0; i < print_number_index; i++)
1420 PRINT_NUMBER_OBJECT (Vprint_number_table, i)
1421 = PRINT_NUMBER_OBJECT (old_table, i);
1422 PRINT_NUMBER_STATUS (Vprint_number_table, i)
1423 = PRINT_NUMBER_STATUS (old_table, i);
1426 PRINT_NUMBER_OBJECT (Vprint_number_table, print_number_index) = obj;
1427 /* If Vprint_continuous_numbering is non-nil and OBJ is a gensym,
1428 always print the gensym with a number. This is a special for
1429 the lisp function byte-compile-output-docform. */
1430 if (!NILP (Vprint_continuous_numbering)
1431 && SYMBOLP (obj)
1432 && !SYMBOL_INTERNED_P (obj))
1433 PRINT_NUMBER_STATUS (Vprint_number_table, print_number_index) = Qt;
1434 print_number_index++;
1437 switch (XGCTYPE (obj))
1439 case Lisp_String:
1440 /* A string may have text properties, which can be circular. */
1441 traverse_intervals_noorder (STRING_INTERVALS (obj),
1442 print_preprocess_string, Qnil);
1443 break;
1445 case Lisp_Cons:
1446 /* Use HALFTAIL and LOOP_COUNT to detect circular lists,
1447 just as in print_object. */
1448 if (loop_count && EQ (obj, halftail))
1449 break;
1450 print_preprocess (XCAR (obj));
1451 obj = XCDR (obj);
1452 loop_count++;
1453 if (!(loop_count & 1))
1454 halftail = XCDR (halftail);
1455 goto loop;
1457 case Lisp_Vectorlike:
1458 size = XVECTOR (obj)->size;
1459 if (size & PSEUDOVECTOR_FLAG)
1460 size &= PSEUDOVECTOR_SIZE_MASK;
1461 for (i = 0; i < size; i++)
1462 print_preprocess (XVECTOR (obj)->contents[i]);
1463 break;
1465 default:
1466 break;
1469 print_depth--;
1472 static void
1473 print_preprocess_string (interval, arg)
1474 INTERVAL interval;
1475 Lisp_Object arg;
1477 print_preprocess (interval->plist);
1480 static void
1481 print_object (obj, printcharfun, escapeflag)
1482 Lisp_Object obj;
1483 register Lisp_Object printcharfun;
1484 int escapeflag;
1486 char buf[40];
1488 QUIT;
1490 /* See similar code in print_preprocess. */
1491 if (print_depth > PRINT_CIRCLE)
1492 error ("Apparently circular structure being printed");
1494 /* Detect circularities and truncate them. */
1495 if (STRINGP (obj) || CONSP (obj) || VECTORP (obj)
1496 || COMPILEDP (obj) || CHAR_TABLE_P (obj)
1497 || (! NILP (Vprint_gensym)
1498 && SYMBOLP (obj)
1499 && !SYMBOL_INTERNED_P (obj)))
1501 if (NILP (Vprint_circle) && NILP (Vprint_gensym))
1503 /* Simple but incomplete way. */
1504 int i;
1505 for (i = 0; i < print_depth; i++)
1506 if (EQ (obj, being_printed[i]))
1508 sprintf (buf, "#%d", i);
1509 strout (buf, -1, -1, printcharfun, 0);
1510 return;
1512 being_printed[print_depth] = obj;
1514 else
1516 /* With the print-circle feature. */
1517 int i;
1518 for (i = 0; i < print_number_index; i++)
1519 if (EQ (PRINT_NUMBER_OBJECT (Vprint_number_table, i), obj))
1521 if (NILP (PRINT_NUMBER_STATUS (Vprint_number_table, i)))
1523 /* Add a prefix #n= if OBJ has not yet been printed;
1524 that is, its status field is nil. */
1525 sprintf (buf, "#%d=", i + 1);
1526 strout (buf, -1, -1, printcharfun, 0);
1527 /* OBJ is going to be printed. Set the status to t. */
1528 PRINT_NUMBER_STATUS (Vprint_number_table, i) = Qt;
1529 break;
1531 else
1533 /* Just print #n# if OBJ has already been printed. */
1534 sprintf (buf, "#%d#", i + 1);
1535 strout (buf, -1, -1, printcharfun, 0);
1536 return;
1542 print_depth++;
1544 #ifdef MAX_PRINT_CHARS
1545 if (max_print && print_chars > max_print)
1547 PRINTCHAR ('\n');
1548 print_chars = 0;
1550 #endif /* MAX_PRINT_CHARS */
1552 switch (XGCTYPE (obj))
1554 case Lisp_Int:
1555 if (sizeof (int) == sizeof (EMACS_INT))
1556 sprintf (buf, "%d", XINT (obj));
1557 else if (sizeof (long) == sizeof (EMACS_INT))
1558 sprintf (buf, "%ld", (long) XINT (obj));
1559 else
1560 abort ();
1561 strout (buf, -1, -1, printcharfun, 0);
1562 break;
1564 case Lisp_Float:
1566 char pigbuf[350]; /* see comments in float_to_string */
1568 float_to_string (pigbuf, XFLOAT_DATA (obj));
1569 strout (pigbuf, -1, -1, printcharfun, 0);
1571 break;
1573 case Lisp_String:
1574 if (!escapeflag)
1575 print_string (obj, printcharfun);
1576 else
1578 register int i, i_byte;
1579 struct gcpro gcpro1;
1580 unsigned char *str;
1581 int size_byte;
1582 /* 1 means we must ensure that the next character we output
1583 cannot be taken as part of a hex character escape. */
1584 int need_nonhex = 0;
1585 int multibyte = STRING_MULTIBYTE (obj);
1587 GCPRO1 (obj);
1589 if (!NULL_INTERVAL_P (STRING_INTERVALS (obj)))
1591 PRINTCHAR ('#');
1592 PRINTCHAR ('(');
1595 PRINTCHAR ('\"');
1596 str = SDATA (obj);
1597 size_byte = SBYTES (obj);
1599 for (i = 0, i_byte = 0; i_byte < size_byte;)
1601 /* Here, we must convert each multi-byte form to the
1602 corresponding character code before handing it to PRINTCHAR. */
1603 int len;
1604 int c;
1606 if (multibyte)
1608 c = STRING_CHAR_AND_LENGTH (str + i_byte,
1609 size_byte - i_byte, len);
1610 if (CHAR_VALID_P (c, 0))
1611 i_byte += len;
1612 else
1613 c = str[i_byte++];
1615 else
1616 c = str[i_byte++];
1618 QUIT;
1620 if (c == '\n' && print_escape_newlines)
1622 PRINTCHAR ('\\');
1623 PRINTCHAR ('n');
1625 else if (c == '\f' && print_escape_newlines)
1627 PRINTCHAR ('\\');
1628 PRINTCHAR ('f');
1630 else if (multibyte
1631 && ! ASCII_BYTE_P (c)
1632 && (SINGLE_BYTE_CHAR_P (c) || print_escape_multibyte))
1634 /* When multibyte is disabled,
1635 print multibyte string chars using hex escapes.
1636 For a char code that could be in a unibyte string,
1637 when found in a multibyte string, always use a hex escape
1638 so it reads back as multibyte. */
1639 unsigned char outbuf[50];
1640 sprintf (outbuf, "\\x%x", c);
1641 strout (outbuf, -1, -1, printcharfun, 0);
1642 need_nonhex = 1;
1644 else if (! multibyte
1645 && SINGLE_BYTE_CHAR_P (c) && ! ASCII_BYTE_P (c)
1646 && print_escape_nonascii)
1648 /* When printing in a multibyte buffer
1649 or when explicitly requested,
1650 print single-byte non-ASCII string chars
1651 using octal escapes. */
1652 unsigned char outbuf[5];
1653 sprintf (outbuf, "\\%03o", c);
1654 strout (outbuf, -1, -1, printcharfun, 0);
1656 else
1658 /* If we just had a hex escape, and this character
1659 could be taken as part of it,
1660 output `\ ' to prevent that. */
1661 if (need_nonhex)
1663 need_nonhex = 0;
1664 if ((c >= 'a' && c <= 'f')
1665 || (c >= 'A' && c <= 'F')
1666 || (c >= '0' && c <= '9'))
1667 strout ("\\ ", -1, -1, printcharfun, 0);
1670 if (c == '\"' || c == '\\')
1671 PRINTCHAR ('\\');
1672 PRINTCHAR (c);
1675 PRINTCHAR ('\"');
1677 if (!NULL_INTERVAL_P (STRING_INTERVALS (obj)))
1679 traverse_intervals (STRING_INTERVALS (obj),
1680 0, print_interval, printcharfun);
1681 PRINTCHAR (')');
1684 UNGCPRO;
1686 break;
1688 case Lisp_Symbol:
1690 register int confusing;
1691 register unsigned char *p = SDATA (SYMBOL_NAME (obj));
1692 register unsigned char *end = p + SBYTES (SYMBOL_NAME (obj));
1693 register int c;
1694 int i, i_byte, size_byte;
1695 Lisp_Object name;
1697 name = SYMBOL_NAME (obj);
1699 if (p != end && (*p == '-' || *p == '+')) p++;
1700 if (p == end)
1701 confusing = 0;
1702 /* If symbol name begins with a digit, and ends with a digit,
1703 and contains nothing but digits and `e', it could be treated
1704 as a number. So set CONFUSING.
1706 Symbols that contain periods could also be taken as numbers,
1707 but periods are always escaped, so we don't have to worry
1708 about them here. */
1709 else if (*p >= '0' && *p <= '9'
1710 && end[-1] >= '0' && end[-1] <= '9')
1712 while (p != end && ((*p >= '0' && *p <= '9')
1713 /* Needed for \2e10. */
1714 || *p == 'e'))
1715 p++;
1716 confusing = (end == p);
1718 else
1719 confusing = 0;
1721 if (! NILP (Vprint_gensym) && !SYMBOL_INTERNED_P (obj))
1723 PRINTCHAR ('#');
1724 PRINTCHAR (':');
1727 size_byte = SBYTES (name);
1729 for (i = 0, i_byte = 0; i_byte < size_byte;)
1731 /* Here, we must convert each multi-byte form to the
1732 corresponding character code before handing it to PRINTCHAR. */
1733 FETCH_STRING_CHAR_ADVANCE (c, name, i, i_byte);
1734 QUIT;
1736 if (escapeflag)
1738 if (c == '\"' || c == '\\' || c == '\''
1739 || c == ';' || c == '#' || c == '(' || c == ')'
1740 || c == ',' || c =='.' || c == '`'
1741 || c == '[' || c == ']' || c == '?' || c <= 040
1742 || confusing)
1743 PRINTCHAR ('\\'), confusing = 0;
1745 PRINTCHAR (c);
1748 break;
1750 case Lisp_Cons:
1751 /* If deeper than spec'd depth, print placeholder. */
1752 if (INTEGERP (Vprint_level)
1753 && print_depth > XINT (Vprint_level))
1754 strout ("...", -1, -1, printcharfun, 0);
1755 else if (print_quoted && CONSP (XCDR (obj)) && NILP (XCDR (XCDR (obj)))
1756 && (EQ (XCAR (obj), Qquote)))
1758 PRINTCHAR ('\'');
1759 print_object (XCAR (XCDR (obj)), printcharfun, escapeflag);
1761 else if (print_quoted && CONSP (XCDR (obj)) && NILP (XCDR (XCDR (obj)))
1762 && (EQ (XCAR (obj), Qfunction)))
1764 PRINTCHAR ('#');
1765 PRINTCHAR ('\'');
1766 print_object (XCAR (XCDR (obj)), printcharfun, escapeflag);
1768 else if (print_quoted && CONSP (XCDR (obj)) && NILP (XCDR (XCDR (obj)))
1769 && ((EQ (XCAR (obj), Qbackquote))))
1771 print_object (XCAR (obj), printcharfun, 0);
1772 new_backquote_output++;
1773 print_object (XCAR (XCDR (obj)), printcharfun, escapeflag);
1774 new_backquote_output--;
1776 else if (print_quoted && CONSP (XCDR (obj)) && NILP (XCDR (XCDR (obj)))
1777 && new_backquote_output
1778 && ((EQ (XCAR (obj), Qbackquote)
1779 || EQ (XCAR (obj), Qcomma)
1780 || EQ (XCAR (obj), Qcomma_at)
1781 || EQ (XCAR (obj), Qcomma_dot))))
1783 print_object (XCAR (obj), printcharfun, 0);
1784 new_backquote_output--;
1785 print_object (XCAR (XCDR (obj)), printcharfun, escapeflag);
1786 new_backquote_output++;
1788 else
1790 PRINTCHAR ('(');
1792 /* If the first element is a backquote form,
1793 print it old-style so it won't be misunderstood. */
1794 if (print_quoted && CONSP (XCAR (obj))
1795 && CONSP (XCDR (XCAR (obj)))
1796 && NILP (XCDR (XCDR (XCAR (obj))))
1797 && EQ (XCAR (XCAR (obj)), Qbackquote))
1799 Lisp_Object tem;
1800 tem = XCAR (obj);
1801 PRINTCHAR ('(');
1803 print_object (Qbackquote, printcharfun, 0);
1804 PRINTCHAR (' ');
1806 print_object (XCAR (XCDR (tem)), printcharfun, 0);
1807 PRINTCHAR (')');
1809 obj = XCDR (obj);
1813 int print_length, i;
1814 Lisp_Object halftail = obj;
1816 /* Negative values of print-length are invalid in CL.
1817 Treat them like nil, as CMUCL does. */
1818 if (NATNUMP (Vprint_length))
1819 print_length = XFASTINT (Vprint_length);
1820 else
1821 print_length = 0;
1823 i = 0;
1824 while (CONSP (obj))
1826 /* Detect circular list. */
1827 if (NILP (Vprint_circle))
1829 /* Simple but imcomplete way. */
1830 if (i != 0 && EQ (obj, halftail))
1832 sprintf (buf, " . #%d", i / 2);
1833 strout (buf, -1, -1, printcharfun, 0);
1834 goto end_of_list;
1837 else
1839 /* With the print-circle feature. */
1840 if (i != 0)
1842 int i;
1843 for (i = 0; i < print_number_index; i++)
1844 if (EQ (PRINT_NUMBER_OBJECT (Vprint_number_table, i),
1845 obj))
1847 if (NILP (PRINT_NUMBER_STATUS (Vprint_number_table, i)))
1849 strout (" . ", 3, 3, printcharfun, 0);
1850 print_object (obj, printcharfun, escapeflag);
1852 else
1854 sprintf (buf, " . #%d#", i + 1);
1855 strout (buf, -1, -1, printcharfun, 0);
1857 goto end_of_list;
1862 if (i++)
1863 PRINTCHAR (' ');
1865 if (print_length && i > print_length)
1867 strout ("...", 3, 3, printcharfun, 0);
1868 goto end_of_list;
1871 print_object (XCAR (obj), printcharfun, escapeflag);
1873 obj = XCDR (obj);
1874 if (!(i & 1))
1875 halftail = XCDR (halftail);
1879 /* OBJ non-nil here means it's the end of a dotted list. */
1880 if (!NILP (obj))
1882 strout (" . ", 3, 3, printcharfun, 0);
1883 print_object (obj, printcharfun, escapeflag);
1886 end_of_list:
1887 PRINTCHAR (')');
1889 break;
1891 case Lisp_Vectorlike:
1892 if (PROCESSP (obj))
1894 if (escapeflag)
1896 strout ("#<process ", -1, -1, printcharfun, 0);
1897 print_string (XPROCESS (obj)->name, printcharfun);
1898 PRINTCHAR ('>');
1900 else
1901 print_string (XPROCESS (obj)->name, printcharfun);
1903 else if (BOOL_VECTOR_P (obj))
1905 register int i;
1906 register unsigned char c;
1907 struct gcpro gcpro1;
1908 int size_in_chars
1909 = ((XBOOL_VECTOR (obj)->size + BOOL_VECTOR_BITS_PER_CHAR - 1)
1910 / BOOL_VECTOR_BITS_PER_CHAR);
1912 GCPRO1 (obj);
1914 PRINTCHAR ('#');
1915 PRINTCHAR ('&');
1916 sprintf (buf, "%ld", (long) XBOOL_VECTOR (obj)->size);
1917 strout (buf, -1, -1, printcharfun, 0);
1918 PRINTCHAR ('\"');
1920 /* Don't print more characters than the specified maximum.
1921 Negative values of print-length are invalid. Treat them
1922 like a print-length of nil. */
1923 if (NATNUMP (Vprint_length)
1924 && XFASTINT (Vprint_length) < size_in_chars)
1925 size_in_chars = XFASTINT (Vprint_length);
1927 for (i = 0; i < size_in_chars; i++)
1929 QUIT;
1930 c = XBOOL_VECTOR (obj)->data[i];
1931 if (c == '\n' && print_escape_newlines)
1933 PRINTCHAR ('\\');
1934 PRINTCHAR ('n');
1936 else if (c == '\f' && print_escape_newlines)
1938 PRINTCHAR ('\\');
1939 PRINTCHAR ('f');
1941 else if (c > '\177')
1943 /* Use octal escapes to avoid encoding issues. */
1944 PRINTCHAR ('\\');
1945 PRINTCHAR ('0' + ((c >> 6) & 3));
1946 PRINTCHAR ('0' + ((c >> 3) & 7));
1947 PRINTCHAR ('0' + (c & 7));
1949 else
1951 if (c == '\"' || c == '\\')
1952 PRINTCHAR ('\\');
1953 PRINTCHAR (c);
1956 PRINTCHAR ('\"');
1958 UNGCPRO;
1960 else if (SUBRP (obj))
1962 strout ("#<subr ", -1, -1, printcharfun, 0);
1963 strout (XSUBR (obj)->symbol_name, -1, -1, printcharfun, 0);
1964 PRINTCHAR ('>');
1966 else if (WINDOWP (obj))
1968 strout ("#<window ", -1, -1, printcharfun, 0);
1969 sprintf (buf, "%ld", (long) XFASTINT (XWINDOW (obj)->sequence_number));
1970 strout (buf, -1, -1, printcharfun, 0);
1971 if (!NILP (XWINDOW (obj)->buffer))
1973 strout (" on ", -1, -1, printcharfun, 0);
1974 print_string (XBUFFER (XWINDOW (obj)->buffer)->name, printcharfun);
1976 PRINTCHAR ('>');
1978 else if (HASH_TABLE_P (obj))
1980 struct Lisp_Hash_Table *h = XHASH_TABLE (obj);
1981 strout ("#<hash-table", -1, -1, printcharfun, 0);
1982 if (SYMBOLP (h->test))
1984 PRINTCHAR (' ');
1985 PRINTCHAR ('\'');
1986 strout (SDATA (SYMBOL_NAME (h->test)), -1, -1, printcharfun, 0);
1987 PRINTCHAR (' ');
1988 strout (SDATA (SYMBOL_NAME (h->weak)), -1, -1, printcharfun, 0);
1989 PRINTCHAR (' ');
1990 sprintf (buf, "%ld/%ld", (long) XFASTINT (h->count),
1991 (long) XVECTOR (h->next)->size);
1992 strout (buf, -1, -1, printcharfun, 0);
1994 sprintf (buf, " 0x%lx", (unsigned long) h);
1995 strout (buf, -1, -1, printcharfun, 0);
1996 PRINTCHAR ('>');
1998 else if (BUFFERP (obj))
2000 if (NILP (XBUFFER (obj)->name))
2001 strout ("#<killed buffer>", -1, -1, printcharfun, 0);
2002 else if (escapeflag)
2004 strout ("#<buffer ", -1, -1, printcharfun, 0);
2005 print_string (XBUFFER (obj)->name, printcharfun);
2006 PRINTCHAR ('>');
2008 else
2009 print_string (XBUFFER (obj)->name, printcharfun);
2011 else if (WINDOW_CONFIGURATIONP (obj))
2013 strout ("#<window-configuration>", -1, -1, printcharfun, 0);
2015 else if (FRAMEP (obj))
2017 strout ((FRAME_LIVE_P (XFRAME (obj))
2018 ? "#<frame " : "#<dead frame "),
2019 -1, -1, printcharfun, 0);
2020 print_string (XFRAME (obj)->name, printcharfun);
2021 sprintf (buf, " 0x%lx", (unsigned long) (XFRAME (obj)));
2022 strout (buf, -1, -1, printcharfun, 0);
2023 PRINTCHAR ('>');
2025 else
2027 EMACS_INT size = XVECTOR (obj)->size;
2028 if (COMPILEDP (obj))
2030 PRINTCHAR ('#');
2031 size &= PSEUDOVECTOR_SIZE_MASK;
2033 if (CHAR_TABLE_P (obj))
2035 /* We print a char-table as if it were a vector,
2036 lumping the parent and default slots in with the
2037 character slots. But we add #^ as a prefix. */
2038 PRINTCHAR ('#');
2039 PRINTCHAR ('^');
2040 if (SUB_CHAR_TABLE_P (obj))
2041 PRINTCHAR ('^');
2042 size &= PSEUDOVECTOR_SIZE_MASK;
2044 if (size & PSEUDOVECTOR_FLAG)
2045 goto badtype;
2047 PRINTCHAR ('[');
2049 register int i;
2050 register Lisp_Object tem;
2051 int real_size = size;
2053 /* Don't print more elements than the specified maximum. */
2054 if (NATNUMP (Vprint_length)
2055 && XFASTINT (Vprint_length) < size)
2056 size = XFASTINT (Vprint_length);
2058 for (i = 0; i < size; i++)
2060 if (i) PRINTCHAR (' ');
2061 tem = XVECTOR (obj)->contents[i];
2062 print_object (tem, printcharfun, escapeflag);
2064 if (size < real_size)
2065 strout (" ...", 4, 4, printcharfun, 0);
2067 PRINTCHAR (']');
2069 break;
2071 case Lisp_Misc:
2072 switch (XMISCTYPE (obj))
2074 case Lisp_Misc_Marker:
2075 strout ("#<marker ", -1, -1, printcharfun, 0);
2076 /* Do you think this is necessary? */
2077 if (XMARKER (obj)->insertion_type != 0)
2078 strout ("(moves after insertion) ", -1, -1, printcharfun, 0);
2079 if (! XMARKER (obj)->buffer)
2080 strout ("in no buffer", -1, -1, printcharfun, 0);
2081 else
2083 sprintf (buf, "at %d", marker_position (obj));
2084 strout (buf, -1, -1, printcharfun, 0);
2085 strout (" in ", -1, -1, printcharfun, 0);
2086 print_string (XMARKER (obj)->buffer->name, printcharfun);
2088 PRINTCHAR ('>');
2089 break;
2091 case Lisp_Misc_Overlay:
2092 strout ("#<overlay ", -1, -1, printcharfun, 0);
2093 if (! XMARKER (OVERLAY_START (obj))->buffer)
2094 strout ("in no buffer", -1, -1, printcharfun, 0);
2095 else
2097 sprintf (buf, "from %d to %d in ",
2098 marker_position (OVERLAY_START (obj)),
2099 marker_position (OVERLAY_END (obj)));
2100 strout (buf, -1, -1, printcharfun, 0);
2101 print_string (XMARKER (OVERLAY_START (obj))->buffer->name,
2102 printcharfun);
2104 PRINTCHAR ('>');
2105 break;
2107 /* Remaining cases shouldn't happen in normal usage, but let's print
2108 them anyway for the benefit of the debugger. */
2109 case Lisp_Misc_Free:
2110 strout ("#<misc free cell>", -1, -1, printcharfun, 0);
2111 break;
2113 case Lisp_Misc_Intfwd:
2114 sprintf (buf, "#<intfwd to %ld>", (long) *XINTFWD (obj)->intvar);
2115 strout (buf, -1, -1, printcharfun, 0);
2116 break;
2118 case Lisp_Misc_Boolfwd:
2119 sprintf (buf, "#<boolfwd to %s>",
2120 (*XBOOLFWD (obj)->boolvar ? "t" : "nil"));
2121 strout (buf, -1, -1, printcharfun, 0);
2122 break;
2124 case Lisp_Misc_Objfwd:
2125 strout ("#<objfwd to ", -1, -1, printcharfun, 0);
2126 print_object (*XOBJFWD (obj)->objvar, printcharfun, escapeflag);
2127 PRINTCHAR ('>');
2128 break;
2130 case Lisp_Misc_Buffer_Objfwd:
2131 strout ("#<buffer_objfwd to ", -1, -1, printcharfun, 0);
2132 print_object (PER_BUFFER_VALUE (current_buffer,
2133 XBUFFER_OBJFWD (obj)->offset),
2134 printcharfun, escapeflag);
2135 PRINTCHAR ('>');
2136 break;
2138 case Lisp_Misc_Kboard_Objfwd:
2139 strout ("#<kboard_objfwd to ", -1, -1, printcharfun, 0);
2140 print_object (*(Lisp_Object *) ((char *) current_kboard
2141 + XKBOARD_OBJFWD (obj)->offset),
2142 printcharfun, escapeflag);
2143 PRINTCHAR ('>');
2144 break;
2146 case Lisp_Misc_Buffer_Local_Value:
2147 strout ("#<buffer_local_value ", -1, -1, printcharfun, 0);
2148 goto do_buffer_local;
2149 case Lisp_Misc_Some_Buffer_Local_Value:
2150 strout ("#<some_buffer_local_value ", -1, -1, printcharfun, 0);
2151 do_buffer_local:
2152 strout ("[realvalue] ", -1, -1, printcharfun, 0);
2153 print_object (XBUFFER_LOCAL_VALUE (obj)->realvalue,
2154 printcharfun, escapeflag);
2155 if (XBUFFER_LOCAL_VALUE (obj)->found_for_buffer)
2156 strout ("[local in buffer] ", -1, -1, printcharfun, 0);
2157 else
2158 strout ("[buffer] ", -1, -1, printcharfun, 0);
2159 print_object (XBUFFER_LOCAL_VALUE (obj)->buffer,
2160 printcharfun, escapeflag);
2161 if (XBUFFER_LOCAL_VALUE (obj)->check_frame)
2163 if (XBUFFER_LOCAL_VALUE (obj)->found_for_frame)
2164 strout ("[local in frame] ", -1, -1, printcharfun, 0);
2165 else
2166 strout ("[frame] ", -1, -1, printcharfun, 0);
2167 print_object (XBUFFER_LOCAL_VALUE (obj)->frame,
2168 printcharfun, escapeflag);
2170 strout ("[alist-elt] ", -1, -1, printcharfun, 0);
2171 print_object (XCAR (XBUFFER_LOCAL_VALUE (obj)->cdr),
2172 printcharfun, escapeflag);
2173 strout ("[default-value] ", -1, -1, printcharfun, 0);
2174 print_object (XCDR (XBUFFER_LOCAL_VALUE (obj)->cdr),
2175 printcharfun, escapeflag);
2176 PRINTCHAR ('>');
2177 break;
2179 case Lisp_Misc_Save_Value:
2180 strout ("#<save_value ", -1, -1, printcharfun, 0);
2181 sprintf(buf, "ptr=0x%08lx int=%d",
2182 (unsigned long) XSAVE_VALUE (obj)->pointer,
2183 XSAVE_VALUE (obj)->integer);
2184 strout (buf, -1, -1, printcharfun, 0);
2185 PRINTCHAR ('>');
2186 break;
2188 default:
2189 goto badtype;
2191 break;
2193 default:
2194 badtype:
2196 /* We're in trouble if this happens!
2197 Probably should just abort () */
2198 strout ("#<EMACS BUG: INVALID DATATYPE ", -1, -1, printcharfun, 0);
2199 if (MISCP (obj))
2200 sprintf (buf, "(MISC 0x%04x)", (int) XMISCTYPE (obj));
2201 else if (VECTORLIKEP (obj))
2202 sprintf (buf, "(PVEC 0x%08x)", (int) XVECTOR (obj)->size);
2203 else
2204 sprintf (buf, "(0x%02x)", (int) XTYPE (obj));
2205 strout (buf, -1, -1, printcharfun, 0);
2206 strout (" Save your buffers immediately and please report this bug>",
2207 -1, -1, printcharfun, 0);
2211 print_depth--;
2215 /* Print a description of INTERVAL using PRINTCHARFUN.
2216 This is part of printing a string that has text properties. */
2218 void
2219 print_interval (interval, printcharfun)
2220 INTERVAL interval;
2221 Lisp_Object printcharfun;
2223 PRINTCHAR (' ');
2224 print_object (make_number (interval->position), printcharfun, 1);
2225 PRINTCHAR (' ');
2226 print_object (make_number (interval->position + LENGTH (interval)),
2227 printcharfun, 1);
2228 PRINTCHAR (' ');
2229 print_object (interval->plist, printcharfun, 1);
2233 void
2234 syms_of_print ()
2236 Qtemp_buffer_setup_hook = intern ("temp-buffer-setup-hook");
2237 staticpro (&Qtemp_buffer_setup_hook);
2239 DEFVAR_LISP ("standard-output", &Vstandard_output,
2240 doc: /* Output stream `print' uses by default for outputting a character.
2241 This may be any function of one argument.
2242 It may also be a buffer (output is inserted before point)
2243 or a marker (output is inserted and the marker is advanced)
2244 or the symbol t (output appears in the echo area). */);
2245 Vstandard_output = Qt;
2246 Qstandard_output = intern ("standard-output");
2247 staticpro (&Qstandard_output);
2249 DEFVAR_LISP ("float-output-format", &Vfloat_output_format,
2250 doc: /* The format descriptor string used to print floats.
2251 This is a %-spec like those accepted by `printf' in C,
2252 but with some restrictions. It must start with the two characters `%.'.
2253 After that comes an integer precision specification,
2254 and then a letter which controls the format.
2255 The letters allowed are `e', `f' and `g'.
2256 Use `e' for exponential notation \"DIG.DIGITSeEXPT\"
2257 Use `f' for decimal point notation \"DIGITS.DIGITS\".
2258 Use `g' to choose the shorter of those two formats for the number at hand.
2259 The precision in any of these cases is the number of digits following
2260 the decimal point. With `f', a precision of 0 means to omit the
2261 decimal point. 0 is not allowed with `e' or `g'.
2263 A value of nil means to use the shortest notation
2264 that represents the number without losing information. */);
2265 Vfloat_output_format = Qnil;
2266 Qfloat_output_format = intern ("float-output-format");
2267 staticpro (&Qfloat_output_format);
2269 DEFVAR_LISP ("print-length", &Vprint_length,
2270 doc: /* Maximum length of list to print before abbreviating.
2271 A value of nil means no limit. See also `eval-expression-print-length'. */);
2272 Vprint_length = Qnil;
2274 DEFVAR_LISP ("print-level", &Vprint_level,
2275 doc: /* Maximum depth of list nesting to print before abbreviating.
2276 A value of nil means no limit. See also `eval-expression-print-level'. */);
2277 Vprint_level = Qnil;
2279 DEFVAR_BOOL ("print-escape-newlines", &print_escape_newlines,
2280 doc: /* Non-nil means print newlines in strings as `\\n'.
2281 Also print formfeeds as `\\f'. */);
2282 print_escape_newlines = 0;
2284 DEFVAR_BOOL ("print-escape-nonascii", &print_escape_nonascii,
2285 doc: /* Non-nil means print unibyte non-ASCII chars in strings as \\OOO.
2286 \(OOO is the octal representation of the character code.)
2287 Only single-byte characters are affected, and only in `prin1'.
2288 When the output goes in a multibyte buffer, this feature is
2289 enabled regardless of the value of the variable. */);
2290 print_escape_nonascii = 0;
2292 DEFVAR_BOOL ("print-escape-multibyte", &print_escape_multibyte,
2293 doc: /* Non-nil means print multibyte characters in strings as \\xXXXX.
2294 \(XXXX is the hex representation of the character code.)
2295 This affects only `prin1'. */);
2296 print_escape_multibyte = 0;
2298 DEFVAR_BOOL ("print-quoted", &print_quoted,
2299 doc: /* Non-nil means print quoted forms with reader syntax.
2300 I.e., (quote foo) prints as 'foo, (function foo) as #'foo, and backquoted
2301 forms print as in the new syntax. */);
2302 print_quoted = 0;
2304 DEFVAR_LISP ("print-gensym", &Vprint_gensym,
2305 doc: /* Non-nil means print uninterned symbols so they will read as uninterned.
2306 I.e., the value of (make-symbol \"foobar\") prints as #:foobar.
2307 When the uninterned symbol appears within a recursive data structure,
2308 and the symbol appears more than once, in addition use the #N# and #N=
2309 constructs as needed, so that multiple references to the same symbol are
2310 shared once again when the text is read back. */);
2311 Vprint_gensym = Qnil;
2313 DEFVAR_LISP ("print-circle", &Vprint_circle,
2314 doc: /* *Non-nil means print recursive structures using #N= and #N# syntax.
2315 If nil, printing proceeds recursively and may lead to
2316 `max-lisp-eval-depth' being exceeded or an error may occur:
2317 \"Apparently circular structure being printed.\" Also see
2318 `print-length' and `print-level'.
2319 If non-nil, shared substructures anywhere in the structure are printed
2320 with `#N=' before the first occurrence (in the order of the print
2321 representation) and `#N#' in place of each subsequent occurrence,
2322 where N is a positive decimal integer. */);
2323 Vprint_circle = Qnil;
2325 DEFVAR_LISP ("print-continuous-numbering", &Vprint_continuous_numbering,
2326 doc: /* *Non-nil means number continuously across print calls.
2327 This affects the numbers printed for #N= labels and #M# references.
2328 See also `print-circle', `print-gensym', and `print-number-table'.
2329 This variable should not be set with `setq'; bind it with a `let' instead. */);
2330 Vprint_continuous_numbering = Qnil;
2332 DEFVAR_LISP ("print-number-table", &Vprint_number_table,
2333 doc: /* A vector used internally to produce `#N=' labels and `#N#' references.
2334 The Lisp printer uses this vector to detect Lisp objects referenced more
2335 than once.
2337 When you bind `print-continuous-numbering' to t, you should probably
2338 also bind `print-number-table' to nil. This ensures that the value of
2339 `print-number-table' can be garbage-collected once the printing is
2340 done. If all elements of `print-number-table' are nil, it means that
2341 the printing done so far has not found any shared structure or objects
2342 that need to be recorded in the table. */);
2343 Vprint_number_table = Qnil;
2345 /* prin1_to_string_buffer initialized in init_buffer_once in buffer.c */
2346 staticpro (&Vprin1_to_string_buffer);
2348 defsubr (&Sprin1);
2349 defsubr (&Sprin1_to_string);
2350 defsubr (&Serror_message_string);
2351 defsubr (&Sprinc);
2352 defsubr (&Sprint);
2353 defsubr (&Sterpri);
2354 defsubr (&Swrite_char);
2355 defsubr (&Sexternal_debugging_output);
2356 #ifdef WITH_REDIRECT_DEBUGGING_OUTPUT
2357 defsubr (&Sredirect_debugging_output);
2358 #endif
2360 Qexternal_debugging_output = intern ("external-debugging-output");
2361 staticpro (&Qexternal_debugging_output);
2363 Qprint_escape_newlines = intern ("print-escape-newlines");
2364 staticpro (&Qprint_escape_newlines);
2366 Qprint_escape_multibyte = intern ("print-escape-multibyte");
2367 staticpro (&Qprint_escape_multibyte);
2369 Qprint_escape_nonascii = intern ("print-escape-nonascii");
2370 staticpro (&Qprint_escape_nonascii);
2372 defsubr (&Swith_output_to_temp_buffer);
2375 /* arch-tag: bc797170-94ae-41de-86e3-75e20f8f7a39
2376 (do not change this comment) */