1 /* Lisp object printing and output streams.
2 Copyright (C) 1985, 1986, 1988 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 1, 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, 675 Mass Ave, Cambridge, MA 02139, USA. */
31 #include "dispextern.h"
33 #endif /* not standalone */
35 Lisp_Object Vstandard_output
, Qstandard_output
;
37 #ifdef LISP_FLOAT_TYPE
38 Lisp_Object Vfloat_output_format
, Qfloat_output_format
;
39 #endif /* LISP_FLOAT_TYPE */
41 /* Avoid actual stack overflow in print. */
44 /* Maximum length of list to print in full; noninteger means
45 effectively infinity */
47 Lisp_Object Vprint_length
;
49 /* Maximum depth of list to print in full; noninteger means
50 effectively infinity. */
52 Lisp_Object Vprint_level
;
54 /* Nonzero means print newlines in strings as \n. */
56 int print_escape_newlines
;
58 Lisp_Object Qprint_escape_newlines
;
60 /* Nonzero means print newline before next minibuffer message.
63 extern int noninteractive_need_newline
;
64 #ifdef MAX_PRINT_CHARS
65 static int print_chars
;
67 #endif /* MAX_PRINT_CHARS */
70 /* Convert between chars and GLYPHs */
74 register GLYPH
*glyphs
;
84 str_to_glyph_cpy (str
, glyphs
)
88 register GLYPH
*gp
= glyphs
;
89 register char *cp
= str
;
96 str_to_glyph_ncpy (str
, glyphs
, n
)
101 register GLYPH
*gp
= glyphs
;
102 register char *cp
= str
;
109 glyph_to_str_cpy (glyphs
, str
)
113 register GLYPH
*gp
= glyphs
;
114 register char *cp
= str
;
117 *str
++ = *gp
++ & 0377;
121 /* Low level output routines for charaters and strings */
123 /* Lisp functions to do output using a stream
124 must have the stream in a variable called printcharfun
125 and must start with PRINTPREPARE and end with PRINTFINISH.
126 Use PRINTCHAR to output one character,
127 or call strout to output a block of characters.
128 Also, each one must have the declarations
129 struct buffer *old = current_buffer;
130 int old_point = -1, start_point;
131 Lisp_Object original;
134 #define PRINTPREPARE \
135 original = printcharfun; \
136 if (NULL (printcharfun)) printcharfun = Qt; \
137 if (XTYPE (printcharfun) == Lisp_Buffer) \
138 { if (XBUFFER (printcharfun) != current_buffer) Fset_buffer (printcharfun); \
139 printcharfun = Qnil;}\
140 if (XTYPE (printcharfun) == Lisp_Marker) \
141 { if (XMARKER (original)->buffer != current_buffer) \
142 set_buffer_internal (XMARKER (original)->buffer); \
144 SET_PT (marker_position (printcharfun)); \
145 start_point = point; \
146 printcharfun = Qnil;}
148 #define PRINTFINISH \
149 if (XTYPE (original) == Lisp_Marker) \
150 Fset_marker (original, make_number (point), Qnil); \
151 if (old_point >= 0) \
152 SET_PT ((old_point >= start_point ? point - start_point : 0) + old_point); \
153 if (old != current_buffer) \
154 set_buffer_internal (old)
156 #define PRINTCHAR(ch) printchar (ch, printcharfun)
158 /* Index of first unused element of message_buf */
159 static int printbufidx
;
168 #ifdef MAX_PRINT_CHARS
171 #endif /* MAX_PRINT_CHARS */
185 noninteractive_need_newline
= 1;
189 if (echo_area_glyphs
!= SCREEN_MESSAGE_BUF (selected_screen
)
190 || !message_buf_print
)
192 echo_area_glyphs
= SCREEN_MESSAGE_BUF (selected_screen
);
194 message_buf_print
= 1;
197 if (printbufidx
< SCREEN_WIDTH (selected_screen
) - 1)
198 SCREEN_MESSAGE_BUF (selected_screen
)[printbufidx
++] = ch
;
199 SCREEN_MESSAGE_BUF (selected_screen
)[printbufidx
] = 0;
203 #endif /* not standalone */
210 strout (ptr
, size
, printcharfun
)
213 Lisp_Object printcharfun
;
217 if (EQ (printcharfun
, Qnil
))
219 insert (ptr
, size
>= 0 ? size
: strlen (ptr
));
220 #ifdef MAX_PRINT_CHARS
222 print_chars
+= size
>= 0 ? size
: strlen(ptr
);
223 #endif /* MAX_PRINT_CHARS */
226 if (EQ (printcharfun
, Qt
))
228 i
= size
>= 0 ? size
: strlen (ptr
);
229 #ifdef MAX_PRINT_CHARS
232 #endif /* MAX_PRINT_CHARS */
236 fwrite (ptr
, 1, i
, stdout
);
237 noninteractive_need_newline
= 1;
241 if (echo_area_glyphs
!= SCREEN_MESSAGE_BUF (selected_screen
)
242 || !message_buf_print
)
244 echo_area_glyphs
= SCREEN_MESSAGE_BUF (selected_screen
);
246 message_buf_print
= 1;
249 if (i
> SCREEN_WIDTH (selected_screen
) - printbufidx
- 1)
250 i
= SCREEN_WIDTH (selected_screen
) - printbufidx
- 1;
251 bcopy (ptr
, &SCREEN_MESSAGE_BUF (selected_screen
) [printbufidx
], i
);
253 SCREEN_MESSAGE_BUF (selected_screen
) [printbufidx
] = 0;
260 PRINTCHAR (ptr
[i
++]);
263 PRINTCHAR (ptr
[i
++]);
266 /* Print the contents of a string STRING using PRINTCHARFUN.
267 It isn't safe to use strout, because printing one char can relocate. */
269 print_string (string
, printcharfun
)
271 Lisp_Object printcharfun
;
273 if (EQ (printcharfun
, Qnil
) || EQ (printcharfun
, Qt
))
274 /* In predictable cases, strout is safe: output to buffer or screen. */
275 strout (XSTRING (string
)->data
, XSTRING (string
)->size
, printcharfun
);
278 /* Otherwise, fetch the string address for each character. */
280 int size
= XSTRING (string
)->size
;
283 for (i
= 0; i
< size
; i
++)
284 PRINTCHAR (XSTRING (string
)->data
[i
]);
289 DEFUN ("write-char", Fwrite_char
, Swrite_char
, 1, 2, 0,
290 "Output character CHAR to stream STREAM.\n\
291 STREAM defaults to the value of `standard-output' (which see).")
293 Lisp_Object ch
, printcharfun
;
295 struct buffer
*old
= current_buffer
;
298 Lisp_Object original
;
300 if (NULL (printcharfun
))
301 printcharfun
= Vstandard_output
;
302 CHECK_NUMBER (ch
, 0);
304 PRINTCHAR (XINT (ch
));
309 /* Used from outside of print.c to print a block of SIZE chars at DATA
310 on the default output stream.
311 Do not use this on the contents of a Lisp string. */
313 write_string (data
, size
)
317 struct buffer
*old
= current_buffer
;
318 Lisp_Object printcharfun
;
321 Lisp_Object original
;
323 printcharfun
= Vstandard_output
;
326 strout (data
, size
, printcharfun
);
330 /* Used from outside of print.c to print a block of SIZE chars at DATA
331 on a specified stream PRINTCHARFUN.
332 Do not use this on the contents of a Lisp string. */
334 write_string_1 (data
, size
, printcharfun
)
337 Lisp_Object printcharfun
;
339 struct buffer
*old
= current_buffer
;
342 Lisp_Object original
;
345 strout (data
, size
, printcharfun
);
353 temp_output_buffer_setup (bufname
)
356 register struct buffer
*old
= current_buffer
;
357 register Lisp_Object buf
;
359 Fset_buffer (Fget_buffer_create (build_string (bufname
)));
361 current_buffer
->read_only
= Qnil
;
364 XSET (buf
, Lisp_Buffer
, current_buffer
);
365 specbind (Qstandard_output
, buf
);
367 set_buffer_internal (old
);
371 internal_with_output_to_temp_buffer (bufname
, function
, args
)
373 Lisp_Object (*function
) ();
376 int count
= specpdl_ptr
- specpdl
;
377 Lisp_Object buf
, val
;
379 record_unwind_protect (Fset_buffer
, Fcurrent_buffer ());
380 temp_output_buffer_setup (bufname
);
381 buf
= Vstandard_output
;
383 val
= (*function
) (args
);
385 temp_output_buffer_show (buf
);
387 return unbind_to (count
, val
);
390 DEFUN ("with-output-to-temp-buffer", Fwith_output_to_temp_buffer
, Swith_output_to_temp_buffer
,
392 "Bind `standard-output' to buffer BUFNAME, eval BODY, then show that buffer.\n\
393 The buffer is cleared out initially, and marked as unmodified when done.\n\
394 All output done by BODY is inserted in that buffer by default.\n\
395 The buffer is displayed in another window, but not selected.\n\
396 The value of the last form in BODY is returned.\n\
397 If BODY does not finish normally, the buffer BUFNAME is not displayed.\n\n\
398 If variable `temp-buffer-show-hook' is non-nil, call it at the end\n\
399 to get the buffer displayed. It gets one argument, the buffer to display.")
405 int count
= specpdl_ptr
- specpdl
;
406 Lisp_Object buf
, val
;
409 name
= Feval (Fcar (args
));
412 CHECK_STRING (name
, 0);
413 temp_output_buffer_setup (XSTRING (name
)->data
);
414 buf
= Vstandard_output
;
416 val
= Fprogn (Fcdr (args
));
418 temp_output_buffer_show (buf
);
420 return unbind_to (count
, val
);
422 #endif /* not standalone */
424 static void print ();
426 DEFUN ("terpri", Fterpri
, Sterpri
, 0, 1, 0,
427 "Output a newline to STREAM.\n\
428 If STREAM is omitted or nil, the value of `standard-output' is used.")
430 Lisp_Object printcharfun
;
432 struct buffer
*old
= current_buffer
;
435 Lisp_Object original
;
437 if (NULL (printcharfun
))
438 printcharfun
= Vstandard_output
;
445 DEFUN ("prin1", Fprin1
, Sprin1
, 1, 2, 0,
446 "Output the printed representation of OBJECT, any Lisp object.\n\
447 Quoting characters are printed when needed to make output that `read'\n\
448 can handle, whenever this is possible.\n\
449 Output stream is STREAM, or value of `standard-output' (which see).")
451 Lisp_Object obj
, printcharfun
;
453 struct buffer
*old
= current_buffer
;
456 Lisp_Object original
;
458 #ifdef MAX_PRINT_CHARS
460 #endif /* MAX_PRINT_CHARS */
461 if (NULL (printcharfun
))
462 printcharfun
= Vstandard_output
;
465 print (obj
, printcharfun
, 1);
470 /* a buffer which is used to hold output being built by prin1-to-string */
471 Lisp_Object Vprin1_to_string_buffer
;
473 DEFUN ("prin1-to-string", Fprin1_to_string
, Sprin1_to_string
, 1, 2, 0,
474 "Return a string containing the printed representation of OBJECT,\n\
475 any Lisp object. Quoting characters are used when needed to make output\n\
476 that `read' can handle, whenever this is possible, unless the optional\n\
477 second argument NOESCAPE is non-nil.")
479 Lisp_Object obj
, noescape
;
481 struct buffer
*old
= current_buffer
;
484 Lisp_Object original
, printcharfun
;
487 printcharfun
= Vprin1_to_string_buffer
;
490 print (obj
, printcharfun
, NULL (noescape
));
491 /* Make Vprin1_to_string_buffer be the default buffer after PRINTFINSH */
493 set_buffer_internal (XBUFFER (Vprin1_to_string_buffer
));
494 obj
= Fbuffer_string ();
498 set_buffer_internal (old
);
504 DEFUN ("princ", Fprinc
, Sprinc
, 1, 2, 0,
505 "Output the printed representation of OBJECT, any Lisp object.\n\
506 No quoting characters are used; no delimiters are printed around\n\
507 the contents of strings.\n\
508 Output stream is STREAM, or value of standard-output (which see).")
510 Lisp_Object obj
, printcharfun
;
512 struct buffer
*old
= current_buffer
;
515 Lisp_Object original
;
517 if (NULL (printcharfun
))
518 printcharfun
= Vstandard_output
;
521 print (obj
, printcharfun
, 0);
526 DEFUN ("print", Fprint
, Sprint
, 1, 2, 0,
527 "Output the printed representation of OBJECT, with newlines around it.\n\
528 Quoting characters are printed when needed to make output that `read'\n\
529 can handle, whenever this is possible.\n\
530 Output stream is STREAM, or value of `standard-output' (which see).")
532 Lisp_Object obj
, printcharfun
;
534 struct buffer
*old
= current_buffer
;
537 Lisp_Object original
;
540 #ifdef MAX_PRINT_CHARS
542 max_print
= MAX_PRINT_CHARS
;
543 #endif /* MAX_PRINT_CHARS */
544 if (NULL (printcharfun
))
545 printcharfun
= Vstandard_output
;
550 print (obj
, printcharfun
, 1);
553 #ifdef MAX_PRINT_CHARS
556 #endif /* MAX_PRINT_CHARS */
561 /* The subroutine object for external-debugging-output is kept here
562 for the convenience of the debugger. */
563 Lisp_Object Qexternal_debugging_output
;
565 DEFUN ("external-debugging-output",
566 Fexternal_debugging_output
, Sexternal_debugging_output
,
567 1, 1, 0, "Write CHARACTER to stderr.\n\
568 You can call print while debugging emacs, and pass it this function\n\
569 to make it write to the debugging output.\n")
570 (Lisp_Object character
)
572 CHECK_NUMBER (character
, 0);
573 putc (XINT (character
), stderr
);
578 #ifdef LISP_FLOAT_TYPE
581 float_to_string (buf
, data
)
584 * This buffer should be at least as large as the max string size of the
585 * largest float, printed in the biggest notation. This is undoubtably
586 * 20d float_output_format, with the negative of the C-constant "HUGE"
589 * On the vax the worst case is -1e38 in 20d format which takes 61 bytes.
591 * I assume that IEEE-754 format numbers can take 329 bytes for the worst
592 * case of -1e307 in 20d float_output_format. What is one to do (short of
593 * re-writing _doprnt to be more sane)?
598 register unsigned char *cp
, c
;
601 if (NULL (Vfloat_output_format
)
602 || XTYPE (Vfloat_output_format
) != Lisp_String
)
604 sprintf (buf
, "%.20g", data
);
607 /* Check that the spec we have is fully valid.
608 This means not only valid for printf,
609 but meant for floats, and reasonable. */
610 cp
= XSTRING (Vfloat_output_format
)->data
;
619 ((c
= *cp
) >= '0' && c
<= '9');
626 if (*cp
!= 'e' && *cp
!= 'f' && *cp
!= 'g')
629 if (width
< (*cp
!= 'e') || width
> DBL_DIG
)
635 sprintf (buf
, XSTRING (Vfloat_output_format
)->data
, data
);
638 #endif /* LISP_FLOAT_TYPE */
641 print (obj
, printcharfun
, escapeflag
)
642 #ifndef RTPC_REGISTER_BUG
643 register Lisp_Object obj
;
647 register Lisp_Object printcharfun
;
656 if (print_depth
> 200)
657 error ("Apparently circular structure being printed");
658 #ifdef MAX_PRINT_CHARS
659 if (max_print
&& print_chars
> max_print
)
664 #endif /* MAX_PRINT_CHARS */
666 #ifdef SWITCH_ENUM_BUG
667 switch ((int) XTYPE (obj
))
673 /* We're in trouble if this happens!
674 Probably should just abort () */
675 strout ("#<EMACS BUG: INVALID DATATYPE ", -1, printcharfun
);
676 sprintf (buf
, "(#o%3o)", (int) XTYPE (obj
));
677 strout (buf
, -1, printcharfun
);
678 strout (" Save your buffers immediately and please report this bug>",
682 #ifdef LISP_FLOAT_TYPE
685 char pigbuf
[350]; /* see comments in float_to_string */
687 float_to_string (pigbuf
, XFLOAT(obj
)->data
);
688 strout (pigbuf
, -1, printcharfun
);
691 #endif /* LISP_FLOAT_TYPE */
694 sprintf (buf
, "%d", XINT (obj
));
695 strout (buf
, -1, printcharfun
);
700 print_string (obj
, printcharfun
);
704 register unsigned char c
;
708 /* You can't gcpro register variables, so copy obj to a
709 non-register variable so we can gcpro it without
710 making it non-register. */
715 for (i
= 0; i
< XSTRING (obj
)->size
; i
++)
718 c
= XSTRING (obj
)->data
[i
];
719 if (c
== '\n' && print_escape_newlines
)
726 if (c
== '\"' || c
== '\\')
738 register int confusing
;
739 register unsigned char *p
= XSYMBOL (obj
)->name
->data
;
740 register unsigned char *end
= p
+ XSYMBOL (obj
)->name
->size
;
741 register unsigned char c
;
743 if (p
!= end
&& (*p
== '-' || *p
== '+')) p
++;
748 while (p
!= end
&& *p
>= '0' && *p
<= '9')
750 confusing
= (end
== p
);
753 p
= XSYMBOL (obj
)->name
->data
;
760 if (c
== '\"' || c
== '\\' || c
== '\'' || c
== ';' || c
== '#' ||
761 c
== '(' || c
== ')' || c
== ',' || c
=='.' || c
== '`' ||
762 c
== '[' || c
== ']' || c
== '?' || c
<= 040 || confusing
)
763 PRINTCHAR ('\\'), confusing
= 0;
771 /* If deeper than spec'd depth, print placeholder. */
772 if (XTYPE (Vprint_level
) == Lisp_Int
773 && print_depth
> XINT (Vprint_level
))
775 strout ("...", -1, printcharfun
);
782 register int max
= 0;
784 if (XTYPE (Vprint_length
) == Lisp_Int
)
785 max
= XINT (Vprint_length
);
792 strout ("...", 3, printcharfun
);
795 print (Fcar (obj
), printcharfun
, escapeflag
);
799 if (!NULL (obj
) && !CONSP (obj
))
801 strout (" . ", 3, printcharfun
);
802 print (obj
, printcharfun
, escapeflag
);
808 strout ("#<byte-code ", -1, printcharfun
);
813 register Lisp_Object tem
;
814 for (i
= 0; i
< XVECTOR (obj
)->size
; i
++)
816 if (i
) PRINTCHAR (' ');
817 tem
= XVECTOR (obj
)->contents
[i
];
818 print (tem
, printcharfun
, escapeflag
);
822 if (XTYPE (obj
) == Lisp_Compiled
)
828 if (NULL (XBUFFER (obj
)->name
))
829 strout ("#<killed buffer>", -1, printcharfun
);
832 strout ("#<buffer ", -1, printcharfun
);
833 print_string (XBUFFER (obj
)->name
, printcharfun
);
837 print_string (XBUFFER (obj
)->name
, printcharfun
);
843 strout ("#<process ", -1, printcharfun
);
844 print_string (XPROCESS (obj
)->name
, printcharfun
);
848 print_string (XPROCESS (obj
)->name
, printcharfun
);
852 strout ("#<window ", -1, printcharfun
);
853 sprintf (buf
, "%d", XFASTINT (XWINDOW (obj
)->sequence_number
));
854 strout (buf
, -1, printcharfun
);
855 if (!NULL (XWINDOW (obj
)->buffer
))
857 strout (" on ", -1, printcharfun
);
858 print_string (XBUFFER (XWINDOW (obj
)->buffer
)->name
, printcharfun
);
863 case Lisp_Window_Configuration
:
864 strout ("#<window-configuration>", -1, printcharfun
);
869 strout ("#<screen ", -1, printcharfun
);
870 print_string (XSCREEN (obj
)->name
, printcharfun
);
871 sprintf (buf
, " 0x%x", XFASTINT (XSCREEN (obj
)));
872 strout (buf
, -1, printcharfun
);
873 strout (">", -1, printcharfun
);
875 #endif /* MULTI_SCREEN */
878 strout ("#<marker ", -1, printcharfun
);
879 if (!(XMARKER (obj
)->buffer
))
880 strout ("in no buffer", -1, printcharfun
);
883 sprintf (buf
, "at %d", marker_position (obj
));
884 strout (buf
, -1, printcharfun
);
885 strout (" in ", -1, printcharfun
);
886 print_string (XMARKER (obj
)->buffer
->name
, printcharfun
);
890 #endif /* standalone */
893 strout ("#<subr ", -1, printcharfun
);
894 strout (XSUBR (obj
)->symbol_name
, -1, printcharfun
);
905 staticpro (&Qprint_escape_newlines
);
906 Qprint_escape_newlines
= intern ("print-escape-newlines");
908 DEFVAR_LISP ("standard-output", &Vstandard_output
,
909 "Output stream `print' uses by default for outputting a character.\n\
910 This may be any function of one argument.\n\
911 It may also be a buffer (output is inserted before point)\n\
912 or a marker (output is inserted and the marker is advanced)\n\
913 or the symbol t (output appears in the minibuffer line).");
914 Vstandard_output
= Qt
;
915 Qstandard_output
= intern ("standard-output");
916 staticpro (&Qstandard_output
);
918 #ifdef LISP_FLOAT_TYPE
919 DEFVAR_LISP ("float-output-format", &Vfloat_output_format
,
920 "The format descriptor string that lisp uses to print floats.\n\
921 This is a %-spec like those accepted by `printf' in C,\n\
922 but with some restrictions. It must start with the two characters `%.'.\n\
923 After that comes an integer precision specification,\n\
924 and then a letter which controls the format.\n\
925 The letters allowed are `e', `f' and `g'.\n\
926 Use `e' for exponential notation \"DIG.DIGITSeEXPT\"\n\
927 Use `f' for decimal point notation \"DIGITS.DIGITS\".\n\
928 Use `g' to choose the shorter of those two formats for the number at hand.\n\
929 The precision in any of these cases is the number of digits following\n\
930 the decimal point. With `f', a precision of 0 means to omit the\n\
931 decimal point. 0 is not allowed with `f' or `g'.\n\n\
932 A value of nil means to use `%.20g'.");
933 Vfloat_output_format
= Qnil
;
934 Qfloat_output_format
= intern ("float-output-format");
935 staticpro (&Qfloat_output_format
);
936 #endif /* LISP_FLOAT_TYPE */
938 DEFVAR_LISP ("print-length", &Vprint_length
,
939 "Maximum length of list to print before abbreviating.\
940 A value of nil means no limit.");
941 Vprint_length
= Qnil
;
943 DEFVAR_LISP ("print-level", &Vprint_level
,
944 "Maximum depth of list nesting to print before abbreviating.\
945 A value of nil means no limit.");
948 DEFVAR_BOOL ("print-escape-newlines", &print_escape_newlines
,
949 "Non-nil means print newlines in strings as backslash-n.");
950 print_escape_newlines
= 0;
952 /* prin1_to_string_buffer initialized in init_buffer_once in buffer.c */
953 staticpro (&Vprin1_to_string_buffer
);
956 defsubr (&Sprin1_to_string
);
960 defsubr (&Swrite_char
);
961 defsubr (&Sexternal_debugging_output
);
963 Qexternal_debugging_output
= intern ("external-debugging-output");
964 staticpro (&Qexternal_debugging_output
);
967 defsubr (&Swith_output_to_temp_buffer
);
968 #endif /* not standalone */