Don't say "X Windows". From Colin Walters <walters@cis.ohio-state.edu>.
[emacs.git] / src / print.c
blob10e1df934ebb30fa9dcf07941b812bb205cfae60
1 /* Lisp object printing and output streams.
2 Copyright (C) 1985, 86, 88, 93, 94, 95, 97, 98, 1999, 2000
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))
468 chars = multibyte_chars_in_text (XSTRING (string)->data,
469 STRING_BYTES (XSTRING (string)));
470 else
471 chars = STRING_BYTES (XSTRING (string));
473 /* strout is safe for output to a frame (echo area) or to print_buffer. */
474 strout (XSTRING (string)->data,
475 chars, STRING_BYTES (XSTRING (string)),
476 printcharfun, STRING_MULTIBYTE (string));
478 else
480 /* Otherwise, string may be relocated by printing one char.
481 So re-fetch the string address for each character. */
482 int i;
483 int size = XSTRING (string)->size;
484 int size_byte = STRING_BYTES (XSTRING (string));
485 struct gcpro gcpro1;
486 GCPRO1 (string);
487 if (size == size_byte)
488 for (i = 0; i < size; i++)
489 PRINTCHAR (XSTRING (string)->data[i]);
490 else
491 for (i = 0; i < size_byte; i++)
493 /* Here, we must convert each multi-byte form to the
494 corresponding character code before handing it to PRINTCHAR. */
495 int len;
496 int ch = STRING_CHAR_AND_LENGTH (XSTRING (string)->data + i,
497 size_byte - i, len);
498 if (!CHAR_VALID_P (ch, 0))
500 ch = XSTRING (string)->data[i];
501 len = 1;
503 PRINTCHAR (ch);
504 i += len;
506 UNGCPRO;
510 DEFUN ("write-char", Fwrite_char, Swrite_char, 1, 2, 0,
511 "Output character CHARACTER to stream PRINTCHARFUN.\n\
512 PRINTCHARFUN defaults to the value of `standard-output' (which see).")
513 (character, printcharfun)
514 Lisp_Object character, printcharfun;
516 PRINTDECLARE;
518 if (NILP (printcharfun))
519 printcharfun = Vstandard_output;
520 CHECK_NUMBER (character, 0);
521 PRINTPREPARE;
522 PRINTCHAR (XINT (character));
523 PRINTFINISH;
524 return character;
527 /* Used from outside of print.c to print a block of SIZE
528 single-byte chars at DATA on the default output stream.
529 Do not use this on the contents of a Lisp string. */
531 void
532 write_string (data, size)
533 char *data;
534 int size;
536 PRINTDECLARE;
537 Lisp_Object printcharfun;
539 printcharfun = Vstandard_output;
541 PRINTPREPARE;
542 strout (data, size, size, printcharfun, 0);
543 PRINTFINISH;
546 /* Used from outside of print.c to print a block of SIZE
547 single-byte chars at DATA on a specified stream PRINTCHARFUN.
548 Do not use this on the contents of a Lisp string. */
550 void
551 write_string_1 (data, size, printcharfun)
552 char *data;
553 int size;
554 Lisp_Object printcharfun;
556 PRINTDECLARE;
558 PRINTPREPARE;
559 strout (data, size, size, printcharfun, 0);
560 PRINTFINISH;
564 void
565 temp_output_buffer_setup (bufname)
566 char *bufname;
568 int count = specpdl_ptr - specpdl;
569 register struct buffer *old = current_buffer;
570 register Lisp_Object buf;
572 record_unwind_protect (set_buffer_if_live, Fcurrent_buffer ());
574 Fset_buffer (Fget_buffer_create (build_string (bufname)));
576 current_buffer->directory = old->directory;
577 current_buffer->read_only = Qnil;
578 current_buffer->filename = Qnil;
579 current_buffer->undo_list = Qt;
580 current_buffer->overlays_before = Qnil;
581 current_buffer->overlays_after = Qnil;
582 current_buffer->enable_multibyte_characters
583 = buffer_defaults.enable_multibyte_characters;
584 Ferase_buffer ();
585 XSETBUFFER (buf, current_buffer);
587 if (!NILP (Vrun_hooks))
588 call1 (Vrun_hooks, Qtemp_buffer_setup_hook);
590 unbind_to (count, Qnil);
592 specbind (Qstandard_output, buf);
595 Lisp_Object
596 internal_with_output_to_temp_buffer (bufname, function, args)
597 char *bufname;
598 Lisp_Object (*function) P_ ((Lisp_Object));
599 Lisp_Object args;
601 int count = specpdl_ptr - specpdl;
602 Lisp_Object buf, val;
603 struct gcpro gcpro1;
605 GCPRO1 (args);
606 record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
607 temp_output_buffer_setup (bufname);
608 buf = Vstandard_output;
609 UNGCPRO;
611 val = (*function) (args);
613 GCPRO1 (val);
614 temp_output_buffer_show (buf);
615 UNGCPRO;
617 return unbind_to (count, val);
620 DEFUN ("with-output-to-temp-buffer", Fwith_output_to_temp_buffer, Swith_output_to_temp_buffer,
621 1, UNEVALLED, 0,
622 "Bind `standard-output' to buffer BUFNAME, eval BODY, then show that buffer.\n\
623 The buffer is cleared out initially, and marked as unmodified when done.\n\
624 All output done by BODY is inserted in that buffer by default.\n\
625 The buffer is displayed in another window, but not selected.\n\
626 The value of the last form in BODY is returned.\n\
627 If BODY does not finish normally, the buffer BUFNAME is not displayed.\n\
629 The hook `temp-buffer-setup-hook' is run before BODY,\n\
630 with the buffer BUFNAME temporarily current.\n\
631 The hook `temp-buffer-show-hook' is run after the buffer is displayed,\n\
632 with the buffer temporarily current, and the window that was used\n\
633 to display it temporarily selected.\n\
635 If variable `temp-buffer-show-function' is non-nil, call it at the end\n\
636 to get the buffer displayed instead of just displaying the non-selected\n\
637 buffer and calling the hook. It gets one argument, the buffer to display.")
638 (args)
639 Lisp_Object args;
641 struct gcpro gcpro1;
642 Lisp_Object name;
643 int count = specpdl_ptr - specpdl;
644 Lisp_Object buf, val;
646 GCPRO1(args);
647 name = Feval (Fcar (args));
648 UNGCPRO;
650 CHECK_STRING (name, 0);
651 temp_output_buffer_setup (XSTRING (name)->data);
652 buf = Vstandard_output;
654 val = Fprogn (Fcdr (args));
656 temp_output_buffer_show (buf);
658 return unbind_to (count, val);
662 static void print ();
663 static void print_preprocess ();
664 static void print_preprocess_string ();
665 static void print_object ();
667 DEFUN ("terpri", Fterpri, Sterpri, 0, 1, 0,
668 "Output a newline to stream PRINTCHARFUN.\n\
669 If PRINTCHARFUN is omitted or nil, the value of `standard-output' is used.")
670 (printcharfun)
671 Lisp_Object printcharfun;
673 PRINTDECLARE;
675 if (NILP (printcharfun))
676 printcharfun = Vstandard_output;
677 PRINTPREPARE;
678 PRINTCHAR ('\n');
679 PRINTFINISH;
680 return Qt;
683 DEFUN ("prin1", Fprin1, Sprin1, 1, 2, 0,
684 "Output the printed representation of OBJECT, any Lisp object.\n\
685 Quoting characters are printed when needed to make output that `read'\n\
686 can handle, whenever this is possible.\n\
687 Output stream is PRINTCHARFUN, or value of `standard-output' (which see).")
688 (object, printcharfun)
689 Lisp_Object object, printcharfun;
691 PRINTDECLARE;
693 #ifdef MAX_PRINT_CHARS
694 max_print = 0;
695 #endif /* MAX_PRINT_CHARS */
696 if (NILP (printcharfun))
697 printcharfun = Vstandard_output;
698 PRINTPREPARE;
699 print (object, printcharfun, 1);
700 PRINTFINISH;
701 return object;
704 /* a buffer which is used to hold output being built by prin1-to-string */
705 Lisp_Object Vprin1_to_string_buffer;
707 DEFUN ("prin1-to-string", Fprin1_to_string, Sprin1_to_string, 1, 2, 0,
708 "Return a string containing the printed representation of OBJECT,\n\
709 any Lisp object. Quoting characters are used when needed to make output\n\
710 that `read' can handle, whenever this is possible, unless the optional\n\
711 second argument NOESCAPE is non-nil.")
712 (object, noescape)
713 Lisp_Object object, noescape;
715 PRINTDECLARE;
716 Lisp_Object printcharfun;
717 struct gcpro gcpro1, gcpro2;
718 Lisp_Object tem;
720 /* Save and restore this--we are altering a buffer
721 but we don't want to deactivate the mark just for that.
722 No need for specbind, since errors deactivate the mark. */
723 tem = Vdeactivate_mark;
724 GCPRO2 (object, tem);
726 printcharfun = Vprin1_to_string_buffer;
727 PRINTPREPARE;
728 print (object, printcharfun, NILP (noescape));
729 /* Make Vprin1_to_string_buffer be the default buffer after PRINTFINSH */
730 PRINTFINISH;
731 set_buffer_internal (XBUFFER (Vprin1_to_string_buffer));
732 object = Fbuffer_string ();
734 Ferase_buffer ();
735 set_buffer_internal (old);
737 Vdeactivate_mark = tem;
738 UNGCPRO;
740 return object;
743 DEFUN ("princ", Fprinc, Sprinc, 1, 2, 0,
744 "Output the printed representation of OBJECT, any Lisp object.\n\
745 No quoting characters are used; no delimiters are printed around\n\
746 the contents of strings.\n\
747 Output stream is PRINTCHARFUN, or value of standard-output (which see).")
748 (object, printcharfun)
749 Lisp_Object object, printcharfun;
751 PRINTDECLARE;
753 if (NILP (printcharfun))
754 printcharfun = Vstandard_output;
755 PRINTPREPARE;
756 print (object, printcharfun, 0);
757 PRINTFINISH;
758 return object;
761 DEFUN ("print", Fprint, Sprint, 1, 2, 0,
762 "Output the printed representation of OBJECT, with newlines around it.\n\
763 Quoting characters are printed when needed to make output that `read'\n\
764 can handle, whenever this is possible.\n\
765 Output stream is PRINTCHARFUN, or value of `standard-output' (which see).")
766 (object, printcharfun)
767 Lisp_Object object, printcharfun;
769 PRINTDECLARE;
770 struct gcpro gcpro1;
772 #ifdef MAX_PRINT_CHARS
773 print_chars = 0;
774 max_print = MAX_PRINT_CHARS;
775 #endif /* MAX_PRINT_CHARS */
776 if (NILP (printcharfun))
777 printcharfun = Vstandard_output;
778 GCPRO1 (object);
779 PRINTPREPARE;
780 PRINTCHAR ('\n');
781 print (object, printcharfun, 1);
782 PRINTCHAR ('\n');
783 PRINTFINISH;
784 #ifdef MAX_PRINT_CHARS
785 max_print = 0;
786 print_chars = 0;
787 #endif /* MAX_PRINT_CHARS */
788 UNGCPRO;
789 return object;
792 /* The subroutine object for external-debugging-output is kept here
793 for the convenience of the debugger. */
794 Lisp_Object Qexternal_debugging_output;
796 DEFUN ("external-debugging-output", Fexternal_debugging_output, Sexternal_debugging_output, 1, 1, 0,
797 "Write CHARACTER to stderr.\n\
798 You can call print while debugging emacs, and pass it this function\n\
799 to make it write to the debugging output.\n")
800 (character)
801 Lisp_Object character;
803 CHECK_NUMBER (character, 0);
804 putc (XINT (character), stderr);
806 #ifdef WINDOWSNT
807 /* Send the output to a debugger (nothing happens if there isn't one). */
809 char buf[2] = {(char) XINT (character), '\0'};
810 OutputDebugString (buf);
812 #endif
814 return character;
817 /* This is the interface for debugging printing. */
819 void
820 debug_print (arg)
821 Lisp_Object arg;
823 Fprin1 (arg, Qexternal_debugging_output);
824 fprintf (stderr, "\r\n");
827 DEFUN ("error-message-string", Ferror_message_string, Serror_message_string,
828 1, 1, 0,
829 "Convert an error value (ERROR-SYMBOL . DATA) to an error message.")
830 (obj)
831 Lisp_Object obj;
833 struct buffer *old = current_buffer;
834 Lisp_Object value;
835 struct gcpro gcpro1;
837 /* If OBJ is (error STRING), just return STRING.
838 That is not only faster, it also avoids the need to allocate
839 space here when the error is due to memory full. */
840 if (CONSP (obj) && EQ (XCAR (obj), Qerror)
841 && CONSP (XCDR (obj))
842 && STRINGP (XCAR (XCDR (obj)))
843 && NILP (XCDR (XCDR (obj))))
844 return XCAR (XCDR (obj));
846 print_error_message (obj, Vprin1_to_string_buffer);
848 set_buffer_internal (XBUFFER (Vprin1_to_string_buffer));
849 value = Fbuffer_string ();
851 GCPRO1 (value);
852 Ferase_buffer ();
853 set_buffer_internal (old);
854 UNGCPRO;
856 return value;
859 /* Print an error message for the error DATA onto Lisp output stream
860 STREAM (suitable for the print functions). */
862 void
863 print_error_message (data, stream)
864 Lisp_Object data, stream;
866 Lisp_Object errname, errmsg, file_error, tail;
867 struct gcpro gcpro1;
868 int i;
870 errname = Fcar (data);
872 if (EQ (errname, Qerror))
874 data = Fcdr (data);
875 if (!CONSP (data))
876 data = Qnil;
877 errmsg = Fcar (data);
878 file_error = Qnil;
880 else
882 Lisp_Object error_conditions;
883 errmsg = Fget (errname, Qerror_message);
884 error_conditions = Fget (errname, Qerror_conditions);
885 file_error = Fmemq (Qfile_error, error_conditions);
888 /* Print an error message including the data items. */
890 tail = Fcdr_safe (data);
891 GCPRO1 (tail);
893 /* If we know from where the error was signaled, show it in
894 *Messages*. */
895 if (!NILP (Vsignaling_function) && SYMBOLP (Vsignaling_function))
897 char *name = XSYMBOL (Vsignaling_function)->name->data;
898 message_dolog (name, strlen (name), 0, 0);
899 message_dolog (": ", 2, 0, 0);
900 Vsignaling_function = Qnil;
903 /* For file-error, make error message by concatenating
904 all the data items. They are all strings. */
905 if (!NILP (file_error) && CONSP (tail))
906 errmsg = XCAR (tail), tail = XCDR (tail);
908 if (STRINGP (errmsg))
909 Fprinc (errmsg, stream);
910 else
911 write_string_1 ("peculiar error", -1, stream);
913 for (i = 0; CONSP (tail); tail = XCDR (tail), i++)
915 Lisp_Object obj;
917 write_string_1 (i ? ", " : ": ", 2, stream);
918 obj = XCAR (tail);
919 if (!NILP (file_error) || EQ (errname, Qend_of_file))
920 Fprinc (obj, stream);
921 else
922 Fprin1 (obj, stream);
925 UNGCPRO;
931 * The buffer should be at least as large as the max string size of the
932 * largest float, printed in the biggest notation. This is undoubtedly
933 * 20d float_output_format, with the negative of the C-constant "HUGE"
934 * from <math.h>.
936 * On the vax the worst case is -1e38 in 20d format which takes 61 bytes.
938 * I assume that IEEE-754 format numbers can take 329 bytes for the worst
939 * case of -1e307 in 20d float_output_format. What is one to do (short of
940 * re-writing _doprnt to be more sane)?
941 * -wsr
944 void
945 float_to_string (buf, data)
946 unsigned char *buf;
947 double data;
949 unsigned char *cp;
950 int width;
952 /* Check for plus infinity in a way that won't lose
953 if there is no plus infinity. */
954 if (data == data / 2 && data > 1.0)
956 strcpy (buf, "1.0e+INF");
957 return;
959 /* Likewise for minus infinity. */
960 if (data == data / 2 && data < -1.0)
962 strcpy (buf, "-1.0e+INF");
963 return;
965 /* Check for NaN in a way that won't fail if there are no NaNs. */
966 if (! (data * 0.0 >= 0.0))
968 /* Prepend "-" if the NaN's sign bit is negative.
969 The sign bit of a double is the bit that is 1 in -0.0. */
970 int i;
971 union { double d; char c[sizeof (double)]; } u_data, u_minus_zero;
972 u_data.d = data;
973 u_minus_zero.d = - 0.0;
974 for (i = 0; i < sizeof (double); i++)
975 if (u_data.c[i] & u_minus_zero.c[i])
977 *buf++ = '-';
978 break;
981 strcpy (buf, "0.0e+NaN");
982 return;
985 if (NILP (Vfloat_output_format)
986 || !STRINGP (Vfloat_output_format))
987 lose:
989 /* Generate the fewest number of digits that represent the
990 floating point value without losing information.
991 The following method is simple but a bit slow.
992 For ideas about speeding things up, please see:
994 Guy L Steele Jr & Jon L White, How to print floating-point numbers
995 accurately. SIGPLAN notices 25, 6 (June 1990), 112-126.
997 Robert G Burger & R Kent Dybvig, Printing floating point numbers
998 quickly and accurately, SIGPLAN notices 31, 5 (May 1996), 108-116. */
1000 width = fabs (data) < DBL_MIN ? 1 : DBL_DIG;
1002 sprintf (buf, "%.*g", width, data);
1003 while (width++ < DOUBLE_DIGITS_BOUND && atof (buf) != data);
1005 else /* oink oink */
1007 /* Check that the spec we have is fully valid.
1008 This means not only valid for printf,
1009 but meant for floats, and reasonable. */
1010 cp = XSTRING (Vfloat_output_format)->data;
1012 if (cp[0] != '%')
1013 goto lose;
1014 if (cp[1] != '.')
1015 goto lose;
1017 cp += 2;
1019 /* Check the width specification. */
1020 width = -1;
1021 if ('0' <= *cp && *cp <= '9')
1023 width = 0;
1025 width = (width * 10) + (*cp++ - '0');
1026 while (*cp >= '0' && *cp <= '9');
1028 /* A precision of zero is valid only for %f. */
1029 if (width > DBL_DIG
1030 || (width == 0 && *cp != 'f'))
1031 goto lose;
1034 if (*cp != 'e' && *cp != 'f' && *cp != 'g')
1035 goto lose;
1037 if (cp[1] != 0)
1038 goto lose;
1040 sprintf (buf, XSTRING (Vfloat_output_format)->data, data);
1043 /* Make sure there is a decimal point with digit after, or an
1044 exponent, so that the value is readable as a float. But don't do
1045 this with "%.0f"; it's valid for that not to produce a decimal
1046 point. Note that width can be 0 only for %.0f. */
1047 if (width != 0)
1049 for (cp = buf; *cp; cp++)
1050 if ((*cp < '0' || *cp > '9') && *cp != '-')
1051 break;
1053 if (*cp == '.' && cp[1] == 0)
1055 cp[1] = '0';
1056 cp[2] = 0;
1059 if (*cp == 0)
1061 *cp++ = '.';
1062 *cp++ = '0';
1063 *cp++ = 0;
1069 static void
1070 print (obj, printcharfun, escapeflag)
1071 Lisp_Object obj;
1072 register Lisp_Object printcharfun;
1073 int escapeflag;
1075 print_depth = 0;
1077 /* Reset print_number_index and Vprint_number_table only when
1078 the variable Vprint_continuous_numbering is nil. Otherwise,
1079 the values of these variables will be kept between several
1080 print functions. */
1081 if (NILP (Vprint_continuous_numbering))
1083 print_number_index = 0;
1084 Vprint_number_table = Qnil;
1087 /* Construct Vprint_number_table for print-gensym and print-circle. */
1088 if (!NILP (Vprint_gensym) || !NILP (Vprint_circle))
1090 int i, start, index;
1091 /* Construct Vprint_number_table. */
1092 start = index = print_number_index;
1093 print_preprocess (obj);
1094 /* Remove unnecessary objects, which appear only once in OBJ;
1095 that is, whose status is Qnil. */
1096 for (i = start; i < print_number_index; i++)
1097 if (!NILP (PRINT_NUMBER_STATUS (Vprint_number_table, i)))
1099 PRINT_NUMBER_OBJECT (Vprint_number_table, index)
1100 = PRINT_NUMBER_OBJECT (Vprint_number_table, i);
1101 /* Reset the status field for the next print step. Now this
1102 field means whether the object has already been printed. */
1103 PRINT_NUMBER_STATUS (Vprint_number_table, index) = Qnil;
1104 index++;
1106 print_number_index = index;
1109 print_object (obj, printcharfun, escapeflag);
1112 /* Construct Vprint_number_table according to the structure of OBJ.
1113 OBJ itself and all its elements will be added to Vprint_number_table
1114 recursively if it is a list, vector, compiled function, char-table,
1115 string (its text properties will be traced), or a symbol that has
1116 no obarray (this is for the print-gensym feature).
1117 The status fields of Vprint_number_table mean whether each object appears
1118 more than once in OBJ: Qnil at the first time, and Qt after that . */
1119 static void
1120 print_preprocess (obj)
1121 Lisp_Object obj;
1123 int i, size;
1125 loop:
1126 if (STRINGP (obj) || CONSP (obj) || VECTORP (obj)
1127 || COMPILEDP (obj) || CHAR_TABLE_P (obj)
1128 || (! NILP (Vprint_gensym)
1129 && SYMBOLP (obj) && NILP (XSYMBOL (obj)->obarray)))
1131 /* In case print-circle is nil and print-gensym is t,
1132 add OBJ to Vprint_number_table only when OBJ is a symbol. */
1133 if (! NILP (Vprint_circle) || SYMBOLP (obj))
1135 for (i = 0; i < print_number_index; i++)
1136 if (EQ (PRINT_NUMBER_OBJECT (Vprint_number_table, i), obj))
1138 /* OBJ appears more than once. Let's remember that. */
1139 PRINT_NUMBER_STATUS (Vprint_number_table, i) = Qt;
1140 return;
1143 /* OBJ is not yet recorded. Let's add to the table. */
1144 if (print_number_index == 0)
1146 /* Initialize the table. */
1147 Vprint_number_table = Fmake_vector (make_number (40), Qnil);
1149 else if (XVECTOR (Vprint_number_table)->size == print_number_index * 2)
1151 /* Reallocate the table. */
1152 int i = print_number_index * 4;
1153 Lisp_Object old_table = Vprint_number_table;
1154 Vprint_number_table = Fmake_vector (make_number (i), Qnil);
1155 for (i = 0; i < print_number_index; i++)
1157 PRINT_NUMBER_OBJECT (Vprint_number_table, i)
1158 = PRINT_NUMBER_OBJECT (old_table, i);
1159 PRINT_NUMBER_STATUS (Vprint_number_table, i)
1160 = PRINT_NUMBER_STATUS (old_table, i);
1163 PRINT_NUMBER_OBJECT (Vprint_number_table, print_number_index) = obj;
1164 /* If Vprint_continuous_numbering is non-nil and OBJ is a gensym,
1165 always print the gensym with a number. This is a special for
1166 the lisp function byte-compile-output-docform. */
1167 if (! NILP (Vprint_continuous_numbering) && SYMBOLP (obj)
1168 && NILP (XSYMBOL (obj)->obarray))
1169 PRINT_NUMBER_STATUS (Vprint_number_table, print_number_index) = Qt;
1170 print_number_index++;
1173 switch (XGCTYPE (obj))
1175 case Lisp_String:
1176 /* A string may have text properties, which can be circular. */
1177 traverse_intervals (XSTRING (obj)->intervals, 0, 0,
1178 print_preprocess_string, Qnil);
1179 break;
1181 case Lisp_Cons:
1182 print_preprocess (XCAR (obj));
1183 obj = XCDR (obj);
1184 goto loop;
1186 case Lisp_Vectorlike:
1187 size = XVECTOR (obj)->size & PSEUDOVECTOR_SIZE_MASK;
1188 for (i = 0; i < size; i++)
1189 print_preprocess (XVECTOR (obj)->contents[i]);
1190 break;
1192 default:
1193 break;
1198 static void
1199 print_preprocess_string (interval, arg)
1200 INTERVAL interval;
1201 Lisp_Object arg;
1203 print_preprocess (interval->plist);
1206 static void
1207 print_object (obj, printcharfun, escapeflag)
1208 Lisp_Object obj;
1209 register Lisp_Object printcharfun;
1210 int escapeflag;
1212 char buf[30];
1214 QUIT;
1216 /* Detect circularities and truncate them. */
1217 if (STRINGP (obj) || CONSP (obj) || VECTORP (obj)
1218 || COMPILEDP (obj) || CHAR_TABLE_P (obj)
1219 || (! NILP (Vprint_gensym)
1220 && SYMBOLP (obj) && NILP (XSYMBOL (obj)->obarray)))
1222 if (NILP (Vprint_circle) && NILP (Vprint_gensym))
1224 /* Simple but incomplete way. */
1225 int i;
1226 for (i = 0; i < print_depth; i++)
1227 if (EQ (obj, being_printed[i]))
1229 sprintf (buf, "#%d", i);
1230 strout (buf, -1, -1, printcharfun, 0);
1231 return;
1233 being_printed[print_depth] = obj;
1235 else
1237 /* With the print-circle feature. */
1238 int i;
1239 for (i = 0; i < print_number_index; i++)
1240 if (EQ (PRINT_NUMBER_OBJECT (Vprint_number_table, i), obj))
1242 if (NILP (PRINT_NUMBER_STATUS (Vprint_number_table, i)))
1244 /* Add a prefix #n= if OBJ has not yet been printed;
1245 that is, its status field is nil. */
1246 sprintf (buf, "#%d=", i + 1);
1247 strout (buf, -1, -1, printcharfun, 0);
1248 /* OBJ is going to be printed. Set the status to t. */
1249 PRINT_NUMBER_STATUS (Vprint_number_table, i) = Qt;
1250 break;
1252 else
1254 /* Just print #n# if OBJ has already been printed. */
1255 sprintf (buf, "#%d#", i + 1);
1256 strout (buf, -1, -1, printcharfun, 0);
1257 return;
1263 print_depth++;
1265 if (print_depth > PRINT_CIRCLE)
1266 error ("Apparently circular structure being printed");
1267 #ifdef MAX_PRINT_CHARS
1268 if (max_print && print_chars > max_print)
1270 PRINTCHAR ('\n');
1271 print_chars = 0;
1273 #endif /* MAX_PRINT_CHARS */
1275 switch (XGCTYPE (obj))
1277 case Lisp_Int:
1278 if (sizeof (int) == sizeof (EMACS_INT))
1279 sprintf (buf, "%d", XINT (obj));
1280 else if (sizeof (long) == sizeof (EMACS_INT))
1281 sprintf (buf, "%ld", (long) XINT (obj));
1282 else
1283 abort ();
1284 strout (buf, -1, -1, printcharfun, 0);
1285 break;
1287 case Lisp_Float:
1289 char pigbuf[350]; /* see comments in float_to_string */
1291 float_to_string (pigbuf, XFLOAT_DATA (obj));
1292 strout (pigbuf, -1, -1, printcharfun, 0);
1294 break;
1296 case Lisp_String:
1297 if (!escapeflag)
1298 print_string (obj, printcharfun);
1299 else
1301 register int i, i_byte;
1302 struct gcpro gcpro1;
1303 unsigned char *str;
1304 int size_byte;
1305 /* 1 means we must ensure that the next character we output
1306 cannot be taken as part of a hex character escape. */
1307 int need_nonhex = 0;
1309 GCPRO1 (obj);
1311 if (!NULL_INTERVAL_P (XSTRING (obj)->intervals))
1313 PRINTCHAR ('#');
1314 PRINTCHAR ('(');
1317 PRINTCHAR ('\"');
1318 str = XSTRING (obj)->data;
1319 size_byte = STRING_BYTES (XSTRING (obj));
1321 for (i = 0, i_byte = 0; i_byte < size_byte;)
1323 /* Here, we must convert each multi-byte form to the
1324 corresponding character code before handing it to PRINTCHAR. */
1325 int len;
1326 int c;
1328 if (STRING_MULTIBYTE (obj))
1330 c = STRING_CHAR_AND_LENGTH (str + i_byte,
1331 size_byte - i_byte, len);
1332 if (CHAR_VALID_P (c, 0))
1333 i_byte += len;
1334 else
1335 c = str[i_byte++];
1337 else
1338 c = str[i_byte++];
1340 QUIT;
1342 if (c == '\n' && print_escape_newlines)
1344 PRINTCHAR ('\\');
1345 PRINTCHAR ('n');
1347 else if (c == '\f' && print_escape_newlines)
1349 PRINTCHAR ('\\');
1350 PRINTCHAR ('f');
1352 else if (! SINGLE_BYTE_CHAR_P (c) && print_escape_multibyte)
1354 /* When multibyte is disabled,
1355 print multibyte string chars using hex escapes. */
1356 unsigned char outbuf[50];
1357 sprintf (outbuf, "\\x%x", c);
1358 strout (outbuf, -1, -1, printcharfun, 0);
1359 need_nonhex = 1;
1361 else if (SINGLE_BYTE_CHAR_P (c) && ! ASCII_BYTE_P (c)
1362 && print_escape_nonascii)
1364 /* When printing in a multibyte buffer
1365 or when explicitly requested,
1366 print single-byte non-ASCII string chars
1367 using octal escapes. */
1368 unsigned char outbuf[5];
1369 sprintf (outbuf, "\\%03o", c);
1370 strout (outbuf, -1, -1, printcharfun, 0);
1372 else
1374 /* If we just had a hex escape, and this character
1375 could be taken as part of it,
1376 output `\ ' to prevent that. */
1377 if (need_nonhex)
1379 need_nonhex = 0;
1380 if ((c >= 'a' && c <= 'f')
1381 || (c >= 'A' && c <= 'F')
1382 || (c >= '0' && c <= '9'))
1383 strout ("\\ ", -1, -1, printcharfun, 0);
1386 if (c == '\"' || c == '\\')
1387 PRINTCHAR ('\\');
1388 PRINTCHAR (c);
1391 PRINTCHAR ('\"');
1393 if (!NULL_INTERVAL_P (XSTRING (obj)->intervals))
1395 traverse_intervals (XSTRING (obj)->intervals,
1396 0, 0, print_interval, printcharfun);
1397 PRINTCHAR (')');
1400 UNGCPRO;
1402 break;
1404 case Lisp_Symbol:
1406 register int confusing;
1407 register unsigned char *p = XSYMBOL (obj)->name->data;
1408 register unsigned char *end = p + STRING_BYTES (XSYMBOL (obj)->name);
1409 register int c;
1410 int i, i_byte, size_byte;
1411 Lisp_Object name;
1413 XSETSTRING (name, XSYMBOL (obj)->name);
1415 if (p != end && (*p == '-' || *p == '+')) p++;
1416 if (p == end)
1417 confusing = 0;
1418 /* If symbol name begins with a digit, and ends with a digit,
1419 and contains nothing but digits and `e', it could be treated
1420 as a number. So set CONFUSING.
1422 Symbols that contain periods could also be taken as numbers,
1423 but periods are always escaped, so we don't have to worry
1424 about them here. */
1425 else if (*p >= '0' && *p <= '9'
1426 && end[-1] >= '0' && end[-1] <= '9')
1428 while (p != end && ((*p >= '0' && *p <= '9')
1429 /* Needed for \2e10. */
1430 || *p == 'e'))
1431 p++;
1432 confusing = (end == p);
1434 else
1435 confusing = 0;
1437 if (! NILP (Vprint_gensym) && NILP (XSYMBOL (obj)->obarray))
1439 PRINTCHAR ('#');
1440 PRINTCHAR (':');
1443 size_byte = STRING_BYTES (XSTRING (name));
1445 for (i = 0, i_byte = 0; i_byte < size_byte;)
1447 /* Here, we must convert each multi-byte form to the
1448 corresponding character code before handing it to PRINTCHAR. */
1449 FETCH_STRING_CHAR_ADVANCE (c, name, i, i_byte);
1450 QUIT;
1452 if (escapeflag)
1454 if (c == '\"' || c == '\\' || c == '\''
1455 || c == ';' || c == '#' || c == '(' || c == ')'
1456 || c == ',' || c =='.' || c == '`'
1457 || c == '[' || c == ']' || c == '?' || c <= 040
1458 || confusing)
1459 PRINTCHAR ('\\'), confusing = 0;
1461 PRINTCHAR (c);
1464 break;
1466 case Lisp_Cons:
1467 /* If deeper than spec'd depth, print placeholder. */
1468 if (INTEGERP (Vprint_level)
1469 && print_depth > XINT (Vprint_level))
1470 strout ("...", -1, -1, printcharfun, 0);
1471 else if (print_quoted && CONSP (XCDR (obj)) && NILP (XCDR (XCDR (obj)))
1472 && (EQ (XCAR (obj), Qquote)))
1474 PRINTCHAR ('\'');
1475 print_object (XCAR (XCDR (obj)), printcharfun, escapeflag);
1477 else if (print_quoted && CONSP (XCDR (obj)) && NILP (XCDR (XCDR (obj)))
1478 && (EQ (XCAR (obj), Qfunction)))
1480 PRINTCHAR ('#');
1481 PRINTCHAR ('\'');
1482 print_object (XCAR (XCDR (obj)), printcharfun, escapeflag);
1484 else if (print_quoted && CONSP (XCDR (obj)) && NILP (XCDR (XCDR (obj)))
1485 && ((EQ (XCAR (obj), Qbackquote)
1486 || EQ (XCAR (obj), Qcomma)
1487 || EQ (XCAR (obj), Qcomma_at)
1488 || EQ (XCAR (obj), Qcomma_dot))))
1490 print_object (XCAR (obj), printcharfun, 0);
1491 print_object (XCAR (XCDR (obj)), printcharfun, escapeflag);
1493 else
1495 PRINTCHAR ('(');
1497 int print_length, i;
1498 Lisp_Object halftail = obj;
1500 /* Negative values of print-length are invalid in CL.
1501 Treat them like nil, as CMUCL does. */
1502 if (NATNUMP (Vprint_length))
1503 print_length = XFASTINT (Vprint_length);
1504 else
1505 print_length = 0;
1507 i = 0;
1508 while (CONSP (obj))
1510 /* Detect circular list. */
1511 if (NILP (Vprint_circle))
1513 /* Simple but imcomplete way. */
1514 if (i != 0 && EQ (obj, halftail))
1516 sprintf (buf, " . #%d", i / 2);
1517 strout (buf, -1, -1, printcharfun, 0);
1518 goto end_of_list;
1521 else
1523 /* With the print-circle feature. */
1524 if (i != 0)
1526 int i;
1527 for (i = 0; i < print_number_index; i++)
1528 if (EQ (PRINT_NUMBER_OBJECT (Vprint_number_table, i),
1529 obj))
1531 if (NILP (PRINT_NUMBER_STATUS (Vprint_number_table, i)))
1533 strout (" . ", 3, 3, printcharfun, 0);
1534 print_object (obj, printcharfun, escapeflag);
1536 else
1538 sprintf (buf, " . #%d#", i + 1);
1539 strout (buf, -1, -1, printcharfun, 0);
1541 goto end_of_list;
1546 if (i++)
1547 PRINTCHAR (' ');
1549 if (print_length && i > print_length)
1551 strout ("...", 3, 3, printcharfun, 0);
1552 goto end_of_list;
1555 print_object (XCAR (obj), printcharfun, escapeflag);
1557 obj = XCDR (obj);
1558 if (!(i & 1))
1559 halftail = XCDR (halftail);
1563 /* OBJ non-nil here means it's the end of a dotted list. */
1564 if (!NILP (obj))
1566 strout (" . ", 3, 3, printcharfun, 0);
1567 print_object (obj, printcharfun, escapeflag);
1570 end_of_list:
1571 PRINTCHAR (')');
1573 break;
1575 case Lisp_Vectorlike:
1576 if (PROCESSP (obj))
1578 if (escapeflag)
1580 strout ("#<process ", -1, -1, printcharfun, 0);
1581 print_string (XPROCESS (obj)->name, printcharfun);
1582 PRINTCHAR ('>');
1584 else
1585 print_string (XPROCESS (obj)->name, printcharfun);
1587 else if (BOOL_VECTOR_P (obj))
1589 register int i;
1590 register unsigned char c;
1591 struct gcpro gcpro1;
1592 int size_in_chars
1593 = (XBOOL_VECTOR (obj)->size + BITS_PER_CHAR - 1) / BITS_PER_CHAR;
1595 GCPRO1 (obj);
1597 PRINTCHAR ('#');
1598 PRINTCHAR ('&');
1599 sprintf (buf, "%d", XBOOL_VECTOR (obj)->size);
1600 strout (buf, -1, -1, printcharfun, 0);
1601 PRINTCHAR ('\"');
1603 /* Don't print more characters than the specified maximum.
1604 Negative values of print-length are invalid. Treat them
1605 like a print-length of nil. */
1606 if (NATNUMP (Vprint_length)
1607 && XFASTINT (Vprint_length) < size_in_chars)
1608 size_in_chars = XFASTINT (Vprint_length);
1610 for (i = 0; i < size_in_chars; i++)
1612 QUIT;
1613 c = XBOOL_VECTOR (obj)->data[i];
1614 if (c == '\n' && print_escape_newlines)
1616 PRINTCHAR ('\\');
1617 PRINTCHAR ('n');
1619 else if (c == '\f' && print_escape_newlines)
1621 PRINTCHAR ('\\');
1622 PRINTCHAR ('f');
1624 else
1626 if (c == '\"' || c == '\\')
1627 PRINTCHAR ('\\');
1628 PRINTCHAR (c);
1631 PRINTCHAR ('\"');
1633 UNGCPRO;
1635 else if (SUBRP (obj))
1637 strout ("#<subr ", -1, -1, printcharfun, 0);
1638 strout (XSUBR (obj)->symbol_name, -1, -1, printcharfun, 0);
1639 PRINTCHAR ('>');
1641 else if (WINDOWP (obj))
1643 strout ("#<window ", -1, -1, printcharfun, 0);
1644 sprintf (buf, "%d", XFASTINT (XWINDOW (obj)->sequence_number));
1645 strout (buf, -1, -1, printcharfun, 0);
1646 if (!NILP (XWINDOW (obj)->buffer))
1648 strout (" on ", -1, -1, printcharfun, 0);
1649 print_string (XBUFFER (XWINDOW (obj)->buffer)->name, printcharfun);
1651 PRINTCHAR ('>');
1653 else if (HASH_TABLE_P (obj))
1655 struct Lisp_Hash_Table *h = XHASH_TABLE (obj);
1656 strout ("#<hash-table", -1, -1, printcharfun, 0);
1657 if (SYMBOLP (h->test))
1659 PRINTCHAR (' ');
1660 PRINTCHAR ('\'');
1661 strout (XSYMBOL (h->test)->name->data, -1, -1, printcharfun, 0);
1662 PRINTCHAR (' ');
1663 strout (XSYMBOL (h->weak)->name->data, -1, -1, printcharfun, 0);
1664 PRINTCHAR (' ');
1665 sprintf (buf, "%d/%d", XFASTINT (h->count),
1666 XVECTOR (h->next)->size);
1667 strout (buf, -1, -1, printcharfun, 0);
1669 sprintf (buf, " 0x%lx", (unsigned long) h);
1670 strout (buf, -1, -1, printcharfun, 0);
1671 PRINTCHAR ('>');
1673 else if (BUFFERP (obj))
1675 if (NILP (XBUFFER (obj)->name))
1676 strout ("#<killed buffer>", -1, -1, printcharfun, 0);
1677 else if (escapeflag)
1679 strout ("#<buffer ", -1, -1, printcharfun, 0);
1680 print_string (XBUFFER (obj)->name, printcharfun);
1681 PRINTCHAR ('>');
1683 else
1684 print_string (XBUFFER (obj)->name, printcharfun);
1686 else if (WINDOW_CONFIGURATIONP (obj))
1688 strout ("#<window-configuration>", -1, -1, printcharfun, 0);
1690 else if (FRAMEP (obj))
1692 strout ((FRAME_LIVE_P (XFRAME (obj))
1693 ? "#<frame " : "#<dead frame "),
1694 -1, -1, printcharfun, 0);
1695 print_string (XFRAME (obj)->name, printcharfun);
1696 sprintf (buf, " 0x%lx\\ ", (unsigned long) (XFRAME (obj)));
1697 strout (buf, -1, -1, printcharfun, 0);
1698 PRINTCHAR ('>');
1700 else
1702 int size = XVECTOR (obj)->size;
1703 if (COMPILEDP (obj))
1705 PRINTCHAR ('#');
1706 size &= PSEUDOVECTOR_SIZE_MASK;
1708 if (CHAR_TABLE_P (obj))
1710 /* We print a char-table as if it were a vector,
1711 lumping the parent and default slots in with the
1712 character slots. But we add #^ as a prefix. */
1713 PRINTCHAR ('#');
1714 PRINTCHAR ('^');
1715 if (SUB_CHAR_TABLE_P (obj))
1716 PRINTCHAR ('^');
1717 size &= PSEUDOVECTOR_SIZE_MASK;
1719 if (size & PSEUDOVECTOR_FLAG)
1720 goto badtype;
1722 PRINTCHAR ('[');
1724 register int i;
1725 register Lisp_Object tem;
1726 int real_size = size;
1728 /* Don't print more elements than the specified maximum. */
1729 if (NATNUMP (Vprint_length)
1730 && XFASTINT (Vprint_length) < size)
1731 size = XFASTINT (Vprint_length);
1733 for (i = 0; i < size; i++)
1735 if (i) PRINTCHAR (' ');
1736 tem = XVECTOR (obj)->contents[i];
1737 print_object (tem, printcharfun, escapeflag);
1739 if (size < real_size)
1740 strout (" ...", 4, 4, printcharfun, 0);
1742 PRINTCHAR (']');
1744 break;
1746 case Lisp_Misc:
1747 switch (XMISCTYPE (obj))
1749 case Lisp_Misc_Marker:
1750 strout ("#<marker ", -1, -1, printcharfun, 0);
1751 /* Do you think this is necessary? */
1752 if (XMARKER (obj)->insertion_type != 0)
1753 strout ("(before-insertion) ", -1, -1, printcharfun, 0);
1754 if (!(XMARKER (obj)->buffer))
1755 strout ("in no buffer", -1, -1, printcharfun, 0);
1756 else
1758 sprintf (buf, "at %d", marker_position (obj));
1759 strout (buf, -1, -1, printcharfun, 0);
1760 strout (" in ", -1, -1, printcharfun, 0);
1761 print_string (XMARKER (obj)->buffer->name, printcharfun);
1763 PRINTCHAR ('>');
1764 break;
1766 case Lisp_Misc_Overlay:
1767 strout ("#<overlay ", -1, -1, printcharfun, 0);
1768 if (!(XMARKER (OVERLAY_START (obj))->buffer))
1769 strout ("in no buffer", -1, -1, printcharfun, 0);
1770 else
1772 sprintf (buf, "from %d to %d in ",
1773 marker_position (OVERLAY_START (obj)),
1774 marker_position (OVERLAY_END (obj)));
1775 strout (buf, -1, -1, printcharfun, 0);
1776 print_string (XMARKER (OVERLAY_START (obj))->buffer->name,
1777 printcharfun);
1779 PRINTCHAR ('>');
1780 break;
1782 /* Remaining cases shouldn't happen in normal usage, but let's print
1783 them anyway for the benefit of the debugger. */
1784 case Lisp_Misc_Free:
1785 strout ("#<misc free cell>", -1, -1, printcharfun, 0);
1786 break;
1788 case Lisp_Misc_Intfwd:
1789 sprintf (buf, "#<intfwd to %d>", *XINTFWD (obj)->intvar);
1790 strout (buf, -1, -1, printcharfun, 0);
1791 break;
1793 case Lisp_Misc_Boolfwd:
1794 sprintf (buf, "#<boolfwd to %s>",
1795 (*XBOOLFWD (obj)->boolvar ? "t" : "nil"));
1796 strout (buf, -1, -1, printcharfun, 0);
1797 break;
1799 case Lisp_Misc_Objfwd:
1800 strout ("#<objfwd to ", -1, -1, printcharfun, 0);
1801 print_object (*XOBJFWD (obj)->objvar, printcharfun, escapeflag);
1802 PRINTCHAR ('>');
1803 break;
1805 case Lisp_Misc_Buffer_Objfwd:
1806 strout ("#<buffer_objfwd to ", -1, -1, printcharfun, 0);
1807 print_object (PER_BUFFER_VALUE (current_buffer,
1808 XBUFFER_OBJFWD (obj)->offset),
1809 printcharfun, escapeflag);
1810 PRINTCHAR ('>');
1811 break;
1813 case Lisp_Misc_Kboard_Objfwd:
1814 strout ("#<kboard_objfwd to ", -1, -1, printcharfun, 0);
1815 print_object (*(Lisp_Object *)((char *) current_kboard
1816 + XKBOARD_OBJFWD (obj)->offset),
1817 printcharfun, escapeflag);
1818 PRINTCHAR ('>');
1819 break;
1821 case Lisp_Misc_Buffer_Local_Value:
1822 strout ("#<buffer_local_value ", -1, -1, printcharfun, 0);
1823 goto do_buffer_local;
1824 case Lisp_Misc_Some_Buffer_Local_Value:
1825 strout ("#<some_buffer_local_value ", -1, -1, printcharfun, 0);
1826 do_buffer_local:
1827 strout ("[realvalue] ", -1, -1, printcharfun, 0);
1828 print_object (XBUFFER_LOCAL_VALUE (obj)->realvalue,
1829 printcharfun, escapeflag);
1830 if (XBUFFER_LOCAL_VALUE (obj)->found_for_buffer)
1831 strout ("[local in buffer] ", -1, -1, printcharfun, 0);
1832 else
1833 strout ("[buffer] ", -1, -1, printcharfun, 0);
1834 print_object (XBUFFER_LOCAL_VALUE (obj)->buffer,
1835 printcharfun, escapeflag);
1836 if (XBUFFER_LOCAL_VALUE (obj)->check_frame)
1838 if (XBUFFER_LOCAL_VALUE (obj)->found_for_frame)
1839 strout ("[local in frame] ", -1, -1, printcharfun, 0);
1840 else
1841 strout ("[frame] ", -1, -1, printcharfun, 0);
1842 print_object (XBUFFER_LOCAL_VALUE (obj)->frame,
1843 printcharfun, escapeflag);
1845 strout ("[alist-elt] ", -1, -1, printcharfun, 0);
1846 print_object (XCAR (XBUFFER_LOCAL_VALUE (obj)->cdr),
1847 printcharfun, escapeflag);
1848 strout ("[default-value] ", -1, -1, printcharfun, 0);
1849 print_object (XCDR (XBUFFER_LOCAL_VALUE (obj)->cdr),
1850 printcharfun, escapeflag);
1851 PRINTCHAR ('>');
1852 break;
1854 default:
1855 goto badtype;
1857 break;
1859 default:
1860 badtype:
1862 /* We're in trouble if this happens!
1863 Probably should just abort () */
1864 strout ("#<EMACS BUG: INVALID DATATYPE ", -1, -1, printcharfun, 0);
1865 if (MISCP (obj))
1866 sprintf (buf, "(MISC 0x%04x)", (int) XMISCTYPE (obj));
1867 else if (VECTORLIKEP (obj))
1868 sprintf (buf, "(PVEC 0x%08x)", (int) XVECTOR (obj)->size);
1869 else
1870 sprintf (buf, "(0x%02x)", (int) XTYPE (obj));
1871 strout (buf, -1, -1, printcharfun, 0);
1872 strout (" Save your buffers immediately and please report this bug>",
1873 -1, -1, printcharfun, 0);
1877 print_depth--;
1881 /* Print a description of INTERVAL using PRINTCHARFUN.
1882 This is part of printing a string that has text properties. */
1884 void
1885 print_interval (interval, printcharfun)
1886 INTERVAL interval;
1887 Lisp_Object printcharfun;
1889 PRINTCHAR (' ');
1890 print_object (make_number (interval->position), printcharfun, 1);
1891 PRINTCHAR (' ');
1892 print_object (make_number (interval->position + LENGTH (interval)),
1893 printcharfun, 1);
1894 PRINTCHAR (' ');
1895 print_object (interval->plist, printcharfun, 1);
1899 void
1900 syms_of_print ()
1902 Qtemp_buffer_setup_hook = intern ("temp-buffer-setup-hook");
1903 staticpro (&Qtemp_buffer_setup_hook);
1905 DEFVAR_LISP ("standard-output", &Vstandard_output,
1906 "Output stream `print' uses by default for outputting a character.\n\
1907 This may be any function of one argument.\n\
1908 It may also be a buffer (output is inserted before point)\n\
1909 or a marker (output is inserted and the marker is advanced)\n\
1910 or the symbol t (output appears in the echo area).");
1911 Vstandard_output = Qt;
1912 Qstandard_output = intern ("standard-output");
1913 staticpro (&Qstandard_output);
1915 DEFVAR_LISP ("float-output-format", &Vfloat_output_format,
1916 "The format descriptor string used to print floats.\n\
1917 This is a %-spec like those accepted by `printf' in C,\n\
1918 but with some restrictions. It must start with the two characters `%.'.\n\
1919 After that comes an integer precision specification,\n\
1920 and then a letter which controls the format.\n\
1921 The letters allowed are `e', `f' and `g'.\n\
1922 Use `e' for exponential notation \"DIG.DIGITSeEXPT\"\n\
1923 Use `f' for decimal point notation \"DIGITS.DIGITS\".\n\
1924 Use `g' to choose the shorter of those two formats for the number at hand.\n\
1925 The precision in any of these cases is the number of digits following\n\
1926 the decimal point. With `f', a precision of 0 means to omit the\n\
1927 decimal point. 0 is not allowed with `e' or `g'.\n\n\
1928 A value of nil means to use the shortest notation\n\
1929 that represents the number without losing information.");
1930 Vfloat_output_format = Qnil;
1931 Qfloat_output_format = intern ("float-output-format");
1932 staticpro (&Qfloat_output_format);
1934 DEFVAR_LISP ("print-length", &Vprint_length,
1935 "Maximum length of list to print before abbreviating.\n\
1936 A value of nil means no limit.");
1937 Vprint_length = Qnil;
1939 DEFVAR_LISP ("print-level", &Vprint_level,
1940 "Maximum depth of list nesting to print before abbreviating.\n\
1941 A value of nil means no limit.");
1942 Vprint_level = Qnil;
1944 DEFVAR_BOOL ("print-escape-newlines", &print_escape_newlines,
1945 "Non-nil means print newlines in strings as backslash-n.\n\
1946 Also print formfeeds as backslash-f.");
1947 print_escape_newlines = 0;
1949 DEFVAR_BOOL ("print-escape-nonascii", &print_escape_nonascii,
1950 "Non-nil means print unibyte non-ASCII chars in strings as \\OOO.\n\
1951 \(OOO is the octal representation of the character code.)\n\
1952 Only single-byte characters are affected, and only in `prin1'.");
1953 print_escape_nonascii = 0;
1955 DEFVAR_BOOL ("print-escape-multibyte", &print_escape_multibyte,
1956 "Non-nil means print multibyte characters in strings as \\xXXXX.\n\
1957 \(XXX is the hex representation of the character code.)\n\
1958 This affects only `prin1'.");
1959 print_escape_multibyte = 0;
1961 DEFVAR_BOOL ("print-quoted", &print_quoted,
1962 "Non-nil means print quoted forms with reader syntax.\n\
1963 I.e., (quote foo) prints as 'foo, (function foo) as #'foo, and, backquoted\n\
1964 forms print in the new syntax.");
1965 print_quoted = 0;
1967 DEFVAR_LISP ("print-gensym", &Vprint_gensym,
1968 "Non-nil means print uninterned symbols so they will read as uninterned.\n\
1969 I.e., the value of (make-symbol \"foobar\") prints as #:foobar.\n\
1970 When the uninterned symbol appears within a recursive data structure\n\
1971 and the symbol appears more than once, in addition use the #N# and #N=\n\
1972 constructs as needed, so that multiple references to the same symbol are\n\
1973 shared once again when the text is read back.");
1974 Vprint_gensym = Qnil;
1976 DEFVAR_LISP ("print-circle", &Vprint_circle,
1977 "*Non-nil means print recursive structures using #N= and #N# syntax.\n\
1978 If nil, printing proceeds recursively and may lead to\n\
1979 `max-lisp-eval-depth' being exceeded or an error may occur:\n\
1980 \"Apparently circular structure being printed.\" Also see\n\
1981 `print-length' and `print-level'.\n\
1982 If non-nil, shared substructures anywhere in the structure are printed\n\
1983 with `#N=' before the first occurrence (in the order of the print\n\
1984 representation) and `#N#' in place of each subsequent occurrence,\n\
1985 where N is a positive decimal integer.");
1986 Vprint_circle = Qnil;
1988 DEFVAR_LISP ("print-continuous-numbering", &Vprint_continuous_numbering,
1989 "*Non-nil means keep numbering between several print functions.\n\
1990 See `print-gensym' nad `print-circle'. See also `print-number-table'.");
1991 Vprint_continuous_numbering = Qnil;
1993 DEFVAR_LISP ("print-number-table", &Vprint_number_table,
1994 "A vector keeping the information of the current printed object.\n\
1995 This variable shouldn't be modified in Lisp level, but should be binded\n\
1996 with nil using let at the same position with `print-continuous-numbering',\n\
1997 so that the value of this variable can be freed after printing.");
1998 Vprint_number_table = Qnil;
2000 /* prin1_to_string_buffer initialized in init_buffer_once in buffer.c */
2001 staticpro (&Vprin1_to_string_buffer);
2003 defsubr (&Sprin1);
2004 defsubr (&Sprin1_to_string);
2005 defsubr (&Serror_message_string);
2006 defsubr (&Sprinc);
2007 defsubr (&Sprint);
2008 defsubr (&Sterpri);
2009 defsubr (&Swrite_char);
2010 defsubr (&Sexternal_debugging_output);
2012 Qexternal_debugging_output = intern ("external-debugging-output");
2013 staticpro (&Qexternal_debugging_output);
2015 Qprint_escape_newlines = intern ("print-escape-newlines");
2016 staticpro (&Qprint_escape_newlines);
2018 Qprint_escape_multibyte = intern ("print-escape-multibyte");
2019 staticpro (&Qprint_escape_multibyte);
2021 Qprint_escape_nonascii = intern ("print-escape-nonascii");
2022 staticpro (&Qprint_escape_nonascii);
2024 defsubr (&Swith_output_to_temp_buffer);