1 /* Lisp object printing and output streams.
2 Copyright (C) 1985, 86, 88, 93, 94, 95 Free Software Foundation, Inc.
4 This file is part of GNU Emacs.
6 GNU Emacs is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Emacs is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs; see the file COPYING. If not, write to
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
32 #include "dispextern.h"
35 #endif /* not standalone */
37 #ifdef USE_TEXT_PROPERTIES
38 #include "intervals.h"
41 Lisp_Object Vstandard_output
, Qstandard_output
;
43 /* These are used to print like we read. */
44 extern Lisp_Object Qbackquote
, Qcomma
, Qcomma_at
, Qcomma_dot
, Qfunction
;
46 #ifdef LISP_FLOAT_TYPE
47 Lisp_Object Vfloat_output_format
, Qfloat_output_format
;
48 #endif /* LISP_FLOAT_TYPE */
50 /* Avoid actual stack overflow in print. */
53 /* Detect most circularities to print finite output. */
54 #define PRINT_CIRCLE 200
55 Lisp_Object being_printed
[PRINT_CIRCLE
];
57 /* When printing into a buffer, first we put the text in this
58 block, then insert it all at once. */
61 /* Size allocated in print_buffer. */
62 int print_buffer_size
;
63 /* Size used in print_buffer. */
66 /* Maximum length of list to print in full; noninteger means
67 effectively infinity */
69 Lisp_Object Vprint_length
;
71 /* Maximum depth of list to print in full; noninteger means
72 effectively infinity. */
74 Lisp_Object Vprint_level
;
76 /* Nonzero means print newlines in strings as \n. */
78 int print_escape_newlines
;
80 Lisp_Object Qprint_escape_newlines
;
82 /* Nonzero means print (quote foo) forms as 'foo, etc. */
86 /* Non-nil means print #: before uninterned symbols.
87 Neither t nor nil means so that and don't clear Vprint_gensym_alist
88 on entry to and exit from print functions. */
90 Lisp_Object Vprint_gensym
;
92 /* Association list of certain objects that are `eq' in the form being
93 printed and which should be `eq' when read back in, using the #n=object
94 and #n# reader forms. Each element has the form (object . n). */
96 Lisp_Object Vprint_gensym_alist
;
98 /* Nonzero means print newline to stdout before next minibuffer message.
101 extern int noninteractive_need_newline
;
103 extern int minibuffer_auto_raise
;
105 #ifdef MAX_PRINT_CHARS
106 static int print_chars
;
107 static int max_print
;
108 #endif /* MAX_PRINT_CHARS */
110 void print_interval ();
113 /* Convert between chars and GLYPHs */
117 register GLYPH
*glyphs
;
127 str_to_glyph_cpy (str
, glyphs
)
131 register GLYPH
*gp
= glyphs
;
132 register char *cp
= str
;
139 str_to_glyph_ncpy (str
, glyphs
, n
)
144 register GLYPH
*gp
= glyphs
;
145 register char *cp
= str
;
152 glyph_to_str_cpy (glyphs
, str
)
156 register GLYPH
*gp
= glyphs
;
157 register char *cp
= str
;
160 *str
++ = *gp
++ & 0377;
164 /* Low level output routines for characters and strings */
166 /* Lisp functions to do output using a stream
167 must have the stream in a variable called printcharfun
168 and must start with PRINTPREPARE, end with PRINTFINISH,
169 and use PRINTDECLARE to declare common variables.
170 Use PRINTCHAR to output one character,
171 or call strout to output a block of characters.
174 #define PRINTDECLARE \
175 struct buffer *old = current_buffer; \
176 int old_point = -1, start_point; \
177 int specpdl_count = specpdl_ptr - specpdl; \
178 int free_print_buffer = 0; \
181 #define PRINTPREPARE \
182 original = printcharfun; \
183 if (NILP (printcharfun)) printcharfun = Qt; \
184 if (BUFFERP (printcharfun)) \
186 if (XBUFFER (printcharfun) != current_buffer) \
187 Fset_buffer (printcharfun); \
188 printcharfun = Qnil; \
190 if (MARKERP (printcharfun)) \
192 if (!(XMARKER (original)->buffer)) \
193 error ("Marker does not point anywhere"); \
194 if (XMARKER (original)->buffer != current_buffer) \
195 set_buffer_internal (XMARKER (original)->buffer); \
197 SET_PT (marker_position (printcharfun)); \
199 printcharfun = Qnil; \
201 if (NILP (printcharfun)) \
203 if (print_buffer != 0) \
204 record_unwind_protect (print_unwind, \
205 make_string (print_buffer, \
206 print_buffer_pos)); \
209 print_buffer_size = 1000; \
210 print_buffer = (char *) xmalloc (print_buffer_size); \
211 free_print_buffer = 1; \
213 print_buffer_pos = 0; \
215 if (!CONSP (Vprint_gensym)) \
216 Vprint_gensym_alist = Qnil
218 #define PRINTFINISH \
219 if (NILP (printcharfun)) \
220 insert (print_buffer, print_buffer_pos); \
221 if (free_print_buffer) \
223 xfree (print_buffer); \
226 unbind_to (specpdl_count, Qnil); \
227 if (MARKERP (original)) \
228 Fset_marker (original, make_number (PT), Qnil); \
229 if (old_point >= 0) \
230 SET_PT (old_point + (old_point >= start_point \
231 ? PT - start_point : 0)); \
232 if (old != current_buffer) \
233 set_buffer_internal (old); \
234 if (!CONSP (Vprint_gensym)) \
235 Vprint_gensym_alist = Qnil
237 #define PRINTCHAR(ch) printchar (ch, printcharfun)
239 /* Nonzero if there is no room to print any more characters
240 so print might as well return right away. */
242 #define PRINTFULLP() \
243 (EQ (printcharfun, Qt) && !noninteractive \
244 && printbufidx >= FRAME_WIDTH (XFRAME (WINDOW_FRAME (XWINDOW (minibuf_window)))))
246 /* This is used to restore the saved contents of print_buffer
247 when there is a recursive call to print. */
249 print_unwind (saved_text
)
250 Lisp_Object saved_text
;
252 bcopy (XSTRING (saved_text
)->data
, print_buffer
, XSTRING (saved_text
)->size
);
255 /* Index of first unused element of FRAME_MESSAGE_BUF (mini_frame). */
256 static int printbufidx
;
265 #ifdef MAX_PRINT_CHARS
268 #endif /* MAX_PRINT_CHARS */
276 len
= CHAR_STRING (ch
, work
, str
);
277 if (print_buffer_pos
+ len
>= print_buffer_size
)
278 print_buffer
= (char *) xrealloc (print_buffer
,
279 print_buffer_size
*= 2);
280 bcopy (str
, print_buffer
+ print_buffer_pos
, len
);
281 print_buffer_pos
+= len
;
288 = XFRAME (WINDOW_FRAME (XWINDOW (minibuf_window
)));
289 unsigned char work
[4], *str
;
290 int len
= CHAR_STRING (ch
, work
, str
);
297 putchar (*str
), str
++;
298 noninteractive_need_newline
= 1;
302 if (echo_area_glyphs
!= FRAME_MESSAGE_BUF (mini_frame
)
303 || !message_buf_print
)
305 message_log_maybe_newline ();
306 echo_area_glyphs
= FRAME_MESSAGE_BUF (mini_frame
);
308 echo_area_glyphs_length
= 0;
309 message_buf_print
= 1;
311 if (minibuffer_auto_raise
)
313 Lisp_Object mini_window
;
315 /* Get the frame containing the minibuffer
316 that the selected frame is using. */
317 mini_window
= FRAME_MINIBUF_WINDOW (selected_frame
);
319 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window
)));
323 message_dolog (str
, len
, 0);
324 if (printbufidx
< FRAME_MESSAGE_BUF_SIZE (mini_frame
) - len
)
325 bcopy (str
, &FRAME_MESSAGE_BUF (mini_frame
)[printbufidx
], len
),
327 FRAME_MESSAGE_BUF (mini_frame
)[printbufidx
] = 0;
328 echo_area_glyphs_length
= printbufidx
;
332 #endif /* not standalone */
334 XSETFASTINT (ch1
, ch
);
339 strout (ptr
, size
, printcharfun
)
342 Lisp_Object printcharfun
;
349 if (EQ (printcharfun
, Qnil
))
351 if (print_buffer_pos
+ size
> print_buffer_size
)
353 print_buffer_size
= print_buffer_size
* 2 + size
;
354 print_buffer
= (char *) xrealloc (print_buffer
,
357 bcopy (ptr
, print_buffer
+ print_buffer_pos
, size
);
358 print_buffer_pos
+= size
;
360 #ifdef MAX_PRINT_CHARS
363 #endif /* MAX_PRINT_CHARS */
366 if (EQ (printcharfun
, Qt
))
369 = XFRAME (WINDOW_FRAME (XWINDOW (minibuf_window
)));
373 #ifdef MAX_PRINT_CHARS
376 #endif /* MAX_PRINT_CHARS */
380 fwrite (ptr
, 1, size
, stdout
);
381 noninteractive_need_newline
= 1;
385 if (echo_area_glyphs
!= FRAME_MESSAGE_BUF (mini_frame
)
386 || !message_buf_print
)
388 message_log_maybe_newline ();
389 echo_area_glyphs
= FRAME_MESSAGE_BUF (mini_frame
);
391 echo_area_glyphs_length
= 0;
392 message_buf_print
= 1;
394 if (minibuffer_auto_raise
)
396 Lisp_Object mini_window
;
398 /* Get the frame containing the minibuffer
399 that the selected frame is using. */
400 mini_window
= FRAME_MINIBUF_WINDOW (selected_frame
);
402 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window
)));
406 message_dolog (ptr
, size
, 0);
407 if (size
> FRAME_MESSAGE_BUF_SIZE (mini_frame
) - printbufidx
- 1)
409 size
= FRAME_MESSAGE_BUF_SIZE (mini_frame
) - printbufidx
- 1;
410 /* Rewind incomplete multi-byte form. */
411 while (size
&& (unsigned char) ptr
[size
] >= 0xA0) size
--;
413 bcopy (ptr
, &FRAME_MESSAGE_BUF (mini_frame
) [printbufidx
], size
);
415 echo_area_glyphs_length
= printbufidx
;
416 FRAME_MESSAGE_BUF (mini_frame
) [printbufidx
] = 0;
424 /* Here, we must convert each multi-byte form to the
425 corresponding character code before handing it to PRINTCHAR. */
427 int ch
= STRING_CHAR_AND_LENGTH (ptr
+ i
, size
- i
, len
);
434 /* Print the contents of a string STRING using PRINTCHARFUN.
435 It isn't safe to use strout in many cases,
436 because printing one char can relocate. */
438 print_string (string
, printcharfun
)
440 Lisp_Object printcharfun
;
442 if (EQ (printcharfun
, Qt
) || NILP (printcharfun
))
443 /* strout is safe for output to a frame (echo area) or to print_buffer. */
444 strout (XSTRING (string
)->data
, XSTRING (string
)->size
, printcharfun
);
447 /* Otherwise, fetch the string address for each character. */
449 int size
= XSTRING (string
)->size
;
452 for (i
= 0; i
< size
; i
++)
453 PRINTCHAR (XSTRING (string
)->data
[i
]);
458 DEFUN ("write-char", Fwrite_char
, Swrite_char
, 1, 2, 0,
459 "Output character CHARACTER to stream PRINTCHARFUN.\n\
460 PRINTCHARFUN defaults to the value of `standard-output' (which see).")
461 (character
, printcharfun
)
462 Lisp_Object character
, printcharfun
;
466 if (NILP (printcharfun
))
467 printcharfun
= Vstandard_output
;
468 CHECK_NUMBER (character
, 0);
470 PRINTCHAR (XINT (character
));
475 /* Used from outside of print.c to print a block of SIZE chars at DATA
476 on the default output stream.
477 Do not use this on the contents of a Lisp string. */
479 write_string (data
, size
)
484 Lisp_Object printcharfun
;
486 printcharfun
= Vstandard_output
;
489 strout (data
, size
, printcharfun
);
493 /* Used from outside of print.c to print a block of SIZE chars at DATA
494 on a specified stream PRINTCHARFUN.
495 Do not use this on the contents of a Lisp string. */
497 write_string_1 (data
, size
, printcharfun
)
500 Lisp_Object printcharfun
;
505 strout (data
, size
, printcharfun
);
513 temp_output_buffer_setup (bufname
)
516 register struct buffer
*old
= current_buffer
;
517 register Lisp_Object buf
;
519 Fset_buffer (Fget_buffer_create (build_string (bufname
)));
521 current_buffer
->directory
= old
->directory
;
522 current_buffer
->read_only
= Qnil
;
525 XSETBUFFER (buf
, current_buffer
);
526 specbind (Qstandard_output
, buf
);
528 set_buffer_internal (old
);
532 internal_with_output_to_temp_buffer (bufname
, function
, args
)
534 Lisp_Object (*function
) ();
537 int count
= specpdl_ptr
- specpdl
;
538 Lisp_Object buf
, val
;
542 record_unwind_protect (Fset_buffer
, Fcurrent_buffer ());
543 temp_output_buffer_setup (bufname
);
544 buf
= Vstandard_output
;
547 val
= (*function
) (args
);
550 temp_output_buffer_show (buf
);
553 return unbind_to (count
, val
);
556 DEFUN ("with-output-to-temp-buffer", Fwith_output_to_temp_buffer
, Swith_output_to_temp_buffer
,
558 "Bind `standard-output' to buffer BUFNAME, eval BODY, then show that buffer.\n\
559 The buffer is cleared out initially, and marked as unmodified when done.\n\
560 All output done by BODY is inserted in that buffer by default.\n\
561 The buffer is displayed in another window, but not selected.\n\
562 The value of the last form in BODY is returned.\n\
563 If BODY does not finish normally, the buffer BUFNAME is not displayed.\n\n\
564 If variable `temp-buffer-show-function' is non-nil, call it at the end\n\
565 to get the buffer displayed. It gets one argument, the buffer to display.")
571 int count
= specpdl_ptr
- specpdl
;
572 Lisp_Object buf
, val
;
575 name
= Feval (Fcar (args
));
578 CHECK_STRING (name
, 0);
579 temp_output_buffer_setup (XSTRING (name
)->data
);
580 buf
= Vstandard_output
;
582 val
= Fprogn (Fcdr (args
));
584 temp_output_buffer_show (buf
);
586 return unbind_to (count
, val
);
588 #endif /* not standalone */
590 static void print ();
592 DEFUN ("terpri", Fterpri
, Sterpri
, 0, 1, 0,
593 "Output a newline to stream PRINTCHARFUN.\n\
594 If PRINTCHARFUN is omitted or nil, the value of `standard-output' is used.")
596 Lisp_Object printcharfun
;
600 if (NILP (printcharfun
))
601 printcharfun
= Vstandard_output
;
608 DEFUN ("prin1", Fprin1
, Sprin1
, 1, 2, 0,
609 "Output the printed representation of OBJECT, any Lisp object.\n\
610 Quoting characters are printed when needed to make output that `read'\n\
611 can handle, whenever this is possible.\n\
612 Output stream is PRINTCHARFUN, or value of `standard-output' (which see).")
613 (object
, printcharfun
)
614 Lisp_Object object
, printcharfun
;
618 #ifdef MAX_PRINT_CHARS
620 #endif /* MAX_PRINT_CHARS */
621 if (NILP (printcharfun
))
622 printcharfun
= Vstandard_output
;
625 print (object
, printcharfun
, 1);
630 /* a buffer which is used to hold output being built by prin1-to-string */
631 Lisp_Object Vprin1_to_string_buffer
;
633 DEFUN ("prin1-to-string", Fprin1_to_string
, Sprin1_to_string
, 1, 2, 0,
634 "Return a string containing the printed representation of OBJECT,\n\
635 any Lisp object. Quoting characters are used when needed to make output\n\
636 that `read' can handle, whenever this is possible, unless the optional\n\
637 second argument NOESCAPE is non-nil.")
639 Lisp_Object object
, noescape
;
642 Lisp_Object printcharfun
;
643 struct gcpro gcpro1
, gcpro2
;
646 /* Save and restore this--we are altering a buffer
647 but we don't want to deactivate the mark just for that.
648 No need for specbind, since errors deactivate the mark. */
649 tem
= Vdeactivate_mark
;
650 GCPRO2 (object
, tem
);
652 printcharfun
= Vprin1_to_string_buffer
;
655 print (object
, printcharfun
, NILP (noescape
));
656 /* Make Vprin1_to_string_buffer be the default buffer after PRINTFINSH */
658 set_buffer_internal (XBUFFER (Vprin1_to_string_buffer
));
659 object
= Fbuffer_string ();
662 set_buffer_internal (old
);
664 Vdeactivate_mark
= tem
;
670 DEFUN ("princ", Fprinc
, Sprinc
, 1, 2, 0,
671 "Output the printed representation of OBJECT, any Lisp object.\n\
672 No quoting characters are used; no delimiters are printed around\n\
673 the contents of strings.\n\
674 Output stream is PRINTCHARFUN, or value of standard-output (which see).")
675 (object
, printcharfun
)
676 Lisp_Object object
, printcharfun
;
680 if (NILP (printcharfun
))
681 printcharfun
= Vstandard_output
;
684 print (object
, printcharfun
, 0);
689 DEFUN ("print", Fprint
, Sprint
, 1, 2, 0,
690 "Output the printed representation of OBJECT, with newlines around it.\n\
691 Quoting characters are printed when needed to make output that `read'\n\
692 can handle, whenever this is possible.\n\
693 Output stream is PRINTCHARFUN, or value of `standard-output' (which see).")
694 (object
, printcharfun
)
695 Lisp_Object object
, printcharfun
;
700 #ifdef MAX_PRINT_CHARS
702 max_print
= MAX_PRINT_CHARS
;
703 #endif /* MAX_PRINT_CHARS */
704 if (NILP (printcharfun
))
705 printcharfun
= Vstandard_output
;
710 print (object
, printcharfun
, 1);
713 #ifdef MAX_PRINT_CHARS
716 #endif /* MAX_PRINT_CHARS */
721 /* The subroutine object for external-debugging-output is kept here
722 for the convenience of the debugger. */
723 Lisp_Object Qexternal_debugging_output
;
725 DEFUN ("external-debugging-output", Fexternal_debugging_output
, Sexternal_debugging_output
, 1, 1, 0,
726 "Write CHARACTER to stderr.\n\
727 You can call print while debugging emacs, and pass it this function\n\
728 to make it write to the debugging output.\n")
730 Lisp_Object character
;
732 CHECK_NUMBER (character
, 0);
733 putc (XINT (character
), stderr
);
738 /* This is the interface for debugging printing. */
744 Fprin1 (arg
, Qexternal_debugging_output
);
745 fprintf (stderr
, "\r\n");
748 DEFUN ("error-message-string", Ferror_message_string
, Serror_message_string
,
750 "Convert an error value (ERROR-SYMBOL . DATA) to an error message.")
754 struct buffer
*old
= current_buffer
;
755 Lisp_Object original
, printcharfun
, value
;
758 /* If OBJ is (error STRING), just return STRING.
759 That is not only faster, it also avoids the need to allocate
760 space here when the error is due to memory full. */
761 if (CONSP (obj
) && EQ (XCONS (obj
)->car
, Qerror
)
762 && CONSP (XCONS (obj
)->cdr
)
763 && STRINGP (XCONS (XCONS (obj
)->cdr
)->car
)
764 && NILP (XCONS (XCONS (obj
)->cdr
)->cdr
))
765 return XCONS (XCONS (obj
)->cdr
)->car
;
767 print_error_message (obj
, Vprin1_to_string_buffer
, NULL
);
769 set_buffer_internal (XBUFFER (Vprin1_to_string_buffer
));
770 value
= Fbuffer_string ();
774 set_buffer_internal (old
);
780 /* Print an error message for the error DATA
781 onto Lisp output stream STREAM (suitable for the print functions). */
783 print_error_message (data
, stream
)
784 Lisp_Object data
, stream
;
786 Lisp_Object errname
, errmsg
, file_error
, tail
;
790 errname
= Fcar (data
);
792 if (EQ (errname
, Qerror
))
795 if (!CONSP (data
)) data
= Qnil
;
796 errmsg
= Fcar (data
);
801 errmsg
= Fget (errname
, Qerror_message
);
802 file_error
= Fmemq (Qfile_error
,
803 Fget (errname
, Qerror_conditions
));
806 /* Print an error message including the data items. */
808 tail
= Fcdr_safe (data
);
811 /* For file-error, make error message by concatenating
812 all the data items. They are all strings. */
813 if (!NILP (file_error
) && !NILP (tail
))
814 errmsg
= XCONS (tail
)->car
, tail
= XCONS (tail
)->cdr
;
816 if (STRINGP (errmsg
))
817 Fprinc (errmsg
, stream
);
819 write_string_1 ("peculiar error", -1, stream
);
821 for (i
= 0; CONSP (tail
); tail
= Fcdr (tail
), i
++)
823 write_string_1 (i
? ", " : ": ", 2, stream
);
824 if (!NILP (file_error
))
825 Fprinc (Fcar (tail
), stream
);
827 Fprin1 (Fcar (tail
), stream
);
832 #ifdef LISP_FLOAT_TYPE
835 * The buffer should be at least as large as the max string size of the
836 * largest float, printed in the biggest notation. This is undoubtedly
837 * 20d float_output_format, with the negative of the C-constant "HUGE"
840 * On the vax the worst case is -1e38 in 20d format which takes 61 bytes.
842 * I assume that IEEE-754 format numbers can take 329 bytes for the worst
843 * case of -1e307 in 20d float_output_format. What is one to do (short of
844 * re-writing _doprnt to be more sane)?
849 float_to_string (buf
, data
)
856 if (NILP (Vfloat_output_format
)
857 || !STRINGP (Vfloat_output_format
))
860 sprintf (buf
, "%.17g", data
);
865 /* Check that the spec we have is fully valid.
866 This means not only valid for printf,
867 but meant for floats, and reasonable. */
868 cp
= XSTRING (Vfloat_output_format
)->data
;
877 /* Check the width specification. */
879 if ('0' <= *cp
&& *cp
<= '9')
883 width
= (width
* 10) + (*cp
++ - '0');
884 while (*cp
>= '0' && *cp
<= '9');
886 /* A precision of zero is valid only for %f. */
888 || (width
== 0 && *cp
!= 'f'))
892 if (*cp
!= 'e' && *cp
!= 'f' && *cp
!= 'g')
898 sprintf (buf
, XSTRING (Vfloat_output_format
)->data
, data
);
901 /* Make sure there is a decimal point with digit after, or an
902 exponent, so that the value is readable as a float. But don't do
903 this with "%.0f"; it's valid for that not to produce a decimal
904 point. Note that width can be 0 only for %.0f. */
907 for (cp
= buf
; *cp
; cp
++)
908 if ((*cp
< '0' || *cp
> '9') && *cp
!= '-')
911 if (*cp
== '.' && cp
[1] == 0)
925 #endif /* LISP_FLOAT_TYPE */
928 print (obj
, printcharfun
, escapeflag
)
930 register Lisp_Object printcharfun
;
937 #if 1 /* I'm not sure this is really worth doing. */
938 /* Detect circularities and truncate them.
939 No need to offer any alternative--this is better than an error. */
940 if (CONSP (obj
) || VECTORP (obj
) || COMPILEDP (obj
))
943 for (i
= 0; i
< print_depth
; i
++)
944 if (EQ (obj
, being_printed
[i
]))
946 sprintf (buf
, "#%d", i
);
947 strout (buf
, -1, printcharfun
);
953 being_printed
[print_depth
] = obj
;
956 if (print_depth
> PRINT_CIRCLE
)
957 error ("Apparently circular structure being printed");
958 #ifdef MAX_PRINT_CHARS
959 if (max_print
&& print_chars
> max_print
)
964 #endif /* MAX_PRINT_CHARS */
966 switch (XGCTYPE (obj
))
969 if (sizeof (int) == sizeof (EMACS_INT
))
970 sprintf (buf
, "%d", XINT (obj
));
971 else if (sizeof (long) == sizeof (EMACS_INT
))
972 sprintf (buf
, "%ld", XINT (obj
));
975 strout (buf
, -1, printcharfun
);
978 #ifdef LISP_FLOAT_TYPE
981 char pigbuf
[350]; /* see comments in float_to_string */
983 float_to_string (pigbuf
, XFLOAT(obj
)->data
);
984 strout (pigbuf
, -1, printcharfun
);
991 print_string (obj
, printcharfun
);
995 register unsigned char c
;
1000 #ifdef USE_TEXT_PROPERTIES
1001 if (!NULL_INTERVAL_P (XSTRING (obj
)->intervals
))
1009 for (i
= 0; i
< XSTRING (obj
)->size
; i
++)
1012 c
= XSTRING (obj
)->data
[i
];
1013 if (c
== '\n' && print_escape_newlines
)
1018 else if (c
== '\f' && print_escape_newlines
)
1025 if (c
== '\"' || c
== '\\')
1032 #ifdef USE_TEXT_PROPERTIES
1033 if (!NULL_INTERVAL_P (XSTRING (obj
)->intervals
))
1035 traverse_intervals (XSTRING (obj
)->intervals
,
1036 0, 0, print_interval
, printcharfun
);
1047 register int confusing
;
1048 register unsigned char *p
= XSYMBOL (obj
)->name
->data
;
1049 register unsigned char *end
= p
+ XSYMBOL (obj
)->name
->size
;
1050 register unsigned char c
;
1053 if (p
!= end
&& (*p
== '-' || *p
== '+')) p
++;
1056 /* If symbol name begins with a digit, and ends with a digit,
1057 and contains nothing but digits and `e', it could be treated
1058 as a number. So set CONFUSING.
1060 Symbols that contain periods could also be taken as numbers,
1061 but periods are always escaped, so we don't have to worry
1063 else if (*p
>= '0' && *p
<= '9'
1064 && end
[-1] >= '0' && end
[-1] <= '9')
1066 while (p
!= end
&& ((*p
>= '0' && *p
<= '9')
1067 /* Needed for \2e10. */
1070 confusing
= (end
== p
);
1075 /* If we print an uninterned symbol as part of a complex object and
1076 the flag print-gensym is non-nil, prefix it with #n= to read the
1077 object back with the #n# reader syntax later if needed. */
1078 if (! NILP (Vprint_gensym
) && NILP (XSYMBOL (obj
)->obarray
))
1080 if (print_depth
> 1)
1083 tem
= Fassq (obj
, Vprint_gensym_alist
);
1087 print (XCDR (tem
), printcharfun
, escapeflag
);
1093 if (CONSP (Vprint_gensym_alist
))
1094 XSETFASTINT (tem
, XFASTINT (XCDR (XCAR (Vprint_gensym_alist
))) + 1);
1096 XSETFASTINT (tem
, 1);
1097 Vprint_gensym_alist
= Fcons (Fcons (obj
, tem
), Vprint_gensym_alist
);
1100 print (tem
, printcharfun
, escapeflag
);
1108 for (i
= 0; i
< XSYMBOL (obj
)->name
->size
; i
++)
1111 c
= XSYMBOL (obj
)->name
->data
[i
];
1115 if (c
== '\"' || c
== '\\' || c
== '\''
1116 || c
== ';' || c
== '#' || c
== '(' || c
== ')'
1117 || c
== ',' || c
=='.' || c
== '`'
1118 || c
== '[' || c
== ']' || c
== '?' || c
<= 040
1120 PRINTCHAR ('\\'), confusing
= 0;
1128 /* If deeper than spec'd depth, print placeholder. */
1129 if (INTEGERP (Vprint_level
)
1130 && print_depth
> XINT (Vprint_level
))
1131 strout ("...", -1, printcharfun
);
1132 else if (print_quoted
&& CONSP (XCDR (obj
)) && NILP (XCDR (XCDR (obj
)))
1133 && (EQ (XCAR (obj
), Qquote
)))
1136 print (XCAR (XCDR (obj
)), printcharfun
, escapeflag
);
1138 else if (print_quoted
&& CONSP (XCDR (obj
)) && NILP (XCDR (XCDR (obj
)))
1139 && (EQ (XCAR (obj
), Qfunction
)))
1143 print (XCAR (XCDR (obj
)), printcharfun
, escapeflag
);
1145 else if (print_quoted
&& CONSP (XCDR (obj
)) && NILP (XCDR (XCDR (obj
)))
1146 && ((EQ (XCAR (obj
), Qbackquote
)
1147 || EQ (XCAR (obj
), Qcomma
)
1148 || EQ (XCAR (obj
), Qcomma_at
)
1149 || EQ (XCAR (obj
), Qcomma_dot
))))
1151 print (XCAR (obj
), printcharfun
, 0);
1152 print (XCAR (XCDR (obj
)), printcharfun
, escapeflag
);
1159 register int max
= 0;
1161 if (INTEGERP (Vprint_length
))
1162 max
= XINT (Vprint_length
);
1163 /* Could recognize circularities in cdrs here,
1164 but that would make printing of long lists quadratic.
1165 It's not worth doing. */
1172 strout ("...", 3, printcharfun
);
1175 print (XCAR (obj
), printcharfun
, escapeflag
);
1181 strout (" . ", 3, printcharfun
);
1182 print (obj
, printcharfun
, escapeflag
);
1188 case Lisp_Vectorlike
:
1193 strout ("#<process ", -1, printcharfun
);
1194 print_string (XPROCESS (obj
)->name
, printcharfun
);
1198 print_string (XPROCESS (obj
)->name
, printcharfun
);
1200 else if (BOOL_VECTOR_P (obj
))
1203 register unsigned char c
;
1204 struct gcpro gcpro1
;
1206 = (XBOOL_VECTOR (obj
)->size
+ BITS_PER_CHAR
- 1) / BITS_PER_CHAR
;
1212 sprintf (buf
, "%d", XBOOL_VECTOR (obj
)->size
);
1213 strout (buf
, -1, printcharfun
);
1216 /* Don't print more characters than the specified maximum. */
1217 if (INTEGERP (Vprint_length
)
1218 && XINT (Vprint_length
) < size_in_chars
)
1219 size_in_chars
= XINT (Vprint_length
);
1221 for (i
= 0; i
< size_in_chars
; i
++)
1224 c
= XBOOL_VECTOR (obj
)->data
[i
];
1225 if (c
== '\n' && print_escape_newlines
)
1230 else if (c
== '\f' && print_escape_newlines
)
1237 if (c
== '\"' || c
== '\\')
1246 else if (SUBRP (obj
))
1248 strout ("#<subr ", -1, printcharfun
);
1249 strout (XSUBR (obj
)->symbol_name
, -1, printcharfun
);
1253 else if (WINDOWP (obj
))
1255 strout ("#<window ", -1, printcharfun
);
1256 sprintf (buf
, "%d", XFASTINT (XWINDOW (obj
)->sequence_number
));
1257 strout (buf
, -1, printcharfun
);
1258 if (!NILP (XWINDOW (obj
)->buffer
))
1260 strout (" on ", -1, printcharfun
);
1261 print_string (XBUFFER (XWINDOW (obj
)->buffer
)->name
, printcharfun
);
1265 else if (BUFFERP (obj
))
1267 if (NILP (XBUFFER (obj
)->name
))
1268 strout ("#<killed buffer>", -1, printcharfun
);
1269 else if (escapeflag
)
1271 strout ("#<buffer ", -1, printcharfun
);
1272 print_string (XBUFFER (obj
)->name
, printcharfun
);
1276 print_string (XBUFFER (obj
)->name
, printcharfun
);
1278 else if (WINDOW_CONFIGURATIONP (obj
))
1280 strout ("#<window-configuration>", -1, printcharfun
);
1282 else if (FRAMEP (obj
))
1284 strout ((FRAME_LIVE_P (XFRAME (obj
))
1285 ? "#<frame " : "#<dead frame "),
1287 print_string (XFRAME (obj
)->name
, printcharfun
);
1288 sprintf (buf
, " 0x%lx", (unsigned long) (XFRAME (obj
)));
1289 strout (buf
, -1, printcharfun
);
1292 #endif /* not standalone */
1295 int size
= XVECTOR (obj
)->size
;
1296 if (COMPILEDP (obj
))
1299 size
&= PSEUDOVECTOR_SIZE_MASK
;
1301 if (CHAR_TABLE_P (obj
))
1303 /* We print a char-table as if it were a vector,
1304 lumping the parent and default slots in with the
1305 character slots. But we add #^ as a prefix. */
1308 if (SUB_CHAR_TABLE_P (obj
))
1310 size
&= PSEUDOVECTOR_SIZE_MASK
;
1312 if (size
& PSEUDOVECTOR_FLAG
)
1318 register Lisp_Object tem
;
1320 /* Don't print more elements than the specified maximum. */
1321 if (INTEGERP (Vprint_length
)
1322 && XINT (Vprint_length
) < size
)
1323 size
= XINT (Vprint_length
);
1325 for (i
= 0; i
< size
; i
++)
1327 if (i
) PRINTCHAR (' ');
1328 tem
= XVECTOR (obj
)->contents
[i
];
1329 print (tem
, printcharfun
, escapeflag
);
1338 switch (XMISCTYPE (obj
))
1340 case Lisp_Misc_Marker
:
1341 strout ("#<marker ", -1, printcharfun
);
1343 /* Do you think this is necessary? */
1344 if (XMARKER (obj
)->insertion_type
!= 0)
1345 strout ("(before-insertion) ", -1, printcharfun
);
1347 if (!(XMARKER (obj
)->buffer
))
1348 strout ("in no buffer", -1, printcharfun
);
1351 sprintf (buf
, "at %d", marker_position (obj
));
1352 strout (buf
, -1, printcharfun
);
1353 strout (" in ", -1, printcharfun
);
1354 print_string (XMARKER (obj
)->buffer
->name
, printcharfun
);
1359 case Lisp_Misc_Overlay
:
1360 strout ("#<overlay ", -1, printcharfun
);
1361 if (!(XMARKER (OVERLAY_START (obj
))->buffer
))
1362 strout ("in no buffer", -1, printcharfun
);
1365 sprintf (buf
, "from %d to %d in ",
1366 marker_position (OVERLAY_START (obj
)),
1367 marker_position (OVERLAY_END (obj
)));
1368 strout (buf
, -1, printcharfun
);
1369 print_string (XMARKER (OVERLAY_START (obj
))->buffer
->name
,
1375 /* Remaining cases shouldn't happen in normal usage, but let's print
1376 them anyway for the benefit of the debugger. */
1377 case Lisp_Misc_Free
:
1378 strout ("#<misc free cell>", -1, printcharfun
);
1381 case Lisp_Misc_Intfwd
:
1382 sprintf (buf
, "#<intfwd to %d>", *XINTFWD (obj
)->intvar
);
1383 strout (buf
, -1, printcharfun
);
1386 case Lisp_Misc_Boolfwd
:
1387 sprintf (buf
, "#<boolfwd to %s>",
1388 (*XBOOLFWD (obj
)->boolvar
? "t" : "nil"));
1389 strout (buf
, -1, printcharfun
);
1392 case Lisp_Misc_Objfwd
:
1393 strout ("#<objfwd to ", -1, printcharfun
);
1394 print (*XOBJFWD (obj
)->objvar
, printcharfun
, escapeflag
);
1398 case Lisp_Misc_Buffer_Objfwd
:
1399 strout ("#<buffer_objfwd to ", -1, printcharfun
);
1400 print (*(Lisp_Object
*)((char *)current_buffer
1401 + XBUFFER_OBJFWD (obj
)->offset
),
1402 printcharfun
, escapeflag
);
1406 case Lisp_Misc_Kboard_Objfwd
:
1407 strout ("#<kboard_objfwd to ", -1, printcharfun
);
1408 print (*(Lisp_Object
*)((char *) current_kboard
1409 + XKBOARD_OBJFWD (obj
)->offset
),
1410 printcharfun
, escapeflag
);
1414 case Lisp_Misc_Buffer_Local_Value
:
1415 strout ("#<buffer_local_value ", -1, printcharfun
);
1416 goto do_buffer_local
;
1417 case Lisp_Misc_Some_Buffer_Local_Value
:
1418 strout ("#<some_buffer_local_value ", -1, printcharfun
);
1420 strout ("[realvalue] ", -1, printcharfun
);
1421 print (XBUFFER_LOCAL_VALUE (obj
)->car
, printcharfun
, escapeflag
);
1422 strout ("[buffer] ", -1, printcharfun
);
1423 print (XCONS (XBUFFER_LOCAL_VALUE (obj
)->cdr
)->car
,
1424 printcharfun
, escapeflag
);
1425 strout ("[alist-elt] ", -1, printcharfun
);
1426 print (XCONS (XCONS (XBUFFER_LOCAL_VALUE (obj
)->cdr
)->cdr
)->car
,
1427 printcharfun
, escapeflag
);
1428 strout ("[default-value] ", -1, printcharfun
);
1429 print (XCONS (XCONS (XBUFFER_LOCAL_VALUE (obj
)->cdr
)->cdr
)->cdr
,
1430 printcharfun
, escapeflag
);
1438 #endif /* standalone */
1443 /* We're in trouble if this happens!
1444 Probably should just abort () */
1445 strout ("#<EMACS BUG: INVALID DATATYPE ", -1, printcharfun
);
1447 sprintf (buf
, "(MISC 0x%04x)", (int) XMISCTYPE (obj
));
1448 else if (VECTORLIKEP (obj
))
1449 sprintf (buf
, "(PVEC 0x%08x)", (int) XVECTOR (obj
)->size
);
1451 sprintf (buf
, "(0x%02x)", (int) XTYPE (obj
));
1452 strout (buf
, -1, printcharfun
);
1453 strout (" Save your buffers immediately and please report this bug>",
1461 #ifdef USE_TEXT_PROPERTIES
1463 /* Print a description of INTERVAL using PRINTCHARFUN.
1464 This is part of printing a string that has text properties. */
1467 print_interval (interval
, printcharfun
)
1469 Lisp_Object printcharfun
;
1472 print (make_number (interval
->position
), printcharfun
, 1);
1474 print (make_number (interval
->position
+ LENGTH (interval
)),
1477 print (interval
->plist
, printcharfun
, 1);
1480 #endif /* USE_TEXT_PROPERTIES */
1485 DEFVAR_LISP ("standard-output", &Vstandard_output
,
1486 "Output stream `print' uses by default for outputting a character.\n\
1487 This may be any function of one argument.\n\
1488 It may also be a buffer (output is inserted before point)\n\
1489 or a marker (output is inserted and the marker is advanced)\n\
1490 or the symbol t (output appears in the echo area).");
1491 Vstandard_output
= Qt
;
1492 Qstandard_output
= intern ("standard-output");
1493 staticpro (&Qstandard_output
);
1495 #ifdef LISP_FLOAT_TYPE
1496 DEFVAR_LISP ("float-output-format", &Vfloat_output_format
,
1497 "The format descriptor string used to print floats.\n\
1498 This is a %-spec like those accepted by `printf' in C,\n\
1499 but with some restrictions. It must start with the two characters `%.'.\n\
1500 After that comes an integer precision specification,\n\
1501 and then a letter which controls the format.\n\
1502 The letters allowed are `e', `f' and `g'.\n\
1503 Use `e' for exponential notation \"DIG.DIGITSeEXPT\"\n\
1504 Use `f' for decimal point notation \"DIGITS.DIGITS\".\n\
1505 Use `g' to choose the shorter of those two formats for the number at hand.\n\
1506 The precision in any of these cases is the number of digits following\n\
1507 the decimal point. With `f', a precision of 0 means to omit the\n\
1508 decimal point. 0 is not allowed with `e' or `g'.\n\n\
1509 A value of nil means to use `%.17g'.");
1510 Vfloat_output_format
= Qnil
;
1511 Qfloat_output_format
= intern ("float-output-format");
1512 staticpro (&Qfloat_output_format
);
1513 #endif /* LISP_FLOAT_TYPE */
1515 DEFVAR_LISP ("print-length", &Vprint_length
,
1516 "Maximum length of list to print before abbreviating.\n\
1517 A value of nil means no limit.");
1518 Vprint_length
= Qnil
;
1520 DEFVAR_LISP ("print-level", &Vprint_level
,
1521 "Maximum depth of list nesting to print before abbreviating.\n\
1522 A value of nil means no limit.");
1523 Vprint_level
= Qnil
;
1525 DEFVAR_BOOL ("print-escape-newlines", &print_escape_newlines
,
1526 "Non-nil means print newlines in strings as backslash-n.\n\
1527 Also print formfeeds as backslash-f.");
1528 print_escape_newlines
= 0;
1530 DEFVAR_BOOL ("print-quoted", &print_quoted
,
1531 "Non-nil means print quoted forms with reader syntax.\n\
1532 I.e., (quote foo) prints as 'foo, (function foo) as #'foo, and, backquoted\n\
1533 forms print in the new syntax.");
1536 DEFVAR_LISP ("print-gensym", &Vprint_gensym
,
1537 "Non-nil means print uninterned symbols so they will read as uninterned.\n\
1538 I.e., the value of (make-symbol "foobar
") prints as #:foobar.\n\
1539 When the uninterned symbol appears within a larger data structure,\n\
1540 in addition use the #...# and #...= constructs as needed,\n\
1541 so that multiple references to the same symbol are shared once again\n\
1542 when the text is read back.\n\
1544 If the value of `print-gensym' is a cons cell, then in addition refrain from\n\
1545 clearing `print-gensym-alist' on entry to and exit from printing functions,\n\
1546 so that the use of #...# and #...= can carry over for several separately\n\
1548 Vprint_gensym
= Qnil
;
1550 DEFVAR_LISP ("print-gensym-alist", &Vprint_gensym_alist
,
1551 "Association list of elements (GENSYM . N) to guide use of #N# and #N=.\n\
1552 In each element, GENSYM is an uninterned symbol that has been associated\n\
1553 with #N= for the specified value of N.");
1554 Vprint_gensym_alist
= Qnil
;
1556 /* prin1_to_string_buffer initialized in init_buffer_once in buffer.c */
1557 staticpro (&Vprin1_to_string_buffer
);
1560 defsubr (&Sprin1_to_string
);
1561 defsubr (&Serror_message_string
);
1565 defsubr (&Swrite_char
);
1566 defsubr (&Sexternal_debugging_output
);
1568 Qexternal_debugging_output
= intern ("external-debugging-output");
1569 staticpro (&Qexternal_debugging_output
);
1571 Qprint_escape_newlines
= intern ("print-escape-newlines");
1572 staticpro (&Qprint_escape_newlines
);
1575 defsubr (&Swith_output_to_temp_buffer
);
1576 #endif /* not standalone */