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, 2009, 2010 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 of the License, or
11 (at your option) 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. If not, see <http://www.gnu.org/licenses/>. */
27 #include "character.h"
33 #include "dispextern.h"
35 #include "intervals.h"
36 #include "blockinput.h"
37 #include "termhooks.h" /* For struct terminal. */
40 Lisp_Object Vstandard_output
, Qstandard_output
;
42 Lisp_Object Qtemp_buffer_setup_hook
;
44 /* These are used to print like we read. */
46 Lisp_Object Vfloat_output_format
, Qfloat_output_format
;
54 /* Default to values appropriate for IEEE floating point. */
59 #define DBL_MANT_DIG 53
65 #define DBL_MIN 2.2250738585072014e-308
68 #ifdef DBL_MIN_REPLACEMENT
70 #define DBL_MIN DBL_MIN_REPLACEMENT
73 /* Define DOUBLE_DIGITS_BOUND, an upper bound on the number of decimal digits
74 needed to express a float without losing information.
75 The general-case formula is valid for the usual case, IEEE floating point,
76 but many compilers can't optimize the formula to an integer constant,
77 so make a special case for it. */
78 #if FLT_RADIX == 2 && DBL_MANT_DIG == 53
79 #define DOUBLE_DIGITS_BOUND 17 /* IEEE floating point */
81 #define DOUBLE_DIGITS_BOUND ((int) ceil (log10 (pow (FLT_RADIX, DBL_MANT_DIG))))
84 /* Avoid actual stack overflow in print. */
87 /* Level of nesting inside outputting backquote in new style. */
88 int new_backquote_output
;
90 /* Detect most circularities to print finite output. */
91 #define PRINT_CIRCLE 200
92 Lisp_Object being_printed
[PRINT_CIRCLE
];
94 /* When printing into a buffer, first we put the text in this
95 block, then insert it all at once. */
98 /* Size allocated in print_buffer. */
99 EMACS_INT print_buffer_size
;
100 /* Chars stored in print_buffer. */
101 EMACS_INT print_buffer_pos
;
102 /* Bytes stored in print_buffer. */
103 EMACS_INT print_buffer_pos_byte
;
105 /* Maximum length of list to print in full; noninteger means
106 effectively infinity */
108 Lisp_Object Vprint_length
;
110 /* Maximum depth of list to print in full; noninteger means
111 effectively infinity. */
113 Lisp_Object Vprint_level
;
115 /* Nonzero means print newlines in strings as \n. */
117 int print_escape_newlines
;
119 /* Nonzero means to print single-byte non-ascii characters in strings as
122 int print_escape_nonascii
;
124 /* Nonzero means to print multibyte characters in strings as hex escapes. */
126 int print_escape_multibyte
;
128 Lisp_Object Qprint_escape_newlines
;
129 Lisp_Object Qprint_escape_multibyte
, Qprint_escape_nonascii
;
131 /* Nonzero means print (quote foo) forms as 'foo, etc. */
135 /* Non-nil means print #: before uninterned symbols. */
137 Lisp_Object Vprint_gensym
;
139 /* Non-nil means print recursive structures using #n= and #n# syntax. */
141 Lisp_Object Vprint_circle
;
143 /* Non-nil means keep continuous number for #n= and #n# syntax
144 between several print functions. */
146 Lisp_Object Vprint_continuous_numbering
;
148 /* Vprint_number_table is a table, that keeps objects that are going to
149 be printed, to allow use of #n= and #n# to express sharing.
150 For any given object, the table can give the following values:
151 t the object will be printed only once.
152 -N the object will be printed several times and will take number N.
153 N the object has been printed so we can refer to it as #N#.
154 print_number_index holds the largest N already used.
155 N has to be striclty larger than 0 since we need to distinguish -N. */
156 int print_number_index
;
157 Lisp_Object Vprint_number_table
;
159 void print_interval (INTERVAL interval
, Lisp_Object printcharfun
);
161 /* GDB resets this to zero on W32 to disable OutputDebugString calls. */
162 int print_output_debug_flag EXTERNALLY_VISIBLE
= 1;
165 /* Low level output routines for characters and strings */
167 /* Lisp functions to do output using a stream
168 must have the stream in a variable called printcharfun
169 and must start with PRINTPREPARE, end with PRINTFINISH,
170 and use PRINTDECLARE to declare common variables.
171 Use PRINTCHAR to output one character,
172 or call strout to output a block of characters. */
174 #define PRINTDECLARE \
175 struct buffer *old = current_buffer; \
176 EMACS_INT old_point = -1, start_point = -1; \
177 EMACS_INT old_point_byte = -1, start_point_byte = -1; \
178 int specpdl_count = SPECPDL_INDEX (); \
179 int free_print_buffer = 0; \
180 int multibyte = !NILP (current_buffer->enable_multibyte_characters); \
183 #define PRINTPREPARE \
184 original = printcharfun; \
185 if (NILP (printcharfun)) printcharfun = Qt; \
186 if (BUFFERP (printcharfun)) \
188 if (XBUFFER (printcharfun) != current_buffer) \
189 Fset_buffer (printcharfun); \
190 printcharfun = Qnil; \
192 if (MARKERP (printcharfun)) \
194 EMACS_INT marker_pos; \
195 if (! XMARKER (printcharfun)->buffer) \
196 error ("Marker does not point anywhere"); \
197 if (XMARKER (printcharfun)->buffer != current_buffer) \
198 set_buffer_internal (XMARKER (printcharfun)->buffer); \
199 marker_pos = marker_position (printcharfun); \
200 if (marker_pos < BEGV || marker_pos > ZV) \
201 error ("Marker is outside the accessible part of the buffer"); \
203 old_point_byte = PT_BYTE; \
204 SET_PT_BOTH (marker_pos, \
205 marker_byte_position (printcharfun)); \
207 start_point_byte = PT_BYTE; \
208 printcharfun = Qnil; \
210 if (NILP (printcharfun)) \
212 Lisp_Object string; \
213 if (NILP (current_buffer->enable_multibyte_characters) \
214 && ! print_escape_multibyte) \
215 specbind (Qprint_escape_multibyte, Qt); \
216 if (! NILP (current_buffer->enable_multibyte_characters) \
217 && ! print_escape_nonascii) \
218 specbind (Qprint_escape_nonascii, Qt); \
219 if (print_buffer != 0) \
221 string = make_string_from_bytes (print_buffer, \
223 print_buffer_pos_byte); \
224 record_unwind_protect (print_unwind, string); \
228 print_buffer_size = 1000; \
229 print_buffer = (char *) xmalloc (print_buffer_size); \
230 free_print_buffer = 1; \
232 print_buffer_pos = 0; \
233 print_buffer_pos_byte = 0; \
235 if (EQ (printcharfun, Qt) && ! noninteractive) \
236 setup_echo_area_for_printing (multibyte);
238 #define PRINTFINISH \
239 if (NILP (printcharfun)) \
241 if (print_buffer_pos != print_buffer_pos_byte \
242 && NILP (current_buffer->enable_multibyte_characters)) \
244 unsigned char *temp \
245 = (unsigned char *) alloca (print_buffer_pos + 1); \
246 copy_text (print_buffer, temp, print_buffer_pos_byte, \
248 insert_1_both (temp, print_buffer_pos, \
249 print_buffer_pos, 0, 1, 0); \
252 insert_1_both (print_buffer, print_buffer_pos, \
253 print_buffer_pos_byte, 0, 1, 0); \
254 signal_after_change (PT - print_buffer_pos, 0, print_buffer_pos);\
256 if (free_print_buffer) \
258 xfree (print_buffer); \
261 unbind_to (specpdl_count, Qnil); \
262 if (MARKERP (original)) \
263 set_marker_both (original, Qnil, PT, PT_BYTE); \
264 if (old_point >= 0) \
265 SET_PT_BOTH (old_point + (old_point >= start_point \
266 ? PT - start_point : 0), \
267 old_point_byte + (old_point_byte >= start_point_byte \
268 ? PT_BYTE - start_point_byte : 0)); \
269 if (old != current_buffer) \
270 set_buffer_internal (old);
272 #define PRINTCHAR(ch) printchar (ch, printcharfun)
274 /* This is used to restore the saved contents of print_buffer
275 when there is a recursive call to print. */
278 print_unwind (Lisp_Object saved_text
)
280 memcpy (print_buffer
, SDATA (saved_text
), SCHARS (saved_text
));
285 /* Print character CH using method FUN. FUN nil means print to
286 print_buffer. FUN t means print to echo area or stdout if
287 non-interactive. If FUN is neither nil nor t, call FUN with CH as
291 printchar (unsigned int ch
, Lisp_Object fun
)
293 if (!NILP (fun
) && !EQ (fun
, Qt
))
294 call1 (fun
, make_number (ch
));
297 unsigned char str
[MAX_MULTIBYTE_LENGTH
];
298 int len
= CHAR_STRING (ch
, str
);
304 if (print_buffer_pos_byte
+ len
>= print_buffer_size
)
305 print_buffer
= (char *) xrealloc (print_buffer
,
306 print_buffer_size
*= 2);
307 memcpy (print_buffer
+ print_buffer_pos_byte
, str
, len
);
308 print_buffer_pos
+= 1;
309 print_buffer_pos_byte
+= len
;
311 else if (noninteractive
)
313 fwrite (str
, 1, len
, stdout
);
314 noninteractive_need_newline
= 1;
319 = !NILP (current_buffer
->enable_multibyte_characters
);
321 setup_echo_area_for_printing (multibyte_p
);
323 message_dolog (str
, len
, 0, multibyte_p
);
329 /* Output SIZE characters, SIZE_BYTE bytes from string PTR using
330 method PRINTCHARFUN. If SIZE < 0, use the string length of PTR for
331 both SIZE and SIZE_BYTE. PRINTCHARFUN nil means output to
332 print_buffer. PRINTCHARFUN t means output to the echo area or to
333 stdout if non-interactive. If neither nil nor t, call Lisp
334 function PRINTCHARFUN for each character printed. MULTIBYTE
335 non-zero means PTR contains multibyte characters.
337 In the case where PRINTCHARFUN is nil, it is safe for PTR to point
338 to data in a Lisp string. Otherwise that is not safe. */
341 strout (const char *ptr
, EMACS_INT size
, EMACS_INT size_byte
,
342 Lisp_Object printcharfun
, int multibyte
)
345 size_byte
= size
= strlen (ptr
);
347 if (NILP (printcharfun
))
349 if (print_buffer_pos_byte
+ size_byte
> print_buffer_size
)
351 print_buffer_size
= print_buffer_size
* 2 + size_byte
;
352 print_buffer
= (char *) xrealloc (print_buffer
,
355 memcpy (print_buffer
+ print_buffer_pos_byte
, ptr
, size_byte
);
356 print_buffer_pos
+= size
;
357 print_buffer_pos_byte
+= size_byte
;
359 else if (noninteractive
&& EQ (printcharfun
, Qt
))
361 fwrite (ptr
, 1, size_byte
, stdout
);
362 noninteractive_need_newline
= 1;
364 else if (EQ (printcharfun
, Qt
))
366 /* Output to echo area. We're trying to avoid a little overhead
367 here, that's the reason we don't call printchar to do the
371 = !NILP (current_buffer
->enable_multibyte_characters
);
373 setup_echo_area_for_printing (multibyte_p
);
374 message_dolog (ptr
, size_byte
, 0, multibyte_p
);
376 if (size
== size_byte
)
378 for (i
= 0; i
< size
; ++i
)
379 insert_char ((unsigned char) *ptr
++);
384 for (i
= 0; i
< size_byte
; i
+= len
)
386 int ch
= STRING_CHAR_AND_LENGTH (ptr
+ i
, len
);
393 /* PRINTCHARFUN is a Lisp function. */
396 if (size
== size_byte
)
398 while (i
< size_byte
)
406 while (i
< size_byte
)
408 /* Here, we must convert each multi-byte form to the
409 corresponding character code before handing it to
412 int ch
= STRING_CHAR_AND_LENGTH (ptr
+ i
, len
);
420 /* Print the contents of a string STRING using PRINTCHARFUN.
421 It isn't safe to use strout in many cases,
422 because printing one char can relocate. */
425 print_string (Lisp_Object string
, Lisp_Object printcharfun
)
427 if (EQ (printcharfun
, Qt
) || NILP (printcharfun
))
431 if (print_escape_nonascii
)
432 string
= string_escape_byte8 (string
);
434 if (STRING_MULTIBYTE (string
))
435 chars
= SCHARS (string
);
436 else if (! print_escape_nonascii
437 && (EQ (printcharfun
, Qt
)
438 ? ! NILP (buffer_defaults
.enable_multibyte_characters
)
439 : ! NILP (current_buffer
->enable_multibyte_characters
)))
441 /* If unibyte string STRING contains 8-bit codes, we must
442 convert STRING to a multibyte string containing the same
447 chars
= SBYTES (string
);
448 bytes
= parse_str_to_multibyte (SDATA (string
), chars
);
451 newstr
= make_uninit_multibyte_string (chars
, bytes
);
452 memcpy (SDATA (newstr
), SDATA (string
), chars
);
453 str_to_multibyte (SDATA (newstr
), bytes
, chars
);
458 chars
= SBYTES (string
);
460 if (EQ (printcharfun
, Qt
))
462 /* Output to echo area. */
463 EMACS_INT nbytes
= SBYTES (string
);
466 /* Copy the string contents so that relocation of STRING by
467 GC does not cause trouble. */
470 SAFE_ALLOCA (buffer
, char *, nbytes
);
471 memcpy (buffer
, SDATA (string
), nbytes
);
473 strout (buffer
, chars
, SBYTES (string
),
474 printcharfun
, STRING_MULTIBYTE (string
));
479 /* No need to copy, since output to print_buffer can't GC. */
480 strout (SDATA (string
),
481 chars
, SBYTES (string
),
482 printcharfun
, STRING_MULTIBYTE (string
));
486 /* Otherwise, string may be relocated by printing one char.
487 So re-fetch the string address for each character. */
489 EMACS_INT size
= SCHARS (string
);
490 EMACS_INT size_byte
= SBYTES (string
);
493 if (size
== size_byte
)
494 for (i
= 0; i
< size
; i
++)
495 PRINTCHAR (SREF (string
, i
));
497 for (i
= 0; i
< size_byte
; )
499 /* Here, we must convert each multi-byte form to the
500 corresponding character code before handing it to PRINTCHAR. */
502 int ch
= STRING_CHAR_AND_LENGTH (SDATA (string
) + i
, len
);
510 DEFUN ("write-char", Fwrite_char
, Swrite_char
, 1, 2, 0,
511 doc
: /* Output character CHARACTER to stream PRINTCHARFUN.
512 PRINTCHARFUN defaults to the value of `standard-output' (which see). */)
513 (Lisp_Object character
, Lisp_Object printcharfun
)
517 if (NILP (printcharfun
))
518 printcharfun
= Vstandard_output
;
519 CHECK_NUMBER (character
);
521 PRINTCHAR (XINT (character
));
526 /* Used from outside of print.c to print a block of SIZE
527 single-byte chars at DATA on the default output stream.
528 Do not use this on the contents of a Lisp string. */
531 write_string (const char *data
, int size
)
534 Lisp_Object printcharfun
;
536 printcharfun
= Vstandard_output
;
539 strout (data
, size
, size
, printcharfun
, 0);
543 /* Used to print a block of SIZE single-byte chars at DATA on a
544 specified stream PRINTCHARFUN.
545 Do not use this on the contents of a Lisp string. */
548 write_string_1 (const char *data
, int size
, Lisp_Object printcharfun
)
553 strout (data
, size
, size
, printcharfun
, 0);
559 temp_output_buffer_setup (const char *bufname
)
561 int count
= SPECPDL_INDEX ();
562 register struct buffer
*old
= current_buffer
;
563 register Lisp_Object buf
;
565 record_unwind_protect (set_buffer_if_live
, Fcurrent_buffer ());
567 Fset_buffer (Fget_buffer_create (build_string (bufname
)));
569 Fkill_all_local_variables ();
570 delete_all_overlays (current_buffer
);
571 current_buffer
->directory
= old
->directory
;
572 current_buffer
->read_only
= Qnil
;
573 current_buffer
->filename
= Qnil
;
574 current_buffer
->undo_list
= Qt
;
575 eassert (current_buffer
->overlays_before
== NULL
);
576 eassert (current_buffer
->overlays_after
== NULL
);
577 current_buffer
->enable_multibyte_characters
578 = buffer_defaults
.enable_multibyte_characters
;
579 specbind (Qinhibit_read_only
, Qt
);
580 specbind (Qinhibit_modification_hooks
, Qt
);
582 XSETBUFFER (buf
, current_buffer
);
584 Frun_hooks (1, &Qtemp_buffer_setup_hook
);
586 unbind_to (count
, Qnil
);
588 specbind (Qstandard_output
, buf
);
592 internal_with_output_to_temp_buffer (const char *bufname
, Lisp_Object (*function
) (Lisp_Object
), Lisp_Object args
)
594 int count
= SPECPDL_INDEX ();
595 Lisp_Object buf
, val
;
599 record_unwind_protect (Fset_buffer
, Fcurrent_buffer ());
600 temp_output_buffer_setup (bufname
);
601 buf
= Vstandard_output
;
604 val
= (*function
) (args
);
607 temp_output_buffer_show (buf
);
610 return unbind_to (count
, val
);
613 DEFUN ("with-output-to-temp-buffer",
614 Fwith_output_to_temp_buffer
, Swith_output_to_temp_buffer
,
616 doc
: /* Bind `standard-output' to buffer BUFNAME, eval BODY, then show that buffer.
618 This construct makes buffer BUFNAME empty before running BODY.
619 It does not make the buffer current for BODY.
620 Instead it binds `standard-output' to that buffer, so that output
621 generated with `prin1' and similar functions in BODY goes into
624 At the end of BODY, this marks buffer BUFNAME unmodifed and displays
625 it in a window, but does not select it. The normal way to do this is
626 by calling `display-buffer', then running `temp-buffer-show-hook'.
627 However, if `temp-buffer-show-function' is non-nil, it calls that
628 function instead (and does not run `temp-buffer-show-hook'). The
629 function gets one argument, the buffer to display.
631 The return value of `with-output-to-temp-buffer' is the value of the
632 last form in BODY. If BODY does not finish normally, the buffer
633 BUFNAME is not displayed.
635 This runs the hook `temp-buffer-setup-hook' before BODY,
636 with the buffer BUFNAME temporarily current. It runs the hook
637 `temp-buffer-show-hook' after displaying buffer BUFNAME, with that
638 buffer temporarily current, and the window that was used to display it
639 temporarily selected. But it doesn't run `temp-buffer-show-hook'
640 if it uses `temp-buffer-show-function'.
642 usage: (with-output-to-temp-buffer BUFNAME BODY...) */)
647 int count
= SPECPDL_INDEX ();
648 Lisp_Object buf
, val
;
651 name
= Feval (Fcar (args
));
653 temp_output_buffer_setup (SDATA (name
));
654 buf
= Vstandard_output
;
657 val
= Fprogn (XCDR (args
));
660 temp_output_buffer_show (buf
);
663 return unbind_to (count
, val
);
667 static void print (Lisp_Object obj
, register Lisp_Object printcharfun
, int escapeflag
);
668 static void print_preprocess (Lisp_Object obj
);
669 static void print_preprocess_string (INTERVAL interval
, Lisp_Object arg
);
670 static void print_object (Lisp_Object obj
, register Lisp_Object printcharfun
, int escapeflag
);
672 DEFUN ("terpri", Fterpri
, Sterpri
, 0, 1, 0,
673 doc
: /* Output a newline to stream PRINTCHARFUN.
674 If PRINTCHARFUN is omitted or nil, the value of `standard-output' is used. */)
675 (Lisp_Object printcharfun
)
679 if (NILP (printcharfun
))
680 printcharfun
= Vstandard_output
;
687 DEFUN ("prin1", Fprin1
, Sprin1
, 1, 2, 0,
688 doc
: /* Output the printed representation of OBJECT, any Lisp object.
689 Quoting characters are printed when needed to make output that `read'
690 can handle, whenever this is possible. For complex objects, the behavior
691 is controlled by `print-level' and `print-length', which see.
693 OBJECT is any of the Lisp data types: a number, a string, a symbol,
694 a list, a buffer, a window, a frame, etc.
696 A printed representation of an object is text which describes that object.
698 Optional argument PRINTCHARFUN is the output stream, which can be one
701 - a buffer, in which case output is inserted into that buffer at point;
702 - a marker, in which case output is inserted at marker's position;
703 - a function, in which case that function is called once for each
704 character of OBJECT's printed representation;
705 - a symbol, in which case that symbol's function definition is called; or
706 - t, in which case the output is displayed in the echo area.
708 If PRINTCHARFUN is omitted, the value of `standard-output' (which see)
710 (Lisp_Object object
, Lisp_Object printcharfun
)
714 if (NILP (printcharfun
))
715 printcharfun
= Vstandard_output
;
717 print (object
, printcharfun
, 1);
722 /* a buffer which is used to hold output being built by prin1-to-string */
723 Lisp_Object Vprin1_to_string_buffer
;
725 DEFUN ("prin1-to-string", Fprin1_to_string
, Sprin1_to_string
, 1, 2, 0,
726 doc
: /* Return a string containing the printed representation of OBJECT.
727 OBJECT can be any Lisp object. This function outputs quoting characters
728 when necessary to make output that `read' can handle, whenever possible,
729 unless the optional second argument NOESCAPE is non-nil. For complex objects,
730 the behavior is controlled by `print-level' and `print-length', which see.
732 OBJECT is any of the Lisp data types: a number, a string, a symbol,
733 a list, a buffer, a window, a frame, etc.
735 A printed representation of an object is text which describes that object. */)
736 (Lisp_Object object
, Lisp_Object noescape
)
738 Lisp_Object printcharfun
;
739 /* struct gcpro gcpro1, gcpro2; */
740 Lisp_Object save_deactivate_mark
;
741 int count
= SPECPDL_INDEX ();
742 struct buffer
*previous
;
744 specbind (Qinhibit_modification_hooks
, Qt
);
749 /* Save and restore this--we are altering a buffer
750 but we don't want to deactivate the mark just for that.
751 No need for specbind, since errors deactivate the mark. */
752 save_deactivate_mark
= Vdeactivate_mark
;
753 /* GCPRO2 (object, save_deactivate_mark); */
756 printcharfun
= Vprin1_to_string_buffer
;
758 print (object
, printcharfun
, NILP (noescape
));
759 /* Make Vprin1_to_string_buffer be the default buffer after PRINTFINSH */
763 previous
= current_buffer
;
764 set_buffer_internal (XBUFFER (Vprin1_to_string_buffer
));
765 object
= Fbuffer_string ();
766 if (SBYTES (object
) == SCHARS (object
))
767 STRING_SET_UNIBYTE (object
);
769 /* Note that this won't make prepare_to_modify_buffer call
770 ask-user-about-supersession-threat because this buffer
771 does not visit a file. */
773 set_buffer_internal (previous
);
775 Vdeactivate_mark
= save_deactivate_mark
;
779 return unbind_to (count
, object
);
782 DEFUN ("princ", Fprinc
, Sprinc
, 1, 2, 0,
783 doc
: /* Output the printed representation of OBJECT, any Lisp object.
784 No quoting characters are used; no delimiters are printed around
785 the contents of strings.
787 OBJECT is any of the Lisp data types: a number, a string, a symbol,
788 a list, a buffer, a window, a frame, etc.
790 A printed representation of an object is text which describes that object.
792 Optional argument PRINTCHARFUN is the output stream, which can be one
795 - a buffer, in which case output is inserted into that buffer at point;
796 - a marker, in which case output is inserted at marker's position;
797 - a function, in which case that function is called once for each
798 character of OBJECT's printed representation;
799 - a symbol, in which case that symbol's function definition is called; or
800 - t, in which case the output is displayed in the echo area.
802 If PRINTCHARFUN is omitted, the value of `standard-output' (which see)
804 (Lisp_Object object
, Lisp_Object printcharfun
)
808 if (NILP (printcharfun
))
809 printcharfun
= Vstandard_output
;
811 print (object
, printcharfun
, 0);
816 DEFUN ("print", Fprint
, Sprint
, 1, 2, 0,
817 doc
: /* Output the printed representation of OBJECT, with newlines around it.
818 Quoting characters are printed when needed to make output that `read'
819 can handle, whenever this is possible. For complex objects, the behavior
820 is controlled by `print-level' and `print-length', which see.
822 OBJECT is any of the Lisp data types: a number, a string, a symbol,
823 a list, a buffer, a window, a frame, etc.
825 A printed representation of an object is text which describes that object.
827 Optional argument PRINTCHARFUN is the output stream, which can be one
830 - a buffer, in which case output is inserted into that buffer at point;
831 - a marker, in which case output is inserted at marker's position;
832 - a function, in which case that function is called once for each
833 character of OBJECT's printed representation;
834 - a symbol, in which case that symbol's function definition is called; or
835 - t, in which case the output is displayed in the echo area.
837 If PRINTCHARFUN is omitted, the value of `standard-output' (which see)
839 (Lisp_Object object
, Lisp_Object printcharfun
)
844 if (NILP (printcharfun
))
845 printcharfun
= Vstandard_output
;
849 print (object
, printcharfun
, 1);
856 /* The subroutine object for external-debugging-output is kept here
857 for the convenience of the debugger. */
858 Lisp_Object Qexternal_debugging_output
;
860 DEFUN ("external-debugging-output", Fexternal_debugging_output
, Sexternal_debugging_output
, 1, 1, 0,
861 doc
: /* Write CHARACTER to stderr.
862 You can call print while debugging emacs, and pass it this function
863 to make it write to the debugging output. */)
864 (Lisp_Object character
)
866 CHECK_NUMBER (character
);
867 putc ((int) XINT (character
), stderr
);
870 /* Send the output to a debugger (nothing happens if there isn't one). */
871 if (print_output_debug_flag
)
873 char buf
[2] = {(char) XINT (character
), '\0'};
874 OutputDebugString (buf
);
881 /* This function is never called. Its purpose is to prevent
882 print_output_debug_flag from being optimized away. */
885 debug_output_compilation_hack (int x
)
887 print_output_debug_flag
= x
;
890 #if defined (GNU_LINUX)
892 /* This functionality is not vitally important in general, so we rely on
893 non-portable ability to use stderr as lvalue. */
895 #define WITH_REDIRECT_DEBUGGING_OUTPUT 1
897 FILE *initial_stderr_stream
= NULL
;
899 DEFUN ("redirect-debugging-output", Fredirect_debugging_output
, Sredirect_debugging_output
,
901 "FDebug output file: \nP",
902 doc
: /* Redirect debugging output (stderr stream) to file FILE.
903 If FILE is nil, reset target to the initial stderr stream.
904 Optional arg APPEND non-nil (interactively, with prefix arg) means
905 append to existing target file. */)
906 (Lisp_Object file
, Lisp_Object append
)
908 if (initial_stderr_stream
!= NULL
)
914 stderr
= initial_stderr_stream
;
915 initial_stderr_stream
= NULL
;
919 file
= Fexpand_file_name (file
, Qnil
);
920 initial_stderr_stream
= stderr
;
921 stderr
= fopen (SDATA (file
), NILP (append
) ? "w" : "a");
924 stderr
= initial_stderr_stream
;
925 initial_stderr_stream
= NULL
;
926 report_file_error ("Cannot open debugging output stream",
932 #endif /* GNU_LINUX */
935 /* This is the interface for debugging printing. */
938 debug_print (Lisp_Object arg
)
940 Fprin1 (arg
, Qexternal_debugging_output
);
941 fprintf (stderr
, "\r\n");
945 safe_debug_print (Lisp_Object arg
)
947 int valid
= valid_lisp_object_p (arg
);
952 fprintf (stderr
, "#<%s_LISP_OBJECT 0x%08lx>\r\n",
953 !valid
? "INVALID" : "SOME",
954 (unsigned long) XHASH (arg
)
959 DEFUN ("error-message-string", Ferror_message_string
, Serror_message_string
,
961 doc
: /* Convert an error value (ERROR-SYMBOL . DATA) to an error message.
962 See Info anchor `(elisp)Definition of signal' for some details on how this
963 error message is constructed. */)
966 struct buffer
*old
= current_buffer
;
970 /* If OBJ is (error STRING), just return STRING.
971 That is not only faster, it also avoids the need to allocate
972 space here when the error is due to memory full. */
973 if (CONSP (obj
) && EQ (XCAR (obj
), Qerror
)
974 && CONSP (XCDR (obj
))
975 && STRINGP (XCAR (XCDR (obj
)))
976 && NILP (XCDR (XCDR (obj
))))
977 return XCAR (XCDR (obj
));
979 print_error_message (obj
, Vprin1_to_string_buffer
, 0, Qnil
);
981 set_buffer_internal (XBUFFER (Vprin1_to_string_buffer
));
982 value
= Fbuffer_string ();
986 set_buffer_internal (old
);
992 /* Print an error message for the error DATA onto Lisp output stream
993 STREAM (suitable for the print functions).
994 CONTEXT is a C string describing the context of the error.
995 CALLER is the Lisp function inside which the error was signaled. */
998 print_error_message (Lisp_Object data
, Lisp_Object stream
, const char *context
,
1001 Lisp_Object errname
, errmsg
, file_error
, tail
;
1002 struct gcpro gcpro1
;
1006 write_string_1 (context
, -1, stream
);
1008 /* If we know from where the error was signaled, show it in
1010 if (!NILP (caller
) && SYMBOLP (caller
))
1012 Lisp_Object cname
= SYMBOL_NAME (caller
);
1013 char *name
= alloca (SBYTES (cname
));
1014 memcpy (name
, SDATA (cname
), SBYTES (cname
));
1015 message_dolog (name
, SBYTES (cname
), 0, 0);
1016 message_dolog (": ", 2, 0, 0);
1019 errname
= Fcar (data
);
1021 if (EQ (errname
, Qerror
))
1026 errmsg
= Fcar (data
);
1031 Lisp_Object error_conditions
;
1032 errmsg
= Fget (errname
, Qerror_message
);
1033 error_conditions
= Fget (errname
, Qerror_conditions
);
1034 file_error
= Fmemq (Qfile_error
, error_conditions
);
1037 /* Print an error message including the data items. */
1039 tail
= Fcdr_safe (data
);
1042 /* For file-error, make error message by concatenating
1043 all the data items. They are all strings. */
1044 if (!NILP (file_error
) && CONSP (tail
))
1045 errmsg
= XCAR (tail
), tail
= XCDR (tail
);
1047 if (STRINGP (errmsg
))
1048 Fprinc (errmsg
, stream
);
1050 write_string_1 ("peculiar error", -1, stream
);
1052 for (i
= 0; CONSP (tail
); tail
= XCDR (tail
), i
++)
1056 write_string_1 (i
? ", " : ": ", 2, stream
);
1058 if (!NILP (file_error
) || EQ (errname
, Qend_of_file
))
1059 Fprinc (obj
, stream
);
1061 Fprin1 (obj
, stream
);
1070 * The buffer should be at least as large as the max string size of the
1071 * largest float, printed in the biggest notation. This is undoubtedly
1072 * 20d float_output_format, with the negative of the C-constant "HUGE"
1075 * On the vax the worst case is -1e38 in 20d format which takes 61 bytes.
1077 * I assume that IEEE-754 format numbers can take 329 bytes for the worst
1078 * case of -1e307 in 20d float_output_format. What is one to do (short of
1079 * re-writing _doprnt to be more sane)?
1084 float_to_string (unsigned char *buf
, double data
)
1089 /* Check for plus infinity in a way that won't lose
1090 if there is no plus infinity. */
1091 if (data
== data
/ 2 && data
> 1.0)
1093 strcpy (buf
, "1.0e+INF");
1096 /* Likewise for minus infinity. */
1097 if (data
== data
/ 2 && data
< -1.0)
1099 strcpy (buf
, "-1.0e+INF");
1102 /* Check for NaN in a way that won't fail if there are no NaNs. */
1103 if (! (data
* 0.0 >= 0.0))
1105 /* Prepend "-" if the NaN's sign bit is negative.
1106 The sign bit of a double is the bit that is 1 in -0.0. */
1108 union { double d
; char c
[sizeof (double)]; } u_data
, u_minus_zero
;
1110 u_minus_zero
.d
= - 0.0;
1111 for (i
= 0; i
< sizeof (double); i
++)
1112 if (u_data
.c
[i
] & u_minus_zero
.c
[i
])
1118 strcpy (buf
, "0.0e+NaN");
1122 if (NILP (Vfloat_output_format
)
1123 || !STRINGP (Vfloat_output_format
))
1126 /* Generate the fewest number of digits that represent the
1127 floating point value without losing information.
1128 The following method is simple but a bit slow.
1129 For ideas about speeding things up, please see:
1131 Guy L Steele Jr & Jon L White, How to print floating-point numbers
1132 accurately. SIGPLAN notices 25, 6 (June 1990), 112-126.
1134 Robert G Burger & R Kent Dybvig, Printing floating point numbers
1135 quickly and accurately, SIGPLAN notices 31, 5 (May 1996), 108-116. */
1137 width
= fabs (data
) < DBL_MIN
? 1 : DBL_DIG
;
1139 sprintf (buf
, "%.*g", width
, data
);
1140 while (width
++ < DOUBLE_DIGITS_BOUND
&& atof (buf
) != data
);
1142 else /* oink oink */
1144 /* Check that the spec we have is fully valid.
1145 This means not only valid for printf,
1146 but meant for floats, and reasonable. */
1147 cp
= SDATA (Vfloat_output_format
);
1156 /* Check the width specification. */
1158 if ('0' <= *cp
&& *cp
<= '9')
1162 width
= (width
* 10) + (*cp
++ - '0');
1163 while (*cp
>= '0' && *cp
<= '9');
1165 /* A precision of zero is valid only for %f. */
1167 || (width
== 0 && *cp
!= 'f'))
1171 if (*cp
!= 'e' && *cp
!= 'f' && *cp
!= 'g')
1177 sprintf (buf
, SDATA (Vfloat_output_format
), data
);
1180 /* Make sure there is a decimal point with digit after, or an
1181 exponent, so that the value is readable as a float. But don't do
1182 this with "%.0f"; it's valid for that not to produce a decimal
1183 point. Note that width can be 0 only for %.0f. */
1186 for (cp
= buf
; *cp
; cp
++)
1187 if ((*cp
< '0' || *cp
> '9') && *cp
!= '-')
1190 if (*cp
== '.' && cp
[1] == 0)
1207 print (Lisp_Object obj
, register Lisp_Object printcharfun
, int escapeflag
)
1209 new_backquote_output
= 0;
1211 /* Reset print_number_index and Vprint_number_table only when
1212 the variable Vprint_continuous_numbering is nil. Otherwise,
1213 the values of these variables will be kept between several
1215 if (NILP (Vprint_continuous_numbering
)
1216 || NILP (Vprint_number_table
))
1218 print_number_index
= 0;
1219 Vprint_number_table
= Qnil
;
1222 /* Construct Vprint_number_table for print-gensym and print-circle. */
1223 if (!NILP (Vprint_gensym
) || !NILP (Vprint_circle
))
1225 /* Construct Vprint_number_table.
1226 This increments print_number_index for the objects added. */
1228 print_preprocess (obj
);
1230 if (HASH_TABLE_P (Vprint_number_table
))
1231 { /* Remove unnecessary objects, which appear only once in OBJ;
1232 that is, whose status is Qt.
1233 Maybe a better way to do that is to copy elements to
1234 a new hash table. */
1235 struct Lisp_Hash_Table
*h
= XHASH_TABLE (Vprint_number_table
);
1238 for (i
= 0; i
< HASH_TABLE_SIZE (h
); ++i
)
1239 if (!NILP (HASH_HASH (h
, i
))
1240 && EQ (HASH_VALUE (h
, i
), Qt
))
1241 Fremhash (HASH_KEY (h
, i
), Vprint_number_table
);
1246 print_object (obj
, printcharfun
, escapeflag
);
1249 /* Construct Vprint_number_table according to the structure of OBJ.
1250 OBJ itself and all its elements will be added to Vprint_number_table
1251 recursively if it is a list, vector, compiled function, char-table,
1252 string (its text properties will be traced), or a symbol that has
1253 no obarray (this is for the print-gensym feature).
1254 The status fields of Vprint_number_table mean whether each object appears
1255 more than once in OBJ: Qnil at the first time, and Qt after that . */
1257 print_preprocess (Lisp_Object obj
)
1262 Lisp_Object halftail
;
1264 /* Give up if we go so deep that print_object will get an error. */
1265 /* See similar code in print_object. */
1266 if (print_depth
>= PRINT_CIRCLE
)
1267 error ("Apparently circular structure being printed");
1269 /* Avoid infinite recursion for circular nested structure
1270 in the case where Vprint_circle is nil. */
1271 if (NILP (Vprint_circle
))
1273 for (i
= 0; i
< print_depth
; i
++)
1274 if (EQ (obj
, being_printed
[i
]))
1276 being_printed
[print_depth
] = obj
;
1283 if (STRINGP (obj
) || CONSP (obj
) || VECTORP (obj
)
1284 || COMPILEDP (obj
) || CHAR_TABLE_P (obj
) || SUB_CHAR_TABLE_P (obj
)
1285 || HASH_TABLE_P (obj
)
1286 || (! NILP (Vprint_gensym
)
1288 && !SYMBOL_INTERNED_P (obj
)))
1290 if (!HASH_TABLE_P (Vprint_number_table
))
1292 Lisp_Object args
[2];
1295 Vprint_number_table
= Fmake_hash_table (2, args
);
1298 /* In case print-circle is nil and print-gensym is t,
1299 add OBJ to Vprint_number_table only when OBJ is a symbol. */
1300 if (! NILP (Vprint_circle
) || SYMBOLP (obj
))
1302 Lisp_Object num
= Fgethash (obj
, Vprint_number_table
, Qnil
);
1304 /* If Vprint_continuous_numbering is non-nil and OBJ is a gensym,
1305 always print the gensym with a number. This is a special for
1306 the lisp function byte-compile-output-docform. */
1307 || (!NILP (Vprint_continuous_numbering
)
1309 && !SYMBOL_INTERNED_P (obj
)))
1310 { /* OBJ appears more than once. Let's remember that. */
1311 if (!INTEGERP (num
))
1313 print_number_index
++;
1314 /* Negative number indicates it hasn't been printed yet. */
1315 Fputhash (obj
, make_number (- print_number_index
),
1316 Vprint_number_table
);
1322 /* OBJ is not yet recorded. Let's add to the table. */
1323 Fputhash (obj
, Qt
, Vprint_number_table
);
1326 switch (XTYPE (obj
))
1329 /* A string may have text properties, which can be circular. */
1330 traverse_intervals_noorder (STRING_INTERVALS (obj
),
1331 print_preprocess_string
, Qnil
);
1335 /* Use HALFTAIL and LOOP_COUNT to detect circular lists,
1336 just as in print_object. */
1337 if (loop_count
&& EQ (obj
, halftail
))
1339 print_preprocess (XCAR (obj
));
1342 if (!(loop_count
& 1))
1343 halftail
= XCDR (halftail
);
1346 case Lisp_Vectorlike
:
1347 size
= XVECTOR (obj
)->size
;
1348 if (size
& PSEUDOVECTOR_FLAG
)
1349 size
&= PSEUDOVECTOR_SIZE_MASK
;
1350 for (i
= 0; i
< size
; i
++)
1351 print_preprocess (XVECTOR (obj
)->contents
[i
]);
1352 if (HASH_TABLE_P (obj
))
1353 { /* For hash tables, the key_and_value slot is past
1354 `size' because it needs to be marked specially in case
1355 the table is weak. */
1356 struct Lisp_Hash_Table
*h
= XHASH_TABLE (obj
);
1357 print_preprocess (h
->key_and_value
);
1369 print_preprocess_string (INTERVAL interval
, Lisp_Object arg
)
1371 print_preprocess (interval
->plist
);
1374 /* A flag to control printing of `charset' text property.
1375 The default value is Qdefault. */
1376 Lisp_Object Vprint_charset_text_property
;
1378 static void print_check_string_charset_prop (INTERVAL interval
, Lisp_Object string
);
1380 #define PRINT_STRING_NON_CHARSET_FOUND 1
1381 #define PRINT_STRING_UNSAFE_CHARSET_FOUND 2
1383 /* Bitwise or of the above macros. */
1384 static int print_check_string_result
;
1387 print_check_string_charset_prop (INTERVAL interval
, Lisp_Object string
)
1391 if (NILP (interval
->plist
)
1392 || (print_check_string_result
== (PRINT_STRING_NON_CHARSET_FOUND
1393 | PRINT_STRING_UNSAFE_CHARSET_FOUND
)))
1395 for (val
= interval
->plist
; CONSP (val
) && ! EQ (XCAR (val
), Qcharset
);
1396 val
= XCDR (XCDR (val
)));
1399 print_check_string_result
|= PRINT_STRING_NON_CHARSET_FOUND
;
1402 if (! (print_check_string_result
& PRINT_STRING_NON_CHARSET_FOUND
))
1404 if (! EQ (val
, interval
->plist
)
1405 || CONSP (XCDR (XCDR (val
))))
1406 print_check_string_result
|= PRINT_STRING_NON_CHARSET_FOUND
;
1408 if (NILP (Vprint_charset_text_property
)
1409 || ! (print_check_string_result
& PRINT_STRING_UNSAFE_CHARSET_FOUND
))
1412 EMACS_INT charpos
= interval
->position
;
1413 EMACS_INT bytepos
= string_char_to_byte (string
, charpos
);
1414 Lisp_Object charset
;
1416 charset
= XCAR (XCDR (val
));
1417 for (i
= 0; i
< LENGTH (interval
); i
++)
1419 FETCH_STRING_CHAR_ADVANCE (c
, string
, charpos
, bytepos
);
1420 if (! ASCII_CHAR_P (c
)
1421 && ! EQ (CHARSET_NAME (CHAR_CHARSET (c
)), charset
))
1423 print_check_string_result
|= PRINT_STRING_UNSAFE_CHARSET_FOUND
;
1430 /* The value is (charset . nil). */
1431 static Lisp_Object print_prune_charset_plist
;
1434 print_prune_string_charset (Lisp_Object string
)
1436 print_check_string_result
= 0;
1437 traverse_intervals (STRING_INTERVALS (string
), 0,
1438 print_check_string_charset_prop
, string
);
1439 if (! (print_check_string_result
& PRINT_STRING_UNSAFE_CHARSET_FOUND
))
1441 string
= Fcopy_sequence (string
);
1442 if (print_check_string_result
& PRINT_STRING_NON_CHARSET_FOUND
)
1444 if (NILP (print_prune_charset_plist
))
1445 print_prune_charset_plist
= Fcons (Qcharset
, Qnil
);
1446 Fremove_text_properties (make_number (0),
1447 make_number (SCHARS (string
)),
1448 print_prune_charset_plist
, string
);
1451 Fset_text_properties (make_number (0), make_number (SCHARS (string
)),
1458 print_object (Lisp_Object obj
, register Lisp_Object printcharfun
, int escapeflag
)
1464 /* See similar code in print_preprocess. */
1465 if (print_depth
>= PRINT_CIRCLE
)
1466 error ("Apparently circular structure being printed");
1468 /* Detect circularities and truncate them. */
1469 if (STRINGP (obj
) || CONSP (obj
) || VECTORP (obj
)
1470 || COMPILEDP (obj
) || CHAR_TABLE_P (obj
) || SUB_CHAR_TABLE_P (obj
)
1471 || HASH_TABLE_P (obj
)
1472 || (! NILP (Vprint_gensym
)
1474 && !SYMBOL_INTERNED_P (obj
)))
1476 if (NILP (Vprint_circle
) && NILP (Vprint_gensym
))
1478 /* Simple but incomplete way. */
1480 for (i
= 0; i
< print_depth
; i
++)
1481 if (EQ (obj
, being_printed
[i
]))
1483 sprintf (buf
, "#%d", i
);
1484 strout (buf
, -1, -1, printcharfun
, 0);
1487 being_printed
[print_depth
] = obj
;
1491 /* With the print-circle feature. */
1492 Lisp_Object num
= Fgethash (obj
, Vprint_number_table
, Qnil
);
1497 { /* Add a prefix #n= if OBJ has not yet been printed;
1498 that is, its status field is nil. */
1499 sprintf (buf
, "#%d=", -n
);
1500 strout (buf
, -1, -1, printcharfun
, 0);
1501 /* OBJ is going to be printed. Remember that fact. */
1502 Fputhash (obj
, make_number (- n
), Vprint_number_table
);
1506 /* Just print #n# if OBJ has already been printed. */
1507 sprintf (buf
, "#%d#", n
);
1508 strout (buf
, -1, -1, printcharfun
, 0);
1517 switch (XTYPE (obj
))
1520 if (sizeof (int) == sizeof (EMACS_INT
))
1521 sprintf (buf
, "%d", (int) XINT (obj
));
1522 else if (sizeof (long) == sizeof (EMACS_INT
))
1523 sprintf (buf
, "%ld", (long) XINT (obj
));
1526 strout (buf
, -1, -1, printcharfun
, 0);
1531 char pigbuf
[350]; /* see comments in float_to_string */
1533 float_to_string (pigbuf
, XFLOAT_DATA (obj
));
1534 strout (pigbuf
, -1, -1, printcharfun
, 0);
1540 print_string (obj
, printcharfun
);
1543 register EMACS_INT i
, i_byte
;
1544 struct gcpro gcpro1
;
1546 EMACS_INT size_byte
;
1547 /* 1 means we must ensure that the next character we output
1548 cannot be taken as part of a hex character escape. */
1549 int need_nonhex
= 0;
1550 int multibyte
= STRING_MULTIBYTE (obj
);
1554 if (! EQ (Vprint_charset_text_property
, Qt
))
1555 obj
= print_prune_string_charset (obj
);
1557 if (!NULL_INTERVAL_P (STRING_INTERVALS (obj
)))
1565 size_byte
= SBYTES (obj
);
1567 for (i
= 0, i_byte
= 0; i_byte
< size_byte
;)
1569 /* Here, we must convert each multi-byte form to the
1570 corresponding character code before handing it to PRINTCHAR. */
1576 c
= STRING_CHAR_AND_LENGTH (str
+ i_byte
, len
);
1584 if (c
== '\n' && print_escape_newlines
)
1589 else if (c
== '\f' && print_escape_newlines
)
1595 && (CHAR_BYTE8_P (c
)
1596 || (! ASCII_CHAR_P (c
) && print_escape_multibyte
)))
1598 /* When multibyte is disabled,
1599 print multibyte string chars using hex escapes.
1600 For a char code that could be in a unibyte string,
1601 when found in a multibyte string, always use a hex escape
1602 so it reads back as multibyte. */
1603 unsigned char outbuf
[50];
1605 if (CHAR_BYTE8_P (c
))
1606 sprintf (outbuf
, "\\%03o", CHAR_TO_BYTE8 (c
));
1609 sprintf (outbuf
, "\\x%04x", c
);
1612 strout (outbuf
, -1, -1, printcharfun
, 0);
1614 else if (! multibyte
1615 && SINGLE_BYTE_CHAR_P (c
) && ! ASCII_BYTE_P (c
)
1616 && print_escape_nonascii
)
1618 /* When printing in a multibyte buffer
1619 or when explicitly requested,
1620 print single-byte non-ASCII string chars
1621 using octal escapes. */
1622 unsigned char outbuf
[5];
1623 sprintf (outbuf
, "\\%03o", c
);
1624 strout (outbuf
, -1, -1, printcharfun
, 0);
1628 /* If we just had a hex escape, and this character
1629 could be taken as part of it,
1630 output `\ ' to prevent that. */
1634 if ((c
>= 'a' && c
<= 'f')
1635 || (c
>= 'A' && c
<= 'F')
1636 || (c
>= '0' && c
<= '9'))
1637 strout ("\\ ", -1, -1, printcharfun
, 0);
1640 if (c
== '\"' || c
== '\\')
1647 if (!NULL_INTERVAL_P (STRING_INTERVALS (obj
)))
1649 traverse_intervals (STRING_INTERVALS (obj
),
1650 0, print_interval
, printcharfun
);
1660 register int confusing
;
1661 register unsigned char *p
= SDATA (SYMBOL_NAME (obj
));
1662 register unsigned char *end
= p
+ SBYTES (SYMBOL_NAME (obj
));
1665 EMACS_INT size_byte
;
1668 name
= SYMBOL_NAME (obj
);
1670 if (p
!= end
&& (*p
== '-' || *p
== '+')) p
++;
1673 /* If symbol name begins with a digit, and ends with a digit,
1674 and contains nothing but digits and `e', it could be treated
1675 as a number. So set CONFUSING.
1677 Symbols that contain periods could also be taken as numbers,
1678 but periods are always escaped, so we don't have to worry
1680 else if (*p
>= '0' && *p
<= '9'
1681 && end
[-1] >= '0' && end
[-1] <= '9')
1683 while (p
!= end
&& ((*p
>= '0' && *p
<= '9')
1684 /* Needed for \2e10. */
1685 || *p
== 'e' || *p
== 'E'))
1687 confusing
= (end
== p
);
1692 if (! NILP (Vprint_gensym
) && !SYMBOL_INTERNED_P (obj
))
1698 size_byte
= SBYTES (name
);
1700 for (i
= 0, i_byte
= 0; i_byte
< size_byte
;)
1702 /* Here, we must convert each multi-byte form to the
1703 corresponding character code before handing it to PRINTCHAR. */
1704 FETCH_STRING_CHAR_ADVANCE (c
, name
, i
, i_byte
);
1709 if (c
== '\"' || c
== '\\' || c
== '\''
1710 || c
== ';' || c
== '#' || c
== '(' || c
== ')'
1711 || c
== ',' || c
=='.' || c
== '`'
1712 || c
== '[' || c
== ']' || c
== '?' || c
<= 040
1714 PRINTCHAR ('\\'), confusing
= 0;
1722 /* If deeper than spec'd depth, print placeholder. */
1723 if (INTEGERP (Vprint_level
)
1724 && print_depth
> XINT (Vprint_level
))
1725 strout ("...", -1, -1, printcharfun
, 0);
1726 else if (print_quoted
&& CONSP (XCDR (obj
)) && NILP (XCDR (XCDR (obj
)))
1727 && (EQ (XCAR (obj
), Qquote
)))
1730 print_object (XCAR (XCDR (obj
)), printcharfun
, escapeflag
);
1732 else if (print_quoted
&& CONSP (XCDR (obj
)) && NILP (XCDR (XCDR (obj
)))
1733 && (EQ (XCAR (obj
), Qfunction
)))
1737 print_object (XCAR (XCDR (obj
)), printcharfun
, escapeflag
);
1739 else if (print_quoted
&& CONSP (XCDR (obj
)) && NILP (XCDR (XCDR (obj
)))
1740 && ((EQ (XCAR (obj
), Qbackquote
))))
1742 print_object (XCAR (obj
), printcharfun
, 0);
1743 new_backquote_output
++;
1744 print_object (XCAR (XCDR (obj
)), printcharfun
, escapeflag
);
1745 new_backquote_output
--;
1747 else if (print_quoted
&& CONSP (XCDR (obj
)) && NILP (XCDR (XCDR (obj
)))
1748 && new_backquote_output
1749 && ((EQ (XCAR (obj
), Qbackquote
)
1750 || EQ (XCAR (obj
), Qcomma
)
1751 || EQ (XCAR (obj
), Qcomma_at
)
1752 || EQ (XCAR (obj
), Qcomma_dot
))))
1754 print_object (XCAR (obj
), printcharfun
, 0);
1755 new_backquote_output
--;
1756 print_object (XCAR (XCDR (obj
)), printcharfun
, escapeflag
);
1757 new_backquote_output
++;
1763 /* If the first element is a backquote form,
1764 print it old-style so it won't be misunderstood. */
1765 if (print_quoted
&& CONSP (XCAR (obj
))
1766 && CONSP (XCDR (XCAR (obj
)))
1767 && NILP (XCDR (XCDR (XCAR (obj
))))
1768 && EQ (XCAR (XCAR (obj
)), Qbackquote
))
1774 print_object (Qbackquote
, printcharfun
, 0);
1777 print_object (XCAR (XCDR (tem
)), printcharfun
, 0);
1784 EMACS_INT print_length
;
1786 Lisp_Object halftail
= obj
;
1788 /* Negative values of print-length are invalid in CL.
1789 Treat them like nil, as CMUCL does. */
1790 if (NATNUMP (Vprint_length
))
1791 print_length
= XFASTINT (Vprint_length
);
1798 /* Detect circular list. */
1799 if (NILP (Vprint_circle
))
1801 /* Simple but imcomplete way. */
1802 if (i
!= 0 && EQ (obj
, halftail
))
1804 sprintf (buf
, " . #%d", i
/ 2);
1805 strout (buf
, -1, -1, printcharfun
, 0);
1811 /* With the print-circle feature. */
1814 Lisp_Object num
= Fgethash (obj
, Vprint_number_table
, Qnil
);
1817 strout (" . ", 3, 3, printcharfun
, 0);
1818 print_object (obj
, printcharfun
, escapeflag
);
1827 if (print_length
&& i
> print_length
)
1829 strout ("...", 3, 3, printcharfun
, 0);
1833 print_object (XCAR (obj
), printcharfun
, escapeflag
);
1837 halftail
= XCDR (halftail
);
1841 /* OBJ non-nil here means it's the end of a dotted list. */
1844 strout (" . ", 3, 3, printcharfun
, 0);
1845 print_object (obj
, printcharfun
, escapeflag
);
1853 case Lisp_Vectorlike
:
1858 strout ("#<process ", -1, -1, printcharfun
, 0);
1859 print_string (XPROCESS (obj
)->name
, printcharfun
);
1863 print_string (XPROCESS (obj
)->name
, printcharfun
);
1865 else if (BOOL_VECTOR_P (obj
))
1868 register unsigned char c
;
1869 struct gcpro gcpro1
;
1870 EMACS_INT size_in_chars
1871 = ((XBOOL_VECTOR (obj
)->size
+ BOOL_VECTOR_BITS_PER_CHAR
- 1)
1872 / BOOL_VECTOR_BITS_PER_CHAR
);
1878 sprintf (buf
, "%ld", (long) XBOOL_VECTOR (obj
)->size
);
1879 strout (buf
, -1, -1, printcharfun
, 0);
1882 /* Don't print more characters than the specified maximum.
1883 Negative values of print-length are invalid. Treat them
1884 like a print-length of nil. */
1885 if (NATNUMP (Vprint_length
)
1886 && XFASTINT (Vprint_length
) < size_in_chars
)
1887 size_in_chars
= XFASTINT (Vprint_length
);
1889 for (i
= 0; i
< size_in_chars
; i
++)
1892 c
= XBOOL_VECTOR (obj
)->data
[i
];
1893 if (c
== '\n' && print_escape_newlines
)
1898 else if (c
== '\f' && print_escape_newlines
)
1903 else if (c
> '\177')
1905 /* Use octal escapes to avoid encoding issues. */
1907 PRINTCHAR ('0' + ((c
>> 6) & 3));
1908 PRINTCHAR ('0' + ((c
>> 3) & 7));
1909 PRINTCHAR ('0' + (c
& 7));
1913 if (c
== '\"' || c
== '\\')
1922 else if (SUBRP (obj
))
1924 strout ("#<subr ", -1, -1, printcharfun
, 0);
1925 strout (XSUBR (obj
)->symbol_name
, -1, -1, printcharfun
, 0);
1928 else if (WINDOWP (obj
))
1930 strout ("#<window ", -1, -1, printcharfun
, 0);
1931 sprintf (buf
, "%ld", (long) XFASTINT (XWINDOW (obj
)->sequence_number
));
1932 strout (buf
, -1, -1, printcharfun
, 0);
1933 if (!NILP (XWINDOW (obj
)->buffer
))
1935 strout (" on ", -1, -1, printcharfun
, 0);
1936 print_string (XBUFFER (XWINDOW (obj
)->buffer
)->name
, printcharfun
);
1940 else if (TERMINALP (obj
))
1942 struct terminal
*t
= XTERMINAL (obj
);
1943 strout ("#<terminal ", -1, -1, printcharfun
, 0);
1944 sprintf (buf
, "%d", t
->id
);
1945 strout (buf
, -1, -1, printcharfun
, 0);
1948 strout (" on ", -1, -1, printcharfun
, 0);
1949 strout (t
->name
, -1, -1, printcharfun
, 0);
1953 else if (HASH_TABLE_P (obj
))
1955 struct Lisp_Hash_Table
*h
= XHASH_TABLE (obj
);
1957 EMACS_INT real_size
, size
;
1959 strout ("#<hash-table", -1, -1, printcharfun
, 0);
1960 if (SYMBOLP (h
->test
))
1964 strout (SDATA (SYMBOL_NAME (h
->test
)), -1, -1, printcharfun
, 0);
1966 strout (SDATA (SYMBOL_NAME (h
->weak
)), -1, -1, printcharfun
, 0);
1968 sprintf (buf
, "%ld/%ld", (long) h
->count
,
1969 (long) XVECTOR (h
->next
)->size
);
1970 strout (buf
, -1, -1, printcharfun
, 0);
1972 sprintf (buf
, " 0x%lx", (unsigned long) h
);
1973 strout (buf
, -1, -1, printcharfun
, 0);
1976 /* Implement a readable output, e.g.:
1977 #s(hash-table size 2 test equal data (k1 v1 k2 v2)) */
1978 /* Always print the size. */
1979 sprintf (buf
, "#s(hash-table size %ld",
1980 (long) XVECTOR (h
->next
)->size
);
1981 strout (buf
, -1, -1, printcharfun
, 0);
1983 if (!NILP (h
->test
))
1985 strout (" test ", -1, -1, printcharfun
, 0);
1986 print_object (h
->test
, printcharfun
, 0);
1989 if (!NILP (h
->weak
))
1991 strout (" weakness ", -1, -1, printcharfun
, 0);
1992 print_object (h
->weak
, printcharfun
, 0);
1995 if (!NILP (h
->rehash_size
))
1997 strout (" rehash-size ", -1, -1, printcharfun
, 0);
1998 print_object (h
->rehash_size
, printcharfun
, 0);
2001 if (!NILP (h
->rehash_threshold
))
2003 strout (" rehash-threshold ", -1, -1, printcharfun
, 0);
2004 print_object (h
->rehash_threshold
, printcharfun
, 0);
2007 strout (" data ", -1, -1, printcharfun
, 0);
2009 /* Print the data here as a plist. */
2010 real_size
= HASH_TABLE_SIZE (h
);
2013 /* Don't print more elements than the specified maximum. */
2014 if (NATNUMP (Vprint_length
)
2015 && XFASTINT (Vprint_length
) < size
)
2016 size
= XFASTINT (Vprint_length
);
2019 for (i
= 0; i
< size
; i
++)
2020 if (!NILP (HASH_HASH (h
, i
)))
2022 if (i
) PRINTCHAR (' ');
2023 print_object (HASH_KEY (h
, i
), printcharfun
, 1);
2025 print_object (HASH_VALUE (h
, i
), printcharfun
, 1);
2028 if (size
< real_size
)
2029 strout (" ...", 4, 4, printcharfun
, 0);
2035 else if (BUFFERP (obj
))
2037 if (NILP (XBUFFER (obj
)->name
))
2038 strout ("#<killed buffer>", -1, -1, printcharfun
, 0);
2039 else if (escapeflag
)
2041 strout ("#<buffer ", -1, -1, printcharfun
, 0);
2042 print_string (XBUFFER (obj
)->name
, printcharfun
);
2046 print_string (XBUFFER (obj
)->name
, printcharfun
);
2048 else if (WINDOW_CONFIGURATIONP (obj
))
2050 strout ("#<window-configuration>", -1, -1, printcharfun
, 0);
2052 else if (FRAMEP (obj
))
2054 strout ((FRAME_LIVE_P (XFRAME (obj
))
2055 ? "#<frame " : "#<dead frame "),
2056 -1, -1, printcharfun
, 0);
2057 print_string (XFRAME (obj
)->name
, printcharfun
);
2058 sprintf (buf
, " 0x%lx", (unsigned long) (XFRAME (obj
)));
2059 strout (buf
, -1, -1, printcharfun
, 0);
2062 else if (FONTP (obj
))
2066 if (! FONT_OBJECT_P (obj
))
2068 if (FONT_SPEC_P (obj
))
2069 strout ("#<font-spec", -1, -1, printcharfun
, 0);
2071 strout ("#<font-entity", -1, -1, printcharfun
, 0);
2072 for (i
= 0; i
< FONT_SPEC_MAX
; i
++)
2075 if (i
< FONT_WEIGHT_INDEX
|| i
> FONT_WIDTH_INDEX
)
2076 print_object (AREF (obj
, i
), printcharfun
, escapeflag
);
2078 print_object (font_style_symbolic (obj
, i
, 0),
2079 printcharfun
, escapeflag
);
2084 strout ("#<font-object ", -1, -1, printcharfun
, 0);
2085 print_object (AREF (obj
, FONT_NAME_INDEX
), printcharfun
,
2092 EMACS_INT size
= XVECTOR (obj
)->size
;
2093 if (COMPILEDP (obj
))
2096 size
&= PSEUDOVECTOR_SIZE_MASK
;
2098 if (CHAR_TABLE_P (obj
) || SUB_CHAR_TABLE_P (obj
))
2100 /* We print a char-table as if it were a vector,
2101 lumping the parent and default slots in with the
2102 character slots. But we add #^ as a prefix. */
2104 /* Make each lowest sub_char_table start a new line.
2105 Otherwise we'll make a line extremely long, which
2106 results in slow redisplay. */
2107 if (SUB_CHAR_TABLE_P (obj
)
2108 && XINT (XSUB_CHAR_TABLE (obj
)->depth
) == 3)
2112 if (SUB_CHAR_TABLE_P (obj
))
2114 size
&= PSEUDOVECTOR_SIZE_MASK
;
2116 if (size
& PSEUDOVECTOR_FLAG
)
2122 register Lisp_Object tem
;
2123 EMACS_INT real_size
= size
;
2125 /* Don't print more elements than the specified maximum. */
2126 if (NATNUMP (Vprint_length
)
2127 && XFASTINT (Vprint_length
) < size
)
2128 size
= XFASTINT (Vprint_length
);
2130 for (i
= 0; i
< size
; i
++)
2132 if (i
) PRINTCHAR (' ');
2133 tem
= XVECTOR (obj
)->contents
[i
];
2134 print_object (tem
, printcharfun
, escapeflag
);
2136 if (size
< real_size
)
2137 strout (" ...", 4, 4, printcharfun
, 0);
2144 switch (XMISCTYPE (obj
))
2146 case Lisp_Misc_Marker
:
2147 strout ("#<marker ", -1, -1, printcharfun
, 0);
2148 /* Do you think this is necessary? */
2149 if (XMARKER (obj
)->insertion_type
!= 0)
2150 strout ("(moves after insertion) ", -1, -1, printcharfun
, 0);
2151 if (! XMARKER (obj
)->buffer
)
2152 strout ("in no buffer", -1, -1, printcharfun
, 0);
2155 sprintf (buf
, "at %ld", (long)marker_position (obj
));
2156 strout (buf
, -1, -1, printcharfun
, 0);
2157 strout (" in ", -1, -1, printcharfun
, 0);
2158 print_string (XMARKER (obj
)->buffer
->name
, printcharfun
);
2163 case Lisp_Misc_Overlay
:
2164 strout ("#<overlay ", -1, -1, printcharfun
, 0);
2165 if (! XMARKER (OVERLAY_START (obj
))->buffer
)
2166 strout ("in no buffer", -1, -1, printcharfun
, 0);
2169 sprintf (buf
, "from %ld to %ld in ",
2170 (long)marker_position (OVERLAY_START (obj
)),
2171 (long)marker_position (OVERLAY_END (obj
)));
2172 strout (buf
, -1, -1, printcharfun
, 0);
2173 print_string (XMARKER (OVERLAY_START (obj
))->buffer
->name
,
2179 /* Remaining cases shouldn't happen in normal usage, but let's print
2180 them anyway for the benefit of the debugger. */
2181 case Lisp_Misc_Free
:
2182 strout ("#<misc free cell>", -1, -1, printcharfun
, 0);
2185 case Lisp_Misc_Save_Value
:
2186 strout ("#<save_value ", -1, -1, printcharfun
, 0);
2187 sprintf(buf
, "ptr=0x%08lx int=%d",
2188 (unsigned long) XSAVE_VALUE (obj
)->pointer
,
2189 XSAVE_VALUE (obj
)->integer
);
2190 strout (buf
, -1, -1, printcharfun
, 0);
2202 /* We're in trouble if this happens!
2203 Probably should just abort () */
2204 strout ("#<EMACS BUG: INVALID DATATYPE ", -1, -1, printcharfun
, 0);
2206 sprintf (buf
, "(MISC 0x%04x)", (int) XMISCTYPE (obj
));
2207 else if (VECTORLIKEP (obj
))
2208 sprintf (buf
, "(PVEC 0x%08x)", (int) XVECTOR (obj
)->size
);
2210 sprintf (buf
, "(0x%02x)", (int) XTYPE (obj
));
2211 strout (buf
, -1, -1, printcharfun
, 0);
2212 strout (" Save your buffers immediately and please report this bug>",
2213 -1, -1, printcharfun
, 0);
2221 /* Print a description of INTERVAL using PRINTCHARFUN.
2222 This is part of printing a string that has text properties. */
2225 print_interval (INTERVAL interval
, Lisp_Object printcharfun
)
2227 if (NILP (interval
->plist
))
2230 print_object (make_number (interval
->position
), printcharfun
, 1);
2232 print_object (make_number (interval
->position
+ LENGTH (interval
)),
2235 print_object (interval
->plist
, printcharfun
, 1);
2240 syms_of_print (void)
2242 Qtemp_buffer_setup_hook
= intern_c_string ("temp-buffer-setup-hook");
2243 staticpro (&Qtemp_buffer_setup_hook
);
2245 DEFVAR_LISP ("standard-output", &Vstandard_output
,
2246 doc
: /* Output stream `print' uses by default for outputting a character.
2247 This may be any function of one argument.
2248 It may also be a buffer (output is inserted before point)
2249 or a marker (output is inserted and the marker is advanced)
2250 or the symbol t (output appears in the echo area). */);
2251 Vstandard_output
= Qt
;
2252 Qstandard_output
= intern_c_string ("standard-output");
2253 staticpro (&Qstandard_output
);
2255 DEFVAR_LISP ("float-output-format", &Vfloat_output_format
,
2256 doc
: /* The format descriptor string used to print floats.
2257 This is a %-spec like those accepted by `printf' in C,
2258 but with some restrictions. It must start with the two characters `%.'.
2259 After that comes an integer precision specification,
2260 and then a letter which controls the format.
2261 The letters allowed are `e', `f' and `g'.
2262 Use `e' for exponential notation \"DIG.DIGITSeEXPT\"
2263 Use `f' for decimal point notation \"DIGITS.DIGITS\".
2264 Use `g' to choose the shorter of those two formats for the number at hand.
2265 The precision in any of these cases is the number of digits following
2266 the decimal point. With `f', a precision of 0 means to omit the
2267 decimal point. 0 is not allowed with `e' or `g'.
2269 A value of nil means to use the shortest notation
2270 that represents the number without losing information. */);
2271 Vfloat_output_format
= Qnil
;
2272 Qfloat_output_format
= intern_c_string ("float-output-format");
2273 staticpro (&Qfloat_output_format
);
2275 DEFVAR_LISP ("print-length", &Vprint_length
,
2276 doc
: /* Maximum length of list to print before abbreviating.
2277 A value of nil means no limit. See also `eval-expression-print-length'. */);
2278 Vprint_length
= Qnil
;
2280 DEFVAR_LISP ("print-level", &Vprint_level
,
2281 doc
: /* Maximum depth of list nesting to print before abbreviating.
2282 A value of nil means no limit. See also `eval-expression-print-level'. */);
2283 Vprint_level
= Qnil
;
2285 DEFVAR_BOOL ("print-escape-newlines", &print_escape_newlines
,
2286 doc
: /* Non-nil means print newlines in strings as `\\n'.
2287 Also print formfeeds as `\\f'. */);
2288 print_escape_newlines
= 0;
2290 DEFVAR_BOOL ("print-escape-nonascii", &print_escape_nonascii
,
2291 doc
: /* Non-nil means print unibyte non-ASCII chars in strings as \\OOO.
2292 \(OOO is the octal representation of the character code.)
2293 Only single-byte characters are affected, and only in `prin1'.
2294 When the output goes in a multibyte buffer, this feature is
2295 enabled regardless of the value of the variable. */);
2296 print_escape_nonascii
= 0;
2298 DEFVAR_BOOL ("print-escape-multibyte", &print_escape_multibyte
,
2299 doc
: /* Non-nil means print multibyte characters in strings as \\xXXXX.
2300 \(XXXX is the hex representation of the character code.)
2301 This affects only `prin1'. */);
2302 print_escape_multibyte
= 0;
2304 DEFVAR_BOOL ("print-quoted", &print_quoted
,
2305 doc
: /* Non-nil means print quoted forms with reader syntax.
2306 I.e., (quote foo) prints as 'foo, (function foo) as #'foo. */);
2309 DEFVAR_LISP ("print-gensym", &Vprint_gensym
,
2310 doc
: /* Non-nil means print uninterned symbols so they will read as uninterned.
2311 I.e., the value of (make-symbol \"foobar\") prints as #:foobar.
2312 When the uninterned symbol appears within a recursive data structure,
2313 and the symbol appears more than once, in addition use the #N# and #N=
2314 constructs as needed, so that multiple references to the same symbol are
2315 shared once again when the text is read back. */);
2316 Vprint_gensym
= Qnil
;
2318 DEFVAR_LISP ("print-circle", &Vprint_circle
,
2319 doc
: /* *Non-nil means print recursive structures using #N= and #N# syntax.
2320 If nil, printing proceeds recursively and may lead to
2321 `max-lisp-eval-depth' being exceeded or an error may occur:
2322 \"Apparently circular structure being printed.\" Also see
2323 `print-length' and `print-level'.
2324 If non-nil, shared substructures anywhere in the structure are printed
2325 with `#N=' before the first occurrence (in the order of the print
2326 representation) and `#N#' in place of each subsequent occurrence,
2327 where N is a positive decimal integer. */);
2328 Vprint_circle
= Qnil
;
2330 DEFVAR_LISP ("print-continuous-numbering", &Vprint_continuous_numbering
,
2331 doc
: /* *Non-nil means number continuously across print calls.
2332 This affects the numbers printed for #N= labels and #M# references.
2333 See also `print-circle', `print-gensym', and `print-number-table'.
2334 This variable should not be set with `setq'; bind it with a `let' instead. */);
2335 Vprint_continuous_numbering
= Qnil
;
2337 DEFVAR_LISP ("print-number-table", &Vprint_number_table
,
2338 doc
: /* A vector used internally to produce `#N=' labels and `#N#' references.
2339 The Lisp printer uses this vector to detect Lisp objects referenced more
2342 When you bind `print-continuous-numbering' to t, you should probably
2343 also bind `print-number-table' to nil. This ensures that the value of
2344 `print-number-table' can be garbage-collected once the printing is
2345 done. If all elements of `print-number-table' are nil, it means that
2346 the printing done so far has not found any shared structure or objects
2347 that need to be recorded in the table. */);
2348 Vprint_number_table
= Qnil
;
2350 DEFVAR_LISP ("print-charset-text-property", &Vprint_charset_text_property
,
2351 doc
: /* A flag to control printing of `charset' text property on printing a string.
2352 The value must be nil, t, or `default'.
2354 If the value is nil, don't print the text property `charset'.
2356 If the value is t, always print the text property `charset'.
2358 If the value is `default', print the text property `charset' only when
2359 the value is different from what is guessed in the current charset
2361 Vprint_charset_text_property
= Qdefault
;
2363 /* prin1_to_string_buffer initialized in init_buffer_once in buffer.c */
2364 staticpro (&Vprin1_to_string_buffer
);
2367 defsubr (&Sprin1_to_string
);
2368 defsubr (&Serror_message_string
);
2372 defsubr (&Swrite_char
);
2373 defsubr (&Sexternal_debugging_output
);
2374 #ifdef WITH_REDIRECT_DEBUGGING_OUTPUT
2375 defsubr (&Sredirect_debugging_output
);
2378 Qexternal_debugging_output
= intern_c_string ("external-debugging-output");
2379 staticpro (&Qexternal_debugging_output
);
2381 Qprint_escape_newlines
= intern_c_string ("print-escape-newlines");
2382 staticpro (&Qprint_escape_newlines
);
2384 Qprint_escape_multibyte
= intern_c_string ("print-escape-multibyte");
2385 staticpro (&Qprint_escape_multibyte
);
2387 Qprint_escape_nonascii
= intern_c_string ("print-escape-nonascii");
2388 staticpro (&Qprint_escape_nonascii
);
2390 print_prune_charset_plist
= Qnil
;
2391 staticpro (&print_prune_charset_plist
);
2393 defsubr (&Swith_output_to_temp_buffer
);
2396 /* arch-tag: bc797170-94ae-41de-86e3-75e20f8f7a39
2397 (do not change this comment) */