Merge from trunk.
[emacs.git] / src / print.c
blob7a90e6cd96eb0b3c2d920112ba38fef35232991a
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/>. */
22 #include <config.h>
23 #include <stdio.h>
24 #include <setjmp.h>
25 #include "lisp.h"
26 #include "buffer.h"
27 #include "character.h"
28 #include "charset.h"
29 #include "keyboard.h"
30 #include "frame.h"
31 #include "window.h"
32 #include "process.h"
33 #include "dispextern.h"
34 #include "termchar.h"
35 #include "intervals.h"
36 #include "blockinput.h"
37 #include "termhooks.h" /* For struct terminal. */
38 #include "font.h"
40 Lisp_Object Vstandard_output, Qstandard_output;
42 Lisp_Object Qtemp_buffer_setup_hook;
44 /* These are used to print like we read. */
45 extern Lisp_Object Qbackquote, Qcomma, Qcomma_at, Qcomma_dot, Qfunction;
47 Lisp_Object Vfloat_output_format, Qfloat_output_format;
49 #include <math.h>
51 #if STDC_HEADERS
52 #include <float.h>
53 #endif
55 /* Default to values appropriate for IEEE floating point. */
56 #ifndef FLT_RADIX
57 #define FLT_RADIX 2
58 #endif
59 #ifndef DBL_MANT_DIG
60 #define DBL_MANT_DIG 53
61 #endif
62 #ifndef DBL_DIG
63 #define DBL_DIG 15
64 #endif
65 #ifndef DBL_MIN
66 #define DBL_MIN 2.2250738585072014e-308
67 #endif
69 #ifdef DBL_MIN_REPLACEMENT
70 #undef DBL_MIN
71 #define DBL_MIN DBL_MIN_REPLACEMENT
72 #endif
74 /* Define DOUBLE_DIGITS_BOUND, an upper bound on the number of decimal digits
75 needed to express a float without losing information.
76 The general-case formula is valid for the usual case, IEEE floating point,
77 but many compilers can't optimize the formula to an integer constant,
78 so make a special case for it. */
79 #if FLT_RADIX == 2 && DBL_MANT_DIG == 53
80 #define DOUBLE_DIGITS_BOUND 17 /* IEEE floating point */
81 #else
82 #define DOUBLE_DIGITS_BOUND ((int) ceil (log10 (pow (FLT_RADIX, DBL_MANT_DIG))))
83 #endif
85 /* Avoid actual stack overflow in print. */
86 int print_depth;
88 /* Level of nesting inside outputting backquote in new style. */
89 int new_backquote_output;
91 /* Detect most circularities to print finite output. */
92 #define PRINT_CIRCLE 200
93 Lisp_Object being_printed[PRINT_CIRCLE];
95 /* When printing into a buffer, first we put the text in this
96 block, then insert it all at once. */
97 char *print_buffer;
99 /* Size allocated in print_buffer. */
100 int print_buffer_size;
101 /* Chars stored in print_buffer. */
102 int print_buffer_pos;
103 /* Bytes stored in print_buffer. */
104 int print_buffer_pos_byte;
106 /* Maximum length of list to print in full; noninteger means
107 effectively infinity */
109 Lisp_Object Vprint_length;
111 /* Maximum depth of list to print in full; noninteger means
112 effectively infinity. */
114 Lisp_Object Vprint_level;
116 /* Nonzero means print newlines in strings as \n. */
118 int print_escape_newlines;
120 /* Nonzero means to print single-byte non-ascii characters in strings as
121 octal escapes. */
123 int print_escape_nonascii;
125 /* Nonzero means to print multibyte characters in strings as hex escapes. */
127 int print_escape_multibyte;
129 Lisp_Object Qprint_escape_newlines;
130 Lisp_Object Qprint_escape_multibyte, Qprint_escape_nonascii;
132 /* Nonzero means print (quote foo) forms as 'foo, etc. */
134 int print_quoted;
136 /* Non-nil means print #: before uninterned symbols. */
138 Lisp_Object Vprint_gensym;
140 /* Non-nil means print recursive structures using #n= and #n# syntax. */
142 Lisp_Object Vprint_circle;
144 /* Non-nil means keep continuous number for #n= and #n# syntax
145 between several print functions. */
147 Lisp_Object Vprint_continuous_numbering;
149 /* Vprint_number_table is a vector like [OBJ1 STAT1 OBJ2 STAT2 ...],
150 where OBJn are objects going to be printed, and STATn are their status,
151 which may be different meanings during process. See the comments of
152 the functions print and print_preprocess for details.
153 print_number_index keeps the last position the next object should be added,
154 twice of which is the actual vector position in Vprint_number_table. */
155 int print_number_index;
156 Lisp_Object Vprint_number_table;
158 /* PRINT_NUMBER_OBJECT returns the I'th object in Vprint_number_table TABLE.
159 PRINT_NUMBER_STATUS returns the status of the I'th object in TABLE.
160 See the comment of the variable Vprint_number_table. */
161 #define PRINT_NUMBER_OBJECT(table,i) XVECTOR ((table))->contents[(i) * 2]
162 #define PRINT_NUMBER_STATUS(table,i) XVECTOR ((table))->contents[(i) * 2 + 1]
164 /* Nonzero means print newline to stdout before next minibuffer message.
165 Defined in xdisp.c */
167 extern int noninteractive_need_newline;
169 void print_interval (INTERVAL interval, Lisp_Object printcharfun);
171 /* GDB resets this to zero on W32 to disable OutputDebugString calls. */
172 int print_output_debug_flag = 1;
175 /* Low level output routines for characters and strings */
177 /* Lisp functions to do output using a stream
178 must have the stream in a variable called printcharfun
179 and must start with PRINTPREPARE, end with PRINTFINISH,
180 and use PRINTDECLARE to declare common variables.
181 Use PRINTCHAR to output one character,
182 or call strout to output a block of characters. */
184 #define PRINTDECLARE \
185 struct buffer *old = current_buffer; \
186 int old_point = -1, start_point = -1; \
187 int old_point_byte = -1, start_point_byte = -1; \
188 int specpdl_count = SPECPDL_INDEX (); \
189 int free_print_buffer = 0; \
190 int multibyte = !NILP (current_buffer->enable_multibyte_characters); \
191 Lisp_Object original
193 #define PRINTPREPARE \
194 original = printcharfun; \
195 if (NILP (printcharfun)) printcharfun = Qt; \
196 if (BUFFERP (printcharfun)) \
198 if (XBUFFER (printcharfun) != current_buffer) \
199 Fset_buffer (printcharfun); \
200 printcharfun = Qnil; \
202 if (MARKERP (printcharfun)) \
204 EMACS_INT marker_pos; \
205 if (! XMARKER (printcharfun)->buffer) \
206 error ("Marker does not point anywhere"); \
207 if (XMARKER (printcharfun)->buffer != current_buffer) \
208 set_buffer_internal (XMARKER (printcharfun)->buffer); \
209 marker_pos = marker_position (printcharfun); \
210 if (marker_pos < BEGV || marker_pos > ZV) \
211 error ("Marker is outside the accessible part of the buffer"); \
212 old_point = PT; \
213 old_point_byte = PT_BYTE; \
214 SET_PT_BOTH (marker_pos, \
215 marker_byte_position (printcharfun)); \
216 start_point = PT; \
217 start_point_byte = PT_BYTE; \
218 printcharfun = Qnil; \
220 if (NILP (printcharfun)) \
222 Lisp_Object string; \
223 if (NILP (current_buffer->enable_multibyte_characters) \
224 && ! print_escape_multibyte) \
225 specbind (Qprint_escape_multibyte, Qt); \
226 if (! NILP (current_buffer->enable_multibyte_characters) \
227 && ! print_escape_nonascii) \
228 specbind (Qprint_escape_nonascii, Qt); \
229 if (print_buffer != 0) \
231 string = make_string_from_bytes (print_buffer, \
232 print_buffer_pos, \
233 print_buffer_pos_byte); \
234 record_unwind_protect (print_unwind, string); \
236 else \
238 print_buffer_size = 1000; \
239 print_buffer = (char *) xmalloc (print_buffer_size); \
240 free_print_buffer = 1; \
242 print_buffer_pos = 0; \
243 print_buffer_pos_byte = 0; \
245 if (EQ (printcharfun, Qt) && ! noninteractive) \
246 setup_echo_area_for_printing (multibyte);
248 #define PRINTFINISH \
249 if (NILP (printcharfun)) \
251 if (print_buffer_pos != print_buffer_pos_byte \
252 && NILP (current_buffer->enable_multibyte_characters)) \
254 unsigned char *temp \
255 = (unsigned char *) alloca (print_buffer_pos + 1); \
256 copy_text (print_buffer, temp, print_buffer_pos_byte, \
257 1, 0); \
258 insert_1_both (temp, print_buffer_pos, \
259 print_buffer_pos, 0, 1, 0); \
261 else \
262 insert_1_both (print_buffer, print_buffer_pos, \
263 print_buffer_pos_byte, 0, 1, 0); \
264 signal_after_change (PT - print_buffer_pos, 0, print_buffer_pos);\
266 if (free_print_buffer) \
268 xfree (print_buffer); \
269 print_buffer = 0; \
271 unbind_to (specpdl_count, Qnil); \
272 if (MARKERP (original)) \
273 set_marker_both (original, Qnil, PT, PT_BYTE); \
274 if (old_point >= 0) \
275 SET_PT_BOTH (old_point + (old_point >= start_point \
276 ? PT - start_point : 0), \
277 old_point_byte + (old_point_byte >= start_point_byte \
278 ? PT_BYTE - start_point_byte : 0)); \
279 if (old != current_buffer) \
280 set_buffer_internal (old);
282 #define PRINTCHAR(ch) printchar (ch, printcharfun)
284 /* This is used to restore the saved contents of print_buffer
285 when there is a recursive call to print. */
287 static Lisp_Object
288 print_unwind (Lisp_Object saved_text)
290 memcpy (print_buffer, SDATA (saved_text), SCHARS (saved_text));
291 return Qnil;
295 /* Print character CH using method FUN. FUN nil means print to
296 print_buffer. FUN t means print to echo area or stdout if
297 non-interactive. If FUN is neither nil nor t, call FUN with CH as
298 argument. */
300 static void
301 printchar (unsigned int ch, Lisp_Object fun)
303 if (!NILP (fun) && !EQ (fun, Qt))
304 call1 (fun, make_number (ch));
305 else
307 unsigned char str[MAX_MULTIBYTE_LENGTH];
308 int len = CHAR_STRING (ch, str);
310 QUIT;
312 if (NILP (fun))
314 if (print_buffer_pos_byte + len >= print_buffer_size)
315 print_buffer = (char *) xrealloc (print_buffer,
316 print_buffer_size *= 2);
317 memcpy (print_buffer + print_buffer_pos_byte, str, len);
318 print_buffer_pos += 1;
319 print_buffer_pos_byte += len;
321 else if (noninteractive)
323 fwrite (str, 1, len, stdout);
324 noninteractive_need_newline = 1;
326 else
328 int multibyte_p
329 = !NILP (current_buffer->enable_multibyte_characters);
331 setup_echo_area_for_printing (multibyte_p);
332 insert_char (ch);
333 message_dolog (str, len, 0, multibyte_p);
339 /* Output SIZE characters, SIZE_BYTE bytes from string PTR using
340 method PRINTCHARFUN. If SIZE < 0, use the string length of PTR for
341 both SIZE and SIZE_BYTE. PRINTCHARFUN nil means output to
342 print_buffer. PRINTCHARFUN t means output to the echo area or to
343 stdout if non-interactive. If neither nil nor t, call Lisp
344 function PRINTCHARFUN for each character printed. MULTIBYTE
345 non-zero means PTR contains multibyte characters.
347 In the case where PRINTCHARFUN is nil, it is safe for PTR to point
348 to data in a Lisp string. Otherwise that is not safe. */
350 static void
351 strout (const char *ptr, int size, int size_byte, Lisp_Object printcharfun,
352 int multibyte)
354 if (size < 0)
355 size_byte = size = strlen (ptr);
357 if (NILP (printcharfun))
359 if (print_buffer_pos_byte + size_byte > print_buffer_size)
361 print_buffer_size = print_buffer_size * 2 + size_byte;
362 print_buffer = (char *) xrealloc (print_buffer,
363 print_buffer_size);
365 memcpy (print_buffer + print_buffer_pos_byte, ptr, size_byte);
366 print_buffer_pos += size;
367 print_buffer_pos_byte += size_byte;
369 else if (noninteractive && EQ (printcharfun, Qt))
371 fwrite (ptr, 1, size_byte, stdout);
372 noninteractive_need_newline = 1;
374 else if (EQ (printcharfun, Qt))
376 /* Output to echo area. We're trying to avoid a little overhead
377 here, that's the reason we don't call printchar to do the
378 job. */
379 int i;
380 int multibyte_p
381 = !NILP (current_buffer->enable_multibyte_characters);
383 setup_echo_area_for_printing (multibyte_p);
384 message_dolog (ptr, size_byte, 0, multibyte_p);
386 if (size == size_byte)
388 for (i = 0; i < size; ++i)
389 insert_char ((unsigned char) *ptr++);
391 else
393 int len;
394 for (i = 0; i < size_byte; i += len)
396 int ch = STRING_CHAR_AND_LENGTH (ptr + i, len);
397 insert_char (ch);
401 else
403 /* PRINTCHARFUN is a Lisp function. */
404 int i = 0;
406 if (size == size_byte)
408 while (i < size_byte)
410 int ch = ptr[i++];
411 PRINTCHAR (ch);
414 else
416 while (i < size_byte)
418 /* Here, we must convert each multi-byte form to the
419 corresponding character code before handing it to
420 PRINTCHAR. */
421 int len;
422 int ch = STRING_CHAR_AND_LENGTH (ptr + i, len);
423 PRINTCHAR (ch);
424 i += len;
430 /* Print the contents of a string STRING using PRINTCHARFUN.
431 It isn't safe to use strout in many cases,
432 because printing one char can relocate. */
434 static void
435 print_string (Lisp_Object string, Lisp_Object printcharfun)
437 if (EQ (printcharfun, Qt) || NILP (printcharfun))
439 int chars;
441 if (print_escape_nonascii)
442 string = string_escape_byte8 (string);
444 if (STRING_MULTIBYTE (string))
445 chars = SCHARS (string);
446 else if (! print_escape_nonascii
447 && (EQ (printcharfun, Qt)
448 ? ! NILP (buffer_defaults.enable_multibyte_characters)
449 : ! NILP (current_buffer->enable_multibyte_characters)))
451 /* If unibyte string STRING contains 8-bit codes, we must
452 convert STRING to a multibyte string containing the same
453 character codes. */
454 Lisp_Object newstr;
455 int bytes;
457 chars = SBYTES (string);
458 bytes = parse_str_to_multibyte (SDATA (string), chars);
459 if (chars < bytes)
461 newstr = make_uninit_multibyte_string (chars, bytes);
462 memcpy (SDATA (newstr), SDATA (string), chars);
463 str_to_multibyte (SDATA (newstr), bytes, chars);
464 string = newstr;
467 else
468 chars = SBYTES (string);
470 if (EQ (printcharfun, Qt))
472 /* Output to echo area. */
473 int nbytes = SBYTES (string);
474 char *buffer;
476 /* Copy the string contents so that relocation of STRING by
477 GC does not cause trouble. */
478 USE_SAFE_ALLOCA;
480 SAFE_ALLOCA (buffer, char *, nbytes);
481 memcpy (buffer, SDATA (string), nbytes);
483 strout (buffer, chars, SBYTES (string),
484 printcharfun, STRING_MULTIBYTE (string));
486 SAFE_FREE ();
488 else
489 /* No need to copy, since output to print_buffer can't GC. */
490 strout (SDATA (string),
491 chars, SBYTES (string),
492 printcharfun, STRING_MULTIBYTE (string));
494 else
496 /* Otherwise, string may be relocated by printing one char.
497 So re-fetch the string address for each character. */
498 int i;
499 int size = SCHARS (string);
500 int size_byte = SBYTES (string);
501 struct gcpro gcpro1;
502 GCPRO1 (string);
503 if (size == size_byte)
504 for (i = 0; i < size; i++)
505 PRINTCHAR (SREF (string, i));
506 else
507 for (i = 0; i < size_byte; )
509 /* Here, we must convert each multi-byte form to the
510 corresponding character code before handing it to PRINTCHAR. */
511 int len;
512 int ch = STRING_CHAR_AND_LENGTH (SDATA (string) + i, len);
513 PRINTCHAR (ch);
514 i += len;
516 UNGCPRO;
520 DEFUN ("write-char", Fwrite_char, Swrite_char, 1, 2, 0,
521 doc: /* Output character CHARACTER to stream PRINTCHARFUN.
522 PRINTCHARFUN defaults to the value of `standard-output' (which see). */)
523 (Lisp_Object character, Lisp_Object printcharfun)
525 PRINTDECLARE;
527 if (NILP (printcharfun))
528 printcharfun = Vstandard_output;
529 CHECK_NUMBER (character);
530 PRINTPREPARE;
531 PRINTCHAR (XINT (character));
532 PRINTFINISH;
533 return character;
536 /* Used from outside of print.c to print a block of SIZE
537 single-byte chars at DATA on the default output stream.
538 Do not use this on the contents of a Lisp string. */
540 void
541 write_string (const char *data, int size)
543 PRINTDECLARE;
544 Lisp_Object printcharfun;
546 printcharfun = Vstandard_output;
548 PRINTPREPARE;
549 strout (data, size, size, printcharfun, 0);
550 PRINTFINISH;
553 /* Used from outside of print.c to print a block of SIZE
554 single-byte chars at DATA on a specified stream PRINTCHARFUN.
555 Do not use this on the contents of a Lisp string. */
557 void
558 write_string_1 (const char *data, int size, Lisp_Object printcharfun)
560 PRINTDECLARE;
562 PRINTPREPARE;
563 strout (data, size, size, printcharfun, 0);
564 PRINTFINISH;
568 void
569 temp_output_buffer_setup (const char *bufname)
571 int count = SPECPDL_INDEX ();
572 register struct buffer *old = current_buffer;
573 register Lisp_Object buf;
575 record_unwind_protect (set_buffer_if_live, Fcurrent_buffer ());
577 Fset_buffer (Fget_buffer_create (build_string (bufname)));
579 Fkill_all_local_variables ();
580 delete_all_overlays (current_buffer);
581 current_buffer->directory = old->directory;
582 current_buffer->read_only = Qnil;
583 current_buffer->filename = Qnil;
584 current_buffer->undo_list = Qt;
585 eassert (current_buffer->overlays_before == NULL);
586 eassert (current_buffer->overlays_after == NULL);
587 current_buffer->enable_multibyte_characters
588 = buffer_defaults.enable_multibyte_characters;
589 specbind (Qinhibit_read_only, Qt);
590 specbind (Qinhibit_modification_hooks, Qt);
591 Ferase_buffer ();
592 XSETBUFFER (buf, current_buffer);
594 Frun_hooks (1, &Qtemp_buffer_setup_hook);
596 unbind_to (count, Qnil);
598 specbind (Qstandard_output, buf);
601 Lisp_Object
602 internal_with_output_to_temp_buffer (const char *bufname, Lisp_Object (*function) (Lisp_Object), Lisp_Object args)
604 int count = SPECPDL_INDEX ();
605 Lisp_Object buf, val;
606 struct gcpro gcpro1;
608 GCPRO1 (args);
609 record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
610 temp_output_buffer_setup (bufname);
611 buf = Vstandard_output;
612 UNGCPRO;
614 val = (*function) (args);
616 GCPRO1 (val);
617 temp_output_buffer_show (buf);
618 UNGCPRO;
620 return unbind_to (count, val);
623 DEFUN ("with-output-to-temp-buffer",
624 Fwith_output_to_temp_buffer, Swith_output_to_temp_buffer,
625 1, UNEVALLED, 0,
626 doc: /* Bind `standard-output' to buffer BUFNAME, eval BODY, then show that buffer.
628 This construct makes buffer BUFNAME empty before running BODY.
629 It does not make the buffer current for BODY.
630 Instead it binds `standard-output' to that buffer, so that output
631 generated with `prin1' and similar functions in BODY goes into
632 the buffer.
634 At the end of BODY, this marks buffer BUFNAME unmodifed and displays
635 it in a window, but does not select it. The normal way to do this is
636 by calling `display-buffer', then running `temp-buffer-show-hook'.
637 However, if `temp-buffer-show-function' is non-nil, it calls that
638 function instead (and does not run `temp-buffer-show-hook'). The
639 function gets one argument, the buffer to display.
641 The return value of `with-output-to-temp-buffer' is the value of the
642 last form in BODY. If BODY does not finish normally, the buffer
643 BUFNAME is not displayed.
645 This runs the hook `temp-buffer-setup-hook' before BODY,
646 with the buffer BUFNAME temporarily current. It runs the hook
647 `temp-buffer-show-hook' after displaying buffer BUFNAME, with that
648 buffer temporarily current, and the window that was used to display it
649 temporarily selected. But it doesn't run `temp-buffer-show-hook'
650 if it uses `temp-buffer-show-function'.
652 usage: (with-output-to-temp-buffer BUFNAME BODY...) */)
653 (Lisp_Object args)
655 struct gcpro gcpro1;
656 Lisp_Object name;
657 int count = SPECPDL_INDEX ();
658 Lisp_Object buf, val;
660 GCPRO1(args);
661 name = Feval (Fcar (args));
662 CHECK_STRING (name);
663 temp_output_buffer_setup (SDATA (name));
664 buf = Vstandard_output;
665 UNGCPRO;
667 val = Fprogn (XCDR (args));
669 GCPRO1 (val);
670 temp_output_buffer_show (buf);
671 UNGCPRO;
673 return unbind_to (count, val);
677 static void print (Lisp_Object obj, register Lisp_Object printcharfun, int escapeflag);
678 static void print_preprocess (Lisp_Object obj);
679 static void print_preprocess_string (INTERVAL interval, Lisp_Object arg);
680 static void print_object (Lisp_Object obj, register Lisp_Object printcharfun, int escapeflag);
682 DEFUN ("terpri", Fterpri, Sterpri, 0, 1, 0,
683 doc: /* Output a newline to stream PRINTCHARFUN.
684 If PRINTCHARFUN is omitted or nil, the value of `standard-output' is used. */)
685 (Lisp_Object printcharfun)
687 PRINTDECLARE;
689 if (NILP (printcharfun))
690 printcharfun = Vstandard_output;
691 PRINTPREPARE;
692 PRINTCHAR ('\n');
693 PRINTFINISH;
694 return Qt;
697 DEFUN ("prin1", Fprin1, Sprin1, 1, 2, 0,
698 doc: /* Output the printed representation of OBJECT, any Lisp object.
699 Quoting characters are printed when needed to make output that `read'
700 can handle, whenever this is possible. For complex objects, the behavior
701 is controlled by `print-level' and `print-length', which see.
703 OBJECT is any of the Lisp data types: a number, a string, a symbol,
704 a list, a buffer, a window, a frame, etc.
706 A printed representation of an object is text which describes that object.
708 Optional argument PRINTCHARFUN is the output stream, which can be one
709 of these:
711 - a buffer, in which case output is inserted into that buffer at point;
712 - a marker, in which case output is inserted at marker's position;
713 - a function, in which case that function is called once for each
714 character of OBJECT's printed representation;
715 - a symbol, in which case that symbol's function definition is called; or
716 - t, in which case the output is displayed in the echo area.
718 If PRINTCHARFUN is omitted, the value of `standard-output' (which see)
719 is used instead. */)
720 (Lisp_Object object, Lisp_Object printcharfun)
722 PRINTDECLARE;
724 if (NILP (printcharfun))
725 printcharfun = Vstandard_output;
726 PRINTPREPARE;
727 print (object, printcharfun, 1);
728 PRINTFINISH;
729 return object;
732 /* a buffer which is used to hold output being built by prin1-to-string */
733 Lisp_Object Vprin1_to_string_buffer;
735 DEFUN ("prin1-to-string", Fprin1_to_string, Sprin1_to_string, 1, 2, 0,
736 doc: /* Return a string containing the printed representation of OBJECT.
737 OBJECT can be any Lisp object. This function outputs quoting characters
738 when necessary to make output that `read' can handle, whenever possible,
739 unless the optional second argument NOESCAPE is non-nil. For complex objects,
740 the behavior is controlled by `print-level' and `print-length', which see.
742 OBJECT is any of the Lisp data types: a number, a string, a symbol,
743 a list, a buffer, a window, a frame, etc.
745 A printed representation of an object is text which describes that object. */)
746 (Lisp_Object object, Lisp_Object noescape)
748 Lisp_Object printcharfun;
749 /* struct gcpro gcpro1, gcpro2; */
750 Lisp_Object save_deactivate_mark;
751 int count = SPECPDL_INDEX ();
752 struct buffer *previous;
754 specbind (Qinhibit_modification_hooks, Qt);
757 PRINTDECLARE;
759 /* Save and restore this--we are altering a buffer
760 but we don't want to deactivate the mark just for that.
761 No need for specbind, since errors deactivate the mark. */
762 save_deactivate_mark = Vdeactivate_mark;
763 /* GCPRO2 (object, save_deactivate_mark); */
764 abort_on_gc++;
766 printcharfun = Vprin1_to_string_buffer;
767 PRINTPREPARE;
768 print (object, printcharfun, NILP (noescape));
769 /* Make Vprin1_to_string_buffer be the default buffer after PRINTFINSH */
770 PRINTFINISH;
773 previous = current_buffer;
774 set_buffer_internal (XBUFFER (Vprin1_to_string_buffer));
775 object = Fbuffer_string ();
776 if (SBYTES (object) == SCHARS (object))
777 STRING_SET_UNIBYTE (object);
779 /* Note that this won't make prepare_to_modify_buffer call
780 ask-user-about-supersession-threat because this buffer
781 does not visit a file. */
782 Ferase_buffer ();
783 set_buffer_internal (previous);
785 Vdeactivate_mark = save_deactivate_mark;
786 /* UNGCPRO; */
788 abort_on_gc--;
789 return unbind_to (count, object);
792 DEFUN ("princ", Fprinc, Sprinc, 1, 2, 0,
793 doc: /* Output the printed representation of OBJECT, any Lisp object.
794 No quoting characters are used; no delimiters are printed around
795 the contents of strings.
797 OBJECT is any of the Lisp data types: a number, a string, a symbol,
798 a list, a buffer, a window, a frame, etc.
800 A printed representation of an object is text which describes that object.
802 Optional argument PRINTCHARFUN is the output stream, which can be one
803 of these:
805 - a buffer, in which case output is inserted into that buffer at point;
806 - a marker, in which case output is inserted at marker's position;
807 - a function, in which case that function is called once for each
808 character of OBJECT's printed representation;
809 - a symbol, in which case that symbol's function definition is called; or
810 - t, in which case the output is displayed in the echo area.
812 If PRINTCHARFUN is omitted, the value of `standard-output' (which see)
813 is used instead. */)
814 (Lisp_Object object, Lisp_Object printcharfun)
816 PRINTDECLARE;
818 if (NILP (printcharfun))
819 printcharfun = Vstandard_output;
820 PRINTPREPARE;
821 print (object, printcharfun, 0);
822 PRINTFINISH;
823 return object;
826 DEFUN ("print", Fprint, Sprint, 1, 2, 0,
827 doc: /* Output the printed representation of OBJECT, with newlines around it.
828 Quoting characters are printed when needed to make output that `read'
829 can handle, whenever this is possible. For complex objects, the behavior
830 is controlled by `print-level' and `print-length', which see.
832 OBJECT is any of the Lisp data types: a number, a string, a symbol,
833 a list, a buffer, a window, a frame, etc.
835 A printed representation of an object is text which describes that object.
837 Optional argument PRINTCHARFUN is the output stream, which can be one
838 of these:
840 - a buffer, in which case output is inserted into that buffer at point;
841 - a marker, in which case output is inserted at marker's position;
842 - a function, in which case that function is called once for each
843 character of OBJECT's printed representation;
844 - a symbol, in which case that symbol's function definition is called; or
845 - t, in which case the output is displayed in the echo area.
847 If PRINTCHARFUN is omitted, the value of `standard-output' (which see)
848 is used instead. */)
849 (Lisp_Object object, Lisp_Object printcharfun)
851 PRINTDECLARE;
852 struct gcpro gcpro1;
854 if (NILP (printcharfun))
855 printcharfun = Vstandard_output;
856 GCPRO1 (object);
857 PRINTPREPARE;
858 PRINTCHAR ('\n');
859 print (object, printcharfun, 1);
860 PRINTCHAR ('\n');
861 PRINTFINISH;
862 UNGCPRO;
863 return object;
866 /* The subroutine object for external-debugging-output is kept here
867 for the convenience of the debugger. */
868 Lisp_Object Qexternal_debugging_output;
870 DEFUN ("external-debugging-output", Fexternal_debugging_output, Sexternal_debugging_output, 1, 1, 0,
871 doc: /* Write CHARACTER to stderr.
872 You can call print while debugging emacs, and pass it this function
873 to make it write to the debugging output. */)
874 (Lisp_Object character)
876 CHECK_NUMBER (character);
877 putc (XINT (character), stderr);
879 #ifdef WINDOWSNT
880 /* Send the output to a debugger (nothing happens if there isn't one). */
881 if (print_output_debug_flag)
883 char buf[2] = {(char) XINT (character), '\0'};
884 OutputDebugString (buf);
886 #endif
888 return character;
891 /* This function is never called. Its purpose is to prevent
892 print_output_debug_flag from being optimized away. */
894 void
895 debug_output_compilation_hack (int x)
897 print_output_debug_flag = x;
900 #if defined (GNU_LINUX)
902 /* This functionality is not vitally important in general, so we rely on
903 non-portable ability to use stderr as lvalue. */
905 #define WITH_REDIRECT_DEBUGGING_OUTPUT 1
907 FILE *initial_stderr_stream = NULL;
909 DEFUN ("redirect-debugging-output", Fredirect_debugging_output, Sredirect_debugging_output,
910 1, 2,
911 "FDebug output file: \nP",
912 doc: /* Redirect debugging output (stderr stream) to file FILE.
913 If FILE is nil, reset target to the initial stderr stream.
914 Optional arg APPEND non-nil (interactively, with prefix arg) means
915 append to existing target file. */)
916 (Lisp_Object file, Lisp_Object append)
918 if (initial_stderr_stream != NULL)
920 BLOCK_INPUT;
921 fclose (stderr);
922 UNBLOCK_INPUT;
924 stderr = initial_stderr_stream;
925 initial_stderr_stream = NULL;
927 if (STRINGP (file))
929 file = Fexpand_file_name (file, Qnil);
930 initial_stderr_stream = stderr;
931 stderr = fopen (SDATA (file), NILP (append) ? "w" : "a");
932 if (stderr == NULL)
934 stderr = initial_stderr_stream;
935 initial_stderr_stream = NULL;
936 report_file_error ("Cannot open debugging output stream",
937 Fcons (file, Qnil));
940 return Qnil;
942 #endif /* GNU_LINUX */
945 /* This is the interface for debugging printing. */
947 void
948 debug_print (Lisp_Object arg)
950 Fprin1 (arg, Qexternal_debugging_output);
951 fprintf (stderr, "\r\n");
954 void
955 safe_debug_print (Lisp_Object arg)
957 int valid = valid_lisp_object_p (arg);
959 if (valid > 0)
960 debug_print (arg);
961 else
962 fprintf (stderr, "#<%s_LISP_OBJECT 0x%08lx>\r\n",
963 !valid ? "INVALID" : "SOME",
964 (unsigned long) XHASH (arg)
969 DEFUN ("error-message-string", Ferror_message_string, Serror_message_string,
970 1, 1, 0,
971 doc: /* Convert an error value (ERROR-SYMBOL . DATA) to an error message.
972 See Info anchor `(elisp)Definition of signal' for some details on how this
973 error message is constructed. */)
974 (Lisp_Object obj)
976 struct buffer *old = current_buffer;
977 Lisp_Object value;
978 struct gcpro gcpro1;
980 /* If OBJ is (error STRING), just return STRING.
981 That is not only faster, it also avoids the need to allocate
982 space here when the error is due to memory full. */
983 if (CONSP (obj) && EQ (XCAR (obj), Qerror)
984 && CONSP (XCDR (obj))
985 && STRINGP (XCAR (XCDR (obj)))
986 && NILP (XCDR (XCDR (obj))))
987 return XCAR (XCDR (obj));
989 print_error_message (obj, Vprin1_to_string_buffer, 0, Qnil);
991 set_buffer_internal (XBUFFER (Vprin1_to_string_buffer));
992 value = Fbuffer_string ();
994 GCPRO1 (value);
995 Ferase_buffer ();
996 set_buffer_internal (old);
997 UNGCPRO;
999 return value;
1002 /* Print an error message for the error DATA onto Lisp output stream
1003 STREAM (suitable for the print functions).
1004 CONTEXT is a C string describing the context of the error.
1005 CALLER is the Lisp function inside which the error was signaled. */
1007 void
1008 print_error_message (Lisp_Object data, Lisp_Object stream, const char *context,
1009 Lisp_Object caller)
1011 Lisp_Object errname, errmsg, file_error, tail;
1012 struct gcpro gcpro1;
1013 int i;
1015 if (context != 0)
1016 write_string_1 (context, -1, stream);
1018 /* If we know from where the error was signaled, show it in
1019 *Messages*. */
1020 if (!NILP (caller) && SYMBOLP (caller))
1022 Lisp_Object cname = SYMBOL_NAME (caller);
1023 char *name = alloca (SBYTES (cname));
1024 memcpy (name, SDATA (cname), SBYTES (cname));
1025 message_dolog (name, SBYTES (cname), 0, 0);
1026 message_dolog (": ", 2, 0, 0);
1029 errname = Fcar (data);
1031 if (EQ (errname, Qerror))
1033 data = Fcdr (data);
1034 if (!CONSP (data))
1035 data = Qnil;
1036 errmsg = Fcar (data);
1037 file_error = Qnil;
1039 else
1041 Lisp_Object error_conditions;
1042 errmsg = Fget (errname, Qerror_message);
1043 error_conditions = Fget (errname, Qerror_conditions);
1044 file_error = Fmemq (Qfile_error, error_conditions);
1047 /* Print an error message including the data items. */
1049 tail = Fcdr_safe (data);
1050 GCPRO1 (tail);
1052 /* For file-error, make error message by concatenating
1053 all the data items. They are all strings. */
1054 if (!NILP (file_error) && CONSP (tail))
1055 errmsg = XCAR (tail), tail = XCDR (tail);
1057 if (STRINGP (errmsg))
1058 Fprinc (errmsg, stream);
1059 else
1060 write_string_1 ("peculiar error", -1, stream);
1062 for (i = 0; CONSP (tail); tail = XCDR (tail), i++)
1064 Lisp_Object obj;
1066 write_string_1 (i ? ", " : ": ", 2, stream);
1067 obj = XCAR (tail);
1068 if (!NILP (file_error) || EQ (errname, Qend_of_file))
1069 Fprinc (obj, stream);
1070 else
1071 Fprin1 (obj, stream);
1074 UNGCPRO;
1080 * The buffer should be at least as large as the max string size of the
1081 * largest float, printed in the biggest notation. This is undoubtedly
1082 * 20d float_output_format, with the negative of the C-constant "HUGE"
1083 * from <math.h>.
1085 * On the vax the worst case is -1e38 in 20d format which takes 61 bytes.
1087 * I assume that IEEE-754 format numbers can take 329 bytes for the worst
1088 * case of -1e307 in 20d float_output_format. What is one to do (short of
1089 * re-writing _doprnt to be more sane)?
1090 * -wsr
1093 void
1094 float_to_string (unsigned char *buf, double data)
1096 unsigned char *cp;
1097 int width;
1099 /* Check for plus infinity in a way that won't lose
1100 if there is no plus infinity. */
1101 if (data == data / 2 && data > 1.0)
1103 strcpy (buf, "1.0e+INF");
1104 return;
1106 /* Likewise for minus infinity. */
1107 if (data == data / 2 && data < -1.0)
1109 strcpy (buf, "-1.0e+INF");
1110 return;
1112 /* Check for NaN in a way that won't fail if there are no NaNs. */
1113 if (! (data * 0.0 >= 0.0))
1115 /* Prepend "-" if the NaN's sign bit is negative.
1116 The sign bit of a double is the bit that is 1 in -0.0. */
1117 int i;
1118 union { double d; char c[sizeof (double)]; } u_data, u_minus_zero;
1119 u_data.d = data;
1120 u_minus_zero.d = - 0.0;
1121 for (i = 0; i < sizeof (double); i++)
1122 if (u_data.c[i] & u_minus_zero.c[i])
1124 *buf++ = '-';
1125 break;
1128 strcpy (buf, "0.0e+NaN");
1129 return;
1132 if (NILP (Vfloat_output_format)
1133 || !STRINGP (Vfloat_output_format))
1134 lose:
1136 /* Generate the fewest number of digits that represent the
1137 floating point value without losing information.
1138 The following method is simple but a bit slow.
1139 For ideas about speeding things up, please see:
1141 Guy L Steele Jr & Jon L White, How to print floating-point numbers
1142 accurately. SIGPLAN notices 25, 6 (June 1990), 112-126.
1144 Robert G Burger & R Kent Dybvig, Printing floating point numbers
1145 quickly and accurately, SIGPLAN notices 31, 5 (May 1996), 108-116. */
1147 width = fabs (data) < DBL_MIN ? 1 : DBL_DIG;
1149 sprintf (buf, "%.*g", width, data);
1150 while (width++ < DOUBLE_DIGITS_BOUND && atof (buf) != data);
1152 else /* oink oink */
1154 /* Check that the spec we have is fully valid.
1155 This means not only valid for printf,
1156 but meant for floats, and reasonable. */
1157 cp = SDATA (Vfloat_output_format);
1159 if (cp[0] != '%')
1160 goto lose;
1161 if (cp[1] != '.')
1162 goto lose;
1164 cp += 2;
1166 /* Check the width specification. */
1167 width = -1;
1168 if ('0' <= *cp && *cp <= '9')
1170 width = 0;
1172 width = (width * 10) + (*cp++ - '0');
1173 while (*cp >= '0' && *cp <= '9');
1175 /* A precision of zero is valid only for %f. */
1176 if (width > DBL_DIG
1177 || (width == 0 && *cp != 'f'))
1178 goto lose;
1181 if (*cp != 'e' && *cp != 'f' && *cp != 'g')
1182 goto lose;
1184 if (cp[1] != 0)
1185 goto lose;
1187 sprintf (buf, SDATA (Vfloat_output_format), data);
1190 /* Make sure there is a decimal point with digit after, or an
1191 exponent, so that the value is readable as a float. But don't do
1192 this with "%.0f"; it's valid for that not to produce a decimal
1193 point. Note that width can be 0 only for %.0f. */
1194 if (width != 0)
1196 for (cp = buf; *cp; cp++)
1197 if ((*cp < '0' || *cp > '9') && *cp != '-')
1198 break;
1200 if (*cp == '.' && cp[1] == 0)
1202 cp[1] = '0';
1203 cp[2] = 0;
1206 if (*cp == 0)
1208 *cp++ = '.';
1209 *cp++ = '0';
1210 *cp++ = 0;
1216 static void
1217 print (Lisp_Object obj, register Lisp_Object printcharfun, int escapeflag)
1219 new_backquote_output = 0;
1221 /* Reset print_number_index and Vprint_number_table only when
1222 the variable Vprint_continuous_numbering is nil. Otherwise,
1223 the values of these variables will be kept between several
1224 print functions. */
1225 if (NILP (Vprint_continuous_numbering)
1226 || NILP (Vprint_number_table))
1228 print_number_index = 0;
1229 Vprint_number_table = Qnil;
1232 /* Construct Vprint_number_table for print-gensym and print-circle. */
1233 if (!NILP (Vprint_gensym) || !NILP (Vprint_circle))
1235 int i, start, index;
1236 start = index = print_number_index;
1237 /* Construct Vprint_number_table.
1238 This increments print_number_index for the objects added. */
1239 print_depth = 0;
1240 print_preprocess (obj);
1242 /* Remove unnecessary objects, which appear only once in OBJ;
1243 that is, whose status is Qnil. Compactify the necessary objects. */
1244 for (i = start; i < print_number_index; i++)
1245 if (!NILP (PRINT_NUMBER_STATUS (Vprint_number_table, i)))
1247 PRINT_NUMBER_OBJECT (Vprint_number_table, index)
1248 = PRINT_NUMBER_OBJECT (Vprint_number_table, i);
1249 index++;
1252 /* Clear out objects outside the active part of the table. */
1253 for (i = index; i < print_number_index; i++)
1254 PRINT_NUMBER_OBJECT (Vprint_number_table, i) = Qnil;
1256 /* Reset the status field for the next print step. Now this
1257 field means whether the object has already been printed. */
1258 for (i = start; i < print_number_index; i++)
1259 PRINT_NUMBER_STATUS (Vprint_number_table, i) = Qnil;
1261 print_number_index = index;
1264 print_depth = 0;
1265 print_object (obj, printcharfun, escapeflag);
1268 /* Construct Vprint_number_table according to the structure of OBJ.
1269 OBJ itself and all its elements will be added to Vprint_number_table
1270 recursively if it is a list, vector, compiled function, char-table,
1271 string (its text properties will be traced), or a symbol that has
1272 no obarray (this is for the print-gensym feature).
1273 The status fields of Vprint_number_table mean whether each object appears
1274 more than once in OBJ: Qnil at the first time, and Qt after that . */
1275 static void
1276 print_preprocess (Lisp_Object obj)
1278 int i;
1279 EMACS_INT size;
1280 int loop_count = 0;
1281 Lisp_Object halftail;
1283 /* Give up if we go so deep that print_object will get an error. */
1284 /* See similar code in print_object. */
1285 if (print_depth >= PRINT_CIRCLE)
1286 error ("Apparently circular structure being printed");
1288 /* Avoid infinite recursion for circular nested structure
1289 in the case where Vprint_circle is nil. */
1290 if (NILP (Vprint_circle))
1292 for (i = 0; i < print_depth; i++)
1293 if (EQ (obj, being_printed[i]))
1294 return;
1295 being_printed[print_depth] = obj;
1298 print_depth++;
1299 halftail = obj;
1301 loop:
1302 if (STRINGP (obj) || CONSP (obj) || VECTORP (obj)
1303 || COMPILEDP (obj) || CHAR_TABLE_P (obj) || SUB_CHAR_TABLE_P (obj)
1304 || HASH_TABLE_P (obj)
1305 || (! NILP (Vprint_gensym)
1306 && SYMBOLP (obj)
1307 && !SYMBOL_INTERNED_P (obj)))
1309 /* In case print-circle is nil and print-gensym is t,
1310 add OBJ to Vprint_number_table only when OBJ is a symbol. */
1311 if (! NILP (Vprint_circle) || SYMBOLP (obj))
1313 for (i = 0; i < print_number_index; i++)
1314 if (EQ (PRINT_NUMBER_OBJECT (Vprint_number_table, i), obj))
1316 /* OBJ appears more than once. Let's remember that. */
1317 PRINT_NUMBER_STATUS (Vprint_number_table, i) = Qt;
1318 print_depth--;
1319 return;
1322 /* OBJ is not yet recorded. Let's add to the table. */
1323 if (print_number_index == 0)
1325 /* Initialize the table. */
1326 Vprint_number_table = Fmake_vector (make_number (40), Qnil);
1328 else if (XVECTOR (Vprint_number_table)->size == print_number_index * 2)
1330 /* Reallocate the table. */
1331 int i = print_number_index * 4;
1332 Lisp_Object old_table = Vprint_number_table;
1333 Vprint_number_table = Fmake_vector (make_number (i), Qnil);
1334 for (i = 0; i < print_number_index; i++)
1336 PRINT_NUMBER_OBJECT (Vprint_number_table, i)
1337 = PRINT_NUMBER_OBJECT (old_table, i);
1338 PRINT_NUMBER_STATUS (Vprint_number_table, i)
1339 = PRINT_NUMBER_STATUS (old_table, i);
1342 PRINT_NUMBER_OBJECT (Vprint_number_table, print_number_index) = obj;
1343 /* If Vprint_continuous_numbering is non-nil and OBJ is a gensym,
1344 always print the gensym with a number. This is a special for
1345 the lisp function byte-compile-output-docform. */
1346 if (!NILP (Vprint_continuous_numbering)
1347 && SYMBOLP (obj)
1348 && !SYMBOL_INTERNED_P (obj))
1349 PRINT_NUMBER_STATUS (Vprint_number_table, print_number_index) = Qt;
1350 print_number_index++;
1353 switch (XTYPE (obj))
1355 case Lisp_String:
1356 /* A string may have text properties, which can be circular. */
1357 traverse_intervals_noorder (STRING_INTERVALS (obj),
1358 print_preprocess_string, Qnil);
1359 break;
1361 case Lisp_Cons:
1362 /* Use HALFTAIL and LOOP_COUNT to detect circular lists,
1363 just as in print_object. */
1364 if (loop_count && EQ (obj, halftail))
1365 break;
1366 print_preprocess (XCAR (obj));
1367 obj = XCDR (obj);
1368 loop_count++;
1369 if (!(loop_count & 1))
1370 halftail = XCDR (halftail);
1371 goto loop;
1373 case Lisp_Vectorlike:
1374 size = XVECTOR (obj)->size;
1375 if (size & PSEUDOVECTOR_FLAG)
1376 size &= PSEUDOVECTOR_SIZE_MASK;
1377 for (i = 0; i < size; i++)
1378 print_preprocess (XVECTOR (obj)->contents[i]);
1379 if (HASH_TABLE_P (obj))
1380 { /* For hash tables, the key_and_value slot is past
1381 `size' because it needs to be marked specially in case
1382 the table is weak. */
1383 struct Lisp_Hash_Table *h = XHASH_TABLE (obj);
1384 print_preprocess (h->key_and_value);
1386 break;
1388 default:
1389 break;
1392 print_depth--;
1395 static void
1396 print_preprocess_string (INTERVAL interval, Lisp_Object arg)
1398 print_preprocess (interval->plist);
1401 /* A flag to control printing of `charset' text property.
1402 The default value is Qdefault. */
1403 Lisp_Object Vprint_charset_text_property;
1404 extern Lisp_Object Qdefault;
1406 static void print_check_string_charset_prop (INTERVAL interval, Lisp_Object string);
1408 #define PRINT_STRING_NON_CHARSET_FOUND 1
1409 #define PRINT_STRING_UNSAFE_CHARSET_FOUND 2
1411 /* Bitwize or of the abobe macros. */
1412 static int print_check_string_result;
1414 static void
1415 print_check_string_charset_prop (INTERVAL interval, Lisp_Object string)
1417 Lisp_Object val;
1419 if (NILP (interval->plist)
1420 || (print_check_string_result == (PRINT_STRING_NON_CHARSET_FOUND
1421 | PRINT_STRING_UNSAFE_CHARSET_FOUND)))
1422 return;
1423 for (val = interval->plist; CONSP (val) && ! EQ (XCAR (val), Qcharset);
1424 val = XCDR (XCDR (val)));
1425 if (! CONSP (val))
1427 print_check_string_result |= PRINT_STRING_NON_CHARSET_FOUND;
1428 return;
1430 if (! (print_check_string_result & PRINT_STRING_NON_CHARSET_FOUND))
1432 if (! EQ (val, interval->plist)
1433 || CONSP (XCDR (XCDR (val))))
1434 print_check_string_result |= PRINT_STRING_NON_CHARSET_FOUND;
1436 if (NILP (Vprint_charset_text_property)
1437 || ! (print_check_string_result & PRINT_STRING_UNSAFE_CHARSET_FOUND))
1439 int i, c;
1440 int charpos = interval->position;
1441 int bytepos = string_char_to_byte (string, charpos);
1442 Lisp_Object charset;
1444 charset = XCAR (XCDR (val));
1445 for (i = 0; i < LENGTH (interval); i++)
1447 FETCH_STRING_CHAR_ADVANCE (c, string, charpos, bytepos);
1448 if (! ASCII_CHAR_P (c)
1449 && ! EQ (CHARSET_NAME (CHAR_CHARSET (c)), charset))
1451 print_check_string_result |= PRINT_STRING_UNSAFE_CHARSET_FOUND;
1452 break;
1458 /* The value is (charset . nil). */
1459 static Lisp_Object print_prune_charset_plist;
1461 static Lisp_Object
1462 print_prune_string_charset (Lisp_Object string)
1464 print_check_string_result = 0;
1465 traverse_intervals (STRING_INTERVALS (string), 0,
1466 print_check_string_charset_prop, string);
1467 if (! (print_check_string_result & PRINT_STRING_UNSAFE_CHARSET_FOUND))
1469 string = Fcopy_sequence (string);
1470 if (print_check_string_result & PRINT_STRING_NON_CHARSET_FOUND)
1472 if (NILP (print_prune_charset_plist))
1473 print_prune_charset_plist = Fcons (Qcharset, Qnil);
1474 Fremove_text_properties (make_number (0),
1475 make_number (SCHARS (string)),
1476 print_prune_charset_plist, string);
1478 else
1479 Fset_text_properties (make_number (0), make_number (SCHARS (string)),
1480 Qnil, string);
1482 return string;
1485 static void
1486 print_object (Lisp_Object obj, register Lisp_Object printcharfun, int escapeflag)
1488 char buf[40];
1490 QUIT;
1492 /* See similar code in print_preprocess. */
1493 if (print_depth >= PRINT_CIRCLE)
1494 error ("Apparently circular structure being printed");
1496 /* Detect circularities and truncate them. */
1497 if (STRINGP (obj) || CONSP (obj) || VECTORP (obj)
1498 || COMPILEDP (obj) || CHAR_TABLE_P (obj) || SUB_CHAR_TABLE_P (obj)
1499 || HASH_TABLE_P (obj)
1500 || (! NILP (Vprint_gensym)
1501 && SYMBOLP (obj)
1502 && !SYMBOL_INTERNED_P (obj)))
1504 if (NILP (Vprint_circle) && NILP (Vprint_gensym))
1506 /* Simple but incomplete way. */
1507 int i;
1508 for (i = 0; i < print_depth; i++)
1509 if (EQ (obj, being_printed[i]))
1511 sprintf (buf, "#%d", i);
1512 strout (buf, -1, -1, printcharfun, 0);
1513 return;
1515 being_printed[print_depth] = obj;
1517 else
1519 /* With the print-circle feature. */
1520 int i;
1521 for (i = 0; i < print_number_index; i++)
1522 if (EQ (PRINT_NUMBER_OBJECT (Vprint_number_table, i), obj))
1524 if (NILP (PRINT_NUMBER_STATUS (Vprint_number_table, i)))
1526 /* Add a prefix #n= if OBJ has not yet been printed;
1527 that is, its status field is nil. */
1528 sprintf (buf, "#%d=", i + 1);
1529 strout (buf, -1, -1, printcharfun, 0);
1530 /* OBJ is going to be printed. Set the status to t. */
1531 PRINT_NUMBER_STATUS (Vprint_number_table, i) = Qt;
1532 break;
1534 else
1536 /* Just print #n# if OBJ has already been printed. */
1537 sprintf (buf, "#%d#", i + 1);
1538 strout (buf, -1, -1, printcharfun, 0);
1539 return;
1545 print_depth++;
1547 switch (XTYPE (obj))
1549 case_Lisp_Int:
1550 if (sizeof (int) == sizeof (EMACS_INT))
1551 sprintf (buf, "%d", (int) XINT (obj));
1552 else if (sizeof (long) == sizeof (EMACS_INT))
1553 sprintf (buf, "%ld", (long) XINT (obj));
1554 else
1555 abort ();
1556 strout (buf, -1, -1, printcharfun, 0);
1557 break;
1559 case Lisp_Float:
1561 char pigbuf[350]; /* see comments in float_to_string */
1563 float_to_string (pigbuf, XFLOAT_DATA (obj));
1564 strout (pigbuf, -1, -1, printcharfun, 0);
1566 break;
1568 case Lisp_String:
1569 if (!escapeflag)
1570 print_string (obj, printcharfun);
1571 else
1573 register int i, i_byte;
1574 struct gcpro gcpro1;
1575 unsigned char *str;
1576 int size_byte;
1577 /* 1 means we must ensure that the next character we output
1578 cannot be taken as part of a hex character escape. */
1579 int need_nonhex = 0;
1580 int multibyte = STRING_MULTIBYTE (obj);
1582 GCPRO1 (obj);
1584 if (! EQ (Vprint_charset_text_property, Qt))
1585 obj = print_prune_string_charset (obj);
1587 if (!NULL_INTERVAL_P (STRING_INTERVALS (obj)))
1589 PRINTCHAR ('#');
1590 PRINTCHAR ('(');
1593 PRINTCHAR ('\"');
1594 str = SDATA (obj);
1595 size_byte = SBYTES (obj);
1597 for (i = 0, i_byte = 0; i_byte < size_byte;)
1599 /* Here, we must convert each multi-byte form to the
1600 corresponding character code before handing it to PRINTCHAR. */
1601 int len;
1602 int c;
1604 if (multibyte)
1606 c = STRING_CHAR_AND_LENGTH (str + i_byte, len);
1607 i_byte += len;
1609 else
1610 c = str[i_byte++];
1612 QUIT;
1614 if (c == '\n' && print_escape_newlines)
1616 PRINTCHAR ('\\');
1617 PRINTCHAR ('n');
1619 else if (c == '\f' && print_escape_newlines)
1621 PRINTCHAR ('\\');
1622 PRINTCHAR ('f');
1624 else if (multibyte
1625 && (CHAR_BYTE8_P (c)
1626 || (! ASCII_CHAR_P (c) && print_escape_multibyte)))
1628 /* When multibyte is disabled,
1629 print multibyte string chars using hex escapes.
1630 For a char code that could be in a unibyte string,
1631 when found in a multibyte string, always use a hex escape
1632 so it reads back as multibyte. */
1633 unsigned char outbuf[50];
1635 if (CHAR_BYTE8_P (c))
1636 sprintf (outbuf, "\\%03o", CHAR_TO_BYTE8 (c));
1637 else
1639 sprintf (outbuf, "\\x%04x", c);
1640 need_nonhex = 1;
1642 strout (outbuf, -1, -1, printcharfun, 0);
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' || *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 (TERMINALP (obj))
1980 struct terminal *t = XTERMINAL (obj);
1981 strout ("#<terminal ", -1, -1, printcharfun, 0);
1982 sprintf (buf, "%d", t->id);
1983 strout (buf, -1, -1, printcharfun, 0);
1984 if (t->name)
1986 strout (" on ", -1, -1, printcharfun, 0);
1987 strout (t->name, -1, -1, printcharfun, 0);
1989 PRINTCHAR ('>');
1991 else if (HASH_TABLE_P (obj))
1993 struct Lisp_Hash_Table *h = XHASH_TABLE (obj);
1994 int i, real_size, size;
1995 #if 0
1996 strout ("#<hash-table", -1, -1, printcharfun, 0);
1997 if (SYMBOLP (h->test))
1999 PRINTCHAR (' ');
2000 PRINTCHAR ('\'');
2001 strout (SDATA (SYMBOL_NAME (h->test)), -1, -1, printcharfun, 0);
2002 PRINTCHAR (' ');
2003 strout (SDATA (SYMBOL_NAME (h->weak)), -1, -1, printcharfun, 0);
2004 PRINTCHAR (' ');
2005 sprintf (buf, "%ld/%ld", (long) h->count,
2006 (long) XVECTOR (h->next)->size);
2007 strout (buf, -1, -1, printcharfun, 0);
2009 sprintf (buf, " 0x%lx", (unsigned long) h);
2010 strout (buf, -1, -1, printcharfun, 0);
2011 PRINTCHAR ('>');
2012 #endif
2013 /* Implement a readable output, e.g.:
2014 #s(hash-table size 2 test equal data (k1 v1 k2 v2)) */
2015 /* Always print the size. */
2016 sprintf (buf, "#s(hash-table size %ld",
2017 (long) XVECTOR (h->next)->size);
2018 strout (buf, -1, -1, printcharfun, 0);
2020 if (!NILP (h->test))
2022 strout (" test ", -1, -1, printcharfun, 0);
2023 print_object (h->test, printcharfun, 0);
2026 if (!NILP (h->weak))
2028 strout (" weakness ", -1, -1, printcharfun, 0);
2029 print_object (h->weak, printcharfun, 0);
2032 if (!NILP (h->rehash_size))
2034 strout (" rehash-size ", -1, -1, printcharfun, 0);
2035 print_object (h->rehash_size, printcharfun, 0);
2038 if (!NILP (h->rehash_threshold))
2040 strout (" rehash-threshold ", -1, -1, printcharfun, 0);
2041 print_object (h->rehash_threshold, printcharfun, 0);
2044 strout (" data ", -1, -1, printcharfun, 0);
2046 /* Print the data here as a plist. */
2047 real_size = HASH_TABLE_SIZE (h);
2048 size = real_size;
2050 /* Don't print more elements than the specified maximum. */
2051 if (NATNUMP (Vprint_length)
2052 && XFASTINT (Vprint_length) < size)
2053 size = XFASTINT (Vprint_length);
2055 PRINTCHAR ('(');
2056 for (i = 0; i < size; i++)
2057 if (!NILP (HASH_HASH (h, i)))
2059 if (i) PRINTCHAR (' ');
2060 print_object (HASH_KEY (h, i), printcharfun, 1);
2061 PRINTCHAR (' ');
2062 print_object (HASH_VALUE (h, i), printcharfun, 1);
2065 if (size < real_size)
2066 strout (" ...", 4, 4, printcharfun, 0);
2068 PRINTCHAR (')');
2069 PRINTCHAR (')');
2072 else if (BUFFERP (obj))
2074 if (NILP (XBUFFER (obj)->name))
2075 strout ("#<killed buffer>", -1, -1, printcharfun, 0);
2076 else if (escapeflag)
2078 strout ("#<buffer ", -1, -1, printcharfun, 0);
2079 print_string (XBUFFER (obj)->name, printcharfun);
2080 PRINTCHAR ('>');
2082 else
2083 print_string (XBUFFER (obj)->name, printcharfun);
2085 else if (WINDOW_CONFIGURATIONP (obj))
2087 strout ("#<window-configuration>", -1, -1, printcharfun, 0);
2089 else if (FRAMEP (obj))
2091 strout ((FRAME_LIVE_P (XFRAME (obj))
2092 ? "#<frame " : "#<dead frame "),
2093 -1, -1, printcharfun, 0);
2094 print_string (XFRAME (obj)->name, printcharfun);
2095 sprintf (buf, " 0x%lx", (unsigned long) (XFRAME (obj)));
2096 strout (buf, -1, -1, printcharfun, 0);
2097 PRINTCHAR ('>');
2099 else if (FONTP (obj))
2101 EMACS_INT i;
2103 if (! FONT_OBJECT_P (obj))
2105 if (FONT_SPEC_P (obj))
2106 strout ("#<font-spec", -1, -1, printcharfun, 0);
2107 else
2108 strout ("#<font-entity", -1, -1, printcharfun, 0);
2109 for (i = 0; i < FONT_SPEC_MAX; i++)
2111 PRINTCHAR (' ');
2112 if (i < FONT_WEIGHT_INDEX || i > FONT_WIDTH_INDEX)
2113 print_object (AREF (obj, i), printcharfun, escapeflag);
2114 else
2115 print_object (font_style_symbolic (obj, i, 0),
2116 printcharfun, escapeflag);
2119 else
2121 strout ("#<font-object ", -1, -1, printcharfun, 0);
2122 print_object (AREF (obj, FONT_NAME_INDEX), printcharfun,
2123 escapeflag);
2125 PRINTCHAR ('>');
2127 else
2129 EMACS_INT size = XVECTOR (obj)->size;
2130 if (COMPILEDP (obj))
2132 PRINTCHAR ('#');
2133 size &= PSEUDOVECTOR_SIZE_MASK;
2135 if (CHAR_TABLE_P (obj) || SUB_CHAR_TABLE_P (obj))
2137 /* We print a char-table as if it were a vector,
2138 lumping the parent and default slots in with the
2139 character slots. But we add #^ as a prefix. */
2141 /* Make each lowest sub_char_table start a new line.
2142 Otherwise we'll make a line extremely long, which
2143 results in slow redisplay. */
2144 if (SUB_CHAR_TABLE_P (obj)
2145 && XINT (XSUB_CHAR_TABLE (obj)->depth) == 3)
2146 PRINTCHAR ('\n');
2147 PRINTCHAR ('#');
2148 PRINTCHAR ('^');
2149 if (SUB_CHAR_TABLE_P (obj))
2150 PRINTCHAR ('^');
2151 size &= PSEUDOVECTOR_SIZE_MASK;
2153 if (size & PSEUDOVECTOR_FLAG)
2154 goto badtype;
2156 PRINTCHAR ('[');
2158 register int i;
2159 register Lisp_Object tem;
2160 int real_size = size;
2162 /* Don't print more elements than the specified maximum. */
2163 if (NATNUMP (Vprint_length)
2164 && XFASTINT (Vprint_length) < size)
2165 size = XFASTINT (Vprint_length);
2167 for (i = 0; i < size; i++)
2169 if (i) PRINTCHAR (' ');
2170 tem = XVECTOR (obj)->contents[i];
2171 print_object (tem, printcharfun, escapeflag);
2173 if (size < real_size)
2174 strout (" ...", 4, 4, printcharfun, 0);
2176 PRINTCHAR (']');
2178 break;
2180 case Lisp_Misc:
2181 switch (XMISCTYPE (obj))
2183 case Lisp_Misc_Marker:
2184 strout ("#<marker ", -1, -1, printcharfun, 0);
2185 /* Do you think this is necessary? */
2186 if (XMARKER (obj)->insertion_type != 0)
2187 strout ("(moves after insertion) ", -1, -1, printcharfun, 0);
2188 if (! XMARKER (obj)->buffer)
2189 strout ("in no buffer", -1, -1, printcharfun, 0);
2190 else
2192 sprintf (buf, "at %d", marker_position (obj));
2193 strout (buf, -1, -1, printcharfun, 0);
2194 strout (" in ", -1, -1, printcharfun, 0);
2195 print_string (XMARKER (obj)->buffer->name, printcharfun);
2197 PRINTCHAR ('>');
2198 break;
2200 case Lisp_Misc_Overlay:
2201 strout ("#<overlay ", -1, -1, printcharfun, 0);
2202 if (! XMARKER (OVERLAY_START (obj))->buffer)
2203 strout ("in no buffer", -1, -1, printcharfun, 0);
2204 else
2206 sprintf (buf, "from %d to %d in ",
2207 marker_position (OVERLAY_START (obj)),
2208 marker_position (OVERLAY_END (obj)));
2209 strout (buf, -1, -1, printcharfun, 0);
2210 print_string (XMARKER (OVERLAY_START (obj))->buffer->name,
2211 printcharfun);
2213 PRINTCHAR ('>');
2214 break;
2216 /* Remaining cases shouldn't happen in normal usage, but let's print
2217 them anyway for the benefit of the debugger. */
2218 case Lisp_Misc_Free:
2219 strout ("#<misc free cell>", -1, -1, printcharfun, 0);
2220 break;
2222 case Lisp_Misc_Save_Value:
2223 strout ("#<save_value ", -1, -1, printcharfun, 0);
2224 sprintf(buf, "ptr=0x%08lx int=%d",
2225 (unsigned long) XSAVE_VALUE (obj)->pointer,
2226 XSAVE_VALUE (obj)->integer);
2227 strout (buf, -1, -1, printcharfun, 0);
2228 PRINTCHAR ('>');
2229 break;
2231 default:
2232 goto badtype;
2234 break;
2236 default:
2237 badtype:
2239 /* We're in trouble if this happens!
2240 Probably should just abort () */
2241 strout ("#<EMACS BUG: INVALID DATATYPE ", -1, -1, printcharfun, 0);
2242 if (MISCP (obj))
2243 sprintf (buf, "(MISC 0x%04x)", (int) XMISCTYPE (obj));
2244 else if (VECTORLIKEP (obj))
2245 sprintf (buf, "(PVEC 0x%08x)", (int) XVECTOR (obj)->size);
2246 else
2247 sprintf (buf, "(0x%02x)", (int) XTYPE (obj));
2248 strout (buf, -1, -1, printcharfun, 0);
2249 strout (" Save your buffers immediately and please report this bug>",
2250 -1, -1, printcharfun, 0);
2254 print_depth--;
2258 /* Print a description of INTERVAL using PRINTCHARFUN.
2259 This is part of printing a string that has text properties. */
2261 void
2262 print_interval (INTERVAL interval, Lisp_Object printcharfun)
2264 if (NILP (interval->plist))
2265 return;
2266 PRINTCHAR (' ');
2267 print_object (make_number (interval->position), printcharfun, 1);
2268 PRINTCHAR (' ');
2269 print_object (make_number (interval->position + LENGTH (interval)),
2270 printcharfun, 1);
2271 PRINTCHAR (' ');
2272 print_object (interval->plist, printcharfun, 1);
2276 void
2277 syms_of_print (void)
2279 Qtemp_buffer_setup_hook = intern_c_string ("temp-buffer-setup-hook");
2280 staticpro (&Qtemp_buffer_setup_hook);
2282 DEFVAR_LISP ("standard-output", &Vstandard_output,
2283 doc: /* Output stream `print' uses by default for outputting a character.
2284 This may be any function of one argument.
2285 It may also be a buffer (output is inserted before point)
2286 or a marker (output is inserted and the marker is advanced)
2287 or the symbol t (output appears in the echo area). */);
2288 Vstandard_output = Qt;
2289 Qstandard_output = intern_c_string ("standard-output");
2290 staticpro (&Qstandard_output);
2292 DEFVAR_LISP ("float-output-format", &Vfloat_output_format,
2293 doc: /* The format descriptor string used to print floats.
2294 This is a %-spec like those accepted by `printf' in C,
2295 but with some restrictions. It must start with the two characters `%.'.
2296 After that comes an integer precision specification,
2297 and then a letter which controls the format.
2298 The letters allowed are `e', `f' and `g'.
2299 Use `e' for exponential notation \"DIG.DIGITSeEXPT\"
2300 Use `f' for decimal point notation \"DIGITS.DIGITS\".
2301 Use `g' to choose the shorter of those two formats for the number at hand.
2302 The precision in any of these cases is the number of digits following
2303 the decimal point. With `f', a precision of 0 means to omit the
2304 decimal point. 0 is not allowed with `e' or `g'.
2306 A value of nil means to use the shortest notation
2307 that represents the number without losing information. */);
2308 Vfloat_output_format = Qnil;
2309 Qfloat_output_format = intern_c_string ("float-output-format");
2310 staticpro (&Qfloat_output_format);
2312 DEFVAR_LISP ("print-length", &Vprint_length,
2313 doc: /* Maximum length of list to print before abbreviating.
2314 A value of nil means no limit. See also `eval-expression-print-length'. */);
2315 Vprint_length = Qnil;
2317 DEFVAR_LISP ("print-level", &Vprint_level,
2318 doc: /* Maximum depth of list nesting to print before abbreviating.
2319 A value of nil means no limit. See also `eval-expression-print-level'. */);
2320 Vprint_level = Qnil;
2322 DEFVAR_BOOL ("print-escape-newlines", &print_escape_newlines,
2323 doc: /* Non-nil means print newlines in strings as `\\n'.
2324 Also print formfeeds as `\\f'. */);
2325 print_escape_newlines = 0;
2327 DEFVAR_BOOL ("print-escape-nonascii", &print_escape_nonascii,
2328 doc: /* Non-nil means print unibyte non-ASCII chars in strings as \\OOO.
2329 \(OOO is the octal representation of the character code.)
2330 Only single-byte characters are affected, and only in `prin1'.
2331 When the output goes in a multibyte buffer, this feature is
2332 enabled regardless of the value of the variable. */);
2333 print_escape_nonascii = 0;
2335 DEFVAR_BOOL ("print-escape-multibyte", &print_escape_multibyte,
2336 doc: /* Non-nil means print multibyte characters in strings as \\xXXXX.
2337 \(XXXX is the hex representation of the character code.)
2338 This affects only `prin1'. */);
2339 print_escape_multibyte = 0;
2341 DEFVAR_BOOL ("print-quoted", &print_quoted,
2342 doc: /* Non-nil means print quoted forms with reader syntax.
2343 I.e., (quote foo) prints as 'foo, (function foo) as #'foo. */);
2344 print_quoted = 0;
2346 DEFVAR_LISP ("print-gensym", &Vprint_gensym,
2347 doc: /* Non-nil means print uninterned symbols so they will read as uninterned.
2348 I.e., the value of (make-symbol \"foobar\") prints as #:foobar.
2349 When the uninterned symbol appears within a recursive data structure,
2350 and the symbol appears more than once, in addition use the #N# and #N=
2351 constructs as needed, so that multiple references to the same symbol are
2352 shared once again when the text is read back. */);
2353 Vprint_gensym = Qnil;
2355 DEFVAR_LISP ("print-circle", &Vprint_circle,
2356 doc: /* *Non-nil means print recursive structures using #N= and #N# syntax.
2357 If nil, printing proceeds recursively and may lead to
2358 `max-lisp-eval-depth' being exceeded or an error may occur:
2359 \"Apparently circular structure being printed.\" Also see
2360 `print-length' and `print-level'.
2361 If non-nil, shared substructures anywhere in the structure are printed
2362 with `#N=' before the first occurrence (in the order of the print
2363 representation) and `#N#' in place of each subsequent occurrence,
2364 where N is a positive decimal integer. */);
2365 Vprint_circle = Qnil;
2367 DEFVAR_LISP ("print-continuous-numbering", &Vprint_continuous_numbering,
2368 doc: /* *Non-nil means number continuously across print calls.
2369 This affects the numbers printed for #N= labels and #M# references.
2370 See also `print-circle', `print-gensym', and `print-number-table'.
2371 This variable should not be set with `setq'; bind it with a `let' instead. */);
2372 Vprint_continuous_numbering = Qnil;
2374 DEFVAR_LISP ("print-number-table", &Vprint_number_table,
2375 doc: /* A vector used internally to produce `#N=' labels and `#N#' references.
2376 The Lisp printer uses this vector to detect Lisp objects referenced more
2377 than once.
2379 When you bind `print-continuous-numbering' to t, you should probably
2380 also bind `print-number-table' to nil. This ensures that the value of
2381 `print-number-table' can be garbage-collected once the printing is
2382 done. If all elements of `print-number-table' are nil, it means that
2383 the printing done so far has not found any shared structure or objects
2384 that need to be recorded in the table. */);
2385 Vprint_number_table = Qnil;
2387 DEFVAR_LISP ("print-charset-text-property", &Vprint_charset_text_property,
2388 doc: /* A flag to control printing of `charset' text property on printing a string.
2389 The value must be nil, t, or `default'.
2391 If the value is nil, don't print the text property `charset'.
2393 If the value is t, always print the text property `charset'.
2395 If the value is `default', print the text property `charset' only when
2396 the value is different from what is guessed in the current charset
2397 priorities. */);
2398 Vprint_charset_text_property = Qdefault;
2400 /* prin1_to_string_buffer initialized in init_buffer_once in buffer.c */
2401 staticpro (&Vprin1_to_string_buffer);
2403 defsubr (&Sprin1);
2404 defsubr (&Sprin1_to_string);
2405 defsubr (&Serror_message_string);
2406 defsubr (&Sprinc);
2407 defsubr (&Sprint);
2408 defsubr (&Sterpri);
2409 defsubr (&Swrite_char);
2410 defsubr (&Sexternal_debugging_output);
2411 #ifdef WITH_REDIRECT_DEBUGGING_OUTPUT
2412 defsubr (&Sredirect_debugging_output);
2413 #endif
2415 Qexternal_debugging_output = intern_c_string ("external-debugging-output");
2416 staticpro (&Qexternal_debugging_output);
2418 Qprint_escape_newlines = intern_c_string ("print-escape-newlines");
2419 staticpro (&Qprint_escape_newlines);
2421 Qprint_escape_multibyte = intern_c_string ("print-escape-multibyte");
2422 staticpro (&Qprint_escape_multibyte);
2424 Qprint_escape_nonascii = intern_c_string ("print-escape-nonascii");
2425 staticpro (&Qprint_escape_nonascii);
2427 print_prune_charset_plist = Qnil;
2428 staticpro (&print_prune_charset_plist);
2430 defsubr (&Swith_output_to_temp_buffer);
2433 /* arch-tag: bc797170-94ae-41de-86e3-75e20f8f7a39
2434 (do not change this comment) */