(normal-top-level): Don't operate on the initial
[emacs.git] / src / print.c
blob2fa74543c431a97f386252ef38ede5658c8a2f80
1 /* Lisp object printing and output streams.
2 Copyright (C) 1985, 86, 88, 93, 94, 95, 97, 98, 1999, 2000, 2001
3 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
23 #include <config.h>
24 #include <stdio.h>
25 #include "lisp.h"
26 #include "buffer.h"
27 #include "charset.h"
28 #include "keyboard.h"
29 #include "frame.h"
30 #include "window.h"
31 #include "process.h"
32 #include "dispextern.h"
33 #include "termchar.h"
34 #include "intervals.h"
36 Lisp_Object Vstandard_output, Qstandard_output;
38 Lisp_Object Qtemp_buffer_setup_hook;
40 /* These are used to print like we read. */
41 extern Lisp_Object Qbackquote, Qcomma, Qcomma_at, Qcomma_dot, Qfunction;
43 Lisp_Object Vfloat_output_format, Qfloat_output_format;
45 /* Work around a problem that happens because math.h on hpux 7
46 defines two static variables--which, in Emacs, are not really static,
47 because `static' is defined as nothing. The problem is that they are
48 defined both here and in lread.c.
49 These macros prevent the name conflict. */
50 #if defined (HPUX) && !defined (HPUX8)
51 #define _MAXLDBL print_maxldbl
52 #define _NMAXLDBL print_nmaxldbl
53 #endif
55 #include <math.h>
57 #if STDC_HEADERS
58 #include <float.h>
59 #endif
61 /* Default to values appropriate for IEEE floating point. */
62 #ifndef FLT_RADIX
63 #define FLT_RADIX 2
64 #endif
65 #ifndef DBL_MANT_DIG
66 #define DBL_MANT_DIG 53
67 #endif
68 #ifndef DBL_DIG
69 #define DBL_DIG 15
70 #endif
71 #ifndef DBL_MIN
72 #define DBL_MIN 2.2250738585072014e-308
73 #endif
75 #ifdef DBL_MIN_REPLACEMENT
76 #undef DBL_MIN
77 #define DBL_MIN DBL_MIN_REPLACEMENT
78 #endif
80 /* Define DOUBLE_DIGITS_BOUND, an upper bound on the number of decimal digits
81 needed to express a float without losing information.
82 The general-case formula is valid for the usual case, IEEE floating point,
83 but many compilers can't optimize the formula to an integer constant,
84 so make a special case for it. */
85 #if FLT_RADIX == 2 && DBL_MANT_DIG == 53
86 #define DOUBLE_DIGITS_BOUND 17 /* IEEE floating point */
87 #else
88 #define DOUBLE_DIGITS_BOUND ((int) ceil (log10 (pow (FLT_RADIX, DBL_MANT_DIG))))
89 #endif
91 /* Avoid actual stack overflow in print. */
92 int print_depth;
94 /* Detect most circularities to print finite output. */
95 #define PRINT_CIRCLE 200
96 Lisp_Object being_printed[PRINT_CIRCLE];
98 /* When printing into a buffer, first we put the text in this
99 block, then insert it all at once. */
100 char *print_buffer;
102 /* Size allocated in print_buffer. */
103 int print_buffer_size;
104 /* Chars stored in print_buffer. */
105 int print_buffer_pos;
106 /* Bytes stored in print_buffer. */
107 int print_buffer_pos_byte;
109 /* Maximum length of list to print in full; noninteger means
110 effectively infinity */
112 Lisp_Object Vprint_length;
114 /* Maximum depth of list to print in full; noninteger means
115 effectively infinity. */
117 Lisp_Object Vprint_level;
119 /* Nonzero means print newlines in strings as \n. */
121 int print_escape_newlines;
123 /* Nonzero means to print single-byte non-ascii characters in strings as
124 octal escapes. */
126 int print_escape_nonascii;
128 /* Nonzero means to print multibyte characters in strings as hex escapes. */
130 int print_escape_multibyte;
132 Lisp_Object Qprint_escape_newlines;
133 Lisp_Object Qprint_escape_multibyte, Qprint_escape_nonascii;
135 /* Nonzero means print (quote foo) forms as 'foo, etc. */
137 int print_quoted;
139 /* Non-nil means print #: before uninterned symbols. */
141 Lisp_Object Vprint_gensym;
143 /* Non-nil means print recursive structures using #n= and #n# syntax. */
145 Lisp_Object Vprint_circle;
147 /* Non-nil means keep continuous number for #n= and #n# syntax
148 between several print functions. */
150 Lisp_Object Vprint_continuous_numbering;
152 /* Vprint_number_table is a vector like [OBJ1 STAT1 OBJ2 STAT2 ...],
153 where OBJn are objects going to be printed, and STATn are their status,
154 which may be different meanings during process. See the comments of
155 the functions print and print_preprocess for details.
156 print_number_index keeps the last position the next object should be added,
157 twice of which is the actual vector position in Vprint_number_table. */
158 int print_number_index;
159 Lisp_Object Vprint_number_table;
161 /* PRINT_NUMBER_OBJECT returns the I'th object in Vprint_number_table TABLE.
162 PRINT_NUMBER_STATUS returns the status of the I'th object in TABLE.
163 See the comment of the variable Vprint_number_table. */
164 #define PRINT_NUMBER_OBJECT(table,i) XVECTOR ((table))->contents[(i) * 2]
165 #define PRINT_NUMBER_STATUS(table,i) XVECTOR ((table))->contents[(i) * 2 + 1]
167 /* Nonzero means print newline to stdout before next minibuffer message.
168 Defined in xdisp.c */
170 extern int noninteractive_need_newline;
172 extern int minibuffer_auto_raise;
174 #ifdef MAX_PRINT_CHARS
175 static int print_chars;
176 static int max_print;
177 #endif /* MAX_PRINT_CHARS */
179 void print_interval ();
182 /* Low level output routines for characters and strings */
184 /* Lisp functions to do output using a stream
185 must have the stream in a variable called printcharfun
186 and must start with PRINTPREPARE, end with PRINTFINISH,
187 and use PRINTDECLARE to declare common variables.
188 Use PRINTCHAR to output one character,
189 or call strout to output a block of characters. */
191 #define PRINTDECLARE \
192 struct buffer *old = current_buffer; \
193 int old_point = -1, start_point = -1; \
194 int old_point_byte = -1, start_point_byte = -1; \
195 int specpdl_count = specpdl_ptr - specpdl; \
196 int free_print_buffer = 0; \
197 int multibyte = !NILP (current_buffer->enable_multibyte_characters); \
198 Lisp_Object original
200 #define PRINTPREPARE \
201 original = printcharfun; \
202 if (NILP (printcharfun)) printcharfun = Qt; \
203 if (BUFFERP (printcharfun)) \
205 if (XBUFFER (printcharfun) != current_buffer) \
206 Fset_buffer (printcharfun); \
207 printcharfun = Qnil; \
209 if (MARKERP (printcharfun)) \
211 if (!(XMARKER (original)->buffer)) \
212 error ("Marker does not point anywhere"); \
213 if (XMARKER (original)->buffer != current_buffer) \
214 set_buffer_internal (XMARKER (original)->buffer); \
215 old_point = PT; \
216 old_point_byte = PT_BYTE; \
217 SET_PT_BOTH (marker_position (printcharfun), \
218 marker_byte_position (printcharfun)); \
219 start_point = PT; \
220 start_point_byte = PT_BYTE; \
221 printcharfun = Qnil; \
223 if (NILP (printcharfun)) \
225 Lisp_Object string; \
226 if (NILP (current_buffer->enable_multibyte_characters) \
227 && ! print_escape_multibyte) \
228 specbind (Qprint_escape_multibyte, Qt); \
229 if (! NILP (current_buffer->enable_multibyte_characters) \
230 && ! print_escape_nonascii) \
231 specbind (Qprint_escape_nonascii, Qt); \
232 if (print_buffer != 0) \
234 string = make_string_from_bytes (print_buffer, \
235 print_buffer_pos, \
236 print_buffer_pos_byte); \
237 record_unwind_protect (print_unwind, string); \
239 else \
241 print_buffer_size = 1000; \
242 print_buffer = (char *) xmalloc (print_buffer_size); \
243 free_print_buffer = 1; \
245 print_buffer_pos = 0; \
246 print_buffer_pos_byte = 0; \
248 if (EQ (printcharfun, Qt) && ! noninteractive) \
249 setup_echo_area_for_printing (multibyte);
251 #define PRINTFINISH \
252 if (NILP (printcharfun)) \
254 if (print_buffer_pos != print_buffer_pos_byte \
255 && NILP (current_buffer->enable_multibyte_characters)) \
257 unsigned char *temp \
258 = (unsigned char *) alloca (print_buffer_pos + 1); \
259 copy_text (print_buffer, temp, print_buffer_pos_byte, \
260 1, 0); \
261 insert_1_both (temp, print_buffer_pos, \
262 print_buffer_pos, 0, 1, 0); \
264 else \
265 insert_1_both (print_buffer, print_buffer_pos, \
266 print_buffer_pos_byte, 0, 1, 0); \
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 (saved_text)
291 Lisp_Object saved_text;
293 bcopy (XSTRING (saved_text)->data, print_buffer, XSTRING (saved_text)->size);
294 return Qnil;
298 /* Print character CH using method FUN. FUN nil means print to
299 print_buffer. FUN t means print to echo area or stdout if
300 non-interactive. If FUN is neither nil nor t, call FUN with CH as
301 argument. */
303 static void
304 printchar (ch, fun)
305 unsigned int ch;
306 Lisp_Object fun;
308 #ifdef MAX_PRINT_CHARS
309 if (max_print)
310 print_chars++;
311 #endif /* MAX_PRINT_CHARS */
313 if (!NILP (fun) && !EQ (fun, Qt))
314 call1 (fun, make_number (ch));
315 else
317 unsigned char str[MAX_MULTIBYTE_LENGTH];
318 int len = CHAR_STRING (ch, str);
320 QUIT;
322 if (NILP (fun))
324 if (print_buffer_pos_byte + len >= print_buffer_size)
325 print_buffer = (char *) xrealloc (print_buffer,
326 print_buffer_size *= 2);
327 bcopy (str, print_buffer + print_buffer_pos_byte, len);
328 print_buffer_pos += 1;
329 print_buffer_pos_byte += len;
331 else if (noninteractive)
333 fwrite (str, 1, len, stdout);
334 noninteractive_need_newline = 1;
336 else
338 int multibyte_p
339 = !NILP (current_buffer->enable_multibyte_characters);
341 setup_echo_area_for_printing (multibyte_p);
342 insert_char (ch);
343 message_dolog (str, len, 0, multibyte_p);
349 /* Output SIZE characters, SIZE_BYTE bytes from string PTR using
350 method PRINTCHARFUN. If SIZE < 0, use the string length of PTR for
351 both SIZE and SIZE_BYTE. PRINTCHARFUN nil means output to
352 print_buffer. PRINTCHARFUN t means output to the echo area or to
353 stdout if non-interactive. If neither nil nor t, call Lisp
354 function PRINTCHARFUN for each character printed. MULTIBYTE
355 non-zero means PTR contains multibyte characters. */
357 static void
358 strout (ptr, size, size_byte, printcharfun, multibyte)
359 char *ptr;
360 int size, size_byte;
361 Lisp_Object printcharfun;
362 int multibyte;
364 if (size < 0)
365 size_byte = size = strlen (ptr);
367 if (NILP (printcharfun))
369 if (print_buffer_pos_byte + size_byte > print_buffer_size)
371 print_buffer_size = print_buffer_size * 2 + size_byte;
372 print_buffer = (char *) xrealloc (print_buffer,
373 print_buffer_size);
375 bcopy (ptr, print_buffer + print_buffer_pos_byte, size_byte);
376 print_buffer_pos += size;
377 print_buffer_pos_byte += size_byte;
379 #ifdef MAX_PRINT_CHARS
380 if (max_print)
381 print_chars += size;
382 #endif /* MAX_PRINT_CHARS */
384 else if (noninteractive && EQ (printcharfun, Qt))
386 fwrite (ptr, 1, size_byte, stdout);
387 noninteractive_need_newline = 1;
389 else if (EQ (printcharfun, Qt))
391 /* Output to echo area. We're trying to avoid a little overhead
392 here, that's the reason we don't call printchar to do the
393 job. */
394 int i;
395 int multibyte_p
396 = !NILP (current_buffer->enable_multibyte_characters);
398 setup_echo_area_for_printing (multibyte_p);
399 message_dolog (ptr, size_byte, 0, multibyte_p);
401 if (size == size_byte)
403 for (i = 0; i < size; ++i)
404 insert_char (*ptr++);
406 else
408 int len;
409 for (i = 0; i < size_byte; i += len)
411 int ch = STRING_CHAR_AND_LENGTH (ptr + i, size_byte - i, len);
412 insert_char (ch);
416 #ifdef MAX_PRINT_CHARS
417 if (max_print)
418 print_chars += size;
419 #endif /* MAX_PRINT_CHARS */
421 else
423 /* PRINTCHARFUN is a Lisp function. */
424 int i = 0;
426 if (size == size_byte)
428 while (i < size_byte)
430 int ch = ptr[i++];
431 PRINTCHAR (ch);
434 else
436 while (i < size_byte)
438 /* Here, we must convert each multi-byte form to the
439 corresponding character code before handing it to
440 PRINTCHAR. */
441 int len;
442 int ch = STRING_CHAR_AND_LENGTH (ptr + i, size_byte - i, len);
443 PRINTCHAR (ch);
444 i += len;
450 /* Print the contents of a string STRING using PRINTCHARFUN.
451 It isn't safe to use strout in many cases,
452 because printing one char can relocate. */
454 static void
455 print_string (string, printcharfun)
456 Lisp_Object string;
457 Lisp_Object printcharfun;
459 if (EQ (printcharfun, Qt) || NILP (printcharfun))
461 int chars;
463 if (STRING_MULTIBYTE (string))
464 chars = XSTRING (string)->size;
465 else if (EQ (printcharfun, Qt)
466 ? ! NILP (buffer_defaults.enable_multibyte_characters)
467 : ! NILP (current_buffer->enable_multibyte_characters))
469 /* If unibyte string STRING contains 8-bit codes, we must
470 convert STRING to a multibyte string containing the same
471 character codes. */
472 Lisp_Object newstr;
473 int bytes;
475 chars = STRING_BYTES (XSTRING (string));
476 bytes = parse_str_to_multibyte (XSTRING (string)->data, chars);
477 if (chars < bytes)
479 newstr = make_uninit_multibyte_string (chars, bytes);
480 bcopy (XSTRING (string)->data, XSTRING (newstr)->data, chars);
481 str_to_multibyte (XSTRING (newstr)->data, bytes, chars);
482 string = newstr;
485 else
486 chars = STRING_BYTES (XSTRING (string));
488 /* strout is safe for output to a frame (echo area) or to print_buffer. */
489 strout (XSTRING (string)->data,
490 chars, STRING_BYTES (XSTRING (string)),
491 printcharfun, STRING_MULTIBYTE (string));
493 else
495 /* Otherwise, string may be relocated by printing one char.
496 So re-fetch the string address for each character. */
497 int i;
498 int size = XSTRING (string)->size;
499 int size_byte = STRING_BYTES (XSTRING (string));
500 struct gcpro gcpro1;
501 GCPRO1 (string);
502 if (size == size_byte)
503 for (i = 0; i < size; i++)
504 PRINTCHAR (XSTRING (string)->data[i]);
505 else
506 for (i = 0; i < size_byte; i++)
508 /* Here, we must convert each multi-byte form to the
509 corresponding character code before handing it to PRINTCHAR. */
510 int len;
511 int ch = STRING_CHAR_AND_LENGTH (XSTRING (string)->data + i,
512 size_byte - i, len);
513 if (!CHAR_VALID_P (ch, 0))
515 ch = XSTRING (string)->data[i];
516 len = 1;
518 PRINTCHAR (ch);
519 i += len;
521 UNGCPRO;
525 DEFUN ("write-char", Fwrite_char, Swrite_char, 1, 2, 0,
526 "Output character CHARACTER to stream PRINTCHARFUN.\n\
527 PRINTCHARFUN defaults to the value of `standard-output' (which see).")
528 (character, printcharfun)
529 Lisp_Object character, printcharfun;
531 PRINTDECLARE;
533 if (NILP (printcharfun))
534 printcharfun = Vstandard_output;
535 CHECK_NUMBER (character, 0);
536 PRINTPREPARE;
537 PRINTCHAR (XINT (character));
538 PRINTFINISH;
539 return character;
542 /* Used from outside of print.c to print a block of SIZE
543 single-byte chars at DATA on the default output stream.
544 Do not use this on the contents of a Lisp string. */
546 void
547 write_string (data, size)
548 char *data;
549 int size;
551 PRINTDECLARE;
552 Lisp_Object printcharfun;
554 printcharfun = Vstandard_output;
556 PRINTPREPARE;
557 strout (data, size, size, printcharfun, 0);
558 PRINTFINISH;
561 /* Used from outside of print.c to print a block of SIZE
562 single-byte chars at DATA on a specified stream PRINTCHARFUN.
563 Do not use this on the contents of a Lisp string. */
565 void
566 write_string_1 (data, size, printcharfun)
567 char *data;
568 int size;
569 Lisp_Object printcharfun;
571 PRINTDECLARE;
573 PRINTPREPARE;
574 strout (data, size, size, printcharfun, 0);
575 PRINTFINISH;
579 void
580 temp_output_buffer_setup (bufname)
581 char *bufname;
583 int count = specpdl_ptr - specpdl;
584 register struct buffer *old = current_buffer;
585 register Lisp_Object buf;
587 record_unwind_protect (set_buffer_if_live, Fcurrent_buffer ());
589 Fset_buffer (Fget_buffer_create (build_string (bufname)));
591 current_buffer->directory = old->directory;
592 current_buffer->read_only = Qnil;
593 current_buffer->filename = Qnil;
594 current_buffer->undo_list = Qt;
595 current_buffer->overlays_before = Qnil;
596 current_buffer->overlays_after = Qnil;
597 current_buffer->enable_multibyte_characters
598 = buffer_defaults.enable_multibyte_characters;
599 Ferase_buffer ();
600 XSETBUFFER (buf, current_buffer);
602 if (!NILP (Vrun_hooks))
603 call1 (Vrun_hooks, Qtemp_buffer_setup_hook);
605 unbind_to (count, Qnil);
607 specbind (Qstandard_output, buf);
610 Lisp_Object
611 internal_with_output_to_temp_buffer (bufname, function, args)
612 char *bufname;
613 Lisp_Object (*function) P_ ((Lisp_Object));
614 Lisp_Object args;
616 int count = specpdl_ptr - specpdl;
617 Lisp_Object buf, val;
618 struct gcpro gcpro1;
620 GCPRO1 (args);
621 record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
622 temp_output_buffer_setup (bufname);
623 buf = Vstandard_output;
624 UNGCPRO;
626 val = (*function) (args);
628 GCPRO1 (val);
629 temp_output_buffer_show (buf);
630 UNGCPRO;
632 return unbind_to (count, val);
635 DEFUN ("with-output-to-temp-buffer", Fwith_output_to_temp_buffer, Swith_output_to_temp_buffer,
636 1, UNEVALLED, 0,
637 "Bind `standard-output' to buffer BUFNAME, eval BODY, then show that buffer.\n\
638 The buffer is cleared out initially, and marked as unmodified when done.\n\
639 All output done by BODY is inserted in that buffer by default.\n\
640 The buffer is displayed in another window, but not selected.\n\
641 The value of the last form in BODY is returned.\n\
642 If BODY does not finish normally, the buffer BUFNAME is not displayed.\n\
644 The hook `temp-buffer-setup-hook' is run before BODY,\n\
645 with the buffer BUFNAME temporarily current.\n\
646 The hook `temp-buffer-show-hook' is run after the buffer is displayed,\n\
647 with the buffer temporarily current, and the window that was used\n\
648 to display it temporarily selected.\n\
650 If variable `temp-buffer-show-function' is non-nil, call it at the end\n\
651 to get the buffer displayed instead of just displaying the non-selected\n\
652 buffer and calling the hook. It gets one argument, the buffer to display.")
653 (args)
654 Lisp_Object args;
656 struct gcpro gcpro1;
657 Lisp_Object name;
658 int count = specpdl_ptr - specpdl;
659 Lisp_Object buf, val;
661 GCPRO1(args);
662 name = Feval (Fcar (args));
663 UNGCPRO;
665 CHECK_STRING (name, 0);
666 temp_output_buffer_setup (XSTRING (name)->data);
667 buf = Vstandard_output;
669 val = Fprogn (Fcdr (args));
671 temp_output_buffer_show (buf);
673 return unbind_to (count, val);
677 static void print ();
678 static void print_preprocess ();
679 static void print_preprocess_string ();
680 static void print_object ();
682 DEFUN ("terpri", Fterpri, Sterpri, 0, 1, 0,
683 "Output a newline to stream PRINTCHARFUN.\n\
684 If PRINTCHARFUN is omitted or nil, the value of `standard-output' is used.")
685 (printcharfun)
686 Lisp_Object printcharfun;
688 PRINTDECLARE;
690 if (NILP (printcharfun))
691 printcharfun = Vstandard_output;
692 PRINTPREPARE;
693 PRINTCHAR ('\n');
694 PRINTFINISH;
695 return Qt;
698 DEFUN ("prin1", Fprin1, Sprin1, 1, 2, 0,
699 "Output the printed representation of OBJECT, any Lisp object.\n\
700 Quoting characters are printed when needed to make output that `read'\n\
701 can handle, whenever this is possible.\n\
703 OBJECT is any of the Lisp data types: a number, a string, a symbol,\n\
704 a list, a buffer, a window, a frame, etc.\n\
706 A printed representation of an object is text which describes that object.\n\
708 Optional argument PRINTCHARFUN is the output stream, which can be one\n\
709 of these:\n\
711 - a buffer, in which case output is inserted into that buffer at point;\n\
712 - a marker, in which case output is inserted at marker's position;\n\
713 - a function, in which case that function is called once for each\n\
714 character of OBJECT's printed representation;\n\
715 - a symbol, in which case that symbol's function definition is called; or\n\
716 - t, in which case the output is displayed in the echo area.\n\
718 If PRINTCHARFUN is omitted, the value of `standard-output' (which see)\n\
719 is used instead.")
720 (object, printcharfun)
721 Lisp_Object object, printcharfun;
723 PRINTDECLARE;
725 #ifdef MAX_PRINT_CHARS
726 max_print = 0;
727 #endif /* MAX_PRINT_CHARS */
728 if (NILP (printcharfun))
729 printcharfun = Vstandard_output;
730 PRINTPREPARE;
731 print (object, printcharfun, 1);
732 PRINTFINISH;
733 return object;
736 /* a buffer which is used to hold output being built by prin1-to-string */
737 Lisp_Object Vprin1_to_string_buffer;
739 DEFUN ("prin1-to-string", Fprin1_to_string, Sprin1_to_string, 1, 2, 0,
740 "Return a string containing the printed representation of OBJECT,\n\
741 any Lisp object. Quoting characters are used when needed to make output\n\
742 that `read' can handle, whenever this is possible, unless the optional\n\
743 second argument NOESCAPE is non-nil.\n\
745 OBJECT is any of the Lisp data types: a number, a string, a symbol,\n\
746 a list, a buffer, a window, a frame, etc.\n\
748 A printed representation of an object is text which describes that object.")
749 (object, noescape)
750 Lisp_Object object, noescape;
752 PRINTDECLARE;
753 Lisp_Object printcharfun;
754 struct gcpro gcpro1, gcpro2;
755 Lisp_Object tem;
757 /* Save and restore this--we are altering a buffer
758 but we don't want to deactivate the mark just for that.
759 No need for specbind, since errors deactivate the mark. */
760 tem = Vdeactivate_mark;
761 GCPRO2 (object, tem);
763 printcharfun = Vprin1_to_string_buffer;
764 PRINTPREPARE;
765 print (object, printcharfun, NILP (noescape));
766 /* Make Vprin1_to_string_buffer be the default buffer after PRINTFINSH */
767 PRINTFINISH;
768 set_buffer_internal (XBUFFER (Vprin1_to_string_buffer));
769 object = Fbuffer_string ();
771 Ferase_buffer ();
772 set_buffer_internal (old);
774 Vdeactivate_mark = tem;
775 UNGCPRO;
777 return object;
780 DEFUN ("princ", Fprinc, Sprinc, 1, 2, 0,
781 "Output the printed representation of OBJECT, any Lisp object.\n\
782 No quoting characters are used; no delimiters are printed around\n\
783 the contents of strings.\n\
785 OBJECT is any of the Lisp data types: a number, a string, a symbol,\n\
786 a list, a buffer, a window, a frame, etc.\n\
788 A printed representation of an object is text which describes that object.\n\
790 Optional argument PRINTCHARFUN is the output stream, which can be one\n\
791 of these:\n\
793 - a buffer, in which case output is inserted into that buffer at point;\n\
794 - a marker, in which case output is inserted at marker's position;\n\
795 - a function, in which case that function is called once for each\n\
796 character of OBJECT's printed representation;\n\
797 - a symbol, in which case that symbol's function definition is called; or\n\
798 - t, in which case the output is displayed in the echo area.\n\
800 If PRINTCHARFUN is omitted, the value of `standard-output' (which see)\n\
801 is used instead.")
802 (object, printcharfun)
803 Lisp_Object object, printcharfun;
805 PRINTDECLARE;
807 if (NILP (printcharfun))
808 printcharfun = Vstandard_output;
809 PRINTPREPARE;
810 print (object, printcharfun, 0);
811 PRINTFINISH;
812 return object;
815 DEFUN ("print", Fprint, Sprint, 1, 2, 0,
816 "Output the printed representation of OBJECT, with newlines around it.\n\
817 Quoting characters are printed when needed to make output that `read'\n\
818 can handle, whenever this is possible.\n\
820 OBJECT is any of the Lisp data types: a number, a string, a symbol,\n\
821 a list, a buffer, a window, a frame, etc.\n\
823 A printed representation of an object is text which describes that object.\n\
825 Optional argument PRINTCHARFUN is the output stream, which can be one\n\
826 of these:\n\
828 - a buffer, in which case output is inserted into that buffer at point;\n\
829 - a marker, in which case output is inserted at marker's position;\n\
830 - a function, in which case that function is called once for each\n\
831 character of OBJECT's printed representation;\n\
832 - a symbol, in which case that symbol's function definition is called; or\n\
833 - t, in which case the output is displayed in the echo area.\n\
835 If PRINTCHARFUN is omitted, the value of `standard-output' (which see)\n\
836 is used instead.")
837 (object, printcharfun)
838 Lisp_Object object, printcharfun;
840 PRINTDECLARE;
841 struct gcpro gcpro1;
843 #ifdef MAX_PRINT_CHARS
844 print_chars = 0;
845 max_print = MAX_PRINT_CHARS;
846 #endif /* MAX_PRINT_CHARS */
847 if (NILP (printcharfun))
848 printcharfun = Vstandard_output;
849 GCPRO1 (object);
850 PRINTPREPARE;
851 PRINTCHAR ('\n');
852 print (object, printcharfun, 1);
853 PRINTCHAR ('\n');
854 PRINTFINISH;
855 #ifdef MAX_PRINT_CHARS
856 max_print = 0;
857 print_chars = 0;
858 #endif /* MAX_PRINT_CHARS */
859 UNGCPRO;
860 return object;
863 /* The subroutine object for external-debugging-output is kept here
864 for the convenience of the debugger. */
865 Lisp_Object Qexternal_debugging_output;
867 DEFUN ("external-debugging-output", Fexternal_debugging_output, Sexternal_debugging_output, 1, 1, 0,
868 "Write CHARACTER to stderr.\n\
869 You can call print while debugging emacs, and pass it this function\n\
870 to make it write to the debugging output.\n")
871 (character)
872 Lisp_Object character;
874 CHECK_NUMBER (character, 0);
875 putc (XINT (character), stderr);
877 #ifdef WINDOWSNT
878 /* Send the output to a debugger (nothing happens if there isn't one). */
880 char buf[2] = {(char) XINT (character), '\0'};
881 OutputDebugString (buf);
883 #endif
885 return character;
888 /* This is the interface for debugging printing. */
890 void
891 debug_print (arg)
892 Lisp_Object arg;
894 Fprin1 (arg, Qexternal_debugging_output);
895 fprintf (stderr, "\r\n");
898 DEFUN ("error-message-string", Ferror_message_string, Serror_message_string,
899 1, 1, 0,
900 "Convert an error value (ERROR-SYMBOL . DATA) to an error message.")
901 (obj)
902 Lisp_Object obj;
904 struct buffer *old = current_buffer;
905 Lisp_Object value;
906 struct gcpro gcpro1;
908 /* If OBJ is (error STRING), just return STRING.
909 That is not only faster, it also avoids the need to allocate
910 space here when the error is due to memory full. */
911 if (CONSP (obj) && EQ (XCAR (obj), Qerror)
912 && CONSP (XCDR (obj))
913 && STRINGP (XCAR (XCDR (obj)))
914 && NILP (XCDR (XCDR (obj))))
915 return XCAR (XCDR (obj));
917 print_error_message (obj, Vprin1_to_string_buffer);
919 set_buffer_internal (XBUFFER (Vprin1_to_string_buffer));
920 value = Fbuffer_string ();
922 GCPRO1 (value);
923 Ferase_buffer ();
924 set_buffer_internal (old);
925 UNGCPRO;
927 return value;
930 /* Print an error message for the error DATA onto Lisp output stream
931 STREAM (suitable for the print functions). */
933 void
934 print_error_message (data, stream)
935 Lisp_Object data, stream;
937 Lisp_Object errname, errmsg, file_error, tail;
938 struct gcpro gcpro1;
939 int i;
941 errname = Fcar (data);
943 if (EQ (errname, Qerror))
945 data = Fcdr (data);
946 if (!CONSP (data))
947 data = Qnil;
948 errmsg = Fcar (data);
949 file_error = Qnil;
951 else
953 Lisp_Object error_conditions;
954 errmsg = Fget (errname, Qerror_message);
955 error_conditions = Fget (errname, Qerror_conditions);
956 file_error = Fmemq (Qfile_error, error_conditions);
959 /* Print an error message including the data items. */
961 tail = Fcdr_safe (data);
962 GCPRO1 (tail);
964 /* If we know from where the error was signaled, show it in
965 *Messages*. */
966 if (!NILP (Vsignaling_function) && SYMBOLP (Vsignaling_function))
968 char *name = XSYMBOL (Vsignaling_function)->name->data;
969 message_dolog (name, strlen (name), 0, 0);
970 message_dolog (": ", 2, 0, 0);
971 Vsignaling_function = Qnil;
974 /* For file-error, make error message by concatenating
975 all the data items. They are all strings. */
976 if (!NILP (file_error) && CONSP (tail))
977 errmsg = XCAR (tail), tail = XCDR (tail);
979 if (STRINGP (errmsg))
980 Fprinc (errmsg, stream);
981 else
982 write_string_1 ("peculiar error", -1, stream);
984 for (i = 0; CONSP (tail); tail = XCDR (tail), i++)
986 Lisp_Object obj;
988 write_string_1 (i ? ", " : ": ", 2, stream);
989 obj = XCAR (tail);
990 if (!NILP (file_error) || EQ (errname, Qend_of_file))
991 Fprinc (obj, stream);
992 else
993 Fprin1 (obj, stream);
996 UNGCPRO;
1002 * The buffer should be at least as large as the max string size of the
1003 * largest float, printed in the biggest notation. This is undoubtedly
1004 * 20d float_output_format, with the negative of the C-constant "HUGE"
1005 * from <math.h>.
1007 * On the vax the worst case is -1e38 in 20d format which takes 61 bytes.
1009 * I assume that IEEE-754 format numbers can take 329 bytes for the worst
1010 * case of -1e307 in 20d float_output_format. What is one to do (short of
1011 * re-writing _doprnt to be more sane)?
1012 * -wsr
1015 void
1016 float_to_string (buf, data)
1017 unsigned char *buf;
1018 double data;
1020 unsigned char *cp;
1021 int width;
1023 /* Check for plus infinity in a way that won't lose
1024 if there is no plus infinity. */
1025 if (data == data / 2 && data > 1.0)
1027 strcpy (buf, "1.0e+INF");
1028 return;
1030 /* Likewise for minus infinity. */
1031 if (data == data / 2 && data < -1.0)
1033 strcpy (buf, "-1.0e+INF");
1034 return;
1036 /* Check for NaN in a way that won't fail if there are no NaNs. */
1037 if (! (data * 0.0 >= 0.0))
1039 /* Prepend "-" if the NaN's sign bit is negative.
1040 The sign bit of a double is the bit that is 1 in -0.0. */
1041 int i;
1042 union { double d; char c[sizeof (double)]; } u_data, u_minus_zero;
1043 u_data.d = data;
1044 u_minus_zero.d = - 0.0;
1045 for (i = 0; i < sizeof (double); i++)
1046 if (u_data.c[i] & u_minus_zero.c[i])
1048 *buf++ = '-';
1049 break;
1052 strcpy (buf, "0.0e+NaN");
1053 return;
1056 if (NILP (Vfloat_output_format)
1057 || !STRINGP (Vfloat_output_format))
1058 lose:
1060 /* Generate the fewest number of digits that represent the
1061 floating point value without losing information.
1062 The following method is simple but a bit slow.
1063 For ideas about speeding things up, please see:
1065 Guy L Steele Jr & Jon L White, How to print floating-point numbers
1066 accurately. SIGPLAN notices 25, 6 (June 1990), 112-126.
1068 Robert G Burger & R Kent Dybvig, Printing floating point numbers
1069 quickly and accurately, SIGPLAN notices 31, 5 (May 1996), 108-116. */
1071 width = fabs (data) < DBL_MIN ? 1 : DBL_DIG;
1073 sprintf (buf, "%.*g", width, data);
1074 while (width++ < DOUBLE_DIGITS_BOUND && atof (buf) != data);
1076 else /* oink oink */
1078 /* Check that the spec we have is fully valid.
1079 This means not only valid for printf,
1080 but meant for floats, and reasonable. */
1081 cp = XSTRING (Vfloat_output_format)->data;
1083 if (cp[0] != '%')
1084 goto lose;
1085 if (cp[1] != '.')
1086 goto lose;
1088 cp += 2;
1090 /* Check the width specification. */
1091 width = -1;
1092 if ('0' <= *cp && *cp <= '9')
1094 width = 0;
1096 width = (width * 10) + (*cp++ - '0');
1097 while (*cp >= '0' && *cp <= '9');
1099 /* A precision of zero is valid only for %f. */
1100 if (width > DBL_DIG
1101 || (width == 0 && *cp != 'f'))
1102 goto lose;
1105 if (*cp != 'e' && *cp != 'f' && *cp != 'g')
1106 goto lose;
1108 if (cp[1] != 0)
1109 goto lose;
1111 sprintf (buf, XSTRING (Vfloat_output_format)->data, data);
1114 /* Make sure there is a decimal point with digit after, or an
1115 exponent, so that the value is readable as a float. But don't do
1116 this with "%.0f"; it's valid for that not to produce a decimal
1117 point. Note that width can be 0 only for %.0f. */
1118 if (width != 0)
1120 for (cp = buf; *cp; cp++)
1121 if ((*cp < '0' || *cp > '9') && *cp != '-')
1122 break;
1124 if (*cp == '.' && cp[1] == 0)
1126 cp[1] = '0';
1127 cp[2] = 0;
1130 if (*cp == 0)
1132 *cp++ = '.';
1133 *cp++ = '0';
1134 *cp++ = 0;
1140 static void
1141 print (obj, printcharfun, escapeflag)
1142 Lisp_Object obj;
1143 register Lisp_Object printcharfun;
1144 int escapeflag;
1146 print_depth = 0;
1148 /* Reset print_number_index and Vprint_number_table only when
1149 the variable Vprint_continuous_numbering is nil. Otherwise,
1150 the values of these variables will be kept between several
1151 print functions. */
1152 if (NILP (Vprint_continuous_numbering))
1154 print_number_index = 0;
1155 Vprint_number_table = Qnil;
1158 /* Construct Vprint_number_table for print-gensym and print-circle. */
1159 if (!NILP (Vprint_gensym) || !NILP (Vprint_circle))
1161 int i, start, index;
1162 /* Construct Vprint_number_table. */
1163 start = index = print_number_index;
1164 print_preprocess (obj);
1165 /* Remove unnecessary objects, which appear only once in OBJ;
1166 that is, whose status is Qnil. */
1167 for (i = start; i < print_number_index; i++)
1168 if (!NILP (PRINT_NUMBER_STATUS (Vprint_number_table, i)))
1170 PRINT_NUMBER_OBJECT (Vprint_number_table, index)
1171 = PRINT_NUMBER_OBJECT (Vprint_number_table, i);
1172 /* Reset the status field for the next print step. Now this
1173 field means whether the object has already been printed. */
1174 PRINT_NUMBER_STATUS (Vprint_number_table, index) = Qnil;
1175 index++;
1177 print_number_index = index;
1180 print_object (obj, printcharfun, escapeflag);
1183 /* Construct Vprint_number_table according to the structure of OBJ.
1184 OBJ itself and all its elements will be added to Vprint_number_table
1185 recursively if it is a list, vector, compiled function, char-table,
1186 string (its text properties will be traced), or a symbol that has
1187 no obarray (this is for the print-gensym feature).
1188 The status fields of Vprint_number_table mean whether each object appears
1189 more than once in OBJ: Qnil at the first time, and Qt after that . */
1190 static void
1191 print_preprocess (obj)
1192 Lisp_Object obj;
1194 int i, size;
1196 loop:
1197 if (STRINGP (obj) || CONSP (obj) || VECTORP (obj)
1198 || COMPILEDP (obj) || CHAR_TABLE_P (obj)
1199 || (! NILP (Vprint_gensym)
1200 && SYMBOLP (obj) && NILP (XSYMBOL (obj)->obarray)))
1202 /* In case print-circle is nil and print-gensym is t,
1203 add OBJ to Vprint_number_table only when OBJ is a symbol. */
1204 if (! NILP (Vprint_circle) || SYMBOLP (obj))
1206 for (i = 0; i < print_number_index; i++)
1207 if (EQ (PRINT_NUMBER_OBJECT (Vprint_number_table, i), obj))
1209 /* OBJ appears more than once. Let's remember that. */
1210 PRINT_NUMBER_STATUS (Vprint_number_table, i) = Qt;
1211 return;
1214 /* OBJ is not yet recorded. Let's add to the table. */
1215 if (print_number_index == 0)
1217 /* Initialize the table. */
1218 Vprint_number_table = Fmake_vector (make_number (40), Qnil);
1220 else if (XVECTOR (Vprint_number_table)->size == print_number_index * 2)
1222 /* Reallocate the table. */
1223 int i = print_number_index * 4;
1224 Lisp_Object old_table = Vprint_number_table;
1225 Vprint_number_table = Fmake_vector (make_number (i), Qnil);
1226 for (i = 0; i < print_number_index; i++)
1228 PRINT_NUMBER_OBJECT (Vprint_number_table, i)
1229 = PRINT_NUMBER_OBJECT (old_table, i);
1230 PRINT_NUMBER_STATUS (Vprint_number_table, i)
1231 = PRINT_NUMBER_STATUS (old_table, i);
1234 PRINT_NUMBER_OBJECT (Vprint_number_table, print_number_index) = obj;
1235 /* If Vprint_continuous_numbering is non-nil and OBJ is a gensym,
1236 always print the gensym with a number. This is a special for
1237 the lisp function byte-compile-output-docform. */
1238 if (! NILP (Vprint_continuous_numbering) && SYMBOLP (obj)
1239 && NILP (XSYMBOL (obj)->obarray))
1240 PRINT_NUMBER_STATUS (Vprint_number_table, print_number_index) = Qt;
1241 print_number_index++;
1244 switch (XGCTYPE (obj))
1246 case Lisp_String:
1247 /* A string may have text properties, which can be circular. */
1248 traverse_intervals (XSTRING (obj)->intervals, 0, 0,
1249 print_preprocess_string, Qnil);
1250 break;
1252 case Lisp_Cons:
1253 print_preprocess (XCAR (obj));
1254 obj = XCDR (obj);
1255 goto loop;
1257 case Lisp_Vectorlike:
1258 size = XVECTOR (obj)->size & PSEUDOVECTOR_SIZE_MASK;
1259 for (i = 0; i < size; i++)
1260 print_preprocess (XVECTOR (obj)->contents[i]);
1261 break;
1263 default:
1264 break;
1269 static void
1270 print_preprocess_string (interval, arg)
1271 INTERVAL interval;
1272 Lisp_Object arg;
1274 print_preprocess (interval->plist);
1277 static void
1278 print_object (obj, printcharfun, escapeflag)
1279 Lisp_Object obj;
1280 register Lisp_Object printcharfun;
1281 int escapeflag;
1283 char buf[30];
1285 QUIT;
1287 /* Detect circularities and truncate them. */
1288 if (STRINGP (obj) || CONSP (obj) || VECTORP (obj)
1289 || COMPILEDP (obj) || CHAR_TABLE_P (obj)
1290 || (! NILP (Vprint_gensym)
1291 && SYMBOLP (obj) && NILP (XSYMBOL (obj)->obarray)))
1293 if (NILP (Vprint_circle) && NILP (Vprint_gensym))
1295 /* Simple but incomplete way. */
1296 int i;
1297 for (i = 0; i < print_depth; i++)
1298 if (EQ (obj, being_printed[i]))
1300 sprintf (buf, "#%d", i);
1301 strout (buf, -1, -1, printcharfun, 0);
1302 return;
1304 being_printed[print_depth] = obj;
1306 else
1308 /* With the print-circle feature. */
1309 int i;
1310 for (i = 0; i < print_number_index; i++)
1311 if (EQ (PRINT_NUMBER_OBJECT (Vprint_number_table, i), obj))
1313 if (NILP (PRINT_NUMBER_STATUS (Vprint_number_table, i)))
1315 /* Add a prefix #n= if OBJ has not yet been printed;
1316 that is, its status field is nil. */
1317 sprintf (buf, "#%d=", i + 1);
1318 strout (buf, -1, -1, printcharfun, 0);
1319 /* OBJ is going to be printed. Set the status to t. */
1320 PRINT_NUMBER_STATUS (Vprint_number_table, i) = Qt;
1321 break;
1323 else
1325 /* Just print #n# if OBJ has already been printed. */
1326 sprintf (buf, "#%d#", i + 1);
1327 strout (buf, -1, -1, printcharfun, 0);
1328 return;
1334 print_depth++;
1336 if (print_depth > PRINT_CIRCLE)
1337 error ("Apparently circular structure being printed");
1338 #ifdef MAX_PRINT_CHARS
1339 if (max_print && print_chars > max_print)
1341 PRINTCHAR ('\n');
1342 print_chars = 0;
1344 #endif /* MAX_PRINT_CHARS */
1346 switch (XGCTYPE (obj))
1348 case Lisp_Int:
1349 if (sizeof (int) == sizeof (EMACS_INT))
1350 sprintf (buf, "%d", XINT (obj));
1351 else if (sizeof (long) == sizeof (EMACS_INT))
1352 sprintf (buf, "%ld", (long) XINT (obj));
1353 else
1354 abort ();
1355 strout (buf, -1, -1, printcharfun, 0);
1356 break;
1358 case Lisp_Float:
1360 char pigbuf[350]; /* see comments in float_to_string */
1362 float_to_string (pigbuf, XFLOAT_DATA (obj));
1363 strout (pigbuf, -1, -1, printcharfun, 0);
1365 break;
1367 case Lisp_String:
1368 if (!escapeflag)
1369 print_string (obj, printcharfun);
1370 else
1372 register int i, i_byte;
1373 struct gcpro gcpro1;
1374 unsigned char *str;
1375 int size_byte;
1376 /* 1 means we must ensure that the next character we output
1377 cannot be taken as part of a hex character escape. */
1378 int need_nonhex = 0;
1380 GCPRO1 (obj);
1382 if (!NULL_INTERVAL_P (XSTRING (obj)->intervals))
1384 PRINTCHAR ('#');
1385 PRINTCHAR ('(');
1388 PRINTCHAR ('\"');
1389 str = XSTRING (obj)->data;
1390 size_byte = STRING_BYTES (XSTRING (obj));
1392 for (i = 0, i_byte = 0; i_byte < size_byte;)
1394 /* Here, we must convert each multi-byte form to the
1395 corresponding character code before handing it to PRINTCHAR. */
1396 int len;
1397 int c;
1399 if (STRING_MULTIBYTE (obj))
1401 c = STRING_CHAR_AND_LENGTH (str + i_byte,
1402 size_byte - i_byte, len);
1403 if (CHAR_VALID_P (c, 0))
1404 i_byte += len;
1405 else
1406 c = str[i_byte++];
1408 else
1409 c = str[i_byte++];
1411 QUIT;
1413 if (c == '\n' && print_escape_newlines)
1415 PRINTCHAR ('\\');
1416 PRINTCHAR ('n');
1418 else if (c == '\f' && print_escape_newlines)
1420 PRINTCHAR ('\\');
1421 PRINTCHAR ('f');
1423 else if (! SINGLE_BYTE_CHAR_P (c) && print_escape_multibyte)
1425 /* When multibyte is disabled,
1426 print multibyte string chars using hex escapes. */
1427 unsigned char outbuf[50];
1428 sprintf (outbuf, "\\x%x", c);
1429 strout (outbuf, -1, -1, printcharfun, 0);
1430 need_nonhex = 1;
1432 else if (SINGLE_BYTE_CHAR_P (c) && ! ASCII_BYTE_P (c)
1433 && print_escape_nonascii)
1435 /* When printing in a multibyte buffer
1436 or when explicitly requested,
1437 print single-byte non-ASCII string chars
1438 using octal escapes. */
1439 unsigned char outbuf[5];
1440 sprintf (outbuf, "\\%03o", c);
1441 strout (outbuf, -1, -1, printcharfun, 0);
1443 else
1445 /* If we just had a hex escape, and this character
1446 could be taken as part of it,
1447 output `\ ' to prevent that. */
1448 if (need_nonhex)
1450 need_nonhex = 0;
1451 if ((c >= 'a' && c <= 'f')
1452 || (c >= 'A' && c <= 'F')
1453 || (c >= '0' && c <= '9'))
1454 strout ("\\ ", -1, -1, printcharfun, 0);
1457 if (c == '\"' || c == '\\')
1458 PRINTCHAR ('\\');
1459 PRINTCHAR (c);
1462 PRINTCHAR ('\"');
1464 if (!NULL_INTERVAL_P (XSTRING (obj)->intervals))
1466 traverse_intervals (XSTRING (obj)->intervals,
1467 0, 0, print_interval, printcharfun);
1468 PRINTCHAR (')');
1471 UNGCPRO;
1473 break;
1475 case Lisp_Symbol:
1477 register int confusing;
1478 register unsigned char *p = XSYMBOL (obj)->name->data;
1479 register unsigned char *end = p + STRING_BYTES (XSYMBOL (obj)->name);
1480 register int c;
1481 int i, i_byte, size_byte;
1482 Lisp_Object name;
1484 XSETSTRING (name, XSYMBOL (obj)->name);
1486 if (p != end && (*p == '-' || *p == '+')) p++;
1487 if (p == end)
1488 confusing = 0;
1489 /* If symbol name begins with a digit, and ends with a digit,
1490 and contains nothing but digits and `e', it could be treated
1491 as a number. So set CONFUSING.
1493 Symbols that contain periods could also be taken as numbers,
1494 but periods are always escaped, so we don't have to worry
1495 about them here. */
1496 else if (*p >= '0' && *p <= '9'
1497 && end[-1] >= '0' && end[-1] <= '9')
1499 while (p != end && ((*p >= '0' && *p <= '9')
1500 /* Needed for \2e10. */
1501 || *p == 'e'))
1502 p++;
1503 confusing = (end == p);
1505 else
1506 confusing = 0;
1508 if (! NILP (Vprint_gensym) && NILP (XSYMBOL (obj)->obarray))
1510 PRINTCHAR ('#');
1511 PRINTCHAR (':');
1514 size_byte = STRING_BYTES (XSTRING (name));
1516 for (i = 0, i_byte = 0; i_byte < size_byte;)
1518 /* Here, we must convert each multi-byte form to the
1519 corresponding character code before handing it to PRINTCHAR. */
1520 FETCH_STRING_CHAR_ADVANCE (c, name, i, i_byte);
1521 QUIT;
1523 if (escapeflag)
1525 if (c == '\"' || c == '\\' || c == '\''
1526 || c == ';' || c == '#' || c == '(' || c == ')'
1527 || c == ',' || c =='.' || c == '`'
1528 || c == '[' || c == ']' || c == '?' || c <= 040
1529 || confusing)
1530 PRINTCHAR ('\\'), confusing = 0;
1532 PRINTCHAR (c);
1535 break;
1537 case Lisp_Cons:
1538 /* If deeper than spec'd depth, print placeholder. */
1539 if (INTEGERP (Vprint_level)
1540 && print_depth > XINT (Vprint_level))
1541 strout ("...", -1, -1, printcharfun, 0);
1542 else if (print_quoted && CONSP (XCDR (obj)) && NILP (XCDR (XCDR (obj)))
1543 && (EQ (XCAR (obj), Qquote)))
1545 PRINTCHAR ('\'');
1546 print_object (XCAR (XCDR (obj)), printcharfun, escapeflag);
1548 else if (print_quoted && CONSP (XCDR (obj)) && NILP (XCDR (XCDR (obj)))
1549 && (EQ (XCAR (obj), Qfunction)))
1551 PRINTCHAR ('#');
1552 PRINTCHAR ('\'');
1553 print_object (XCAR (XCDR (obj)), printcharfun, escapeflag);
1555 else if (print_quoted && CONSP (XCDR (obj)) && NILP (XCDR (XCDR (obj)))
1556 && ((EQ (XCAR (obj), Qbackquote)
1557 || EQ (XCAR (obj), Qcomma)
1558 || EQ (XCAR (obj), Qcomma_at)
1559 || EQ (XCAR (obj), Qcomma_dot))))
1561 print_object (XCAR (obj), printcharfun, 0);
1562 print_object (XCAR (XCDR (obj)), printcharfun, escapeflag);
1564 else
1566 PRINTCHAR ('(');
1568 int print_length, i;
1569 Lisp_Object halftail = obj;
1571 /* Negative values of print-length are invalid in CL.
1572 Treat them like nil, as CMUCL does. */
1573 if (NATNUMP (Vprint_length))
1574 print_length = XFASTINT (Vprint_length);
1575 else
1576 print_length = 0;
1578 i = 0;
1579 while (CONSP (obj))
1581 /* Detect circular list. */
1582 if (NILP (Vprint_circle))
1584 /* Simple but imcomplete way. */
1585 if (i != 0 && EQ (obj, halftail))
1587 sprintf (buf, " . #%d", i / 2);
1588 strout (buf, -1, -1, printcharfun, 0);
1589 goto end_of_list;
1592 else
1594 /* With the print-circle feature. */
1595 if (i != 0)
1597 int i;
1598 for (i = 0; i < print_number_index; i++)
1599 if (EQ (PRINT_NUMBER_OBJECT (Vprint_number_table, i),
1600 obj))
1602 if (NILP (PRINT_NUMBER_STATUS (Vprint_number_table, i)))
1604 strout (" . ", 3, 3, printcharfun, 0);
1605 print_object (obj, printcharfun, escapeflag);
1607 else
1609 sprintf (buf, " . #%d#", i + 1);
1610 strout (buf, -1, -1, printcharfun, 0);
1612 goto end_of_list;
1617 if (i++)
1618 PRINTCHAR (' ');
1620 if (print_length && i > print_length)
1622 strout ("...", 3, 3, printcharfun, 0);
1623 goto end_of_list;
1626 print_object (XCAR (obj), printcharfun, escapeflag);
1628 obj = XCDR (obj);
1629 if (!(i & 1))
1630 halftail = XCDR (halftail);
1634 /* OBJ non-nil here means it's the end of a dotted list. */
1635 if (!NILP (obj))
1637 strout (" . ", 3, 3, printcharfun, 0);
1638 print_object (obj, printcharfun, escapeflag);
1641 end_of_list:
1642 PRINTCHAR (')');
1644 break;
1646 case Lisp_Vectorlike:
1647 if (PROCESSP (obj))
1649 if (escapeflag)
1651 strout ("#<process ", -1, -1, printcharfun, 0);
1652 print_string (XPROCESS (obj)->name, printcharfun);
1653 PRINTCHAR ('>');
1655 else
1656 print_string (XPROCESS (obj)->name, printcharfun);
1658 else if (BOOL_VECTOR_P (obj))
1660 register int i;
1661 register unsigned char c;
1662 struct gcpro gcpro1;
1663 int size_in_chars
1664 = (XBOOL_VECTOR (obj)->size + BITS_PER_CHAR - 1) / BITS_PER_CHAR;
1666 GCPRO1 (obj);
1668 PRINTCHAR ('#');
1669 PRINTCHAR ('&');
1670 sprintf (buf, "%d", XBOOL_VECTOR (obj)->size);
1671 strout (buf, -1, -1, printcharfun, 0);
1672 PRINTCHAR ('\"');
1674 /* Don't print more characters than the specified maximum.
1675 Negative values of print-length are invalid. Treat them
1676 like a print-length of nil. */
1677 if (NATNUMP (Vprint_length)
1678 && XFASTINT (Vprint_length) < size_in_chars)
1679 size_in_chars = XFASTINT (Vprint_length);
1681 for (i = 0; i < size_in_chars; i++)
1683 QUIT;
1684 c = XBOOL_VECTOR (obj)->data[i];
1685 if (c == '\n' && print_escape_newlines)
1687 PRINTCHAR ('\\');
1688 PRINTCHAR ('n');
1690 else if (c == '\f' && print_escape_newlines)
1692 PRINTCHAR ('\\');
1693 PRINTCHAR ('f');
1695 else
1697 if (c == '\"' || c == '\\')
1698 PRINTCHAR ('\\');
1699 PRINTCHAR (c);
1702 PRINTCHAR ('\"');
1704 UNGCPRO;
1706 else if (SUBRP (obj))
1708 strout ("#<subr ", -1, -1, printcharfun, 0);
1709 strout (XSUBR (obj)->symbol_name, -1, -1, printcharfun, 0);
1710 PRINTCHAR ('>');
1712 else if (WINDOWP (obj))
1714 strout ("#<window ", -1, -1, printcharfun, 0);
1715 sprintf (buf, "%d", XFASTINT (XWINDOW (obj)->sequence_number));
1716 strout (buf, -1, -1, printcharfun, 0);
1717 if (!NILP (XWINDOW (obj)->buffer))
1719 strout (" on ", -1, -1, printcharfun, 0);
1720 print_string (XBUFFER (XWINDOW (obj)->buffer)->name, printcharfun);
1722 PRINTCHAR ('>');
1724 else if (HASH_TABLE_P (obj))
1726 struct Lisp_Hash_Table *h = XHASH_TABLE (obj);
1727 strout ("#<hash-table", -1, -1, printcharfun, 0);
1728 if (SYMBOLP (h->test))
1730 PRINTCHAR (' ');
1731 PRINTCHAR ('\'');
1732 strout (XSYMBOL (h->test)->name->data, -1, -1, printcharfun, 0);
1733 PRINTCHAR (' ');
1734 strout (XSYMBOL (h->weak)->name->data, -1, -1, printcharfun, 0);
1735 PRINTCHAR (' ');
1736 sprintf (buf, "%d/%d", XFASTINT (h->count),
1737 XVECTOR (h->next)->size);
1738 strout (buf, -1, -1, printcharfun, 0);
1740 sprintf (buf, " 0x%lx", (unsigned long) h);
1741 strout (buf, -1, -1, printcharfun, 0);
1742 PRINTCHAR ('>');
1744 else if (BUFFERP (obj))
1746 if (NILP (XBUFFER (obj)->name))
1747 strout ("#<killed buffer>", -1, -1, printcharfun, 0);
1748 else if (escapeflag)
1750 strout ("#<buffer ", -1, -1, printcharfun, 0);
1751 print_string (XBUFFER (obj)->name, printcharfun);
1752 PRINTCHAR ('>');
1754 else
1755 print_string (XBUFFER (obj)->name, printcharfun);
1757 else if (WINDOW_CONFIGURATIONP (obj))
1759 strout ("#<window-configuration>", -1, -1, printcharfun, 0);
1761 else if (FRAMEP (obj))
1763 strout ((FRAME_LIVE_P (XFRAME (obj))
1764 ? "#<frame " : "#<dead frame "),
1765 -1, -1, printcharfun, 0);
1766 print_string (XFRAME (obj)->name, printcharfun);
1767 sprintf (buf, " 0x%lx\\ ", (unsigned long) (XFRAME (obj)));
1768 strout (buf, -1, -1, printcharfun, 0);
1769 PRINTCHAR ('>');
1771 else
1773 int size = XVECTOR (obj)->size;
1774 if (COMPILEDP (obj))
1776 PRINTCHAR ('#');
1777 size &= PSEUDOVECTOR_SIZE_MASK;
1779 if (CHAR_TABLE_P (obj))
1781 /* We print a char-table as if it were a vector,
1782 lumping the parent and default slots in with the
1783 character slots. But we add #^ as a prefix. */
1784 PRINTCHAR ('#');
1785 PRINTCHAR ('^');
1786 if (SUB_CHAR_TABLE_P (obj))
1787 PRINTCHAR ('^');
1788 size &= PSEUDOVECTOR_SIZE_MASK;
1790 if (size & PSEUDOVECTOR_FLAG)
1791 goto badtype;
1793 PRINTCHAR ('[');
1795 register int i;
1796 register Lisp_Object tem;
1797 int real_size = size;
1799 /* Don't print more elements than the specified maximum. */
1800 if (NATNUMP (Vprint_length)
1801 && XFASTINT (Vprint_length) < size)
1802 size = XFASTINT (Vprint_length);
1804 for (i = 0; i < size; i++)
1806 if (i) PRINTCHAR (' ');
1807 tem = XVECTOR (obj)->contents[i];
1808 print_object (tem, printcharfun, escapeflag);
1810 if (size < real_size)
1811 strout (" ...", 4, 4, printcharfun, 0);
1813 PRINTCHAR (']');
1815 break;
1817 case Lisp_Misc:
1818 switch (XMISCTYPE (obj))
1820 case Lisp_Misc_Marker:
1821 strout ("#<marker ", -1, -1, printcharfun, 0);
1822 /* Do you think this is necessary? */
1823 if (XMARKER (obj)->insertion_type != 0)
1824 strout ("(before-insertion) ", -1, -1, printcharfun, 0);
1825 if (!(XMARKER (obj)->buffer))
1826 strout ("in no buffer", -1, -1, printcharfun, 0);
1827 else
1829 sprintf (buf, "at %d", marker_position (obj));
1830 strout (buf, -1, -1, printcharfun, 0);
1831 strout (" in ", -1, -1, printcharfun, 0);
1832 print_string (XMARKER (obj)->buffer->name, printcharfun);
1834 PRINTCHAR ('>');
1835 break;
1837 case Lisp_Misc_Overlay:
1838 strout ("#<overlay ", -1, -1, printcharfun, 0);
1839 if (!(XMARKER (OVERLAY_START (obj))->buffer))
1840 strout ("in no buffer", -1, -1, printcharfun, 0);
1841 else
1843 sprintf (buf, "from %d to %d in ",
1844 marker_position (OVERLAY_START (obj)),
1845 marker_position (OVERLAY_END (obj)));
1846 strout (buf, -1, -1, printcharfun, 0);
1847 print_string (XMARKER (OVERLAY_START (obj))->buffer->name,
1848 printcharfun);
1850 PRINTCHAR ('>');
1851 break;
1853 /* Remaining cases shouldn't happen in normal usage, but let's print
1854 them anyway for the benefit of the debugger. */
1855 case Lisp_Misc_Free:
1856 strout ("#<misc free cell>", -1, -1, printcharfun, 0);
1857 break;
1859 case Lisp_Misc_Intfwd:
1860 sprintf (buf, "#<intfwd to %d>", *XINTFWD (obj)->intvar);
1861 strout (buf, -1, -1, printcharfun, 0);
1862 break;
1864 case Lisp_Misc_Boolfwd:
1865 sprintf (buf, "#<boolfwd to %s>",
1866 (*XBOOLFWD (obj)->boolvar ? "t" : "nil"));
1867 strout (buf, -1, -1, printcharfun, 0);
1868 break;
1870 case Lisp_Misc_Objfwd:
1871 strout ("#<objfwd to ", -1, -1, printcharfun, 0);
1872 print_object (*XOBJFWD (obj)->objvar, printcharfun, escapeflag);
1873 PRINTCHAR ('>');
1874 break;
1876 case Lisp_Misc_Buffer_Objfwd:
1877 strout ("#<buffer_objfwd to ", -1, -1, printcharfun, 0);
1878 print_object (PER_BUFFER_VALUE (current_buffer,
1879 XBUFFER_OBJFWD (obj)->offset),
1880 printcharfun, escapeflag);
1881 PRINTCHAR ('>');
1882 break;
1884 case Lisp_Misc_Kboard_Objfwd:
1885 strout ("#<kboard_objfwd to ", -1, -1, printcharfun, 0);
1886 print_object (*(Lisp_Object *)((char *) current_kboard
1887 + XKBOARD_OBJFWD (obj)->offset),
1888 printcharfun, escapeflag);
1889 PRINTCHAR ('>');
1890 break;
1892 case Lisp_Misc_Buffer_Local_Value:
1893 strout ("#<buffer_local_value ", -1, -1, printcharfun, 0);
1894 goto do_buffer_local;
1895 case Lisp_Misc_Some_Buffer_Local_Value:
1896 strout ("#<some_buffer_local_value ", -1, -1, printcharfun, 0);
1897 do_buffer_local:
1898 strout ("[realvalue] ", -1, -1, printcharfun, 0);
1899 print_object (XBUFFER_LOCAL_VALUE (obj)->realvalue,
1900 printcharfun, escapeflag);
1901 if (XBUFFER_LOCAL_VALUE (obj)->found_for_buffer)
1902 strout ("[local in buffer] ", -1, -1, printcharfun, 0);
1903 else
1904 strout ("[buffer] ", -1, -1, printcharfun, 0);
1905 print_object (XBUFFER_LOCAL_VALUE (obj)->buffer,
1906 printcharfun, escapeflag);
1907 if (XBUFFER_LOCAL_VALUE (obj)->check_frame)
1909 if (XBUFFER_LOCAL_VALUE (obj)->found_for_frame)
1910 strout ("[local in frame] ", -1, -1, printcharfun, 0);
1911 else
1912 strout ("[frame] ", -1, -1, printcharfun, 0);
1913 print_object (XBUFFER_LOCAL_VALUE (obj)->frame,
1914 printcharfun, escapeflag);
1916 strout ("[alist-elt] ", -1, -1, printcharfun, 0);
1917 print_object (XCAR (XBUFFER_LOCAL_VALUE (obj)->cdr),
1918 printcharfun, escapeflag);
1919 strout ("[default-value] ", -1, -1, printcharfun, 0);
1920 print_object (XCDR (XBUFFER_LOCAL_VALUE (obj)->cdr),
1921 printcharfun, escapeflag);
1922 PRINTCHAR ('>');
1923 break;
1925 default:
1926 goto badtype;
1928 break;
1930 default:
1931 badtype:
1933 /* We're in trouble if this happens!
1934 Probably should just abort () */
1935 strout ("#<EMACS BUG: INVALID DATATYPE ", -1, -1, printcharfun, 0);
1936 if (MISCP (obj))
1937 sprintf (buf, "(MISC 0x%04x)", (int) XMISCTYPE (obj));
1938 else if (VECTORLIKEP (obj))
1939 sprintf (buf, "(PVEC 0x%08x)", (int) XVECTOR (obj)->size);
1940 else
1941 sprintf (buf, "(0x%02x)", (int) XTYPE (obj));
1942 strout (buf, -1, -1, printcharfun, 0);
1943 strout (" Save your buffers immediately and please report this bug>",
1944 -1, -1, printcharfun, 0);
1948 print_depth--;
1952 /* Print a description of INTERVAL using PRINTCHARFUN.
1953 This is part of printing a string that has text properties. */
1955 void
1956 print_interval (interval, printcharfun)
1957 INTERVAL interval;
1958 Lisp_Object printcharfun;
1960 PRINTCHAR (' ');
1961 print_object (make_number (interval->position), printcharfun, 1);
1962 PRINTCHAR (' ');
1963 print_object (make_number (interval->position + LENGTH (interval)),
1964 printcharfun, 1);
1965 PRINTCHAR (' ');
1966 print_object (interval->plist, printcharfun, 1);
1970 void
1971 syms_of_print ()
1973 Qtemp_buffer_setup_hook = intern ("temp-buffer-setup-hook");
1974 staticpro (&Qtemp_buffer_setup_hook);
1976 DEFVAR_LISP ("standard-output", &Vstandard_output,
1977 "Output stream `print' uses by default for outputting a character.\n\
1978 This may be any function of one argument.\n\
1979 It may also be a buffer (output is inserted before point)\n\
1980 or a marker (output is inserted and the marker is advanced)\n\
1981 or the symbol t (output appears in the echo area).");
1982 Vstandard_output = Qt;
1983 Qstandard_output = intern ("standard-output");
1984 staticpro (&Qstandard_output);
1986 DEFVAR_LISP ("float-output-format", &Vfloat_output_format,
1987 "The format descriptor string used to print floats.\n\
1988 This is a %-spec like those accepted by `printf' in C,\n\
1989 but with some restrictions. It must start with the two characters `%.'.\n\
1990 After that comes an integer precision specification,\n\
1991 and then a letter which controls the format.\n\
1992 The letters allowed are `e', `f' and `g'.\n\
1993 Use `e' for exponential notation \"DIG.DIGITSeEXPT\"\n\
1994 Use `f' for decimal point notation \"DIGITS.DIGITS\".\n\
1995 Use `g' to choose the shorter of those two formats for the number at hand.\n\
1996 The precision in any of these cases is the number of digits following\n\
1997 the decimal point. With `f', a precision of 0 means to omit the\n\
1998 decimal point. 0 is not allowed with `e' or `g'.\n\n\
1999 A value of nil means to use the shortest notation\n\
2000 that represents the number without losing information.");
2001 Vfloat_output_format = Qnil;
2002 Qfloat_output_format = intern ("float-output-format");
2003 staticpro (&Qfloat_output_format);
2005 DEFVAR_LISP ("print-length", &Vprint_length,
2006 "Maximum length of list to print before abbreviating.\n\
2007 A value of nil means no limit. See also `eval-expression-print-length'.");
2008 Vprint_length = Qnil;
2010 DEFVAR_LISP ("print-level", &Vprint_level,
2011 "Maximum depth of list nesting to print before abbreviating.\n\
2012 A value of nil means no limit. See also `eval-expression-print-level'.");
2013 Vprint_level = Qnil;
2015 DEFVAR_BOOL ("print-escape-newlines", &print_escape_newlines,
2016 "Non-nil means print newlines in strings as `\\n'.\n\
2017 Also print formfeeds as `\\f'.");
2018 print_escape_newlines = 0;
2020 DEFVAR_BOOL ("print-escape-nonascii", &print_escape_nonascii,
2021 "Non-nil means print unibyte non-ASCII chars in strings as \\OOO.\n\
2022 \(OOO is the octal representation of the character code.)\n\
2023 Only single-byte characters are affected, and only in `prin1'.");
2024 print_escape_nonascii = 0;
2026 DEFVAR_BOOL ("print-escape-multibyte", &print_escape_multibyte,
2027 "Non-nil means print multibyte characters in strings as \\xXXXX.\n\
2028 \(XXXX is the hex representation of the character code.)\n\
2029 This affects only `prin1'.");
2030 print_escape_multibyte = 0;
2032 DEFVAR_BOOL ("print-quoted", &print_quoted,
2033 "Non-nil means print quoted forms with reader syntax.\n\
2034 I.e., (quote foo) prints as 'foo, (function foo) as #'foo, and backquoted\n\
2035 forms print as in the new syntax.");
2036 print_quoted = 0;
2038 DEFVAR_LISP ("print-gensym", &Vprint_gensym,
2039 "Non-nil means print uninterned symbols so they will read as uninterned.\n\
2040 I.e., the value of (make-symbol \"foobar\") prints as #:foobar.\n\
2041 When the uninterned symbol appears within a recursive data structure,\n\
2042 and the symbol appears more than once, in addition use the #N# and #N=\n\
2043 constructs as needed, so that multiple references to the same symbol are\n\
2044 shared once again when the text is read back.");
2045 Vprint_gensym = Qnil;
2047 DEFVAR_LISP ("print-circle", &Vprint_circle,
2048 "*Non-nil means print recursive structures using #N= and #N# syntax.\n\
2049 If nil, printing proceeds recursively and may lead to\n\
2050 `max-lisp-eval-depth' being exceeded or an error may occur:\n\
2051 \"Apparently circular structure being printed.\" Also see\n\
2052 `print-length' and `print-level'.\n\
2053 If non-nil, shared substructures anywhere in the structure are printed\n\
2054 with `#N=' before the first occurrence (in the order of the print\n\
2055 representation) and `#N#' in place of each subsequent occurrence,\n\
2056 where N is a positive decimal integer.");
2057 Vprint_circle = Qnil;
2059 DEFVAR_LISP ("print-continuous-numbering", &Vprint_continuous_numbering,
2060 "*Non-nil means number continuously across print calls.\n\
2061 This affects the numbers printed for #N= labels and #M# references.\n\
2062 See also `print-circle', `print-gensym', and `print-number-table'.\n\
2063 This variable should not be set with `setq'; bind it with a `let' instead.");
2064 Vprint_continuous_numbering = Qnil;
2066 DEFVAR_LISP ("print-number-table", &Vprint_number_table,
2067 "A vector used internally to produce `#N=' labels and `#N#' references.\n\
2068 The Lisp printer uses this vector to detect Lisp objects referenced more\n\
2069 than once. When `print-continuous-numbering' is bound to t, you should\n\
2070 probably also bind `print-number-table' to nil. This ensures that the\n\
2071 value of `print-number-table' can be garbage-collected once the printing\n\
2072 is done.");
2073 Vprint_number_table = Qnil;
2075 /* prin1_to_string_buffer initialized in init_buffer_once in buffer.c */
2076 staticpro (&Vprin1_to_string_buffer);
2078 defsubr (&Sprin1);
2079 defsubr (&Sprin1_to_string);
2080 defsubr (&Serror_message_string);
2081 defsubr (&Sprinc);
2082 defsubr (&Sprint);
2083 defsubr (&Sterpri);
2084 defsubr (&Swrite_char);
2085 defsubr (&Sexternal_debugging_output);
2087 Qexternal_debugging_output = intern ("external-debugging-output");
2088 staticpro (&Qexternal_debugging_output);
2090 Qprint_escape_newlines = intern ("print-escape-newlines");
2091 staticpro (&Qprint_escape_newlines);
2093 Qprint_escape_multibyte = intern ("print-escape-multibyte");
2094 staticpro (&Qprint_escape_multibyte);
2096 Qprint_escape_nonascii = intern ("print-escape-nonascii");
2097 staticpro (&Qprint_escape_nonascii);
2099 defsubr (&Swith_output_to_temp_buffer);