1 /* Lisp object printing and output streams.
2 Copyright (C) 1985, 1986, 1988, 1992-1995 Free Software Foundation, Inc.
3 Copyright (C) 1995, 1996 Ben Wing.
5 This file is part of XEmacs.
7 XEmacs is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 2, or (at your option) any
12 XEmacs is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with XEmacs; 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. */
22 /* Synched up with: Not synched with FSF. */
24 /* This file has been Mule-ized. */
26 /* Seriously hacked on by Ben Wing for Mule. */
31 #include "backtrace.h"
34 #include "console-tty.h"
35 #include "console-stream.h"
44 /* Define if not in float.h */
49 Lisp_Object Vstandard_output, Qstandard_output;
51 /* The subroutine object for external-debugging-output is kept here
52 for the convenience of the debugger. */
53 Lisp_Object Qexternal_debugging_output;
54 Lisp_Object Qalternate_debugging_output;
56 /* Avoid actual stack overflow in print. */
57 static int print_depth;
59 /* Detect most circularities to print finite output. */
60 #define PRINT_CIRCLE 200
61 Lisp_Object being_printed[PRINT_CIRCLE];
63 /* Maximum length of list or vector to print in full; noninteger means
64 effectively infinity */
66 Lisp_Object Vprint_length;
67 Lisp_Object Qprint_length;
69 /* Maximum length of string to print in full; noninteger means
70 effectively infinity */
72 Lisp_Object Vprint_string_length;
73 Lisp_Object Qprint_string_length;
75 /* Maximum depth of list to print in full; noninteger means
76 effectively infinity. */
78 Lisp_Object Vprint_level;
80 /* Label to use when making echo-area messages. */
82 Lisp_Object Vprint_message_label;
84 /* Nonzero means print newlines in strings as \n. */
86 int print_escape_newlines;
89 /* Non-nil means print #: before uninterned symbols.
90 Neither t nor nil means so that and don't clear Vprint_gensym_alist
91 on entry to and exit from print functions. */
92 Lisp_Object Vprint_gensym;
93 Lisp_Object Vprint_gensym_alist;
95 Lisp_Object Qprint_escape_newlines;
96 Lisp_Object Qprint_readably;
98 Lisp_Object Qdisplay_error;
99 Lisp_Object Qprint_message_label;
101 /* Force immediate output of all printed data. Used for debugging. */
102 int print_unbuffered;
104 FILE *termscript; /* Stdio stream being used for copy of all output. */
108 int stdout_needs_newline;
110 /* Write a string (in internal format) to stdio stream STREAM. */
113 write_string_to_stdio_stream (FILE *stream, struct console *con,
115 Bytecount offset, Bytecount len,
116 enum external_data_format fmt)
119 CONST Extbyte *extptr;
121 GET_CHARPTR_EXT_DATA_ALLOCA (str + offset, len, fmt, extptr, extlen);
124 fwrite (extptr, 1, extlen, stream);
126 /* Q122442 says that pipes are "treated as files, not as
127 devices", and that this is a feature. Before I found that
128 article, I thought it was a bug. Thanks MS, I feel much
130 if (stream == stdout || stream == stderr)
136 assert (CONSOLE_TTY_P (con));
137 Lstream_write (XLSTREAM (CONSOLE_TTY_DATA (con)->outstream),
140 if (stream == stdout || stream == stderr ||
141 (!stream && CONSOLE_TTY_DATA (con)->is_stdio))
145 fwrite (extptr, 1, extlen, termscript);
148 stdout_needs_newline = (extptr[extlen - 1] != '\n');
152 /* Write a string to the output location specified in FUNCTION.
153 Arguments NONRELOC, RELOC, OFFSET, and LEN are as in
154 buffer_insert_string_1() in insdel.c. */
157 output_string (Lisp_Object function, CONST Bufbyte *nonreloc,
158 Lisp_Object reloc, Bytecount offset, Bytecount len)
160 /* This function can GC */
162 /* We change the value of nonreloc (fetching it from reloc as
163 necessary), but we don't want to pass this changed value on to
164 other functions that take both a nonreloc and a reloc, or things
165 may get confused and an assertion failure in
166 fixup_internal_substring() may get triggered. */
167 CONST Bufbyte *newnonreloc = nonreloc;
168 struct gcpro gcpro1, gcpro2;
170 /* Emacs won't print while GCing, but an external debugger might */
171 if (gc_in_progress) return;
173 /* Perhaps not necessary but probably safer. */
174 GCPRO2 (function, reloc);
176 fixup_internal_substring (newnonreloc, reloc, offset, &len);
179 newnonreloc = XSTRING_DATA (reloc);
181 cclen = bytecount_to_charcount (newnonreloc + offset, len);
183 if (LSTREAMP (function))
187 /* Protect against Lstream_write() causing a GC and
188 relocating the string. For small strings, we do it by
189 alloc'ing the string and using a copy; for large strings,
193 Bufbyte *copied = alloca_array (Bufbyte, len);
194 memcpy (copied, newnonreloc + offset, len);
195 Lstream_write (XLSTREAM (function), copied, len);
199 int speccount = specpdl_depth ();
200 record_unwind_protect (restore_gc_inhibit,
201 make_int (gc_currently_forbidden));
202 gc_currently_forbidden = 1;
203 Lstream_write (XLSTREAM (function), newnonreloc + offset, len);
204 unbind_to (speccount, Qnil);
208 Lstream_write (XLSTREAM (function), newnonreloc + offset, len);
210 if (print_unbuffered)
211 Lstream_flush (XLSTREAM (function));
213 else if (BUFFERP (function))
215 CHECK_LIVE_BUFFER (function);
216 buffer_insert_string (XBUFFER (function), nonreloc, reloc, offset, len);
218 else if (MARKERP (function))
220 /* marker_position() will err if marker doesn't point anywhere. */
221 Bufpos spoint = marker_position (function);
223 buffer_insert_string_1 (XMARKER (function)->buffer,
224 spoint, nonreloc, reloc, offset, len,
226 Fset_marker (function, make_int (spoint + cclen),
227 Fmarker_buffer (function));
229 else if (FRAMEP (function))
231 /* This gets used by functions not invoking print_prepare(),
232 such as Fwrite_char, Fterpri, etc.. */
233 struct frame *f = XFRAME (function);
234 CHECK_LIVE_FRAME (function);
236 if (!EQ (Vprint_message_label, echo_area_status (f)))
237 clear_echo_area_from_print (f, Qnil, 1);
238 echo_area_append (f, nonreloc, reloc, offset, len, Vprint_message_label);
240 else if (EQ (function, Qt) || EQ (function, Qnil))
242 write_string_to_stdio_stream (stdout, 0, newnonreloc, offset, len,
247 Charcount ccoff = bytecount_to_charcount (newnonreloc, offset);
250 for (iii = ccoff; iii < cclen + ccoff; iii++)
253 make_char (charptr_emchar_n (newnonreloc, iii)));
255 newnonreloc = XSTRING_DATA (reloc);
262 #define RESET_PRINT_GENSYM do { \
263 if (!CONSP (Vprint_gensym)) \
264 Vprint_gensym_alist = Qnil; \
268 canonicalize_printcharfun (Lisp_Object printcharfun)
270 if (NILP (printcharfun))
271 printcharfun = Vstandard_output;
273 if (EQ (printcharfun, Qt) || NILP (printcharfun))
274 printcharfun = Fselected_frame (Qnil); /* print to minibuffer */
280 print_prepare (Lisp_Object printcharfun, Lisp_Object *frame_kludge)
282 /* Emacs won't print while GCing, but an external debugger might */
288 printcharfun = canonicalize_printcharfun (printcharfun);
290 /* Here we could safely return the canonicalized PRINTCHARFUN.
291 However, if PRINTCHARFUN is a frame, printing of complex
292 structures becomes very expensive, because `append-message'
293 (called by echo_area_append) gets called as many times as
294 output_string() is called (and that's a *lot*). append-message
295 tries to keep top of the message-stack in sync with the contents
296 of " *Echo Area" buffer, consing a new string for each component
297 of the printed structure. For instance, if you print (a a),
298 append-message will cons up the following strings:
306 and will use only the last one. With larger objects, this turns
307 into an O(n^2) consing frenzy that locks up XEmacs in incessant
310 We prevent this by creating a resizing_buffer stream and letting
311 the printer write into it. print_finish() will notice this
312 stream, and invoke echo_area_append() with the stream's buffer,
314 if (FRAMEP (printcharfun))
316 CHECK_LIVE_FRAME (printcharfun);
317 *frame_kludge = printcharfun;
318 printcharfun = make_resizing_buffer_output_stream ();
325 print_finish (Lisp_Object stream, Lisp_Object frame_kludge)
327 /* Emacs won't print while GCing, but an external debugger might */
333 /* See the comment in print_prepare(). */
334 if (FRAMEP (frame_kludge))
336 struct frame *f = XFRAME (frame_kludge);
337 Lstream *str = XLSTREAM (stream);
338 CHECK_LIVE_FRAME (frame_kludge);
341 if (!EQ (Vprint_message_label, echo_area_status (f)))
342 clear_echo_area_from_print (f, Qnil, 1);
343 echo_area_append (f, resizing_buffer_stream_ptr (str),
344 Qnil, 0, Lstream_byte_count (str),
345 Vprint_message_label);
346 Lstream_delete (str);
350 /* Used for printing a single-byte character (*not* any Emchar). */
351 #define write_char_internal(string_of_length_1, stream) \
352 output_string (stream, (CONST Bufbyte *) (string_of_length_1), \
355 /* NOTE: Do not call this with the data of a Lisp_String, as
356 printcharfun might cause a GC, which might cause the string's data
357 to be relocated. To princ a Lisp string, use:
359 print_internal (string, printcharfun, 0);
361 Also note that STREAM should be the result of
362 canonicalize_printcharfun() (i.e. Qnil means stdout, not
363 Vstandard_output, etc.) */
365 write_string_1 (CONST Bufbyte *str, Bytecount size, Lisp_Object stream)
367 /* This function can GC */
368 #ifdef ERROR_CHECK_BUFPOS
371 output_string (stream, str, Qnil, 0, size);
375 write_c_string (CONST char *str, Lisp_Object stream)
377 /* This function can GC */
378 write_string_1 ((CONST Bufbyte *) str, strlen (str), stream);
382 DEFUN ("write-char", Fwrite_char, 1, 2, 0, /*
383 Output character CH to stream STREAM.
384 STREAM defaults to the value of `standard-output' (which see).
388 /* This function can GC */
389 Bufbyte str[MAX_EMCHAR_LEN];
392 CHECK_CHAR_COERCE_INT (ch);
393 len = set_charptr_emchar (str, XCHAR (ch));
394 output_string (canonicalize_printcharfun (stream), str, Qnil, 0, len);
399 temp_output_buffer_setup (Lisp_Object bufname)
401 /* This function can GC */
402 struct buffer *old = current_buffer;
406 /* #### This function should accept a Lisp_Object instead of a char *,
407 so that proper translation on the buffer name can occur. */
410 Fset_buffer (Fget_buffer_create (bufname));
412 current_buffer->read_only = Qnil;
413 Ferase_buffer (Qnil);
415 XSETBUFFER (buf, current_buffer);
416 specbind (Qstandard_output, buf);
418 set_buffer_internal (old);
422 internal_with_output_to_temp_buffer (Lisp_Object bufname,
423 Lisp_Object (*function) (Lisp_Object arg),
425 Lisp_Object same_frame)
427 int speccount = specpdl_depth ();
428 struct gcpro gcpro1, gcpro2, gcpro3;
429 Lisp_Object buf = Qnil;
431 GCPRO3 (buf, arg, same_frame);
433 temp_output_buffer_setup (bufname);
434 buf = Vstandard_output;
436 arg = (*function) (arg);
438 temp_output_buffer_show (buf, same_frame);
441 return unbind_to (speccount, arg);
444 DEFUN ("with-output-to-temp-buffer", Fwith_output_to_temp_buffer, 1, UNEVALLED, 0, /*
445 Bind `standard-output' to buffer BUFNAME, eval BODY, then show that buffer.
446 The buffer is cleared out initially, and marked as unmodified when done.
447 All output done by BODY is inserted in that buffer by default.
448 The buffer is displayed in another window, but not selected.
449 The value of the last form in BODY is returned.
450 If BODY does not finish normally, the buffer BUFNAME is not displayed.
452 If variable `temp-buffer-show-function' is non-nil, call it at the end
453 to get the buffer displayed. It gets one argument, the buffer to display.
457 /* This function can GC */
458 Lisp_Object name = Qnil;
459 int speccount = specpdl_depth ();
460 struct gcpro gcpro1, gcpro2;
461 Lisp_Object val = Qnil;
464 /* #### should set the buffer to be translating. See print_internal(). */
468 name = Feval (XCAR (args));
472 temp_output_buffer_setup (name);
475 val = Fprogn (XCDR (args));
477 temp_output_buffer_show (Vstandard_output, Qnil);
479 return unbind_to (speccount, val);
482 DEFUN ("terpri", Fterpri, 0, 1, 0, /*
483 Output a newline to STREAM.
484 If STREAM is omitted or nil, the value of `standard-output' is used.
488 /* This function can GC */
489 write_char_internal ("\n", canonicalize_printcharfun (stream));
493 DEFUN ("prin1", Fprin1, 1, 2, 0, /*
494 Output the printed representation of OBJECT, any Lisp object.
495 Quoting characters are printed when needed to make output that `read'
496 can handle, whenever this is possible.
497 Output stream is STREAM, or value of `standard-output' (which see).
501 /* This function can GC */
502 Lisp_Object frame = Qnil;
503 struct gcpro gcpro1, gcpro2;
504 GCPRO2 (object, stream);
507 stream = print_prepare (stream, &frame);
508 print_internal (object, stream, 1);
509 print_finish (stream, frame);
515 DEFUN ("prin1-to-string", Fprin1_to_string, 1, 2, 0, /*
516 Return a string containing the printed representation of OBJECT,
517 any Lisp object. Quoting characters are used when needed to make output
518 that `read' can handle, whenever this is possible, unless the optional
519 second argument NOESCAPE is non-nil.
523 /* This function can GC */
524 Lisp_Object result = Qnil;
525 Lisp_Object stream = make_resizing_buffer_output_stream ();
526 Lstream *str = XLSTREAM (stream);
527 /* gcpro OBJECT in case a caller forgot to do so */
528 struct gcpro gcpro1, gcpro2, gcpro3;
529 GCPRO3 (object, stream, result);
533 print_internal (object, stream, NILP (noescape));
537 result = make_string (resizing_buffer_stream_ptr (str),
538 Lstream_byte_count (str));
539 Lstream_delete (str);
543 DEFUN ("princ", Fprinc, 1, 2, 0, /*
544 Output the printed representation of OBJECT, any Lisp object.
545 No quoting characters are used; no delimiters are printed around
546 the contents of strings.
547 Output stream is STREAM, or value of standard-output (which see).
551 /* This function can GC */
552 Lisp_Object frame = Qnil;
553 struct gcpro gcpro1, gcpro2;
555 GCPRO2 (object, stream);
556 stream = print_prepare (stream, &frame);
558 print_internal (object, stream, 0);
559 print_finish (stream, frame);
564 DEFUN ("print", Fprint, 1, 2, 0, /*
565 Output the printed representation of OBJECT, with newlines around it.
566 Quoting characters are printed when needed to make output that `read'
567 can handle, whenever this is possible.
568 Output stream is STREAM, or value of `standard-output' (which see).
572 /* This function can GC */
573 Lisp_Object frame = Qnil;
574 struct gcpro gcpro1, gcpro2;
576 GCPRO2 (object, stream);
577 stream = print_prepare (stream, &frame);
579 write_char_internal ("\n", stream);
580 print_internal (object, stream, 1);
581 write_char_internal ("\n", stream);
582 print_finish (stream, frame);
587 /* Print an error message for the error DATA to STREAM. This is a
588 complete implementation of `display-error', which used to be in
589 Lisp (see prim/cmdloop.el). It was ported to C so it can be used
590 efficiently by Ferror_message_string. Fdisplay_error and
591 Ferror_message_string are trivial wrappers around this function.
593 STREAM should be the result of canonicalize_printcharfun(). */
595 print_error_message (Lisp_Object error_object, Lisp_Object stream)
597 /* This function can GC */
598 Lisp_Object type = Fcar_safe (error_object);
599 Lisp_Object method = Qnil;
602 /* No need to GCPRO anything under the assumption that ERROR_OBJECT
605 if (! (CONSP (error_object) && SYMBOLP (type)
606 && CONSP (Fget (type, Qerror_conditions, Qnil))))
609 tail = XCDR (error_object);
617 tail = Fget (type, Qerror_conditions, Qnil);
620 if (!(CONSP (tail) && SYMBOLP (XCAR (tail))))
622 else if (!NILP (Fget (XCAR (tail), Qdisplay_error, Qnil)))
624 method = Fget (XCAR (tail), Qdisplay_error, Qnil);
633 int speccount = specpdl_depth ();
635 specbind (Qprint_message_label, Qerror);
636 tail = Fcdr (error_object);
637 if (EQ (type, Qerror))
639 print_internal (Fcar (tail), stream, 0);
644 Lisp_Object errmsg = Fget (type, Qerror_message, Qnil);
646 print_internal (type, stream, 0);
648 print_internal (LISP_GETTEXT (errmsg), stream, 0);
652 write_c_string (first ? ": " : ", ", stream);
653 print_internal (Fcar (tail), stream, 1);
657 unbind_to (speccount, Qnil);
665 write_c_string (GETTEXT ("Peculiar error "), stream);
666 print_internal (error_object, stream, 1);
671 call2 (method, error_object, stream);
675 DEFUN ("error-message-string", Ferror_message_string, 1, 1, 0, /*
676 Convert ERROR-OBJECT to an error message, and return it.
678 The format of ERROR-OBJECT should be (ERROR-SYMBOL . DATA). The
679 message is equivalent to the one that would be issued by
680 `display-error' with the same argument.
684 /* This function can GC */
685 Lisp_Object result = Qnil;
686 Lisp_Object stream = make_resizing_buffer_output_stream ();
690 print_error_message (error_object, stream);
691 Lstream_flush (XLSTREAM (stream));
692 result = make_string (resizing_buffer_stream_ptr (XLSTREAM (stream)),
693 Lstream_byte_count (XLSTREAM (stream)));
694 Lstream_delete (XLSTREAM (stream));
700 DEFUN ("display-error", Fdisplay_error, 2, 2, 0, /*
701 Display ERROR-OBJECT on STREAM in a user-friendly way.
703 (error_object, stream))
705 /* This function can GC */
706 print_error_message (error_object, canonicalize_printcharfun (stream));
711 #ifdef LISP_FLOAT_TYPE
713 Lisp_Object Vfloat_output_format;
714 Lisp_Object Qfloat_output_format;
717 * This buffer should be at least as large as the max string size of the
718 * largest float, printed in the biggest notation. This is undoubtably
719 * 20d float_output_format, with the negative of the C-constant "HUGE"
722 * On the vax the worst case is -1e38 in 20d format which takes 61 bytes.
724 * I assume that IEEE-754 format numbers can take 329 bytes for the worst
725 * case of -1e307 in 20d float_output_format. What is one to do (short of
726 * re-writing _doprnt to be more sane)?
730 float_to_string (char *buf, double data)
735 if (NILP (Vfloat_output_format)
736 || !STRINGP (Vfloat_output_format))
738 sprintf (buf, "%.16g", data);
741 /* Check that the spec we have is fully valid.
742 This means not only valid for printf,
743 but meant for floats, and reasonable. */
744 cp = XSTRING_DATA (Vfloat_output_format);
752 for (width = 0; (c = *cp, isdigit (c)); cp++)
758 if (*cp != 'e' && *cp != 'f' && *cp != 'g' && *cp != 'E' && *cp != 'G')
761 if (width < (int) (*cp != 'e' && *cp != 'E') || width > DBL_DIG)
767 sprintf (buf, (char *) XSTRING_DATA (Vfloat_output_format),
771 /* added by jwz: don't allow "1.0" to print as "1"; that destroys
772 the read-equivalence of lisp objects. (* x 1) and (* x 1.0) do
773 not do the same thing, so it's important that the printed
774 representation of that form not be corrupted by the printer.
777 Bufbyte *s = (Bufbyte *) buf; /* don't use signed chars here!
778 isdigit() can't hack them! */
781 /* if there's a non-digit, then there is a decimal point, or
782 it's in exponential notation, both of which are ok. */
785 /* otherwise, we need to hack it. */
792 /* Some machines print "0.4" as ".4". I don't like that. */
793 if (buf [0] == '.' || (buf [0] == '-' && buf [1] == '.'))
796 for (i = strlen (buf) + 1; i >= 0; i--)
798 buf [(buf [0] == '-' ? 1 : 0)] = '0';
801 #endif /* LISP_FLOAT_TYPE */
803 /* Print NUMBER to BUFFER. The digits are first written in reverse
804 order (the least significant digit first), and are then reversed.
805 This is equivalent to sprintf(buffer, "%ld", number), only much
808 BUFFER should accept 24 bytes. This should suffice for the longest
809 numbers on 64-bit machines, including the `-' sign and the trailing
812 long_to_string (char *buffer, long number)
814 #if (SIZEOF_LONG != 4) && (SIZEOF_LONG != 8)
816 sprintf (buffer, "%ld", number);
817 #else /* (SIZEOF_LONG == 4) || (SIZEOF_LONG == 8) */
827 #define FROB(figure) do { \
828 if (force || number >= figure) \
829 *p++ = number / figure + '0', number %= figure, force = 1; \
832 FROB (1000000000000000000L);
833 FROB (100000000000000000L);
834 FROB (10000000000000000L);
835 FROB (1000000000000000L);
836 FROB (100000000000000L);
837 FROB (10000000000000L);
838 FROB (1000000000000L);
839 FROB (100000000000L);
841 #endif /* SIZEOF_LONG == 8 */
854 #endif /* (SIZEOF_LONG == 4) || (SIZEOF_LONG == 8) */
858 print_vector_internal (CONST char *start, CONST char *end,
860 Lisp_Object printcharfun, int escapeflag)
862 /* This function can GC */
864 int len = XVECTOR_LENGTH (obj);
866 struct gcpro gcpro1, gcpro2;
867 GCPRO2 (obj, printcharfun);
869 if (INTP (Vprint_length))
871 int max = XINT (Vprint_length);
872 if (max < len) last = max;
875 write_c_string (start, printcharfun);
876 for (i = 0; i < last; i++)
878 Lisp_Object elt = XVECTOR_DATA (obj)[i];
879 if (i != 0) write_char_internal (" ", printcharfun);
880 print_internal (elt, printcharfun, escapeflag);
884 write_c_string (" ...", printcharfun);
885 write_c_string (end, printcharfun);
889 print_cons (Lisp_Object obj, Lisp_Object printcharfun, int escapeflag)
891 /* This function can GC */
892 struct gcpro gcpro1, gcpro2;
894 /* If print_readably is on, print (quote -foo-) as '-foo-
895 (Yeah, this should really be what print-pretty does, but we
896 don't have the rest of a pretty printer, and this actually
897 has non-negligible impact on size/speed of .elc files.)
899 if (print_readably &&
900 EQ (XCAR (obj), Qquote) &&
901 CONSP (XCDR (obj)) &&
902 NILP (XCDR (XCDR (obj))))
904 obj = XCAR (XCDR (obj));
905 GCPRO2 (obj, printcharfun);
906 write_char_internal ("\'", printcharfun);
908 print_internal (obj, printcharfun, escapeflag);
912 GCPRO2 (obj, printcharfun);
913 write_char_internal ("(", printcharfun);
917 int max = INTP (Vprint_length) ? XINT (Vprint_length) : INT_MAX;
918 Lisp_Object tortoise;
919 /* Use tortoise/hare to make sure circular lists don't infloop */
921 for (tortoise = obj, len = 0;
923 obj = XCDR (obj), len++)
926 write_char_internal (" ", printcharfun);
927 if (EQ (obj, tortoise) && len > 0)
930 error ("printing unreadable circular list");
932 write_c_string ("... <circular list>", printcharfun);
936 tortoise = XCDR (tortoise);
939 write_c_string ("...", printcharfun);
942 print_internal (XCAR (obj), printcharfun, escapeflag);
947 write_c_string (" . ", printcharfun);
948 print_internal (obj, printcharfun, escapeflag);
952 write_char_internal (")", printcharfun);
957 print_vector (Lisp_Object obj, Lisp_Object printcharfun, int escapeflag)
959 print_vector_internal ("[", "]", obj, printcharfun, escapeflag);
963 print_string (Lisp_Object obj, Lisp_Object printcharfun, int escapeflag)
965 struct Lisp_String *s = XSTRING (obj);
966 /* We distinguish between Bytecounts and Charcounts, to make
967 Vprint_string_length work correctly under Mule. */
968 Charcount size = string_char_length (s);
969 Charcount max = size;
970 Bytecount bcmax = string_length (s);
971 struct gcpro gcpro1, gcpro2;
972 GCPRO2 (obj, printcharfun);
974 if (INTP (Vprint_string_length) &&
975 XINT (Vprint_string_length) < max)
977 max = XINT (Vprint_string_length);
978 bcmax = charcount_to_bytecount (string_data (s), max);
988 /* This deals with GC-relocation and Mule. */
989 output_string (printcharfun, 0, obj, 0, bcmax);
991 write_c_string (" ...", printcharfun);
995 Bytecount i, last = 0;
997 write_char_internal ("\"", printcharfun);
998 for (i = 0; i < bcmax; i++)
1000 Bufbyte ch = string_byte (s, i);
1001 if (ch == '\"' || ch == '\\'
1002 || (ch == '\n' && print_escape_newlines))
1006 output_string (printcharfun, 0, obj, last,
1011 write_c_string ("\\n", printcharfun);
1015 write_char_internal ("\\", printcharfun);
1016 /* This is correct for Mule because the
1017 character is either \ or " */
1018 write_char_internal (string_data (s) + i, printcharfun);
1025 output_string (printcharfun, 0, obj, last,
1029 write_c_string (" ...", printcharfun);
1030 write_char_internal ("\"", printcharfun);
1036 default_object_printer (Lisp_Object obj, Lisp_Object printcharfun,
1039 struct lcrecord_header *header =
1040 (struct lcrecord_header *) XPNTR (obj);
1044 error ("printing unreadable object #<%s 0x%x>",
1045 LHEADER_IMPLEMENTATION (&header->lheader)->name,
1048 sprintf (buf, "#<%s 0x%x>",
1049 LHEADER_IMPLEMENTATION (&header->lheader)->name,
1051 write_c_string (buf, printcharfun);
1055 internal_object_printer (Lisp_Object obj, Lisp_Object printcharfun,
1059 sprintf (buf, "#<INTERNAL OBJECT (XEmacs bug?) (%s) 0x%lx>",
1060 XRECORD_LHEADER_IMPLEMENTATION (obj)->name,
1061 (unsigned long) XPNTR (obj));
1062 write_c_string (buf, printcharfun);
1066 print_internal (Lisp_Object obj, Lisp_Object printcharfun, int escapeflag)
1068 /* This function can GC */
1072 /* Emacs won't print while GCing, but an external debugger might */
1073 if (gc_in_progress) return;
1076 /* #### Both input and output streams should have a flag associated
1077 with them indicating whether output to that stream, or strings
1078 read from the stream, get translated using Fgettext(). Such a
1079 stream is called a "translating stream". For the minibuffer and
1080 external-debugging-output this is always true on output, and
1081 with-output-to-temp-buffer sets the flag to true for the buffer
1082 it creates. This flag should also be user-settable. Perhaps it
1083 should be split up into two flags, one for input and one for
1087 /* Detect circularities and truncate them.
1088 No need to offer any alternative--this is better than an error. */
1089 if (CONSP (obj) || VECTORP (obj) || COMPILED_FUNCTIONP (obj))
1092 for (i = 0; i < print_depth; i++)
1093 if (EQ (obj, being_printed[i]))
1097 long_to_string (buf + 1, i);
1098 write_c_string (buf, printcharfun);
1103 being_printed[print_depth] = obj;
1106 if (print_depth > PRINT_CIRCLE)
1107 error ("Apparently circular structure being printed");
1109 switch (XTYPE (obj))
1111 case Lisp_Type_Int_Even:
1112 case Lisp_Type_Int_Odd:
1115 long_to_string (buf, XINT (obj));
1116 write_c_string (buf, printcharfun);
1120 case Lisp_Type_Char:
1122 /* God intended that this be #\..., you know. */
1124 Emchar ch = XCHAR (obj);
1128 *p++ = '\\', *p++ = 'n';
1129 else if (ch == '\r')
1130 *p++ = '\\', *p++ = 'r';
1131 else if (ch == '\t')
1132 *p++ = '\\', *p++ = 't';
1135 *p++ = '\\', *p++ = '^';
1137 if ((ch + 64) == '\\')
1141 *p++ = '\\', *p++ = '^', *p++ = '?';
1142 else if (ch >= 128 && ch < 160)
1144 *p++ = '\\', *p++ = '^';
1145 p += set_charptr_emchar ((Bufbyte *)p, ch + 64);
1150 && ch != '^') /* must not backslash this or it will
1151 be interpreted as the start of a
1153 *p++ = '\\', *p++ = ch;
1155 p += set_charptr_emchar ((Bufbyte *)p, ch);
1156 output_string (printcharfun, (Bufbyte *)buf, Qnil, 0, p - buf);
1160 case Lisp_Type_Record:
1162 struct lrecord_header *lheader = XRECORD_LHEADER (obj);
1163 struct gcpro gcpro1, gcpro2;
1165 if (CONSP (obj) || VECTORP(obj))
1167 /* If deeper than spec'd depth, print placeholder. */
1168 if (INTP (Vprint_level)
1169 && print_depth > XINT (Vprint_level))
1171 GCPRO2 (obj, printcharfun);
1172 write_c_string ("...", printcharfun);
1178 GCPRO2 (obj, printcharfun);
1179 if (LHEADER_IMPLEMENTATION (lheader)->printer)
1180 ((LHEADER_IMPLEMENTATION (lheader)->printer)
1181 (obj, printcharfun, escapeflag));
1183 default_object_printer (obj, printcharfun, escapeflag);
1190 #ifdef ERROR_CHECK_TYPECHECK
1192 #else /* not ERROR_CHECK_TYPECHECK */
1194 /* We're in trouble if this happens! */
1196 error ("printing illegal data type #o%03o",
1198 write_c_string ("#<EMACS BUG: ILLEGAL DATATYPE ",
1200 sprintf (buf, "(#o%3o)", (int) XTYPE (obj));
1201 write_c_string (buf, printcharfun);
1203 (" Save your buffers immediately and please report this bug>",
1205 #endif /* not ERROR_CHECK_TYPECHECK */
1214 #ifdef LISP_FLOAT_TYPE
1216 print_float (Lisp_Object obj, Lisp_Object printcharfun, int escapeflag)
1218 char pigbuf[350]; /* see comments in float_to_string */
1220 float_to_string (pigbuf, XFLOAT_DATA (obj));
1221 write_c_string (pigbuf, printcharfun);
1223 #endif /* LISP_FLOAT_TYPE */
1226 print_symbol (Lisp_Object obj, Lisp_Object printcharfun, int escapeflag)
1228 /* This function can GC */
1229 /* #### Bug!! (intern "") isn't printed in some distinguished way */
1230 /* #### (the reader also loses on it) */
1231 struct Lisp_String *name = symbol_name (XSYMBOL (obj));
1232 Bytecount size = string_length (name);
1233 struct gcpro gcpro1, gcpro2;
1237 /* This deals with GC-relocation */
1238 Lisp_Object nameobj;
1239 XSETSTRING (nameobj, name);
1240 output_string (printcharfun, 0, nameobj, 0, size);
1243 GCPRO2 (obj, printcharfun);
1245 /* If we print an uninterned symbol as part of a complex object and
1246 the flag print-gensym is non-nil, prefix it with #n= to read the
1247 object back with the #n# reader syntax later if needed. */
1248 if (!NILP (Vprint_gensym)
1249 /* #### Test whether this produces a noticable slow-down for
1250 printing when print-gensym is non-nil. */
1251 && !EQ (obj, oblookup (Vobarray,
1252 string_data (symbol_name (XSYMBOL (obj))),
1253 string_length (symbol_name (XSYMBOL (obj))))))
1255 if (print_depth > 1)
1257 Lisp_Object tem = Fassq (obj, Vprint_gensym_alist);
1260 write_char_internal ("#", printcharfun);
1261 print_internal (XCDR (tem), printcharfun, escapeflag);
1262 write_char_internal ("#", printcharfun);
1267 if (CONSP (Vprint_gensym_alist))
1269 /* Vprint_gensym_alist is exposed to Lisp, so we
1270 have to be careful. */
1271 CHECK_CONS (XCAR (Vprint_gensym_alist));
1272 CHECK_INT (XCDR (XCAR (Vprint_gensym_alist)));
1273 XSETINT (tem, XINT (XCDR (XCAR (Vprint_gensym_alist))) + 1);
1277 Vprint_gensym_alist = Fcons (Fcons (obj, tem), Vprint_gensym_alist);
1279 write_char_internal ("#", printcharfun);
1280 print_internal (tem, printcharfun, escapeflag);
1281 write_char_internal ("=", printcharfun);
1284 write_c_string ("#:", printcharfun);
1287 /* Does it look like an integer or a float? */
1289 Bufbyte *data = string_data (name);
1290 Bytecount confusing = 0;
1293 goto not_yet_confused; /* Really confusing */
1294 else if (isdigit (data[0]))
1297 goto not_yet_confused;
1298 else if (data[0] == '-' || data[0] == '+')
1301 goto not_yet_confused;
1303 for (; confusing < size; confusing++)
1305 if (!isdigit (data[confusing]))
1313 #ifdef LISP_FLOAT_TYPE
1315 /* #### Ugh, this is needlessly complex and slow for what we
1316 need here. It might be a good idea to copy equivalent code
1317 from FSF. --hniksic */
1318 confusing = isfloat_string ((char *) data);
1321 write_char_internal ("\\", printcharfun);
1325 Lisp_Object nameobj;
1329 XSETSTRING (nameobj, name);
1330 for (i = 0; i < size; i++)
1332 switch (string_byte (name, i))
1334 case 0: case 1: case 2: case 3:
1335 case 4: case 5: case 6: case 7:
1336 case 8: case 9: case 10: case 11:
1337 case 12: case 13: case 14: case 15:
1338 case 16: case 17: case 18: case 19:
1339 case 20: case 21: case 22: case 23:
1340 case 24: case 25: case 26: case 27:
1341 case 28: case 29: case 30: case 31:
1342 case ' ': case '\"': case '\\': case '\'':
1343 case ';': case '#' : case '(' : case ')':
1344 case ',': case '.' : case '`' :
1345 case '[': case ']' : case '?' :
1347 output_string (printcharfun, 0, nameobj, last, i - last);
1348 write_char_internal ("\\", printcharfun);
1352 output_string (printcharfun, 0, nameobj, last, size - last);
1357 /* #ifdef DEBUG_XEMACS */
1359 /* I don't like seeing `Note: Strange doc (not fboundp) for function
1360 alternate-debugging-output @ 429542' -slb */
1361 /* #### Eek! Any clue how to get rid of it? In fact, how about
1362 getting rid of this function altogether? Does anything actually
1363 *use* it? --hniksic */
1365 int alternate_do_pointer;
1366 char alternate_do_string[5000];
1368 DEFUN ("alternate-debugging-output", Falternate_debugging_output, 1, 1, 0, /*
1369 Append CHARACTER to the array `alternate_do_string'.
1370 This can be used in place of `external-debugging-output' as a function
1371 to be passed to `print'. Before calling `print', set `alternate_do_pointer'
1376 Bufbyte str[MAX_EMCHAR_LEN];
1379 CONST Extbyte *extptr;
1381 CHECK_CHAR_COERCE_INT (character);
1382 len = set_charptr_emchar (str, XCHAR (character));
1383 GET_CHARPTR_EXT_DATA_ALLOCA (str, len, FORMAT_TERMINAL, extptr, extlen);
1384 memcpy (alternate_do_string + alternate_do_pointer, extptr, extlen);
1385 alternate_do_pointer += extlen;
1386 alternate_do_string[alternate_do_pointer] = 0;
1389 /* #endif / * DEBUG_XEMACS */
1391 DEFUN ("external-debugging-output", Fexternal_debugging_output, 1, 3, 0, /*
1392 Write CHAR-OR-STRING to stderr or stdout.
1393 If optional arg STDOUT-P is non-nil, write to stdout; otherwise, write
1394 to stderr. You can use this function to write directly to the terminal.
1395 This function can be used as the STREAM argument of Fprint() or the like.
1397 If you have opened a termscript file (using `open-termscript'), then
1398 the output also will be logged to this file.
1400 (char_or_string, stdout_p, device))
1403 struct console *con = 0;
1407 if (!NILP (stdout_p))
1414 CHECK_LIVE_DEVICE (device);
1415 if (!DEVICE_TTY_P (XDEVICE (device)) &&
1416 !DEVICE_STREAM_P (XDEVICE (device)))
1417 signal_simple_error ("Must be tty or stream device", device);
1418 con = XCONSOLE (DEVICE_CONSOLE (XDEVICE (device)));
1419 if (DEVICE_TTY_P (XDEVICE (device)))
1421 else if (!NILP (stdout_p))
1422 file = CONSOLE_STREAM_DATA (con)->out;
1424 file = CONSOLE_STREAM_DATA (con)->err;
1427 if (STRINGP (char_or_string))
1428 write_string_to_stdio_stream (file, con,
1429 XSTRING_DATA (char_or_string),
1430 0, XSTRING_LENGTH (char_or_string),
1434 Bufbyte str[MAX_EMCHAR_LEN];
1437 CHECK_CHAR_COERCE_INT (char_or_string);
1438 len = set_charptr_emchar (str, XCHAR (char_or_string));
1439 write_string_to_stdio_stream (file, con, str, 0, len, FORMAT_TERMINAL);
1442 return char_or_string;
1445 DEFUN ("open-termscript", Fopen_termscript, 1, 1, "FOpen termscript file: ", /*
1446 Start writing all terminal output to FILE as well as the terminal.
1447 FILE = nil means just close any termscript file currently open.
1451 /* This function can GC */
1452 if (termscript != 0)
1453 fclose (termscript);
1458 file = Fexpand_file_name (file, Qnil);
1459 termscript = fopen ((char *) XSTRING_DATA (file), "w");
1460 if (termscript == NULL)
1461 report_file_error ("Opening termscript", list1 (file));
1467 /* Debugging kludge -- unbuffered */
1468 static int debug_print_length = 50;
1469 static int debug_print_level = 15;
1470 Lisp_Object debug_temp;
1473 debug_print_no_newline (Lisp_Object debug_print_obj)
1475 /* This function can GC */
1476 int old_print_readably = print_readably;
1477 int old_print_depth = print_depth;
1478 Lisp_Object old_print_length = Vprint_length;
1479 Lisp_Object old_print_level = Vprint_level;
1480 Lisp_Object old_inhibit_quit = Vinhibit_quit;
1481 struct gcpro gcpro1, gcpro2, gcpro3;
1482 GCPRO3 (old_print_level, old_print_length, old_inhibit_quit);
1485 stderr_out ("** gc-in-progress! Bad idea to print anything! **\n");
1490 /* Could use unwind-protect, but why bother? */
1491 if (debug_print_length > 0)
1492 Vprint_length = make_int (debug_print_length);
1493 if (debug_print_level > 0)
1494 Vprint_level = make_int (debug_print_level);
1495 print_internal (debug_print_obj, Qexternal_debugging_output, 1);
1496 Vinhibit_quit = old_inhibit_quit;
1497 Vprint_level = old_print_level;
1498 Vprint_length = old_print_length;
1499 print_depth = old_print_depth;
1500 print_readably = old_print_readably;
1506 debug_print (Lisp_Object debug_print_obj)
1508 debug_print_no_newline (debug_print_obj);
1513 /* Debugging kludge -- unbuffered */
1514 /* This function provided for the benefit of the debugger. */
1515 void debug_backtrace (void);
1517 debug_backtrace (void)
1519 /* This function can GC */
1520 int old_print_readably = print_readably;
1521 int old_print_depth = print_depth;
1522 Lisp_Object old_print_length = Vprint_length;
1523 Lisp_Object old_print_level = Vprint_level;
1524 Lisp_Object old_inhibit_quit = Vinhibit_quit;
1526 struct gcpro gcpro1, gcpro2, gcpro3;
1527 GCPRO3 (old_print_level, old_print_length, old_inhibit_quit);
1530 stderr_out ("** gc-in-progress! Bad idea to print anything! **\n");
1535 /* Could use unwind-protect, but why bother? */
1536 if (debug_print_length > 0)
1537 Vprint_length = make_int (debug_print_length);
1538 if (debug_print_level > 0)
1539 Vprint_level = make_int (debug_print_level);
1541 Fbacktrace (Qexternal_debugging_output, Qt);
1545 Vinhibit_quit = old_inhibit_quit;
1546 Vprint_level = old_print_level;
1547 Vprint_length = old_print_length;
1548 print_depth = old_print_depth;
1549 print_readably = old_print_readably;
1556 debug_short_backtrace (int length)
1559 struct backtrace *bt = backtrace_list;
1562 while (length > 0 && bt)
1569 if (COMPILED_FUNCTIONP (*bt->function))
1571 #if defined(COMPILED_FUNCTION_ANNOTATION_HACK)
1573 compiled_function_annotation (XCOMPILED_FUNCTION (*bt->function));
1575 Lisp_Object ann = Qnil;
1579 stderr_out ("<compiled-function from ");
1581 debug_print_no_newline (ann);
1587 stderr_out ("<compiled-function of unknown origin>");
1592 debug_print_no_newline (*bt->function);
1601 #endif /* debugging kludge */
1605 syms_of_print (void)
1607 defsymbol (&Qprint_escape_newlines, "print-escape-newlines");
1608 defsymbol (&Qprint_readably, "print-readably");
1610 defsymbol (&Qstandard_output, "standard-output");
1612 #ifdef LISP_FLOAT_TYPE
1613 defsymbol (&Qfloat_output_format, "float-output-format");
1616 defsymbol (&Qprint_length, "print-length");
1618 defsymbol (&Qprint_string_length, "print-string-length");
1620 defsymbol (&Qdisplay_error, "display-error");
1621 defsymbol (&Qprint_message_label, "print-message-label");
1624 DEFSUBR (Fprin1_to_string);
1627 DEFSUBR (Ferror_message_string);
1628 DEFSUBR (Fdisplay_error);
1630 DEFSUBR (Fwrite_char);
1631 DEFSUBR (Falternate_debugging_output);
1632 defsymbol (&Qalternate_debugging_output, "alternate-debugging-output");
1633 DEFSUBR (Fexternal_debugging_output);
1634 DEFSUBR (Fopen_termscript);
1635 defsymbol (&Qexternal_debugging_output, "external-debugging-output");
1636 DEFSUBR (Fwith_output_to_temp_buffer);
1640 vars_of_print (void)
1642 alternate_do_pointer = 0;
1644 DEFVAR_LISP ("standard-output", &Vstandard_output /*
1645 Output stream `print' uses by default for outputting a character.
1646 This may be any function of one argument.
1647 It may also be a buffer (output is inserted before point)
1648 or a marker (output is inserted and the marker is advanced)
1649 or the symbol t (output appears in the minibuffer line).
1651 Vstandard_output = Qt;
1653 #ifdef LISP_FLOAT_TYPE
1654 DEFVAR_LISP ("float-output-format", &Vfloat_output_format /*
1655 The format descriptor string that lisp uses to print floats.
1656 This is a %-spec like those accepted by `printf' in C,
1657 but with some restrictions. It must start with the two characters `%.'.
1658 After that comes an integer precision specification,
1659 and then a letter which controls the format.
1660 The letters allowed are `e', `f' and `g'.
1661 Use `e' for exponential notation "DIG.DIGITSeEXPT"
1662 Use `f' for decimal point notation "DIGITS.DIGITS".
1663 Use `g' to choose the shorter of those two formats for the number at hand.
1664 The precision in any of these cases is the number of digits following
1665 the decimal point. With `f', a precision of 0 means to omit the
1666 decimal point. 0 is not allowed with `f' or `g'.
1668 A value of nil means to use `%.16g'.
1670 Regardless of the value of `float-output-format', a floating point number
1671 will never be printed in such a way that it is ambiguous with an integer;
1672 that is, a floating-point number will always be printed with a decimal
1673 point and/or an exponent, even if the digits following the decimal point
1674 are all zero. This is to preserve read-equivalence.
1676 Vfloat_output_format = Qnil;
1677 #endif /* LISP_FLOAT_TYPE */
1679 DEFVAR_LISP ("print-length", &Vprint_length /*
1680 Maximum length of list or vector to print before abbreviating.
1681 A value of nil means no limit.
1683 Vprint_length = Qnil;
1685 DEFVAR_LISP ("print-string-length", &Vprint_string_length /*
1686 Maximum length of string to print before abbreviating.
1687 A value of nil means no limit.
1689 Vprint_string_length = Qnil;
1691 DEFVAR_LISP ("print-level", &Vprint_level /*
1692 Maximum depth of list nesting to print before abbreviating.
1693 A value of nil means no limit.
1695 Vprint_level = Qnil;
1697 DEFVAR_BOOL ("print-escape-newlines", &print_escape_newlines /*
1698 Non-nil means print newlines in strings as backslash-n.
1700 print_escape_newlines = 0;
1702 DEFVAR_BOOL ("print-readably", &print_readably /*
1703 If non-nil, then all objects will be printed in a readable form.
1704 If an object has no readable representation, then an error is signalled.
1705 When print-readably is true, compiled-function objects will be written in
1706 #[...] form instead of in #<compiled-function [...]> form, and two-element
1707 lists of the form (quote object) will be written as the equivalent 'object.
1708 Do not SET this variable; bind it instead.
1712 /* #### I think this should default to t. But we'd better wait
1713 until we see that it works out. */
1714 DEFVAR_LISP ("print-gensym", &Vprint_gensym /*
1715 If non-nil, then uninterned symbols will be printed specially.
1716 Uninterned symbols are those which are not present in `obarray', that is,
1717 those which were made with `make-symbol' or by calling `intern' with a
1720 When print-gensym is true, such symbols will be preceded by "#:",
1721 which causes the reader to create a new symbol instead of interning
1722 and returning an existing one. Beware: the #: syntax creates a new
1723 symbol each time it is seen, so if you print an object which contains
1724 two pointers to the same uninterned symbol, `read' will not duplicate
1727 If the value of `print-gensym' is a cons cell, then in addition
1728 refrain from clearing `print-gensym-alist' on entry to and exit from
1729 printing functions, so that the use of #...# and #...= can carry over
1730 for several separately printed objects.
1732 Vprint_gensym = Qnil;
1734 DEFVAR_LISP ("print-gensym-alist", &Vprint_gensym_alist /*
1735 Association list of elements (GENSYM . N) to guide use of #N# and #N=.
1736 In each element, GENSYM is an uninterned symbol that has been associated
1737 with #N= for the specified value of N.
1739 Vprint_gensym_alist = Qnil;
1741 DEFVAR_LISP ("print-message-label", &Vprint_message_label /*
1742 Label for minibuffer messages created with `print'. This should
1743 generally be bound with `let' rather than set. (See `display-message'.)
1745 Vprint_message_label = Qprint;