Code cleanup. Added ChangeLog entry.
[emacs.git] / src / print.c
blob97f6494678f05150d9d61b162dd7cb680f3e376b
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 extern int minibuffer_auto_raise;
171 void print_interval (INTERVAL interval, Lisp_Object printcharfun);
173 /* GDB resets this to zero on W32 to disable OutputDebugString calls. */
174 int print_output_debug_flag = 1;
177 /* Low level output routines for characters and strings */
179 /* Lisp functions to do output using a stream
180 must have the stream in a variable called printcharfun
181 and must start with PRINTPREPARE, end with PRINTFINISH,
182 and use PRINTDECLARE to declare common variables.
183 Use PRINTCHAR to output one character,
184 or call strout to output a block of characters. */
186 #define PRINTDECLARE \
187 struct buffer *old = current_buffer; \
188 int old_point = -1, start_point = -1; \
189 int old_point_byte = -1, start_point_byte = -1; \
190 int specpdl_count = SPECPDL_INDEX (); \
191 int free_print_buffer = 0; \
192 int multibyte = !NILP (current_buffer->enable_multibyte_characters); \
193 Lisp_Object original
195 #define PRINTPREPARE \
196 original = printcharfun; \
197 if (NILP (printcharfun)) printcharfun = Qt; \
198 if (BUFFERP (printcharfun)) \
200 if (XBUFFER (printcharfun) != current_buffer) \
201 Fset_buffer (printcharfun); \
202 printcharfun = Qnil; \
204 if (MARKERP (printcharfun)) \
206 EMACS_INT marker_pos; \
207 if (! XMARKER (printcharfun)->buffer) \
208 error ("Marker does not point anywhere"); \
209 if (XMARKER (printcharfun)->buffer != current_buffer) \
210 set_buffer_internal (XMARKER (printcharfun)->buffer); \
211 marker_pos = marker_position (printcharfun); \
212 if (marker_pos < BEGV || marker_pos > ZV) \
213 error ("Marker is outside the accessible part of the buffer"); \
214 old_point = PT; \
215 old_point_byte = PT_BYTE; \
216 SET_PT_BOTH (marker_pos, \
217 marker_byte_position (printcharfun)); \
218 start_point = PT; \
219 start_point_byte = PT_BYTE; \
220 printcharfun = Qnil; \
222 if (NILP (printcharfun)) \
224 Lisp_Object string; \
225 if (NILP (current_buffer->enable_multibyte_characters) \
226 && ! print_escape_multibyte) \
227 specbind (Qprint_escape_multibyte, Qt); \
228 if (! NILP (current_buffer->enable_multibyte_characters) \
229 && ! print_escape_nonascii) \
230 specbind (Qprint_escape_nonascii, Qt); \
231 if (print_buffer != 0) \
233 string = make_string_from_bytes (print_buffer, \
234 print_buffer_pos, \
235 print_buffer_pos_byte); \
236 record_unwind_protect (print_unwind, string); \
238 else \
240 print_buffer_size = 1000; \
241 print_buffer = (char *) xmalloc (print_buffer_size); \
242 free_print_buffer = 1; \
244 print_buffer_pos = 0; \
245 print_buffer_pos_byte = 0; \
247 if (EQ (printcharfun, Qt) && ! noninteractive) \
248 setup_echo_area_for_printing (multibyte);
250 #define PRINTFINISH \
251 if (NILP (printcharfun)) \
253 if (print_buffer_pos != print_buffer_pos_byte \
254 && NILP (current_buffer->enable_multibyte_characters)) \
256 unsigned char *temp \
257 = (unsigned char *) alloca (print_buffer_pos + 1); \
258 copy_text (print_buffer, temp, print_buffer_pos_byte, \
259 1, 0); \
260 insert_1_both (temp, print_buffer_pos, \
261 print_buffer_pos, 0, 1, 0); \
263 else \
264 insert_1_both (print_buffer, print_buffer_pos, \
265 print_buffer_pos_byte, 0, 1, 0); \
266 signal_after_change (PT - print_buffer_pos, 0, print_buffer_pos);\
268 if (free_print_buffer) \
270 xfree (print_buffer); \
271 print_buffer = 0; \
273 unbind_to (specpdl_count, Qnil); \
274 if (MARKERP (original)) \
275 set_marker_both (original, Qnil, PT, PT_BYTE); \
276 if (old_point >= 0) \
277 SET_PT_BOTH (old_point + (old_point >= start_point \
278 ? PT - start_point : 0), \
279 old_point_byte + (old_point_byte >= start_point_byte \
280 ? PT_BYTE - start_point_byte : 0)); \
281 if (old != current_buffer) \
282 set_buffer_internal (old);
284 #define PRINTCHAR(ch) printchar (ch, printcharfun)
286 /* This is used to restore the saved contents of print_buffer
287 when there is a recursive call to print. */
289 static Lisp_Object
290 print_unwind (Lisp_Object saved_text)
292 memcpy (print_buffer, SDATA (saved_text), SCHARS (saved_text));
293 return Qnil;
297 /* Print character CH using method FUN. FUN nil means print to
298 print_buffer. FUN t means print to echo area or stdout if
299 non-interactive. If FUN is neither nil nor t, call FUN with CH as
300 argument. */
302 static void
303 printchar (unsigned int ch, Lisp_Object fun)
305 if (!NILP (fun) && !EQ (fun, Qt))
306 call1 (fun, make_number (ch));
307 else
309 unsigned char str[MAX_MULTIBYTE_LENGTH];
310 int len = CHAR_STRING (ch, str);
312 QUIT;
314 if (NILP (fun))
316 if (print_buffer_pos_byte + len >= print_buffer_size)
317 print_buffer = (char *) xrealloc (print_buffer,
318 print_buffer_size *= 2);
319 memcpy (print_buffer + print_buffer_pos_byte, str, len);
320 print_buffer_pos += 1;
321 print_buffer_pos_byte += len;
323 else if (noninteractive)
325 fwrite (str, 1, len, stdout);
326 noninteractive_need_newline = 1;
328 else
330 int multibyte_p
331 = !NILP (current_buffer->enable_multibyte_characters);
333 setup_echo_area_for_printing (multibyte_p);
334 insert_char (ch);
335 message_dolog (str, len, 0, multibyte_p);
341 /* Output SIZE characters, SIZE_BYTE bytes from string PTR using
342 method PRINTCHARFUN. If SIZE < 0, use the string length of PTR for
343 both SIZE and SIZE_BYTE. PRINTCHARFUN nil means output to
344 print_buffer. PRINTCHARFUN t means output to the echo area or to
345 stdout if non-interactive. If neither nil nor t, call Lisp
346 function PRINTCHARFUN for each character printed. MULTIBYTE
347 non-zero means PTR contains multibyte characters.
349 In the case where PRINTCHARFUN is nil, it is safe for PTR to point
350 to data in a Lisp string. Otherwise that is not safe. */
352 static void
353 strout (const char *ptr, int size, int size_byte, Lisp_Object printcharfun,
354 int multibyte)
356 if (size < 0)
357 size_byte = size = strlen (ptr);
359 if (NILP (printcharfun))
361 if (print_buffer_pos_byte + size_byte > print_buffer_size)
363 print_buffer_size = print_buffer_size * 2 + size_byte;
364 print_buffer = (char *) xrealloc (print_buffer,
365 print_buffer_size);
367 memcpy (print_buffer + print_buffer_pos_byte, ptr, size_byte);
368 print_buffer_pos += size;
369 print_buffer_pos_byte += size_byte;
371 else if (noninteractive && EQ (printcharfun, Qt))
373 fwrite (ptr, 1, size_byte, stdout);
374 noninteractive_need_newline = 1;
376 else if (EQ (printcharfun, Qt))
378 /* Output to echo area. We're trying to avoid a little overhead
379 here, that's the reason we don't call printchar to do the
380 job. */
381 int i;
382 int multibyte_p
383 = !NILP (current_buffer->enable_multibyte_characters);
385 setup_echo_area_for_printing (multibyte_p);
386 message_dolog (ptr, size_byte, 0, multibyte_p);
388 if (size == size_byte)
390 for (i = 0; i < size; ++i)
391 insert_char ((unsigned char) *ptr++);
393 else
395 int len;
396 for (i = 0; i < size_byte; i += len)
398 int ch = STRING_CHAR_AND_LENGTH (ptr + i, len);
399 insert_char (ch);
403 else
405 /* PRINTCHARFUN is a Lisp function. */
406 int i = 0;
408 if (size == size_byte)
410 while (i < size_byte)
412 int ch = ptr[i++];
413 PRINTCHAR (ch);
416 else
418 while (i < size_byte)
420 /* Here, we must convert each multi-byte form to the
421 corresponding character code before handing it to
422 PRINTCHAR. */
423 int len;
424 int ch = STRING_CHAR_AND_LENGTH (ptr + i, len);
425 PRINTCHAR (ch);
426 i += len;
432 /* Print the contents of a string STRING using PRINTCHARFUN.
433 It isn't safe to use strout in many cases,
434 because printing one char can relocate. */
436 static void
437 print_string (Lisp_Object string, Lisp_Object printcharfun)
439 if (EQ (printcharfun, Qt) || NILP (printcharfun))
441 int chars;
443 if (print_escape_nonascii)
444 string = string_escape_byte8 (string);
446 if (STRING_MULTIBYTE (string))
447 chars = SCHARS (string);
448 else if (! print_escape_nonascii
449 && (EQ (printcharfun, Qt)
450 ? ! NILP (buffer_defaults.enable_multibyte_characters)
451 : ! NILP (current_buffer->enable_multibyte_characters)))
453 /* If unibyte string STRING contains 8-bit codes, we must
454 convert STRING to a multibyte string containing the same
455 character codes. */
456 Lisp_Object newstr;
457 int bytes;
459 chars = SBYTES (string);
460 bytes = parse_str_to_multibyte (SDATA (string), chars);
461 if (chars < bytes)
463 newstr = make_uninit_multibyte_string (chars, bytes);
464 memcpy (SDATA (newstr), SDATA (string), chars);
465 str_to_multibyte (SDATA (newstr), bytes, chars);
466 string = newstr;
469 else
470 chars = SBYTES (string);
472 if (EQ (printcharfun, Qt))
474 /* Output to echo area. */
475 int nbytes = SBYTES (string);
476 char *buffer;
478 /* Copy the string contents so that relocation of STRING by
479 GC does not cause trouble. */
480 USE_SAFE_ALLOCA;
482 SAFE_ALLOCA (buffer, char *, nbytes);
483 memcpy (buffer, SDATA (string), nbytes);
485 strout (buffer, chars, SBYTES (string),
486 printcharfun, STRING_MULTIBYTE (string));
488 SAFE_FREE ();
490 else
491 /* No need to copy, since output to print_buffer can't GC. */
492 strout (SDATA (string),
493 chars, SBYTES (string),
494 printcharfun, STRING_MULTIBYTE (string));
496 else
498 /* Otherwise, string may be relocated by printing one char.
499 So re-fetch the string address for each character. */
500 int i;
501 int size = SCHARS (string);
502 int size_byte = SBYTES (string);
503 struct gcpro gcpro1;
504 GCPRO1 (string);
505 if (size == size_byte)
506 for (i = 0; i < size; i++)
507 PRINTCHAR (SREF (string, i));
508 else
509 for (i = 0; i < size_byte; )
511 /* Here, we must convert each multi-byte form to the
512 corresponding character code before handing it to PRINTCHAR. */
513 int len;
514 int ch = STRING_CHAR_AND_LENGTH (SDATA (string) + i, len);
515 PRINTCHAR (ch);
516 i += len;
518 UNGCPRO;
522 DEFUN ("write-char", Fwrite_char, Swrite_char, 1, 2, 0,
523 doc: /* Output character CHARACTER to stream PRINTCHARFUN.
524 PRINTCHARFUN defaults to the value of `standard-output' (which see). */)
525 (Lisp_Object character, Lisp_Object printcharfun)
527 PRINTDECLARE;
529 if (NILP (printcharfun))
530 printcharfun = Vstandard_output;
531 CHECK_NUMBER (character);
532 PRINTPREPARE;
533 PRINTCHAR (XINT (character));
534 PRINTFINISH;
535 return character;
538 /* Used from outside of print.c to print a block of SIZE
539 single-byte chars at DATA on the default output stream.
540 Do not use this on the contents of a Lisp string. */
542 void
543 write_string (const char *data, int size)
545 PRINTDECLARE;
546 Lisp_Object printcharfun;
548 printcharfun = Vstandard_output;
550 PRINTPREPARE;
551 strout (data, size, size, printcharfun, 0);
552 PRINTFINISH;
555 /* Used from outside of print.c to print a block of SIZE
556 single-byte chars at DATA on a specified stream PRINTCHARFUN.
557 Do not use this on the contents of a Lisp string. */
559 void
560 write_string_1 (const char *data, int size, Lisp_Object printcharfun)
562 PRINTDECLARE;
564 PRINTPREPARE;
565 strout (data, size, size, printcharfun, 0);
566 PRINTFINISH;
570 void
571 temp_output_buffer_setup (const char *bufname)
573 int count = SPECPDL_INDEX ();
574 register struct buffer *old = current_buffer;
575 register Lisp_Object buf;
577 record_unwind_protect (set_buffer_if_live, Fcurrent_buffer ());
579 Fset_buffer (Fget_buffer_create (build_string (bufname)));
581 Fkill_all_local_variables ();
582 delete_all_overlays (current_buffer);
583 current_buffer->directory = old->directory;
584 current_buffer->read_only = Qnil;
585 current_buffer->filename = Qnil;
586 current_buffer->undo_list = Qt;
587 eassert (current_buffer->overlays_before == NULL);
588 eassert (current_buffer->overlays_after == NULL);
589 current_buffer->enable_multibyte_characters
590 = buffer_defaults.enable_multibyte_characters;
591 specbind (Qinhibit_read_only, Qt);
592 specbind (Qinhibit_modification_hooks, Qt);
593 Ferase_buffer ();
594 XSETBUFFER (buf, current_buffer);
596 Frun_hooks (1, &Qtemp_buffer_setup_hook);
598 unbind_to (count, Qnil);
600 specbind (Qstandard_output, buf);
603 Lisp_Object
604 internal_with_output_to_temp_buffer (const char *bufname, Lisp_Object (*function) (Lisp_Object), Lisp_Object args)
606 int count = SPECPDL_INDEX ();
607 Lisp_Object buf, val;
608 struct gcpro gcpro1;
610 GCPRO1 (args);
611 record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
612 temp_output_buffer_setup (bufname);
613 buf = Vstandard_output;
614 UNGCPRO;
616 val = (*function) (args);
618 GCPRO1 (val);
619 temp_output_buffer_show (buf);
620 UNGCPRO;
622 return unbind_to (count, val);
625 DEFUN ("with-output-to-temp-buffer",
626 Fwith_output_to_temp_buffer, Swith_output_to_temp_buffer,
627 1, UNEVALLED, 0,
628 doc: /* Bind `standard-output' to buffer BUFNAME, eval BODY, then show that buffer.
630 This construct makes buffer BUFNAME empty before running BODY.
631 It does not make the buffer current for BODY.
632 Instead it binds `standard-output' to that buffer, so that output
633 generated with `prin1' and similar functions in BODY goes into
634 the buffer.
636 At the end of BODY, this marks buffer BUFNAME unmodifed and displays
637 it in a window, but does not select it. The normal way to do this is
638 by calling `display-buffer', then running `temp-buffer-show-hook'.
639 However, if `temp-buffer-show-function' is non-nil, it calls that
640 function instead (and does not run `temp-buffer-show-hook'). The
641 function gets one argument, the buffer to display.
643 The return value of `with-output-to-temp-buffer' is the value of the
644 last form in BODY. If BODY does not finish normally, the buffer
645 BUFNAME is not displayed.
647 This runs the hook `temp-buffer-setup-hook' before BODY,
648 with the buffer BUFNAME temporarily current. It runs the hook
649 `temp-buffer-show-hook' after displaying buffer BUFNAME, with that
650 buffer temporarily current, and the window that was used to display it
651 temporarily selected. But it doesn't run `temp-buffer-show-hook'
652 if it uses `temp-buffer-show-function'.
654 usage: (with-output-to-temp-buffer BUFNAME BODY...) */)
655 (Lisp_Object args)
657 struct gcpro gcpro1;
658 Lisp_Object name;
659 int count = SPECPDL_INDEX ();
660 Lisp_Object buf, val;
662 GCPRO1(args);
663 name = Feval (Fcar (args));
664 CHECK_STRING (name);
665 temp_output_buffer_setup (SDATA (name));
666 buf = Vstandard_output;
667 UNGCPRO;
669 val = Fprogn (XCDR (args));
671 GCPRO1 (val);
672 temp_output_buffer_show (buf);
673 UNGCPRO;
675 return unbind_to (count, val);
679 static void print (Lisp_Object obj, register Lisp_Object printcharfun, int escapeflag);
680 static void print_preprocess (Lisp_Object obj);
681 static void print_preprocess_string (INTERVAL interval, Lisp_Object arg);
682 static void print_object (Lisp_Object obj, register Lisp_Object printcharfun, int escapeflag);
684 DEFUN ("terpri", Fterpri, Sterpri, 0, 1, 0,
685 doc: /* Output a newline to stream PRINTCHARFUN.
686 If PRINTCHARFUN is omitted or nil, the value of `standard-output' is used. */)
687 (Lisp_Object printcharfun)
689 PRINTDECLARE;
691 if (NILP (printcharfun))
692 printcharfun = Vstandard_output;
693 PRINTPREPARE;
694 PRINTCHAR ('\n');
695 PRINTFINISH;
696 return Qt;
699 DEFUN ("prin1", Fprin1, Sprin1, 1, 2, 0,
700 doc: /* Output the printed representation of OBJECT, any Lisp object.
701 Quoting characters are printed when needed to make output that `read'
702 can handle, whenever this is possible. For complex objects, the behavior
703 is controlled by `print-level' and `print-length', which see.
705 OBJECT is any of the Lisp data types: a number, a string, a symbol,
706 a list, a buffer, a window, a frame, etc.
708 A printed representation of an object is text which describes that object.
710 Optional argument PRINTCHARFUN is the output stream, which can be one
711 of these:
713 - a buffer, in which case output is inserted into that buffer at point;
714 - a marker, in which case output is inserted at marker's position;
715 - a function, in which case that function is called once for each
716 character of OBJECT's printed representation;
717 - a symbol, in which case that symbol's function definition is called; or
718 - t, in which case the output is displayed in the echo area.
720 If PRINTCHARFUN is omitted, the value of `standard-output' (which see)
721 is used instead. */)
722 (Lisp_Object object, Lisp_Object printcharfun)
724 PRINTDECLARE;
726 if (NILP (printcharfun))
727 printcharfun = Vstandard_output;
728 PRINTPREPARE;
729 print (object, printcharfun, 1);
730 PRINTFINISH;
731 return object;
734 /* a buffer which is used to hold output being built by prin1-to-string */
735 Lisp_Object Vprin1_to_string_buffer;
737 DEFUN ("prin1-to-string", Fprin1_to_string, Sprin1_to_string, 1, 2, 0,
738 doc: /* Return a string containing the printed representation of OBJECT.
739 OBJECT can be any Lisp object. This function outputs quoting characters
740 when necessary to make output that `read' can handle, whenever possible,
741 unless the optional second argument NOESCAPE is non-nil. For complex objects,
742 the behavior is controlled by `print-level' and `print-length', which see.
744 OBJECT is any of the Lisp data types: a number, a string, a symbol,
745 a list, a buffer, a window, a frame, etc.
747 A printed representation of an object is text which describes that object. */)
748 (Lisp_Object object, Lisp_Object noescape)
750 Lisp_Object printcharfun;
751 /* struct gcpro gcpro1, gcpro2; */
752 Lisp_Object save_deactivate_mark;
753 int count = SPECPDL_INDEX ();
754 struct buffer *previous;
756 specbind (Qinhibit_modification_hooks, Qt);
759 PRINTDECLARE;
761 /* Save and restore this--we are altering a buffer
762 but we don't want to deactivate the mark just for that.
763 No need for specbind, since errors deactivate the mark. */
764 save_deactivate_mark = Vdeactivate_mark;
765 /* GCPRO2 (object, save_deactivate_mark); */
766 abort_on_gc++;
768 printcharfun = Vprin1_to_string_buffer;
769 PRINTPREPARE;
770 print (object, printcharfun, NILP (noescape));
771 /* Make Vprin1_to_string_buffer be the default buffer after PRINTFINSH */
772 PRINTFINISH;
775 previous = current_buffer;
776 set_buffer_internal (XBUFFER (Vprin1_to_string_buffer));
777 object = Fbuffer_string ();
778 if (SBYTES (object) == SCHARS (object))
779 STRING_SET_UNIBYTE (object);
781 /* Note that this won't make prepare_to_modify_buffer call
782 ask-user-about-supersession-threat because this buffer
783 does not visit a file. */
784 Ferase_buffer ();
785 set_buffer_internal (previous);
787 Vdeactivate_mark = save_deactivate_mark;
788 /* UNGCPRO; */
790 abort_on_gc--;
791 return unbind_to (count, object);
794 DEFUN ("princ", Fprinc, Sprinc, 1, 2, 0,
795 doc: /* Output the printed representation of OBJECT, any Lisp object.
796 No quoting characters are used; no delimiters are printed around
797 the contents of strings.
799 OBJECT is any of the Lisp data types: a number, a string, a symbol,
800 a list, a buffer, a window, a frame, etc.
802 A printed representation of an object is text which describes that object.
804 Optional argument PRINTCHARFUN is the output stream, which can be one
805 of these:
807 - a buffer, in which case output is inserted into that buffer at point;
808 - a marker, in which case output is inserted at marker's position;
809 - a function, in which case that function is called once for each
810 character of OBJECT's printed representation;
811 - a symbol, in which case that symbol's function definition is called; or
812 - t, in which case the output is displayed in the echo area.
814 If PRINTCHARFUN is omitted, the value of `standard-output' (which see)
815 is used instead. */)
816 (Lisp_Object object, Lisp_Object printcharfun)
818 PRINTDECLARE;
820 if (NILP (printcharfun))
821 printcharfun = Vstandard_output;
822 PRINTPREPARE;
823 print (object, printcharfun, 0);
824 PRINTFINISH;
825 return object;
828 DEFUN ("print", Fprint, Sprint, 1, 2, 0,
829 doc: /* Output the printed representation of OBJECT, with newlines around it.
830 Quoting characters are printed when needed to make output that `read'
831 can handle, whenever this is possible. For complex objects, the behavior
832 is controlled by `print-level' and `print-length', which see.
834 OBJECT is any of the Lisp data types: a number, a string, a symbol,
835 a list, a buffer, a window, a frame, etc.
837 A printed representation of an object is text which describes that object.
839 Optional argument PRINTCHARFUN is the output stream, which can be one
840 of these:
842 - a buffer, in which case output is inserted into that buffer at point;
843 - a marker, in which case output is inserted at marker's position;
844 - a function, in which case that function is called once for each
845 character of OBJECT's printed representation;
846 - a symbol, in which case that symbol's function definition is called; or
847 - t, in which case the output is displayed in the echo area.
849 If PRINTCHARFUN is omitted, the value of `standard-output' (which see)
850 is used instead. */)
851 (Lisp_Object object, Lisp_Object printcharfun)
853 PRINTDECLARE;
854 struct gcpro gcpro1;
856 if (NILP (printcharfun))
857 printcharfun = Vstandard_output;
858 GCPRO1 (object);
859 PRINTPREPARE;
860 PRINTCHAR ('\n');
861 print (object, printcharfun, 1);
862 PRINTCHAR ('\n');
863 PRINTFINISH;
864 UNGCPRO;
865 return object;
868 /* The subroutine object for external-debugging-output is kept here
869 for the convenience of the debugger. */
870 Lisp_Object Qexternal_debugging_output;
872 DEFUN ("external-debugging-output", Fexternal_debugging_output, Sexternal_debugging_output, 1, 1, 0,
873 doc: /* Write CHARACTER to stderr.
874 You can call print while debugging emacs, and pass it this function
875 to make it write to the debugging output. */)
876 (Lisp_Object character)
878 CHECK_NUMBER (character);
879 putc (XINT (character), stderr);
881 #ifdef WINDOWSNT
882 /* Send the output to a debugger (nothing happens if there isn't one). */
883 if (print_output_debug_flag)
885 char buf[2] = {(char) XINT (character), '\0'};
886 OutputDebugString (buf);
888 #endif
890 return character;
893 /* This function is never called. Its purpose is to prevent
894 print_output_debug_flag from being optimized away. */
896 void
897 debug_output_compilation_hack (int x)
899 print_output_debug_flag = x;
902 #if defined (GNU_LINUX)
904 /* This functionality is not vitally important in general, so we rely on
905 non-portable ability to use stderr as lvalue. */
907 #define WITH_REDIRECT_DEBUGGING_OUTPUT 1
909 FILE *initial_stderr_stream = NULL;
911 DEFUN ("redirect-debugging-output", Fredirect_debugging_output, Sredirect_debugging_output,
912 1, 2,
913 "FDebug output file: \nP",
914 doc: /* Redirect debugging output (stderr stream) to file FILE.
915 If FILE is nil, reset target to the initial stderr stream.
916 Optional arg APPEND non-nil (interactively, with prefix arg) means
917 append to existing target file. */)
918 (Lisp_Object file, Lisp_Object append)
920 if (initial_stderr_stream != NULL)
922 BLOCK_INPUT;
923 fclose (stderr);
924 UNBLOCK_INPUT;
926 stderr = initial_stderr_stream;
927 initial_stderr_stream = NULL;
929 if (STRINGP (file))
931 file = Fexpand_file_name (file, Qnil);
932 initial_stderr_stream = stderr;
933 stderr = fopen (SDATA (file), NILP (append) ? "w" : "a");
934 if (stderr == NULL)
936 stderr = initial_stderr_stream;
937 initial_stderr_stream = NULL;
938 report_file_error ("Cannot open debugging output stream",
939 Fcons (file, Qnil));
942 return Qnil;
944 #endif /* GNU_LINUX */
947 /* This is the interface for debugging printing. */
949 void
950 debug_print (Lisp_Object arg)
952 Fprin1 (arg, Qexternal_debugging_output);
953 fprintf (stderr, "\r\n");
956 void
957 safe_debug_print (Lisp_Object arg)
959 int valid = valid_lisp_object_p (arg);
961 if (valid > 0)
962 debug_print (arg);
963 else
964 fprintf (stderr, "#<%s_LISP_OBJECT 0x%08lx>\r\n",
965 !valid ? "INVALID" : "SOME",
966 (unsigned long) XHASH (arg)
971 DEFUN ("error-message-string", Ferror_message_string, Serror_message_string,
972 1, 1, 0,
973 doc: /* Convert an error value (ERROR-SYMBOL . DATA) to an error message.
974 See Info anchor `(elisp)Definition of signal' for some details on how this
975 error message is constructed. */)
976 (Lisp_Object obj)
978 struct buffer *old = current_buffer;
979 Lisp_Object value;
980 struct gcpro gcpro1;
982 /* If OBJ is (error STRING), just return STRING.
983 That is not only faster, it also avoids the need to allocate
984 space here when the error is due to memory full. */
985 if (CONSP (obj) && EQ (XCAR (obj), Qerror)
986 && CONSP (XCDR (obj))
987 && STRINGP (XCAR (XCDR (obj)))
988 && NILP (XCDR (XCDR (obj))))
989 return XCAR (XCDR (obj));
991 print_error_message (obj, Vprin1_to_string_buffer, 0, Qnil);
993 set_buffer_internal (XBUFFER (Vprin1_to_string_buffer));
994 value = Fbuffer_string ();
996 GCPRO1 (value);
997 Ferase_buffer ();
998 set_buffer_internal (old);
999 UNGCPRO;
1001 return value;
1004 /* Print an error message for the error DATA onto Lisp output stream
1005 STREAM (suitable for the print functions).
1006 CONTEXT is a C string describing the context of the error.
1007 CALLER is the Lisp function inside which the error was signaled. */
1009 void
1010 print_error_message (Lisp_Object data, Lisp_Object stream, const char *context,
1011 Lisp_Object caller)
1013 Lisp_Object errname, errmsg, file_error, tail;
1014 struct gcpro gcpro1;
1015 int i;
1017 if (context != 0)
1018 write_string_1 (context, -1, stream);
1020 /* If we know from where the error was signaled, show it in
1021 *Messages*. */
1022 if (!NILP (caller) && SYMBOLP (caller))
1024 Lisp_Object cname = SYMBOL_NAME (caller);
1025 char *name = alloca (SBYTES (cname));
1026 memcpy (name, SDATA (cname), SBYTES (cname));
1027 message_dolog (name, SBYTES (cname), 0, 0);
1028 message_dolog (": ", 2, 0, 0);
1031 errname = Fcar (data);
1033 if (EQ (errname, Qerror))
1035 data = Fcdr (data);
1036 if (!CONSP (data))
1037 data = Qnil;
1038 errmsg = Fcar (data);
1039 file_error = Qnil;
1041 else
1043 Lisp_Object error_conditions;
1044 errmsg = Fget (errname, Qerror_message);
1045 error_conditions = Fget (errname, Qerror_conditions);
1046 file_error = Fmemq (Qfile_error, error_conditions);
1049 /* Print an error message including the data items. */
1051 tail = Fcdr_safe (data);
1052 GCPRO1 (tail);
1054 /* For file-error, make error message by concatenating
1055 all the data items. They are all strings. */
1056 if (!NILP (file_error) && CONSP (tail))
1057 errmsg = XCAR (tail), tail = XCDR (tail);
1059 if (STRINGP (errmsg))
1060 Fprinc (errmsg, stream);
1061 else
1062 write_string_1 ("peculiar error", -1, stream);
1064 for (i = 0; CONSP (tail); tail = XCDR (tail), i++)
1066 Lisp_Object obj;
1068 write_string_1 (i ? ", " : ": ", 2, stream);
1069 obj = XCAR (tail);
1070 if (!NILP (file_error) || EQ (errname, Qend_of_file))
1071 Fprinc (obj, stream);
1072 else
1073 Fprin1 (obj, stream);
1076 UNGCPRO;
1082 * The buffer should be at least as large as the max string size of the
1083 * largest float, printed in the biggest notation. This is undoubtedly
1084 * 20d float_output_format, with the negative of the C-constant "HUGE"
1085 * from <math.h>.
1087 * On the vax the worst case is -1e38 in 20d format which takes 61 bytes.
1089 * I assume that IEEE-754 format numbers can take 329 bytes for the worst
1090 * case of -1e307 in 20d float_output_format. What is one to do (short of
1091 * re-writing _doprnt to be more sane)?
1092 * -wsr
1095 void
1096 float_to_string (unsigned char *buf, double data)
1098 unsigned char *cp;
1099 int width;
1101 /* Check for plus infinity in a way that won't lose
1102 if there is no plus infinity. */
1103 if (data == data / 2 && data > 1.0)
1105 strcpy (buf, "1.0e+INF");
1106 return;
1108 /* Likewise for minus infinity. */
1109 if (data == data / 2 && data < -1.0)
1111 strcpy (buf, "-1.0e+INF");
1112 return;
1114 /* Check for NaN in a way that won't fail if there are no NaNs. */
1115 if (! (data * 0.0 >= 0.0))
1117 /* Prepend "-" if the NaN's sign bit is negative.
1118 The sign bit of a double is the bit that is 1 in -0.0. */
1119 int i;
1120 union { double d; char c[sizeof (double)]; } u_data, u_minus_zero;
1121 u_data.d = data;
1122 u_minus_zero.d = - 0.0;
1123 for (i = 0; i < sizeof (double); i++)
1124 if (u_data.c[i] & u_minus_zero.c[i])
1126 *buf++ = '-';
1127 break;
1130 strcpy (buf, "0.0e+NaN");
1131 return;
1134 if (NILP (Vfloat_output_format)
1135 || !STRINGP (Vfloat_output_format))
1136 lose:
1138 /* Generate the fewest number of digits that represent the
1139 floating point value without losing information.
1140 The following method is simple but a bit slow.
1141 For ideas about speeding things up, please see:
1143 Guy L Steele Jr & Jon L White, How to print floating-point numbers
1144 accurately. SIGPLAN notices 25, 6 (June 1990), 112-126.
1146 Robert G Burger & R Kent Dybvig, Printing floating point numbers
1147 quickly and accurately, SIGPLAN notices 31, 5 (May 1996), 108-116. */
1149 width = fabs (data) < DBL_MIN ? 1 : DBL_DIG;
1151 sprintf (buf, "%.*g", width, data);
1152 while (width++ < DOUBLE_DIGITS_BOUND && atof (buf) != data);
1154 else /* oink oink */
1156 /* Check that the spec we have is fully valid.
1157 This means not only valid for printf,
1158 but meant for floats, and reasonable. */
1159 cp = SDATA (Vfloat_output_format);
1161 if (cp[0] != '%')
1162 goto lose;
1163 if (cp[1] != '.')
1164 goto lose;
1166 cp += 2;
1168 /* Check the width specification. */
1169 width = -1;
1170 if ('0' <= *cp && *cp <= '9')
1172 width = 0;
1174 width = (width * 10) + (*cp++ - '0');
1175 while (*cp >= '0' && *cp <= '9');
1177 /* A precision of zero is valid only for %f. */
1178 if (width > DBL_DIG
1179 || (width == 0 && *cp != 'f'))
1180 goto lose;
1183 if (*cp != 'e' && *cp != 'f' && *cp != 'g')
1184 goto lose;
1186 if (cp[1] != 0)
1187 goto lose;
1189 sprintf (buf, SDATA (Vfloat_output_format), data);
1192 /* Make sure there is a decimal point with digit after, or an
1193 exponent, so that the value is readable as a float. But don't do
1194 this with "%.0f"; it's valid for that not to produce a decimal
1195 point. Note that width can be 0 only for %.0f. */
1196 if (width != 0)
1198 for (cp = buf; *cp; cp++)
1199 if ((*cp < '0' || *cp > '9') && *cp != '-')
1200 break;
1202 if (*cp == '.' && cp[1] == 0)
1204 cp[1] = '0';
1205 cp[2] = 0;
1208 if (*cp == 0)
1210 *cp++ = '.';
1211 *cp++ = '0';
1212 *cp++ = 0;
1218 static void
1219 print (Lisp_Object obj, register Lisp_Object printcharfun, int escapeflag)
1221 new_backquote_output = 0;
1223 /* Reset print_number_index and Vprint_number_table only when
1224 the variable Vprint_continuous_numbering is nil. Otherwise,
1225 the values of these variables will be kept between several
1226 print functions. */
1227 if (NILP (Vprint_continuous_numbering)
1228 || NILP (Vprint_number_table))
1230 print_number_index = 0;
1231 Vprint_number_table = Qnil;
1234 /* Construct Vprint_number_table for print-gensym and print-circle. */
1235 if (!NILP (Vprint_gensym) || !NILP (Vprint_circle))
1237 int i, start, index;
1238 start = index = print_number_index;
1239 /* Construct Vprint_number_table.
1240 This increments print_number_index for the objects added. */
1241 print_depth = 0;
1242 print_preprocess (obj);
1244 /* Remove unnecessary objects, which appear only once in OBJ;
1245 that is, whose status is Qnil. Compactify the necessary objects. */
1246 for (i = start; i < print_number_index; i++)
1247 if (!NILP (PRINT_NUMBER_STATUS (Vprint_number_table, i)))
1249 PRINT_NUMBER_OBJECT (Vprint_number_table, index)
1250 = PRINT_NUMBER_OBJECT (Vprint_number_table, i);
1251 index++;
1254 /* Clear out objects outside the active part of the table. */
1255 for (i = index; i < print_number_index; i++)
1256 PRINT_NUMBER_OBJECT (Vprint_number_table, i) = Qnil;
1258 /* Reset the status field for the next print step. Now this
1259 field means whether the object has already been printed. */
1260 for (i = start; i < print_number_index; i++)
1261 PRINT_NUMBER_STATUS (Vprint_number_table, i) = Qnil;
1263 print_number_index = index;
1266 print_depth = 0;
1267 print_object (obj, printcharfun, escapeflag);
1270 /* Construct Vprint_number_table according to the structure of OBJ.
1271 OBJ itself and all its elements will be added to Vprint_number_table
1272 recursively if it is a list, vector, compiled function, char-table,
1273 string (its text properties will be traced), or a symbol that has
1274 no obarray (this is for the print-gensym feature).
1275 The status fields of Vprint_number_table mean whether each object appears
1276 more than once in OBJ: Qnil at the first time, and Qt after that . */
1277 static void
1278 print_preprocess (Lisp_Object obj)
1280 int i;
1281 EMACS_INT size;
1282 int loop_count = 0;
1283 Lisp_Object halftail;
1285 /* Give up if we go so deep that print_object will get an error. */
1286 /* See similar code in print_object. */
1287 if (print_depth >= PRINT_CIRCLE)
1288 error ("Apparently circular structure being printed");
1290 /* Avoid infinite recursion for circular nested structure
1291 in the case where Vprint_circle is nil. */
1292 if (NILP (Vprint_circle))
1294 for (i = 0; i < print_depth; i++)
1295 if (EQ (obj, being_printed[i]))
1296 return;
1297 being_printed[print_depth] = obj;
1300 print_depth++;
1301 halftail = obj;
1303 loop:
1304 if (STRINGP (obj) || CONSP (obj) || VECTORP (obj)
1305 || COMPILEDP (obj) || CHAR_TABLE_P (obj) || SUB_CHAR_TABLE_P (obj)
1306 || HASH_TABLE_P (obj)
1307 || (! NILP (Vprint_gensym)
1308 && SYMBOLP (obj)
1309 && !SYMBOL_INTERNED_P (obj)))
1311 /* In case print-circle is nil and print-gensym is t,
1312 add OBJ to Vprint_number_table only when OBJ is a symbol. */
1313 if (! NILP (Vprint_circle) || SYMBOLP (obj))
1315 for (i = 0; i < print_number_index; i++)
1316 if (EQ (PRINT_NUMBER_OBJECT (Vprint_number_table, i), obj))
1318 /* OBJ appears more than once. Let's remember that. */
1319 PRINT_NUMBER_STATUS (Vprint_number_table, i) = Qt;
1320 print_depth--;
1321 return;
1324 /* OBJ is not yet recorded. Let's add to the table. */
1325 if (print_number_index == 0)
1327 /* Initialize the table. */
1328 Vprint_number_table = Fmake_vector (make_number (40), Qnil);
1330 else if (XVECTOR (Vprint_number_table)->size == print_number_index * 2)
1332 /* Reallocate the table. */
1333 int i = print_number_index * 4;
1334 Lisp_Object old_table = Vprint_number_table;
1335 Vprint_number_table = Fmake_vector (make_number (i), Qnil);
1336 for (i = 0; i < print_number_index; i++)
1338 PRINT_NUMBER_OBJECT (Vprint_number_table, i)
1339 = PRINT_NUMBER_OBJECT (old_table, i);
1340 PRINT_NUMBER_STATUS (Vprint_number_table, i)
1341 = PRINT_NUMBER_STATUS (old_table, i);
1344 PRINT_NUMBER_OBJECT (Vprint_number_table, print_number_index) = obj;
1345 /* If Vprint_continuous_numbering is non-nil and OBJ is a gensym,
1346 always print the gensym with a number. This is a special for
1347 the lisp function byte-compile-output-docform. */
1348 if (!NILP (Vprint_continuous_numbering)
1349 && SYMBOLP (obj)
1350 && !SYMBOL_INTERNED_P (obj))
1351 PRINT_NUMBER_STATUS (Vprint_number_table, print_number_index) = Qt;
1352 print_number_index++;
1355 switch (XTYPE (obj))
1357 case Lisp_String:
1358 /* A string may have text properties, which can be circular. */
1359 traverse_intervals_noorder (STRING_INTERVALS (obj),
1360 print_preprocess_string, Qnil);
1361 break;
1363 case Lisp_Cons:
1364 /* Use HALFTAIL and LOOP_COUNT to detect circular lists,
1365 just as in print_object. */
1366 if (loop_count && EQ (obj, halftail))
1367 break;
1368 print_preprocess (XCAR (obj));
1369 obj = XCDR (obj);
1370 loop_count++;
1371 if (!(loop_count & 1))
1372 halftail = XCDR (halftail);
1373 goto loop;
1375 case Lisp_Vectorlike:
1376 size = XVECTOR (obj)->size;
1377 if (size & PSEUDOVECTOR_FLAG)
1378 size &= PSEUDOVECTOR_SIZE_MASK;
1379 for (i = 0; i < size; i++)
1380 print_preprocess (XVECTOR (obj)->contents[i]);
1381 if (HASH_TABLE_P (obj))
1382 { /* For hash tables, the key_and_value slot is past
1383 `size' because it needs to be marked specially in case
1384 the table is weak. */
1385 struct Lisp_Hash_Table *h = XHASH_TABLE (obj);
1386 print_preprocess (h->key_and_value);
1388 break;
1390 default:
1391 break;
1394 print_depth--;
1397 static void
1398 print_preprocess_string (INTERVAL interval, Lisp_Object arg)
1400 print_preprocess (interval->plist);
1403 /* A flag to control printing of `charset' text property.
1404 The default value is Qdefault. */
1405 Lisp_Object Vprint_charset_text_property;
1406 extern Lisp_Object Qdefault;
1408 static void print_check_string_charset_prop (INTERVAL interval, Lisp_Object string);
1410 #define PRINT_STRING_NON_CHARSET_FOUND 1
1411 #define PRINT_STRING_UNSAFE_CHARSET_FOUND 2
1413 /* Bitwize or of the abobe macros. */
1414 static int print_check_string_result;
1416 static void
1417 print_check_string_charset_prop (INTERVAL interval, Lisp_Object string)
1419 Lisp_Object val;
1421 if (NILP (interval->plist)
1422 || (print_check_string_result == (PRINT_STRING_NON_CHARSET_FOUND
1423 | PRINT_STRING_UNSAFE_CHARSET_FOUND)))
1424 return;
1425 for (val = interval->plist; CONSP (val) && ! EQ (XCAR (val), Qcharset);
1426 val = XCDR (XCDR (val)));
1427 if (! CONSP (val))
1429 print_check_string_result |= PRINT_STRING_NON_CHARSET_FOUND;
1430 return;
1432 if (! (print_check_string_result & PRINT_STRING_NON_CHARSET_FOUND))
1434 if (! EQ (val, interval->plist)
1435 || CONSP (XCDR (XCDR (val))))
1436 print_check_string_result |= PRINT_STRING_NON_CHARSET_FOUND;
1438 if (NILP (Vprint_charset_text_property)
1439 || ! (print_check_string_result & PRINT_STRING_UNSAFE_CHARSET_FOUND))
1441 int i, c;
1442 int charpos = interval->position;
1443 int bytepos = string_char_to_byte (string, charpos);
1444 Lisp_Object charset;
1446 charset = XCAR (XCDR (val));
1447 for (i = 0; i < LENGTH (interval); i++)
1449 FETCH_STRING_CHAR_ADVANCE (c, string, charpos, bytepos);
1450 if (! ASCII_CHAR_P (c)
1451 && ! EQ (CHARSET_NAME (CHAR_CHARSET (c)), charset))
1453 print_check_string_result |= PRINT_STRING_UNSAFE_CHARSET_FOUND;
1454 break;
1460 /* The value is (charset . nil). */
1461 static Lisp_Object print_prune_charset_plist;
1463 static Lisp_Object
1464 print_prune_string_charset (Lisp_Object string)
1466 print_check_string_result = 0;
1467 traverse_intervals (STRING_INTERVALS (string), 0,
1468 print_check_string_charset_prop, string);
1469 if (! (print_check_string_result & PRINT_STRING_UNSAFE_CHARSET_FOUND))
1471 string = Fcopy_sequence (string);
1472 if (print_check_string_result & PRINT_STRING_NON_CHARSET_FOUND)
1474 if (NILP (print_prune_charset_plist))
1475 print_prune_charset_plist = Fcons (Qcharset, Qnil);
1476 Fremove_text_properties (make_number (0),
1477 make_number (SCHARS (string)),
1478 print_prune_charset_plist, string);
1480 else
1481 Fset_text_properties (make_number (0), make_number (SCHARS (string)),
1482 Qnil, string);
1484 return string;
1487 static void
1488 print_object (Lisp_Object obj, register Lisp_Object printcharfun, int escapeflag)
1490 char buf[40];
1492 QUIT;
1494 /* See similar code in print_preprocess. */
1495 if (print_depth >= PRINT_CIRCLE)
1496 error ("Apparently circular structure being printed");
1498 /* Detect circularities and truncate them. */
1499 if (STRINGP (obj) || CONSP (obj) || VECTORP (obj)
1500 || COMPILEDP (obj) || CHAR_TABLE_P (obj) || SUB_CHAR_TABLE_P (obj)
1501 || HASH_TABLE_P (obj)
1502 || (! NILP (Vprint_gensym)
1503 && SYMBOLP (obj)
1504 && !SYMBOL_INTERNED_P (obj)))
1506 if (NILP (Vprint_circle) && NILP (Vprint_gensym))
1508 /* Simple but incomplete way. */
1509 int i;
1510 for (i = 0; i < print_depth; i++)
1511 if (EQ (obj, being_printed[i]))
1513 sprintf (buf, "#%d", i);
1514 strout (buf, -1, -1, printcharfun, 0);
1515 return;
1517 being_printed[print_depth] = obj;
1519 else
1521 /* With the print-circle feature. */
1522 int i;
1523 for (i = 0; i < print_number_index; i++)
1524 if (EQ (PRINT_NUMBER_OBJECT (Vprint_number_table, i), obj))
1526 if (NILP (PRINT_NUMBER_STATUS (Vprint_number_table, i)))
1528 /* Add a prefix #n= if OBJ has not yet been printed;
1529 that is, its status field is nil. */
1530 sprintf (buf, "#%d=", i + 1);
1531 strout (buf, -1, -1, printcharfun, 0);
1532 /* OBJ is going to be printed. Set the status to t. */
1533 PRINT_NUMBER_STATUS (Vprint_number_table, i) = Qt;
1534 break;
1536 else
1538 /* Just print #n# if OBJ has already been printed. */
1539 sprintf (buf, "#%d#", i + 1);
1540 strout (buf, -1, -1, printcharfun, 0);
1541 return;
1547 print_depth++;
1549 switch (XTYPE (obj))
1551 case_Lisp_Int:
1552 if (sizeof (int) == sizeof (EMACS_INT))
1553 sprintf (buf, "%d", (int) XINT (obj));
1554 else if (sizeof (long) == sizeof (EMACS_INT))
1555 sprintf (buf, "%ld", (long) XINT (obj));
1556 else
1557 abort ();
1558 strout (buf, -1, -1, printcharfun, 0);
1559 break;
1561 case Lisp_Float:
1563 char pigbuf[350]; /* see comments in float_to_string */
1565 float_to_string (pigbuf, XFLOAT_DATA (obj));
1566 strout (pigbuf, -1, -1, printcharfun, 0);
1568 break;
1570 case Lisp_String:
1571 if (!escapeflag)
1572 print_string (obj, printcharfun);
1573 else
1575 register int i, i_byte;
1576 struct gcpro gcpro1;
1577 unsigned char *str;
1578 int size_byte;
1579 /* 1 means we must ensure that the next character we output
1580 cannot be taken as part of a hex character escape. */
1581 int need_nonhex = 0;
1582 int multibyte = STRING_MULTIBYTE (obj);
1584 GCPRO1 (obj);
1586 if (! EQ (Vprint_charset_text_property, Qt))
1587 obj = print_prune_string_charset (obj);
1589 if (!NULL_INTERVAL_P (STRING_INTERVALS (obj)))
1591 PRINTCHAR ('#');
1592 PRINTCHAR ('(');
1595 PRINTCHAR ('\"');
1596 str = SDATA (obj);
1597 size_byte = SBYTES (obj);
1599 for (i = 0, i_byte = 0; i_byte < size_byte;)
1601 /* Here, we must convert each multi-byte form to the
1602 corresponding character code before handing it to PRINTCHAR. */
1603 int len;
1604 int c;
1606 if (multibyte)
1608 c = STRING_CHAR_AND_LENGTH (str + i_byte, len);
1609 i_byte += len;
1611 else
1612 c = str[i_byte++];
1614 QUIT;
1616 if (c == '\n' && print_escape_newlines)
1618 PRINTCHAR ('\\');
1619 PRINTCHAR ('n');
1621 else if (c == '\f' && print_escape_newlines)
1623 PRINTCHAR ('\\');
1624 PRINTCHAR ('f');
1626 else if (multibyte
1627 && (CHAR_BYTE8_P (c)
1628 || (! ASCII_CHAR_P (c) && print_escape_multibyte)))
1630 /* When multibyte is disabled,
1631 print multibyte string chars using hex escapes.
1632 For a char code that could be in a unibyte string,
1633 when found in a multibyte string, always use a hex escape
1634 so it reads back as multibyte. */
1635 unsigned char outbuf[50];
1637 if (CHAR_BYTE8_P (c))
1638 sprintf (outbuf, "\\%03o", CHAR_TO_BYTE8 (c));
1639 else
1641 sprintf (outbuf, "\\x%04x", c);
1642 need_nonhex = 1;
1644 strout (outbuf, -1, -1, printcharfun, 0);
1646 else if (! multibyte
1647 && SINGLE_BYTE_CHAR_P (c) && ! ASCII_BYTE_P (c)
1648 && print_escape_nonascii)
1650 /* When printing in a multibyte buffer
1651 or when explicitly requested,
1652 print single-byte non-ASCII string chars
1653 using octal escapes. */
1654 unsigned char outbuf[5];
1655 sprintf (outbuf, "\\%03o", c);
1656 strout (outbuf, -1, -1, printcharfun, 0);
1658 else
1660 /* If we just had a hex escape, and this character
1661 could be taken as part of it,
1662 output `\ ' to prevent that. */
1663 if (need_nonhex)
1665 need_nonhex = 0;
1666 if ((c >= 'a' && c <= 'f')
1667 || (c >= 'A' && c <= 'F')
1668 || (c >= '0' && c <= '9'))
1669 strout ("\\ ", -1, -1, printcharfun, 0);
1672 if (c == '\"' || c == '\\')
1673 PRINTCHAR ('\\');
1674 PRINTCHAR (c);
1677 PRINTCHAR ('\"');
1679 if (!NULL_INTERVAL_P (STRING_INTERVALS (obj)))
1681 traverse_intervals (STRING_INTERVALS (obj),
1682 0, print_interval, printcharfun);
1683 PRINTCHAR (')');
1686 UNGCPRO;
1688 break;
1690 case Lisp_Symbol:
1692 register int confusing;
1693 register unsigned char *p = SDATA (SYMBOL_NAME (obj));
1694 register unsigned char *end = p + SBYTES (SYMBOL_NAME (obj));
1695 register int c;
1696 int i, i_byte, size_byte;
1697 Lisp_Object name;
1699 name = SYMBOL_NAME (obj);
1701 if (p != end && (*p == '-' || *p == '+')) p++;
1702 if (p == end)
1703 confusing = 0;
1704 /* If symbol name begins with a digit, and ends with a digit,
1705 and contains nothing but digits and `e', it could be treated
1706 as a number. So set CONFUSING.
1708 Symbols that contain periods could also be taken as numbers,
1709 but periods are always escaped, so we don't have to worry
1710 about them here. */
1711 else if (*p >= '0' && *p <= '9'
1712 && end[-1] >= '0' && end[-1] <= '9')
1714 while (p != end && ((*p >= '0' && *p <= '9')
1715 /* Needed for \2e10. */
1716 || *p == 'e' || *p == 'E'))
1717 p++;
1718 confusing = (end == p);
1720 else
1721 confusing = 0;
1723 if (! NILP (Vprint_gensym) && !SYMBOL_INTERNED_P (obj))
1725 PRINTCHAR ('#');
1726 PRINTCHAR (':');
1729 size_byte = SBYTES (name);
1731 for (i = 0, i_byte = 0; i_byte < size_byte;)
1733 /* Here, we must convert each multi-byte form to the
1734 corresponding character code before handing it to PRINTCHAR. */
1735 FETCH_STRING_CHAR_ADVANCE (c, name, i, i_byte);
1736 QUIT;
1738 if (escapeflag)
1740 if (c == '\"' || c == '\\' || c == '\''
1741 || c == ';' || c == '#' || c == '(' || c == ')'
1742 || c == ',' || c =='.' || c == '`'
1743 || c == '[' || c == ']' || c == '?' || c <= 040
1744 || confusing)
1745 PRINTCHAR ('\\'), confusing = 0;
1747 PRINTCHAR (c);
1750 break;
1752 case Lisp_Cons:
1753 /* If deeper than spec'd depth, print placeholder. */
1754 if (INTEGERP (Vprint_level)
1755 && print_depth > XINT (Vprint_level))
1756 strout ("...", -1, -1, printcharfun, 0);
1757 else if (print_quoted && CONSP (XCDR (obj)) && NILP (XCDR (XCDR (obj)))
1758 && (EQ (XCAR (obj), Qquote)))
1760 PRINTCHAR ('\'');
1761 print_object (XCAR (XCDR (obj)), printcharfun, escapeflag);
1763 else if (print_quoted && CONSP (XCDR (obj)) && NILP (XCDR (XCDR (obj)))
1764 && (EQ (XCAR (obj), Qfunction)))
1766 PRINTCHAR ('#');
1767 PRINTCHAR ('\'');
1768 print_object (XCAR (XCDR (obj)), printcharfun, escapeflag);
1770 else if (print_quoted && CONSP (XCDR (obj)) && NILP (XCDR (XCDR (obj)))
1771 && ((EQ (XCAR (obj), Qbackquote))))
1773 print_object (XCAR (obj), printcharfun, 0);
1774 new_backquote_output++;
1775 print_object (XCAR (XCDR (obj)), printcharfun, escapeflag);
1776 new_backquote_output--;
1778 else if (print_quoted && CONSP (XCDR (obj)) && NILP (XCDR (XCDR (obj)))
1779 && new_backquote_output
1780 && ((EQ (XCAR (obj), Qbackquote)
1781 || EQ (XCAR (obj), Qcomma)
1782 || EQ (XCAR (obj), Qcomma_at)
1783 || EQ (XCAR (obj), Qcomma_dot))))
1785 print_object (XCAR (obj), printcharfun, 0);
1786 new_backquote_output--;
1787 print_object (XCAR (XCDR (obj)), printcharfun, escapeflag);
1788 new_backquote_output++;
1790 else
1792 PRINTCHAR ('(');
1794 /* If the first element is a backquote form,
1795 print it old-style so it won't be misunderstood. */
1796 if (print_quoted && CONSP (XCAR (obj))
1797 && CONSP (XCDR (XCAR (obj)))
1798 && NILP (XCDR (XCDR (XCAR (obj))))
1799 && EQ (XCAR (XCAR (obj)), Qbackquote))
1801 Lisp_Object tem;
1802 tem = XCAR (obj);
1803 PRINTCHAR ('(');
1805 print_object (Qbackquote, printcharfun, 0);
1806 PRINTCHAR (' ');
1808 print_object (XCAR (XCDR (tem)), printcharfun, 0);
1809 PRINTCHAR (')');
1811 obj = XCDR (obj);
1815 int print_length, i;
1816 Lisp_Object halftail = obj;
1818 /* Negative values of print-length are invalid in CL.
1819 Treat them like nil, as CMUCL does. */
1820 if (NATNUMP (Vprint_length))
1821 print_length = XFASTINT (Vprint_length);
1822 else
1823 print_length = 0;
1825 i = 0;
1826 while (CONSP (obj))
1828 /* Detect circular list. */
1829 if (NILP (Vprint_circle))
1831 /* Simple but imcomplete way. */
1832 if (i != 0 && EQ (obj, halftail))
1834 sprintf (buf, " . #%d", i / 2);
1835 strout (buf, -1, -1, printcharfun, 0);
1836 goto end_of_list;
1839 else
1841 /* With the print-circle feature. */
1842 if (i != 0)
1844 int i;
1845 for (i = 0; i < print_number_index; i++)
1846 if (EQ (PRINT_NUMBER_OBJECT (Vprint_number_table, i),
1847 obj))
1849 if (NILP (PRINT_NUMBER_STATUS (Vprint_number_table, i)))
1851 strout (" . ", 3, 3, printcharfun, 0);
1852 print_object (obj, printcharfun, escapeflag);
1854 else
1856 sprintf (buf, " . #%d#", i + 1);
1857 strout (buf, -1, -1, printcharfun, 0);
1859 goto end_of_list;
1864 if (i++)
1865 PRINTCHAR (' ');
1867 if (print_length && i > print_length)
1869 strout ("...", 3, 3, printcharfun, 0);
1870 goto end_of_list;
1873 print_object (XCAR (obj), printcharfun, escapeflag);
1875 obj = XCDR (obj);
1876 if (!(i & 1))
1877 halftail = XCDR (halftail);
1881 /* OBJ non-nil here means it's the end of a dotted list. */
1882 if (!NILP (obj))
1884 strout (" . ", 3, 3, printcharfun, 0);
1885 print_object (obj, printcharfun, escapeflag);
1888 end_of_list:
1889 PRINTCHAR (')');
1891 break;
1893 case Lisp_Vectorlike:
1894 if (PROCESSP (obj))
1896 if (escapeflag)
1898 strout ("#<process ", -1, -1, printcharfun, 0);
1899 print_string (XPROCESS (obj)->name, printcharfun);
1900 PRINTCHAR ('>');
1902 else
1903 print_string (XPROCESS (obj)->name, printcharfun);
1905 else if (BOOL_VECTOR_P (obj))
1907 register int i;
1908 register unsigned char c;
1909 struct gcpro gcpro1;
1910 int size_in_chars
1911 = ((XBOOL_VECTOR (obj)->size + BOOL_VECTOR_BITS_PER_CHAR - 1)
1912 / BOOL_VECTOR_BITS_PER_CHAR);
1914 GCPRO1 (obj);
1916 PRINTCHAR ('#');
1917 PRINTCHAR ('&');
1918 sprintf (buf, "%ld", (long) XBOOL_VECTOR (obj)->size);
1919 strout (buf, -1, -1, printcharfun, 0);
1920 PRINTCHAR ('\"');
1922 /* Don't print more characters than the specified maximum.
1923 Negative values of print-length are invalid. Treat them
1924 like a print-length of nil. */
1925 if (NATNUMP (Vprint_length)
1926 && XFASTINT (Vprint_length) < size_in_chars)
1927 size_in_chars = XFASTINT (Vprint_length);
1929 for (i = 0; i < size_in_chars; i++)
1931 QUIT;
1932 c = XBOOL_VECTOR (obj)->data[i];
1933 if (c == '\n' && print_escape_newlines)
1935 PRINTCHAR ('\\');
1936 PRINTCHAR ('n');
1938 else if (c == '\f' && print_escape_newlines)
1940 PRINTCHAR ('\\');
1941 PRINTCHAR ('f');
1943 else if (c > '\177')
1945 /* Use octal escapes to avoid encoding issues. */
1946 PRINTCHAR ('\\');
1947 PRINTCHAR ('0' + ((c >> 6) & 3));
1948 PRINTCHAR ('0' + ((c >> 3) & 7));
1949 PRINTCHAR ('0' + (c & 7));
1951 else
1953 if (c == '\"' || c == '\\')
1954 PRINTCHAR ('\\');
1955 PRINTCHAR (c);
1958 PRINTCHAR ('\"');
1960 UNGCPRO;
1962 else if (SUBRP (obj))
1964 strout ("#<subr ", -1, -1, printcharfun, 0);
1965 strout (XSUBR (obj)->symbol_name, -1, -1, printcharfun, 0);
1966 PRINTCHAR ('>');
1968 else if (WINDOWP (obj))
1970 strout ("#<window ", -1, -1, printcharfun, 0);
1971 sprintf (buf, "%ld", (long) XFASTINT (XWINDOW (obj)->sequence_number));
1972 strout (buf, -1, -1, printcharfun, 0);
1973 if (!NILP (XWINDOW (obj)->buffer))
1975 strout (" on ", -1, -1, printcharfun, 0);
1976 print_string (XBUFFER (XWINDOW (obj)->buffer)->name, printcharfun);
1978 PRINTCHAR ('>');
1980 else if (TERMINALP (obj))
1982 struct terminal *t = XTERMINAL (obj);
1983 strout ("#<terminal ", -1, -1, printcharfun, 0);
1984 sprintf (buf, "%d", t->id);
1985 strout (buf, -1, -1, printcharfun, 0);
1986 if (t->name)
1988 strout (" on ", -1, -1, printcharfun, 0);
1989 strout (t->name, -1, -1, printcharfun, 0);
1991 PRINTCHAR ('>');
1993 else if (HASH_TABLE_P (obj))
1995 struct Lisp_Hash_Table *h = XHASH_TABLE (obj);
1996 int i, real_size, size;
1997 #if 0
1998 strout ("#<hash-table", -1, -1, printcharfun, 0);
1999 if (SYMBOLP (h->test))
2001 PRINTCHAR (' ');
2002 PRINTCHAR ('\'');
2003 strout (SDATA (SYMBOL_NAME (h->test)), -1, -1, printcharfun, 0);
2004 PRINTCHAR (' ');
2005 strout (SDATA (SYMBOL_NAME (h->weak)), -1, -1, printcharfun, 0);
2006 PRINTCHAR (' ');
2007 sprintf (buf, "%ld/%ld", (long) h->count,
2008 (long) XVECTOR (h->next)->size);
2009 strout (buf, -1, -1, printcharfun, 0);
2011 sprintf (buf, " 0x%lx", (unsigned long) h);
2012 strout (buf, -1, -1, printcharfun, 0);
2013 PRINTCHAR ('>');
2014 #endif
2015 /* Implement a readable output, e.g.:
2016 #s(hash-table size 2 test equal data (k1 v1 k2 v2)) */
2017 /* Always print the size. */
2018 sprintf (buf, "#s(hash-table size %ld",
2019 (long) XVECTOR (h->next)->size);
2020 strout (buf, -1, -1, printcharfun, 0);
2022 if (!NILP (h->test))
2024 strout (" test ", -1, -1, printcharfun, 0);
2025 print_object (h->test, printcharfun, 0);
2028 if (!NILP (h->weak))
2030 strout (" weakness ", -1, -1, printcharfun, 0);
2031 print_object (h->weak, printcharfun, 0);
2034 if (!NILP (h->rehash_size))
2036 strout (" rehash-size ", -1, -1, printcharfun, 0);
2037 print_object (h->rehash_size, printcharfun, 0);
2040 if (!NILP (h->rehash_threshold))
2042 strout (" rehash-threshold ", -1, -1, printcharfun, 0);
2043 print_object (h->rehash_threshold, printcharfun, 0);
2046 strout (" data ", -1, -1, printcharfun, 0);
2048 /* Print the data here as a plist. */
2049 real_size = HASH_TABLE_SIZE (h);
2050 size = real_size;
2052 /* Don't print more elements than the specified maximum. */
2053 if (NATNUMP (Vprint_length)
2054 && XFASTINT (Vprint_length) < size)
2055 size = XFASTINT (Vprint_length);
2057 PRINTCHAR ('(');
2058 for (i = 0; i < size; i++)
2059 if (!NILP (HASH_HASH (h, i)))
2061 if (i) PRINTCHAR (' ');
2062 print_object (HASH_KEY (h, i), printcharfun, 1);
2063 PRINTCHAR (' ');
2064 print_object (HASH_VALUE (h, i), printcharfun, 1);
2067 if (size < real_size)
2068 strout (" ...", 4, 4, printcharfun, 0);
2070 PRINTCHAR (')');
2071 PRINTCHAR (')');
2074 else if (BUFFERP (obj))
2076 if (NILP (XBUFFER (obj)->name))
2077 strout ("#<killed buffer>", -1, -1, printcharfun, 0);
2078 else if (escapeflag)
2080 strout ("#<buffer ", -1, -1, printcharfun, 0);
2081 print_string (XBUFFER (obj)->name, printcharfun);
2082 PRINTCHAR ('>');
2084 else
2085 print_string (XBUFFER (obj)->name, printcharfun);
2087 else if (WINDOW_CONFIGURATIONP (obj))
2089 strout ("#<window-configuration>", -1, -1, printcharfun, 0);
2091 else if (FRAMEP (obj))
2093 strout ((FRAME_LIVE_P (XFRAME (obj))
2094 ? "#<frame " : "#<dead frame "),
2095 -1, -1, printcharfun, 0);
2096 print_string (XFRAME (obj)->name, printcharfun);
2097 sprintf (buf, " 0x%lx", (unsigned long) (XFRAME (obj)));
2098 strout (buf, -1, -1, printcharfun, 0);
2099 PRINTCHAR ('>');
2101 else if (FONTP (obj))
2103 EMACS_INT i;
2105 if (! FONT_OBJECT_P (obj))
2107 if (FONT_SPEC_P (obj))
2108 strout ("#<font-spec", -1, -1, printcharfun, 0);
2109 else
2110 strout ("#<font-entity", -1, -1, printcharfun, 0);
2111 for (i = 0; i < FONT_SPEC_MAX; i++)
2113 PRINTCHAR (' ');
2114 if (i < FONT_WEIGHT_INDEX || i > FONT_WIDTH_INDEX)
2115 print_object (AREF (obj, i), printcharfun, escapeflag);
2116 else
2117 print_object (font_style_symbolic (obj, i, 0),
2118 printcharfun, escapeflag);
2121 else
2123 strout ("#<font-object ", -1, -1, printcharfun, 0);
2124 print_object (AREF (obj, FONT_NAME_INDEX), printcharfun,
2125 escapeflag);
2127 PRINTCHAR ('>');
2129 else
2131 EMACS_INT size = XVECTOR (obj)->size;
2132 if (COMPILEDP (obj))
2134 PRINTCHAR ('#');
2135 size &= PSEUDOVECTOR_SIZE_MASK;
2137 if (CHAR_TABLE_P (obj) || SUB_CHAR_TABLE_P (obj))
2139 /* We print a char-table as if it were a vector,
2140 lumping the parent and default slots in with the
2141 character slots. But we add #^ as a prefix. */
2143 /* Make each lowest sub_char_table start a new line.
2144 Otherwise we'll make a line extremely long, which
2145 results in slow redisplay. */
2146 if (SUB_CHAR_TABLE_P (obj)
2147 && XINT (XSUB_CHAR_TABLE (obj)->depth) == 3)
2148 PRINTCHAR ('\n');
2149 PRINTCHAR ('#');
2150 PRINTCHAR ('^');
2151 if (SUB_CHAR_TABLE_P (obj))
2152 PRINTCHAR ('^');
2153 size &= PSEUDOVECTOR_SIZE_MASK;
2155 if (size & PSEUDOVECTOR_FLAG)
2156 goto badtype;
2158 PRINTCHAR ('[');
2160 register int i;
2161 register Lisp_Object tem;
2162 int real_size = size;
2164 /* Don't print more elements than the specified maximum. */
2165 if (NATNUMP (Vprint_length)
2166 && XFASTINT (Vprint_length) < size)
2167 size = XFASTINT (Vprint_length);
2169 for (i = 0; i < size; i++)
2171 if (i) PRINTCHAR (' ');
2172 tem = XVECTOR (obj)->contents[i];
2173 print_object (tem, printcharfun, escapeflag);
2175 if (size < real_size)
2176 strout (" ...", 4, 4, printcharfun, 0);
2178 PRINTCHAR (']');
2180 break;
2182 case Lisp_Misc:
2183 switch (XMISCTYPE (obj))
2185 case Lisp_Misc_Marker:
2186 strout ("#<marker ", -1, -1, printcharfun, 0);
2187 /* Do you think this is necessary? */
2188 if (XMARKER (obj)->insertion_type != 0)
2189 strout ("(moves after insertion) ", -1, -1, printcharfun, 0);
2190 if (! XMARKER (obj)->buffer)
2191 strout ("in no buffer", -1, -1, printcharfun, 0);
2192 else
2194 sprintf (buf, "at %d", marker_position (obj));
2195 strout (buf, -1, -1, printcharfun, 0);
2196 strout (" in ", -1, -1, printcharfun, 0);
2197 print_string (XMARKER (obj)->buffer->name, printcharfun);
2199 PRINTCHAR ('>');
2200 break;
2202 case Lisp_Misc_Overlay:
2203 strout ("#<overlay ", -1, -1, printcharfun, 0);
2204 if (! XMARKER (OVERLAY_START (obj))->buffer)
2205 strout ("in no buffer", -1, -1, printcharfun, 0);
2206 else
2208 sprintf (buf, "from %d to %d in ",
2209 marker_position (OVERLAY_START (obj)),
2210 marker_position (OVERLAY_END (obj)));
2211 strout (buf, -1, -1, printcharfun, 0);
2212 print_string (XMARKER (OVERLAY_START (obj))->buffer->name,
2213 printcharfun);
2215 PRINTCHAR ('>');
2216 break;
2218 /* Remaining cases shouldn't happen in normal usage, but let's print
2219 them anyway for the benefit of the debugger. */
2220 case Lisp_Misc_Free:
2221 strout ("#<misc free cell>", -1, -1, printcharfun, 0);
2222 break;
2224 case Lisp_Misc_Save_Value:
2225 strout ("#<save_value ", -1, -1, printcharfun, 0);
2226 sprintf(buf, "ptr=0x%08lx int=%d",
2227 (unsigned long) XSAVE_VALUE (obj)->pointer,
2228 XSAVE_VALUE (obj)->integer);
2229 strout (buf, -1, -1, printcharfun, 0);
2230 PRINTCHAR ('>');
2231 break;
2233 default:
2234 goto badtype;
2236 break;
2238 default:
2239 badtype:
2241 /* We're in trouble if this happens!
2242 Probably should just abort () */
2243 strout ("#<EMACS BUG: INVALID DATATYPE ", -1, -1, printcharfun, 0);
2244 if (MISCP (obj))
2245 sprintf (buf, "(MISC 0x%04x)", (int) XMISCTYPE (obj));
2246 else if (VECTORLIKEP (obj))
2247 sprintf (buf, "(PVEC 0x%08x)", (int) XVECTOR (obj)->size);
2248 else
2249 sprintf (buf, "(0x%02x)", (int) XTYPE (obj));
2250 strout (buf, -1, -1, printcharfun, 0);
2251 strout (" Save your buffers immediately and please report this bug>",
2252 -1, -1, printcharfun, 0);
2256 print_depth--;
2260 /* Print a description of INTERVAL using PRINTCHARFUN.
2261 This is part of printing a string that has text properties. */
2263 void
2264 print_interval (INTERVAL interval, Lisp_Object printcharfun)
2266 if (NILP (interval->plist))
2267 return;
2268 PRINTCHAR (' ');
2269 print_object (make_number (interval->position), printcharfun, 1);
2270 PRINTCHAR (' ');
2271 print_object (make_number (interval->position + LENGTH (interval)),
2272 printcharfun, 1);
2273 PRINTCHAR (' ');
2274 print_object (interval->plist, printcharfun, 1);
2278 void
2279 syms_of_print (void)
2281 Qtemp_buffer_setup_hook = intern_c_string ("temp-buffer-setup-hook");
2282 staticpro (&Qtemp_buffer_setup_hook);
2284 DEFVAR_LISP ("standard-output", &Vstandard_output,
2285 doc: /* Output stream `print' uses by default for outputting a character.
2286 This may be any function of one argument.
2287 It may also be a buffer (output is inserted before point)
2288 or a marker (output is inserted and the marker is advanced)
2289 or the symbol t (output appears in the echo area). */);
2290 Vstandard_output = Qt;
2291 Qstandard_output = intern_c_string ("standard-output");
2292 staticpro (&Qstandard_output);
2294 DEFVAR_LISP ("float-output-format", &Vfloat_output_format,
2295 doc: /* The format descriptor string used to print floats.
2296 This is a %-spec like those accepted by `printf' in C,
2297 but with some restrictions. It must start with the two characters `%.'.
2298 After that comes an integer precision specification,
2299 and then a letter which controls the format.
2300 The letters allowed are `e', `f' and `g'.
2301 Use `e' for exponential notation \"DIG.DIGITSeEXPT\"
2302 Use `f' for decimal point notation \"DIGITS.DIGITS\".
2303 Use `g' to choose the shorter of those two formats for the number at hand.
2304 The precision in any of these cases is the number of digits following
2305 the decimal point. With `f', a precision of 0 means to omit the
2306 decimal point. 0 is not allowed with `e' or `g'.
2308 A value of nil means to use the shortest notation
2309 that represents the number without losing information. */);
2310 Vfloat_output_format = Qnil;
2311 Qfloat_output_format = intern_c_string ("float-output-format");
2312 staticpro (&Qfloat_output_format);
2314 DEFVAR_LISP ("print-length", &Vprint_length,
2315 doc: /* Maximum length of list to print before abbreviating.
2316 A value of nil means no limit. See also `eval-expression-print-length'. */);
2317 Vprint_length = Qnil;
2319 DEFVAR_LISP ("print-level", &Vprint_level,
2320 doc: /* Maximum depth of list nesting to print before abbreviating.
2321 A value of nil means no limit. See also `eval-expression-print-level'. */);
2322 Vprint_level = Qnil;
2324 DEFVAR_BOOL ("print-escape-newlines", &print_escape_newlines,
2325 doc: /* Non-nil means print newlines in strings as `\\n'.
2326 Also print formfeeds as `\\f'. */);
2327 print_escape_newlines = 0;
2329 DEFVAR_BOOL ("print-escape-nonascii", &print_escape_nonascii,
2330 doc: /* Non-nil means print unibyte non-ASCII chars in strings as \\OOO.
2331 \(OOO is the octal representation of the character code.)
2332 Only single-byte characters are affected, and only in `prin1'.
2333 When the output goes in a multibyte buffer, this feature is
2334 enabled regardless of the value of the variable. */);
2335 print_escape_nonascii = 0;
2337 DEFVAR_BOOL ("print-escape-multibyte", &print_escape_multibyte,
2338 doc: /* Non-nil means print multibyte characters in strings as \\xXXXX.
2339 \(XXXX is the hex representation of the character code.)
2340 This affects only `prin1'. */);
2341 print_escape_multibyte = 0;
2343 DEFVAR_BOOL ("print-quoted", &print_quoted,
2344 doc: /* Non-nil means print quoted forms with reader syntax.
2345 I.e., (quote foo) prints as 'foo, (function foo) as #'foo. */);
2346 print_quoted = 0;
2348 DEFVAR_LISP ("print-gensym", &Vprint_gensym,
2349 doc: /* Non-nil means print uninterned symbols so they will read as uninterned.
2350 I.e., the value of (make-symbol \"foobar\") prints as #:foobar.
2351 When the uninterned symbol appears within a recursive data structure,
2352 and the symbol appears more than once, in addition use the #N# and #N=
2353 constructs as needed, so that multiple references to the same symbol are
2354 shared once again when the text is read back. */);
2355 Vprint_gensym = Qnil;
2357 DEFVAR_LISP ("print-circle", &Vprint_circle,
2358 doc: /* *Non-nil means print recursive structures using #N= and #N# syntax.
2359 If nil, printing proceeds recursively and may lead to
2360 `max-lisp-eval-depth' being exceeded or an error may occur:
2361 \"Apparently circular structure being printed.\" Also see
2362 `print-length' and `print-level'.
2363 If non-nil, shared substructures anywhere in the structure are printed
2364 with `#N=' before the first occurrence (in the order of the print
2365 representation) and `#N#' in place of each subsequent occurrence,
2366 where N is a positive decimal integer. */);
2367 Vprint_circle = Qnil;
2369 DEFVAR_LISP ("print-continuous-numbering", &Vprint_continuous_numbering,
2370 doc: /* *Non-nil means number continuously across print calls.
2371 This affects the numbers printed for #N= labels and #M# references.
2372 See also `print-circle', `print-gensym', and `print-number-table'.
2373 This variable should not be set with `setq'; bind it with a `let' instead. */);
2374 Vprint_continuous_numbering = Qnil;
2376 DEFVAR_LISP ("print-number-table", &Vprint_number_table,
2377 doc: /* A vector used internally to produce `#N=' labels and `#N#' references.
2378 The Lisp printer uses this vector to detect Lisp objects referenced more
2379 than once.
2381 When you bind `print-continuous-numbering' to t, you should probably
2382 also bind `print-number-table' to nil. This ensures that the value of
2383 `print-number-table' can be garbage-collected once the printing is
2384 done. If all elements of `print-number-table' are nil, it means that
2385 the printing done so far has not found any shared structure or objects
2386 that need to be recorded in the table. */);
2387 Vprint_number_table = Qnil;
2389 DEFVAR_LISP ("print-charset-text-property", &Vprint_charset_text_property,
2390 doc: /* A flag to control printing of `charset' text property on printing a string.
2391 The value must be nil, t, or `default'.
2393 If the value is nil, don't print the text property `charset'.
2395 If the value is t, always print the text property `charset'.
2397 If the value is `default', print the text property `charset' only when
2398 the value is different from what is guessed in the current charset
2399 priorities. */);
2400 Vprint_charset_text_property = Qdefault;
2402 /* prin1_to_string_buffer initialized in init_buffer_once in buffer.c */
2403 staticpro (&Vprin1_to_string_buffer);
2405 defsubr (&Sprin1);
2406 defsubr (&Sprin1_to_string);
2407 defsubr (&Serror_message_string);
2408 defsubr (&Sprinc);
2409 defsubr (&Sprint);
2410 defsubr (&Sterpri);
2411 defsubr (&Swrite_char);
2412 defsubr (&Sexternal_debugging_output);
2413 #ifdef WITH_REDIRECT_DEBUGGING_OUTPUT
2414 defsubr (&Sredirect_debugging_output);
2415 #endif
2417 Qexternal_debugging_output = intern_c_string ("external-debugging-output");
2418 staticpro (&Qexternal_debugging_output);
2420 Qprint_escape_newlines = intern_c_string ("print-escape-newlines");
2421 staticpro (&Qprint_escape_newlines);
2423 Qprint_escape_multibyte = intern_c_string ("print-escape-multibyte");
2424 staticpro (&Qprint_escape_multibyte);
2426 Qprint_escape_nonascii = intern_c_string ("print-escape-nonascii");
2427 staticpro (&Qprint_escape_nonascii);
2429 print_prune_charset_plist = Qnil;
2430 staticpro (&print_prune_charset_plist);
2432 defsubr (&Swith_output_to_temp_buffer);
2435 /* arch-tag: bc797170-94ae-41de-86e3-75e20f8f7a39
2436 (do not change this comment) */