(xmalloc, xrealloc, xfree): Define using POINTER_TYPE.
[emacs.git] / src / print.c
blob1943ed4082f4ace4397e35483f735169d51c314c
1 /* Lisp object printing and output streams.
2 Copyright (C) 1985, 86, 88, 93, 94, 95, 97, 98, 1999
3 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
23 #include <config.h>
24 #include <stdio.h>
25 #include "lisp.h"
26 #include "buffer.h"
27 #include "charset.h"
28 #include "frame.h"
29 #include "window.h"
30 #include "process.h"
31 #include "dispextern.h"
32 #include "termchar.h"
33 #include "keyboard.h"
34 #include "intervals.h"
36 Lisp_Object Vstandard_output, Qstandard_output;
38 Lisp_Object Qtemp_buffer_setup_hook;
40 /* These are used to print like we read. */
41 extern Lisp_Object Qbackquote, Qcomma, Qcomma_at, Qcomma_dot, Qfunction;
43 Lisp_Object Vfloat_output_format, Qfloat_output_format;
45 /* Work around a problem that happens because math.h on hpux 7
46 defines two static variables--which, in Emacs, are not really static,
47 because `static' is defined as nothing. The problem is that they are
48 defined both here and in lread.c.
49 These macros prevent the name conflict. */
50 #if defined (HPUX) && !defined (HPUX8)
51 #define _MAXLDBL print_maxldbl
52 #define _NMAXLDBL print_nmaxldbl
53 #endif
55 #include <math.h>
57 #if STDC_HEADERS
58 #include <float.h>
59 #endif
61 /* Default to values appropriate for IEEE floating point. */
62 #ifndef FLT_RADIX
63 #define FLT_RADIX 2
64 #endif
65 #ifndef DBL_MANT_DIG
66 #define DBL_MANT_DIG 53
67 #endif
68 #ifndef DBL_DIG
69 #define DBL_DIG 15
70 #endif
71 #ifndef DBL_MIN
72 #define DBL_MIN 2.2250738585072014e-308
73 #endif
75 #ifdef DBL_MIN_REPLACEMENT
76 #undef DBL_MIN
77 #define DBL_MIN DBL_MIN_REPLACEMENT
78 #endif
80 /* Define DOUBLE_DIGITS_BOUND, an upper bound on the number of decimal digits
81 needed to express a float without losing information.
82 The general-case formula is valid for the usual case, IEEE floating point,
83 but many compilers can't optimize the formula to an integer constant,
84 so make a special case for it. */
85 #if FLT_RADIX == 2 && DBL_MANT_DIG == 53
86 #define DOUBLE_DIGITS_BOUND 17 /* IEEE floating point */
87 #else
88 #define DOUBLE_DIGITS_BOUND ((int) ceil (log10 (pow (FLT_RADIX, DBL_MANT_DIG))))
89 #endif
91 /* Avoid actual stack overflow in print. */
92 int print_depth;
94 /* Detect most circularities to print finite output. */
95 #define PRINT_CIRCLE 200
96 Lisp_Object being_printed[PRINT_CIRCLE];
98 /* When printing into a buffer, first we put the text in this
99 block, then insert it all at once. */
100 char *print_buffer;
102 /* Size allocated in print_buffer. */
103 int print_buffer_size;
104 /* Chars stored in print_buffer. */
105 int print_buffer_pos;
106 /* Bytes stored in print_buffer. */
107 int print_buffer_pos_byte;
109 /* Maximum length of list to print in full; noninteger means
110 effectively infinity */
112 Lisp_Object Vprint_length;
114 /* Maximum depth of list to print in full; noninteger means
115 effectively infinity. */
117 Lisp_Object Vprint_level;
119 /* Nonzero means print newlines in strings as \n. */
121 int print_escape_newlines;
123 /* Nonzero means to print single-byte non-ascii characters in strings as
124 octal escapes. */
126 int print_escape_nonascii;
128 /* Nonzero means to print multibyte characters in strings as hex escapes. */
130 int print_escape_multibyte;
132 Lisp_Object Qprint_escape_newlines;
133 Lisp_Object Qprint_escape_multibyte, Qprint_escape_nonascii;
135 /* Nonzero means print (quote foo) forms as 'foo, etc. */
137 int print_quoted;
139 /* Non-nil means print #: before uninterned symbols. */
141 Lisp_Object Vprint_gensym;
143 /* Non-nil means print recursive structures using #n= and #n# syntax. */
145 Lisp_Object Vprint_circle;
147 /* Non-nil means keep continuous number for #n= and #n# syntax
148 between several print functions. */
150 Lisp_Object Vprint_continuous_numbering;
152 /* Vprint_number_table is a vector like [OBJ1 STAT1 OBJ2 STAT2 ...],
153 where OBJn are objects going to be printed, and STATn are their status,
154 which may be different meanings during process. See the comments of
155 the functions print and print_preprocess for details.
156 print_number_index keeps the last position the next object should be added,
157 twice of which is the actual vector position in Vprint_number_table. */
158 int print_number_index;
159 Lisp_Object Vprint_number_table;
161 /* PRINT_NUMBER_OBJECT returns the I'th object in Vprint_number_table TABLE.
162 PRINT_NUMBER_STATUS returns the status of the I'th object in TABLE.
163 See the comment of the variable Vprint_number_table. */
164 #define PRINT_NUMBER_OBJECT(table,i) XVECTOR ((table))->contents[(i) * 2]
165 #define PRINT_NUMBER_STATUS(table,i) XVECTOR ((table))->contents[(i) * 2 + 1]
167 /* Nonzero means print newline to stdout before next minibuffer message.
168 Defined in xdisp.c */
170 extern int noninteractive_need_newline;
172 extern int minibuffer_auto_raise;
174 #ifdef MAX_PRINT_CHARS
175 static int print_chars;
176 static int max_print;
177 #endif /* MAX_PRINT_CHARS */
179 void print_interval ();
182 /* Low level output routines for characters and strings */
184 /* Lisp functions to do output using a stream
185 must have the stream in a variable called printcharfun
186 and must start with PRINTPREPARE, end with PRINTFINISH,
187 and use PRINTDECLARE to declare common variables.
188 Use PRINTCHAR to output one character,
189 or call strout to output a block of characters. */
191 #define PRINTDECLARE \
192 struct buffer *old = current_buffer; \
193 int old_point = -1, start_point; \
194 int old_point_byte, start_point_byte; \
195 int specpdl_count = specpdl_ptr - specpdl; \
196 int free_print_buffer = 0; \
197 int multibyte = !NILP (current_buffer->enable_multibyte_characters); \
198 Lisp_Object original
200 #define PRINTPREPARE \
201 original = printcharfun; \
202 if (NILP (printcharfun)) printcharfun = Qt; \
203 if (BUFFERP (printcharfun)) \
205 if (XBUFFER (printcharfun) != current_buffer) \
206 Fset_buffer (printcharfun); \
207 printcharfun = Qnil; \
209 if (MARKERP (printcharfun)) \
211 if (!(XMARKER (original)->buffer)) \
212 error ("Marker does not point anywhere"); \
213 if (XMARKER (original)->buffer != current_buffer) \
214 set_buffer_internal (XMARKER (original)->buffer); \
215 old_point = PT; \
216 old_point_byte = PT_BYTE; \
217 SET_PT_BOTH (marker_position (printcharfun), \
218 marker_byte_position (printcharfun)); \
219 start_point = PT; \
220 start_point_byte = PT_BYTE; \
221 printcharfun = Qnil; \
223 if (NILP (printcharfun)) \
225 Lisp_Object string; \
226 if (NILP (current_buffer->enable_multibyte_characters) \
227 && ! print_escape_multibyte) \
228 specbind (Qprint_escape_multibyte, Qt); \
229 if (! NILP (current_buffer->enable_multibyte_characters) \
230 && ! print_escape_nonascii) \
231 specbind (Qprint_escape_nonascii, Qt); \
232 if (print_buffer != 0) \
234 string = make_string_from_bytes (print_buffer, \
235 print_buffer_pos, \
236 print_buffer_pos_byte); \
237 record_unwind_protect (print_unwind, string); \
239 else \
241 print_buffer_size = 1000; \
242 print_buffer = (char *) xmalloc (print_buffer_size); \
243 free_print_buffer = 1; \
245 print_buffer_pos = 0; \
246 print_buffer_pos_byte = 0; \
248 if (EQ (printcharfun, Qt) && ! noninteractive) \
249 setup_echo_area_for_printing (multibyte);
251 #define PRINTFINISH \
252 if (NILP (printcharfun)) \
254 if (print_buffer_pos != print_buffer_pos_byte \
255 && NILP (current_buffer->enable_multibyte_characters)) \
257 unsigned char *temp \
258 = (unsigned char *) alloca (print_buffer_pos + 1); \
259 copy_text (print_buffer, temp, print_buffer_pos_byte, \
260 1, 0); \
261 insert_1_both (temp, print_buffer_pos, \
262 print_buffer_pos, 0, 1, 0); \
264 else \
265 insert_1_both (print_buffer, print_buffer_pos, \
266 print_buffer_pos_byte, 0, 1, 0); \
268 if (free_print_buffer) \
270 xfree (print_buffer); \
271 print_buffer = 0; \
273 unbind_to (specpdl_count, Qnil); \
274 if (MARKERP (original)) \
275 set_marker_both (original, Qnil, PT, PT_BYTE); \
276 if (old_point >= 0) \
277 SET_PT_BOTH (old_point + (old_point >= start_point \
278 ? PT - start_point : 0), \
279 old_point_byte + (old_point_byte >= start_point_byte \
280 ? PT_BYTE - start_point_byte : 0)); \
281 if (old != current_buffer) \
282 set_buffer_internal (old);
284 #define PRINTCHAR(ch) printchar (ch, printcharfun)
286 /* This is used to restore the saved contents of print_buffer
287 when there is a recursive call to print. */
289 static Lisp_Object
290 print_unwind (saved_text)
291 Lisp_Object saved_text;
293 bcopy (XSTRING (saved_text)->data, print_buffer, XSTRING (saved_text)->size);
297 /* Print character CH using method FUN. FUN nil means print to
298 print_buffer. FUN t means print to echo area or stdout if
299 non-interactive. If FUN is neither nil nor t, call FUN with CH as
300 argument. */
302 static void
303 printchar (ch, fun)
304 unsigned int ch;
305 Lisp_Object fun;
307 #ifdef MAX_PRINT_CHARS
308 if (max_print)
309 print_chars++;
310 #endif /* MAX_PRINT_CHARS */
312 if (!NILP (fun) && !EQ (fun, Qt))
313 call1 (fun, make_number (ch));
314 else
316 unsigned char str[MAX_MULTIBYTE_LENGTH];
317 int len = CHAR_STRING (ch, str);
319 QUIT;
321 if (NILP (fun))
323 if (print_buffer_pos_byte + len >= print_buffer_size)
324 print_buffer = (char *) xrealloc (print_buffer,
325 print_buffer_size *= 2);
326 bcopy (str, print_buffer + print_buffer_pos_byte, len);
327 print_buffer_pos += 1;
328 print_buffer_pos_byte += len;
330 else if (noninteractive)
332 fwrite (str, 1, len, stdout);
333 noninteractive_need_newline = 1;
335 else
337 int multibyte_p
338 = !NILP (current_buffer->enable_multibyte_characters);
340 setup_echo_area_for_printing (multibyte_p);
341 insert_char (ch);
342 message_dolog (str, len, 0, multibyte_p);
348 /* Output SIZE characters, SIZE_BYTE bytes from string PTR using
349 method PRINTCHARFUN. If SIZE < 0, use the string length of PTR for
350 both SIZE and SIZE_BYTE. PRINTCHARFUN nil means output to
351 print_buffer. PRINTCHARFUN t means output to the echo area or to
352 stdout if non-interactive. If neither nil nor t, call Lisp
353 function PRINTCHARFUN for each character printed. MULTIBYTE
354 non-zero means PTR contains multibyte characters. */
356 static void
357 strout (ptr, size, size_byte, printcharfun, multibyte)
358 char *ptr;
359 int size, size_byte;
360 Lisp_Object printcharfun;
361 int multibyte;
363 if (size < 0)
364 size_byte = size = strlen (ptr);
366 if (NILP (printcharfun))
368 if (print_buffer_pos_byte + size_byte > print_buffer_size)
370 print_buffer_size = print_buffer_size * 2 + size_byte;
371 print_buffer = (char *) xrealloc (print_buffer,
372 print_buffer_size);
374 bcopy (ptr, print_buffer + print_buffer_pos_byte, size_byte);
375 print_buffer_pos += size;
376 print_buffer_pos_byte += size_byte;
378 #ifdef MAX_PRINT_CHARS
379 if (max_print)
380 print_chars += size;
381 #endif /* MAX_PRINT_CHARS */
383 else if (noninteractive && EQ (printcharfun, Qt))
385 fwrite (ptr, 1, size_byte, stdout);
386 noninteractive_need_newline = 1;
388 else if (EQ (printcharfun, Qt))
390 /* Output to echo area. We're trying to avoid a little overhead
391 here, that's the reason we don't call printchar to do the
392 job. */
393 int i;
394 int multibyte_p
395 = !NILP (current_buffer->enable_multibyte_characters);
397 setup_echo_area_for_printing (multibyte_p);
398 message_dolog (ptr, size_byte, 0, multibyte_p);
400 if (size == size_byte)
402 for (i = 0; i < size; ++i)
403 insert_char (*ptr++);
405 else
407 int len;
408 for (i = 0; i < size_byte; i += len)
410 int ch = STRING_CHAR_AND_LENGTH (ptr + i, size_byte - i, len);
411 insert_char (ch);
415 #ifdef MAX_PRINT_CHARS
416 if (max_print)
417 print_chars += size;
418 #endif /* MAX_PRINT_CHARS */
420 else
422 /* PRINTCHARFUN is a Lisp function. */
423 int i = 0;
425 if (size == size_byte)
427 while (i < size_byte)
429 int ch = ptr[i++];
430 PRINTCHAR (ch);
433 else
435 while (i < size_byte)
437 /* Here, we must convert each multi-byte form to the
438 corresponding character code before handing it to
439 PRINTCHAR. */
440 int len;
441 int ch = STRING_CHAR_AND_LENGTH (ptr + i, size_byte - i, len);
442 PRINTCHAR (ch);
443 i += len;
449 /* Print the contents of a string STRING using PRINTCHARFUN.
450 It isn't safe to use strout in many cases,
451 because printing one char can relocate. */
453 static void
454 print_string (string, printcharfun)
455 Lisp_Object string;
456 Lisp_Object printcharfun;
458 if (EQ (printcharfun, Qt) || NILP (printcharfun))
460 int chars;
462 if (STRING_MULTIBYTE (string))
463 chars = XSTRING (string)->size;
464 else if (EQ (printcharfun, Qt)
465 ? ! NILP (buffer_defaults.enable_multibyte_characters)
466 : ! NILP (current_buffer->enable_multibyte_characters))
467 chars = multibyte_chars_in_text (XSTRING (string)->data,
468 STRING_BYTES (XSTRING (string)));
469 else
470 chars = STRING_BYTES (XSTRING (string));
472 /* strout is safe for output to a frame (echo area) or to print_buffer. */
473 strout (XSTRING (string)->data,
474 chars, STRING_BYTES (XSTRING (string)),
475 printcharfun, STRING_MULTIBYTE (string));
477 else
479 /* Otherwise, string may be relocated by printing one char.
480 So re-fetch the string address for each character. */
481 int i;
482 int size = XSTRING (string)->size;
483 int size_byte = STRING_BYTES (XSTRING (string));
484 struct gcpro gcpro1;
485 GCPRO1 (string);
486 if (size == size_byte)
487 for (i = 0; i < size; i++)
488 PRINTCHAR (XSTRING (string)->data[i]);
489 else
490 for (i = 0; i < size_byte; i++)
492 /* Here, we must convert each multi-byte form to the
493 corresponding character code before handing it to PRINTCHAR. */
494 int len;
495 int ch = STRING_CHAR_AND_LENGTH (XSTRING (string)->data + i,
496 size_byte - i, len);
497 if (!CHAR_VALID_P (ch, 0))
499 ch = XSTRING (string)->data[i];
500 len = 1;
502 PRINTCHAR (ch);
503 i += len;
505 UNGCPRO;
509 DEFUN ("write-char", Fwrite_char, Swrite_char, 1, 2, 0,
510 "Output character CHARACTER to stream PRINTCHARFUN.\n\
511 PRINTCHARFUN defaults to the value of `standard-output' (which see).")
512 (character, printcharfun)
513 Lisp_Object character, printcharfun;
515 PRINTDECLARE;
517 if (NILP (printcharfun))
518 printcharfun = Vstandard_output;
519 CHECK_NUMBER (character, 0);
520 PRINTPREPARE;
521 PRINTCHAR (XINT (character));
522 PRINTFINISH;
523 return character;
526 /* Used from outside of print.c to print a block of SIZE
527 single-byte chars at DATA on the default output stream.
528 Do not use this on the contents of a Lisp string. */
530 void
531 write_string (data, size)
532 char *data;
533 int size;
535 PRINTDECLARE;
536 Lisp_Object printcharfun;
538 printcharfun = Vstandard_output;
540 PRINTPREPARE;
541 strout (data, size, size, printcharfun, 0);
542 PRINTFINISH;
545 /* Used from outside of print.c to print a block of SIZE
546 single-byte chars at DATA on a specified stream PRINTCHARFUN.
547 Do not use this on the contents of a Lisp string. */
549 void
550 write_string_1 (data, size, printcharfun)
551 char *data;
552 int size;
553 Lisp_Object printcharfun;
555 PRINTDECLARE;
557 PRINTPREPARE;
558 strout (data, size, size, printcharfun, 0);
559 PRINTFINISH;
563 void
564 temp_output_buffer_setup (bufname)
565 char *bufname;
567 int count = specpdl_ptr - specpdl;
568 register struct buffer *old = current_buffer;
569 register Lisp_Object buf;
571 record_unwind_protect (set_buffer_if_live, Fcurrent_buffer ());
573 Fset_buffer (Fget_buffer_create (build_string (bufname)));
575 current_buffer->directory = old->directory;
576 current_buffer->read_only = Qnil;
577 current_buffer->filename = Qnil;
578 current_buffer->undo_list = Qt;
579 current_buffer->overlays_before = Qnil;
580 current_buffer->overlays_after = Qnil;
581 current_buffer->enable_multibyte_characters
582 = buffer_defaults.enable_multibyte_characters;
583 Ferase_buffer ();
584 XSETBUFFER (buf, current_buffer);
586 call1 (Vrun_hooks, Qtemp_buffer_setup_hook);
588 unbind_to (count, Qnil);
590 specbind (Qstandard_output, buf);
593 Lisp_Object
594 internal_with_output_to_temp_buffer (bufname, function, args)
595 char *bufname;
596 Lisp_Object (*function) P_ ((Lisp_Object));
597 Lisp_Object args;
599 int count = specpdl_ptr - specpdl;
600 Lisp_Object buf, val;
601 struct gcpro gcpro1;
603 GCPRO1 (args);
604 record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
605 temp_output_buffer_setup (bufname);
606 buf = Vstandard_output;
607 UNGCPRO;
609 val = (*function) (args);
611 GCPRO1 (val);
612 temp_output_buffer_show (buf);
613 UNGCPRO;
615 return unbind_to (count, val);
618 DEFUN ("with-output-to-temp-buffer", Fwith_output_to_temp_buffer, Swith_output_to_temp_buffer,
619 1, UNEVALLED, 0,
620 "Bind `standard-output' to buffer BUFNAME, eval BODY, then show that buffer.\n\
621 The buffer is cleared out initially, and marked as unmodified when done.\n\
622 All output done by BODY is inserted in that buffer by default.\n\
623 The buffer is displayed in another window, but not selected.\n\
624 The value of the last form in BODY is returned.\n\
625 If BODY does not finish normally, the buffer BUFNAME is not displayed.\n\
627 The hook `temp-buffer-setup-hook' is run before BODY,\n\
628 with the buffer BUFNAME temporarily current.\n\
629 The hook `temp-buffer-show-hook' is run after the buffer is displayed,\n\
630 with the buffer temporarily current, and the window that was used\n\
631 to display it temporarily selected.\n\
633 If variable `temp-buffer-show-function' is non-nil, call it at the end\n\
634 to get the buffer displayed instead of just displaying the non-selected\n\
635 buffer and calling the hook. It gets one argument, the buffer to display.")
636 (args)
637 Lisp_Object args;
639 struct gcpro gcpro1;
640 Lisp_Object name;
641 int count = specpdl_ptr - specpdl;
642 Lisp_Object buf, val;
644 GCPRO1(args);
645 name = Feval (Fcar (args));
646 UNGCPRO;
648 CHECK_STRING (name, 0);
649 temp_output_buffer_setup (XSTRING (name)->data);
650 buf = Vstandard_output;
652 val = Fprogn (Fcdr (args));
654 temp_output_buffer_show (buf);
656 return unbind_to (count, val);
660 static void print ();
661 static void print_preprocess ();
662 static void print_preprocess_string ();
663 static void print_object ();
665 DEFUN ("terpri", Fterpri, Sterpri, 0, 1, 0,
666 "Output a newline to stream PRINTCHARFUN.\n\
667 If PRINTCHARFUN is omitted or nil, the value of `standard-output' is used.")
668 (printcharfun)
669 Lisp_Object printcharfun;
671 PRINTDECLARE;
673 if (NILP (printcharfun))
674 printcharfun = Vstandard_output;
675 PRINTPREPARE;
676 PRINTCHAR ('\n');
677 PRINTFINISH;
678 return Qt;
681 DEFUN ("prin1", Fprin1, Sprin1, 1, 2, 0,
682 "Output the printed representation of OBJECT, any Lisp object.\n\
683 Quoting characters are printed when needed to make output that `read'\n\
684 can handle, whenever this is possible.\n\
685 Output stream is PRINTCHARFUN, or value of `standard-output' (which see).")
686 (object, printcharfun)
687 Lisp_Object object, printcharfun;
689 PRINTDECLARE;
691 #ifdef MAX_PRINT_CHARS
692 max_print = 0;
693 #endif /* MAX_PRINT_CHARS */
694 if (NILP (printcharfun))
695 printcharfun = Vstandard_output;
696 PRINTPREPARE;
697 print (object, printcharfun, 1);
698 PRINTFINISH;
699 return object;
702 /* a buffer which is used to hold output being built by prin1-to-string */
703 Lisp_Object Vprin1_to_string_buffer;
705 DEFUN ("prin1-to-string", Fprin1_to_string, Sprin1_to_string, 1, 2, 0,
706 "Return a string containing the printed representation of OBJECT,\n\
707 any Lisp object. Quoting characters are used when needed to make output\n\
708 that `read' can handle, whenever this is possible, unless the optional\n\
709 second argument NOESCAPE is non-nil.")
710 (object, noescape)
711 Lisp_Object object, noescape;
713 PRINTDECLARE;
714 Lisp_Object printcharfun;
715 struct gcpro gcpro1, gcpro2;
716 Lisp_Object tem;
718 /* Save and restore this--we are altering a buffer
719 but we don't want to deactivate the mark just for that.
720 No need for specbind, since errors deactivate the mark. */
721 tem = Vdeactivate_mark;
722 GCPRO2 (object, tem);
724 printcharfun = Vprin1_to_string_buffer;
725 PRINTPREPARE;
726 print (object, printcharfun, NILP (noescape));
727 /* Make Vprin1_to_string_buffer be the default buffer after PRINTFINSH */
728 PRINTFINISH;
729 set_buffer_internal (XBUFFER (Vprin1_to_string_buffer));
730 object = Fbuffer_string ();
732 Ferase_buffer ();
733 set_buffer_internal (old);
735 Vdeactivate_mark = tem;
736 UNGCPRO;
738 return object;
741 DEFUN ("princ", Fprinc, Sprinc, 1, 2, 0,
742 "Output the printed representation of OBJECT, any Lisp object.\n\
743 No quoting characters are used; no delimiters are printed around\n\
744 the contents of strings.\n\
745 Output stream is PRINTCHARFUN, or value of standard-output (which see).")
746 (object, printcharfun)
747 Lisp_Object object, printcharfun;
749 PRINTDECLARE;
751 if (NILP (printcharfun))
752 printcharfun = Vstandard_output;
753 PRINTPREPARE;
754 print (object, printcharfun, 0);
755 PRINTFINISH;
756 return object;
759 DEFUN ("print", Fprint, Sprint, 1, 2, 0,
760 "Output the printed representation of OBJECT, with newlines around it.\n\
761 Quoting characters are printed when needed to make output that `read'\n\
762 can handle, whenever this is possible.\n\
763 Output stream is PRINTCHARFUN, or value of `standard-output' (which see).")
764 (object, printcharfun)
765 Lisp_Object object, printcharfun;
767 PRINTDECLARE;
768 struct gcpro gcpro1;
770 #ifdef MAX_PRINT_CHARS
771 print_chars = 0;
772 max_print = MAX_PRINT_CHARS;
773 #endif /* MAX_PRINT_CHARS */
774 if (NILP (printcharfun))
775 printcharfun = Vstandard_output;
776 GCPRO1 (object);
777 PRINTPREPARE;
778 PRINTCHAR ('\n');
779 print (object, printcharfun, 1);
780 PRINTCHAR ('\n');
781 PRINTFINISH;
782 #ifdef MAX_PRINT_CHARS
783 max_print = 0;
784 print_chars = 0;
785 #endif /* MAX_PRINT_CHARS */
786 UNGCPRO;
787 return object;
790 /* The subroutine object for external-debugging-output is kept here
791 for the convenience of the debugger. */
792 Lisp_Object Qexternal_debugging_output;
794 DEFUN ("external-debugging-output", Fexternal_debugging_output, Sexternal_debugging_output, 1, 1, 0,
795 "Write CHARACTER to stderr.\n\
796 You can call print while debugging emacs, and pass it this function\n\
797 to make it write to the debugging output.\n")
798 (character)
799 Lisp_Object character;
801 CHECK_NUMBER (character, 0);
802 putc (XINT (character), stderr);
804 #ifdef WINDOWSNT
805 /* Send the output to a debugger (nothing happens if there isn't one). */
807 char buf[2] = {(char) XINT (character), '\0'};
808 OutputDebugString (buf);
810 #endif
812 return character;
815 /* This is the interface for debugging printing. */
817 void
818 debug_print (arg)
819 Lisp_Object arg;
821 Fprin1 (arg, Qexternal_debugging_output);
822 fprintf (stderr, "\r\n");
825 DEFUN ("error-message-string", Ferror_message_string, Serror_message_string,
826 1, 1, 0,
827 "Convert an error value (ERROR-SYMBOL . DATA) to an error message.")
828 (obj)
829 Lisp_Object obj;
831 struct buffer *old = current_buffer;
832 Lisp_Object value;
833 struct gcpro gcpro1;
835 /* If OBJ is (error STRING), just return STRING.
836 That is not only faster, it also avoids the need to allocate
837 space here when the error is due to memory full. */
838 if (CONSP (obj) && EQ (XCAR (obj), Qerror)
839 && CONSP (XCDR (obj))
840 && STRINGP (XCAR (XCDR (obj)))
841 && NILP (XCDR (XCDR (obj))))
842 return XCAR (XCDR (obj));
844 print_error_message (obj, Vprin1_to_string_buffer);
846 set_buffer_internal (XBUFFER (Vprin1_to_string_buffer));
847 value = Fbuffer_string ();
849 GCPRO1 (value);
850 Ferase_buffer ();
851 set_buffer_internal (old);
852 UNGCPRO;
854 return value;
857 /* Print an error message for the error DATA onto Lisp output stream
858 STREAM (suitable for the print functions). */
860 void
861 print_error_message (data, stream)
862 Lisp_Object data, stream;
864 Lisp_Object errname, errmsg, file_error, tail;
865 struct gcpro gcpro1;
866 int i;
868 errname = Fcar (data);
870 if (EQ (errname, Qerror))
872 data = Fcdr (data);
873 if (!CONSP (data))
874 data = Qnil;
875 errmsg = Fcar (data);
876 file_error = Qnil;
878 else
880 Lisp_Object error_conditions;
881 errmsg = Fget (errname, Qerror_message);
882 error_conditions = Fget (errname, Qerror_conditions);
883 file_error = Fmemq (Qfile_error, error_conditions);
886 /* Print an error message including the data items. */
888 tail = Fcdr_safe (data);
889 GCPRO1 (tail);
891 /* For file-error, make error message by concatenating
892 all the data items. They are all strings. */
893 if (!NILP (file_error) && CONSP (tail))
894 errmsg = XCAR (tail), tail = XCDR (tail);
896 if (STRINGP (errmsg))
897 Fprinc (errmsg, stream);
898 else
899 write_string_1 ("peculiar error", -1, stream);
901 for (i = 0; CONSP (tail); tail = XCDR (tail), i++)
903 Lisp_Object obj;
905 write_string_1 (i ? ", " : ": ", 2, stream);
906 obj = XCAR (tail);
907 if (!NILP (file_error) || EQ (errname, Qend_of_file))
908 Fprinc (obj, stream);
909 else
910 Fprin1 (obj, stream);
913 UNGCPRO;
919 * The buffer should be at least as large as the max string size of the
920 * largest float, printed in the biggest notation. This is undoubtedly
921 * 20d float_output_format, with the negative of the C-constant "HUGE"
922 * from <math.h>.
924 * On the vax the worst case is -1e38 in 20d format which takes 61 bytes.
926 * I assume that IEEE-754 format numbers can take 329 bytes for the worst
927 * case of -1e307 in 20d float_output_format. What is one to do (short of
928 * re-writing _doprnt to be more sane)?
929 * -wsr
932 void
933 float_to_string (buf, data)
934 unsigned char *buf;
935 double data;
937 unsigned char *cp;
938 int width;
940 /* Check for plus infinity in a way that won't lose
941 if there is no plus infinity. */
942 if (data == data / 2 && data > 1.0)
944 strcpy (buf, "1.0e+INF");
945 return;
947 /* Likewise for minus infinity. */
948 if (data == data / 2 && data < -1.0)
950 strcpy (buf, "-1.0e+INF");
951 return;
953 /* Check for NaN in a way that won't fail if there are no NaNs. */
954 if (! (data * 0.0 >= 0.0))
956 /* Prepend "-" if the NaN's sign bit is negative.
957 The sign bit of a double is the bit that is 1 in -0.0. */
958 int i;
959 union { double d; char c[sizeof (double)]; } u_data, u_minus_zero;
960 u_data.d = data;
961 u_minus_zero.d = - 0.0;
962 for (i = 0; i < sizeof (double); i++)
963 if (u_data.c[i] & u_minus_zero.c[i])
965 *buf++ = '-';
966 break;
969 strcpy (buf, "0.0e+NaN");
970 return;
973 if (NILP (Vfloat_output_format)
974 || !STRINGP (Vfloat_output_format))
975 lose:
977 /* Generate the fewest number of digits that represent the
978 floating point value without losing information.
979 The following method is simple but a bit slow.
980 For ideas about speeding things up, please see:
982 Guy L Steele Jr & Jon L White, How to print floating-point numbers
983 accurately. SIGPLAN notices 25, 6 (June 1990), 112-126.
985 Robert G Burger & R Kent Dybvig, Printing floating point numbers
986 quickly and accurately, SIGPLAN notices 31, 5 (May 1996), 108-116. */
988 width = fabs (data) < DBL_MIN ? 1 : DBL_DIG;
990 sprintf (buf, "%.*g", width, data);
991 while (width++ < DOUBLE_DIGITS_BOUND && atof (buf) != data);
993 else /* oink oink */
995 /* Check that the spec we have is fully valid.
996 This means not only valid for printf,
997 but meant for floats, and reasonable. */
998 cp = XSTRING (Vfloat_output_format)->data;
1000 if (cp[0] != '%')
1001 goto lose;
1002 if (cp[1] != '.')
1003 goto lose;
1005 cp += 2;
1007 /* Check the width specification. */
1008 width = -1;
1009 if ('0' <= *cp && *cp <= '9')
1011 width = 0;
1013 width = (width * 10) + (*cp++ - '0');
1014 while (*cp >= '0' && *cp <= '9');
1016 /* A precision of zero is valid only for %f. */
1017 if (width > DBL_DIG
1018 || (width == 0 && *cp != 'f'))
1019 goto lose;
1022 if (*cp != 'e' && *cp != 'f' && *cp != 'g')
1023 goto lose;
1025 if (cp[1] != 0)
1026 goto lose;
1028 sprintf (buf, XSTRING (Vfloat_output_format)->data, data);
1031 /* Make sure there is a decimal point with digit after, or an
1032 exponent, so that the value is readable as a float. But don't do
1033 this with "%.0f"; it's valid for that not to produce a decimal
1034 point. Note that width can be 0 only for %.0f. */
1035 if (width != 0)
1037 for (cp = buf; *cp; cp++)
1038 if ((*cp < '0' || *cp > '9') && *cp != '-')
1039 break;
1041 if (*cp == '.' && cp[1] == 0)
1043 cp[1] = '0';
1044 cp[2] = 0;
1047 if (*cp == 0)
1049 *cp++ = '.';
1050 *cp++ = '0';
1051 *cp++ = 0;
1057 static void
1058 print (obj, printcharfun, escapeflag)
1059 Lisp_Object obj;
1060 register Lisp_Object printcharfun;
1061 int escapeflag;
1063 print_depth = 0;
1065 /* Reset print_number_index and Vprint_number_table only when
1066 the variable Vprint_continuous_numbering is nil. Otherwise,
1067 the values of these variables will be kept between several
1068 print functions. */
1069 if (NILP (Vprint_continuous_numbering))
1071 print_number_index = 0;
1072 Vprint_number_table = Qnil;
1075 /* Construct Vprint_number_table for print-gensym and print-circle. */
1076 if (!NILP (Vprint_gensym) || !NILP (Vprint_circle))
1078 int i, start, index;
1079 /* Construct Vprint_number_table. */
1080 start = index = print_number_index;
1081 print_preprocess (obj);
1082 /* Remove unnecessary objects, which appear only once in OBJ;
1083 that is, whose status is Qnil. */
1084 for (i = start; i < print_number_index; i++)
1085 if (!NILP (PRINT_NUMBER_STATUS (Vprint_number_table, i)))
1087 PRINT_NUMBER_OBJECT (Vprint_number_table, index)
1088 = PRINT_NUMBER_OBJECT (Vprint_number_table, i);
1089 /* Reset the status field for the next print step. Now this
1090 field means whether the object has already been printed. */
1091 PRINT_NUMBER_STATUS (Vprint_number_table, index) = Qnil;
1092 index++;
1094 print_number_index = index;
1097 print_object (obj, printcharfun, escapeflag);
1100 /* Construct Vprint_number_table according to the structure of OBJ.
1101 OBJ itself and all its elements will be added to Vprint_number_table
1102 recursively if it is a list, vector, compiled function, char-table,
1103 string (its text properties will be traced), or a symbol that has
1104 no obarray (this is for the print-gensym feature).
1105 The status fields of Vprint_number_table mean whether each object appears
1106 more than once in OBJ: Qnil at the first time, and Qt after that . */
1107 static void
1108 print_preprocess (obj)
1109 Lisp_Object obj;
1111 int i, size;
1113 loop:
1114 if (STRINGP (obj) || CONSP (obj) || VECTORP (obj)
1115 || COMPILEDP (obj) || CHAR_TABLE_P (obj)
1116 || (! NILP (Vprint_gensym)
1117 && SYMBOLP (obj) && NILP (XSYMBOL (obj)->obarray)))
1119 /* In case print-circle is nil and print-gensym is t,
1120 add OBJ to Vprint_number_table only when OBJ is a symbol. */
1121 if (! NILP (Vprint_circle) || SYMBOLP (obj))
1123 for (i = 0; i < print_number_index; i++)
1124 if (EQ (PRINT_NUMBER_OBJECT (Vprint_number_table, i), obj))
1126 /* OBJ appears more than once. Let's remember that. */
1127 PRINT_NUMBER_STATUS (Vprint_number_table, i) = Qt;
1128 return;
1131 /* OBJ is not yet recorded. Let's add to the table. */
1132 if (print_number_index == 0)
1134 /* Initialize the table. */
1135 Vprint_number_table = Fmake_vector (make_number (40), Qnil);
1137 else if (XVECTOR (Vprint_number_table)->size == print_number_index * 2)
1139 /* Reallocate the table. */
1140 int i = print_number_index * 4;
1141 Lisp_Object old_table = Vprint_number_table;
1142 Vprint_number_table = Fmake_vector (make_number (i), Qnil);
1143 for (i = 0; i < print_number_index; i++)
1145 PRINT_NUMBER_OBJECT (Vprint_number_table, i)
1146 = PRINT_NUMBER_OBJECT (old_table, i);
1147 PRINT_NUMBER_STATUS (Vprint_number_table, i)
1148 = PRINT_NUMBER_STATUS (old_table, i);
1151 PRINT_NUMBER_OBJECT (Vprint_number_table, print_number_index) = obj;
1152 /* If Vprint_continuous_numbering is non-nil and OBJ is a gensym,
1153 always print the gensym with a number. This is a special for
1154 the lisp function byte-compile-output-docform. */
1155 if (! NILP (Vprint_continuous_numbering) && SYMBOLP (obj)
1156 && NILP (XSYMBOL (obj)->obarray))
1157 PRINT_NUMBER_STATUS (Vprint_number_table, print_number_index) = Qt;
1158 print_number_index++;
1161 switch (XGCTYPE (obj))
1163 case Lisp_String:
1164 /* A string may have text properties, which can be circular. */
1165 traverse_intervals (XSTRING (obj)->intervals, 0, 0,
1166 print_preprocess_string, Qnil);
1167 break;
1169 case Lisp_Cons:
1170 print_preprocess (XCAR (obj));
1171 obj = XCDR (obj);
1172 goto loop;
1174 case Lisp_Vectorlike:
1175 size = XVECTOR (obj)->size & PSEUDOVECTOR_SIZE_MASK;
1176 for (i = 0; i < size; i++)
1177 print_preprocess (XVECTOR (obj)->contents[i]);
1182 static void
1183 print_preprocess_string (interval, arg)
1184 INTERVAL interval;
1185 Lisp_Object arg;
1187 print_preprocess (interval->plist);
1190 static void
1191 print_object (obj, printcharfun, escapeflag)
1192 Lisp_Object obj;
1193 register Lisp_Object printcharfun;
1194 int escapeflag;
1196 char buf[30];
1198 QUIT;
1200 /* Detect circularities and truncate them. */
1201 if (STRINGP (obj) || CONSP (obj) || VECTORP (obj)
1202 || COMPILEDP (obj) || CHAR_TABLE_P (obj)
1203 || (! NILP (Vprint_gensym)
1204 && SYMBOLP (obj) && NILP (XSYMBOL (obj)->obarray)))
1206 if (NILP (Vprint_circle) && NILP (Vprint_gensym))
1208 /* Simple but incomplete way. */
1209 int i;
1210 for (i = 0; i < print_depth; i++)
1211 if (EQ (obj, being_printed[i]))
1213 sprintf (buf, "#%d", i);
1214 strout (buf, -1, -1, printcharfun, 0);
1215 return;
1217 being_printed[print_depth] = obj;
1219 else
1221 /* With the print-circle feature. */
1222 int i;
1223 for (i = 0; i < print_number_index; i++)
1224 if (EQ (PRINT_NUMBER_OBJECT (Vprint_number_table, i), obj))
1226 if (NILP (PRINT_NUMBER_STATUS (Vprint_number_table, i)))
1228 /* Add a prefix #n= if OBJ has not yet been printed;
1229 that is, its status field is nil. */
1230 sprintf (buf, "#%d=", i + 1);
1231 strout (buf, -1, -1, printcharfun, 0);
1232 /* OBJ is going to be printed. Set the status to t. */
1233 PRINT_NUMBER_STATUS (Vprint_number_table, i) = Qt;
1234 break;
1236 else
1238 /* Just print #n# if OBJ has already been printed. */
1239 sprintf (buf, "#%d#", i + 1);
1240 strout (buf, -1, -1, printcharfun, 0);
1241 return;
1247 print_depth++;
1249 if (print_depth > PRINT_CIRCLE)
1250 error ("Apparently circular structure being printed");
1251 #ifdef MAX_PRINT_CHARS
1252 if (max_print && print_chars > max_print)
1254 PRINTCHAR ('\n');
1255 print_chars = 0;
1257 #endif /* MAX_PRINT_CHARS */
1259 switch (XGCTYPE (obj))
1261 case Lisp_Int:
1262 if (sizeof (int) == sizeof (EMACS_INT))
1263 sprintf (buf, "%d", XINT (obj));
1264 else if (sizeof (long) == sizeof (EMACS_INT))
1265 sprintf (buf, "%ld", (long) XINT (obj));
1266 else
1267 abort ();
1268 strout (buf, -1, -1, printcharfun, 0);
1269 break;
1271 case Lisp_Float:
1273 char pigbuf[350]; /* see comments in float_to_string */
1275 float_to_string (pigbuf, XFLOAT_DATA (obj));
1276 strout (pigbuf, -1, -1, printcharfun, 0);
1278 break;
1280 case Lisp_String:
1281 if (!escapeflag)
1282 print_string (obj, printcharfun);
1283 else
1285 register int i, i_byte;
1286 struct gcpro gcpro1;
1287 unsigned char *str;
1288 int size_byte;
1289 /* 1 means we must ensure that the next character we output
1290 cannot be taken as part of a hex character escape. */
1291 int need_nonhex = 0;
1293 GCPRO1 (obj);
1295 if (!NULL_INTERVAL_P (XSTRING (obj)->intervals))
1297 PRINTCHAR ('#');
1298 PRINTCHAR ('(');
1301 PRINTCHAR ('\"');
1302 str = XSTRING (obj)->data;
1303 size_byte = STRING_BYTES (XSTRING (obj));
1305 for (i = 0, i_byte = 0; i_byte < size_byte;)
1307 /* Here, we must convert each multi-byte form to the
1308 corresponding character code before handing it to PRINTCHAR. */
1309 int len;
1310 int c;
1312 if (STRING_MULTIBYTE (obj))
1314 c = STRING_CHAR_AND_LENGTH (str + i_byte,
1315 size_byte - i_byte, len);
1316 if (CHAR_VALID_P (c, 0))
1317 i_byte += len;
1318 else
1319 c = str[i_byte++];
1321 else
1322 c = str[i_byte++];
1324 QUIT;
1326 if (c == '\n' && print_escape_newlines)
1328 PRINTCHAR ('\\');
1329 PRINTCHAR ('n');
1331 else if (c == '\f' && print_escape_newlines)
1333 PRINTCHAR ('\\');
1334 PRINTCHAR ('f');
1336 else if (! SINGLE_BYTE_CHAR_P (c) && print_escape_multibyte)
1338 /* When multibyte is disabled,
1339 print multibyte string chars using hex escapes. */
1340 unsigned char outbuf[50];
1341 sprintf (outbuf, "\\x%x", c);
1342 strout (outbuf, -1, -1, printcharfun, 0);
1343 need_nonhex = 1;
1345 else if (SINGLE_BYTE_CHAR_P (c) && ! ASCII_BYTE_P (c)
1346 && print_escape_nonascii)
1348 /* When printing in a multibyte buffer
1349 or when explicitly requested,
1350 print single-byte non-ASCII string chars
1351 using octal escapes. */
1352 unsigned char outbuf[5];
1353 sprintf (outbuf, "\\%03o", c);
1354 strout (outbuf, -1, -1, printcharfun, 0);
1356 else
1358 /* If we just had a hex escape, and this character
1359 could be taken as part of it,
1360 output `\ ' to prevent that. */
1361 if (need_nonhex)
1363 need_nonhex = 0;
1364 if ((c >= 'a' && c <= 'f')
1365 || (c >= 'A' && c <= 'F')
1366 || (c >= '0' && c <= '9'))
1367 strout ("\\ ", -1, -1, printcharfun, 0);
1370 if (c == '\"' || c == '\\')
1371 PRINTCHAR ('\\');
1372 PRINTCHAR (c);
1375 PRINTCHAR ('\"');
1377 if (!NULL_INTERVAL_P (XSTRING (obj)->intervals))
1379 traverse_intervals (XSTRING (obj)->intervals,
1380 0, 0, print_interval, printcharfun);
1381 PRINTCHAR (')');
1384 UNGCPRO;
1386 break;
1388 case Lisp_Symbol:
1390 register int confusing;
1391 register unsigned char *p = XSYMBOL (obj)->name->data;
1392 register unsigned char *end = p + STRING_BYTES (XSYMBOL (obj)->name);
1393 register int c;
1394 int i, i_byte, size_byte;
1395 Lisp_Object name;
1397 XSETSTRING (name, XSYMBOL (obj)->name);
1399 if (p != end && (*p == '-' || *p == '+')) p++;
1400 if (p == end)
1401 confusing = 0;
1402 /* If symbol name begins with a digit, and ends with a digit,
1403 and contains nothing but digits and `e', it could be treated
1404 as a number. So set CONFUSING.
1406 Symbols that contain periods could also be taken as numbers,
1407 but periods are always escaped, so we don't have to worry
1408 about them here. */
1409 else if (*p >= '0' && *p <= '9'
1410 && end[-1] >= '0' && end[-1] <= '9')
1412 while (p != end && ((*p >= '0' && *p <= '9')
1413 /* Needed for \2e10. */
1414 || *p == 'e'))
1415 p++;
1416 confusing = (end == p);
1418 else
1419 confusing = 0;
1421 if (! NILP (Vprint_gensym) && NILP (XSYMBOL (obj)->obarray))
1423 PRINTCHAR ('#');
1424 PRINTCHAR (':');
1427 size_byte = STRING_BYTES (XSTRING (name));
1429 for (i = 0, i_byte = 0; i_byte < size_byte;)
1431 /* Here, we must convert each multi-byte form to the
1432 corresponding character code before handing it to PRINTCHAR. */
1433 FETCH_STRING_CHAR_ADVANCE (c, name, i, i_byte);
1434 QUIT;
1436 if (escapeflag)
1438 if (c == '\"' || c == '\\' || c == '\''
1439 || c == ';' || c == '#' || c == '(' || c == ')'
1440 || c == ',' || c =='.' || c == '`'
1441 || c == '[' || c == ']' || c == '?' || c <= 040
1442 || confusing)
1443 PRINTCHAR ('\\'), confusing = 0;
1445 PRINTCHAR (c);
1448 break;
1450 case Lisp_Cons:
1451 /* If deeper than spec'd depth, print placeholder. */
1452 if (INTEGERP (Vprint_level)
1453 && print_depth > XINT (Vprint_level))
1454 strout ("...", -1, -1, printcharfun, 0);
1455 else if (print_quoted && CONSP (XCDR (obj)) && NILP (XCDR (XCDR (obj)))
1456 && (EQ (XCAR (obj), Qquote)))
1458 PRINTCHAR ('\'');
1459 print_object (XCAR (XCDR (obj)), printcharfun, escapeflag);
1461 else if (print_quoted && CONSP (XCDR (obj)) && NILP (XCDR (XCDR (obj)))
1462 && (EQ (XCAR (obj), Qfunction)))
1464 PRINTCHAR ('#');
1465 PRINTCHAR ('\'');
1466 print_object (XCAR (XCDR (obj)), printcharfun, escapeflag);
1468 else if (print_quoted && CONSP (XCDR (obj)) && NILP (XCDR (XCDR (obj)))
1469 && ((EQ (XCAR (obj), Qbackquote)
1470 || EQ (XCAR (obj), Qcomma)
1471 || EQ (XCAR (obj), Qcomma_at)
1472 || EQ (XCAR (obj), Qcomma_dot))))
1474 print_object (XCAR (obj), printcharfun, 0);
1475 print_object (XCAR (XCDR (obj)), printcharfun, escapeflag);
1477 else
1479 PRINTCHAR ('(');
1481 int print_length, i;
1482 Lisp_Object halftail = obj;
1484 /* Negative values of print-length are invalid in CL.
1485 Treat them like nil, as CMUCL does. */
1486 if (NATNUMP (Vprint_length))
1487 print_length = XFASTINT (Vprint_length);
1488 else
1489 print_length = 0;
1491 i = 0;
1492 while (CONSP (obj))
1494 /* Detect circular list. */
1495 if (NILP (Vprint_circle))
1497 /* Simple but imcomplete way. */
1498 if (i != 0 && EQ (obj, halftail))
1500 sprintf (buf, " . #%d", i / 2);
1501 strout (buf, -1, -1, printcharfun, 0);
1502 goto end_of_list;
1505 else
1507 /* With the print-circle feature. */
1508 if (i != 0)
1510 int i;
1511 for (i = 0; i < print_number_index; i++)
1512 if (EQ (PRINT_NUMBER_OBJECT (Vprint_number_table, i),
1513 obj))
1515 if (NILP (PRINT_NUMBER_STATUS (Vprint_number_table, i)))
1517 strout (" . ", 3, 3, printcharfun, 0);
1518 print_object (obj, printcharfun, escapeflag);
1520 else
1522 sprintf (buf, " . #%d#", i + 1);
1523 strout (buf, -1, -1, printcharfun, 0);
1525 goto end_of_list;
1530 if (i++)
1531 PRINTCHAR (' ');
1533 if (print_length && i > print_length)
1535 strout ("...", 3, 3, printcharfun, 0);
1536 goto end_of_list;
1539 print_object (XCAR (obj), printcharfun, escapeflag);
1541 obj = XCDR (obj);
1542 if (!(i & 1))
1543 halftail = XCDR (halftail);
1547 /* OBJ non-nil here means it's the end of a dotted list. */
1548 if (!NILP (obj))
1550 strout (" . ", 3, 3, printcharfun, 0);
1551 print_object (obj, printcharfun, escapeflag);
1554 end_of_list:
1555 PRINTCHAR (')');
1557 break;
1559 case Lisp_Vectorlike:
1560 if (PROCESSP (obj))
1562 if (escapeflag)
1564 strout ("#<process ", -1, -1, printcharfun, 0);
1565 print_string (XPROCESS (obj)->name, printcharfun);
1566 PRINTCHAR ('>');
1568 else
1569 print_string (XPROCESS (obj)->name, printcharfun);
1571 else if (BOOL_VECTOR_P (obj))
1573 register int i;
1574 register unsigned char c;
1575 struct gcpro gcpro1;
1576 int size_in_chars
1577 = (XBOOL_VECTOR (obj)->size + BITS_PER_CHAR - 1) / BITS_PER_CHAR;
1579 GCPRO1 (obj);
1581 PRINTCHAR ('#');
1582 PRINTCHAR ('&');
1583 sprintf (buf, "%d", XBOOL_VECTOR (obj)->size);
1584 strout (buf, -1, -1, printcharfun, 0);
1585 PRINTCHAR ('\"');
1587 /* Don't print more characters than the specified maximum.
1588 Negative values of print-length are invalid. Treat them
1589 like a print-length of nil. */
1590 if (NATNUMP (Vprint_length)
1591 && XFASTINT (Vprint_length) < size_in_chars)
1592 size_in_chars = XFASTINT (Vprint_length);
1594 for (i = 0; i < size_in_chars; i++)
1596 QUIT;
1597 c = XBOOL_VECTOR (obj)->data[i];
1598 if (c == '\n' && print_escape_newlines)
1600 PRINTCHAR ('\\');
1601 PRINTCHAR ('n');
1603 else if (c == '\f' && print_escape_newlines)
1605 PRINTCHAR ('\\');
1606 PRINTCHAR ('f');
1608 else
1610 if (c == '\"' || c == '\\')
1611 PRINTCHAR ('\\');
1612 PRINTCHAR (c);
1615 PRINTCHAR ('\"');
1617 UNGCPRO;
1619 else if (SUBRP (obj))
1621 strout ("#<subr ", -1, -1, printcharfun, 0);
1622 strout (XSUBR (obj)->symbol_name, -1, -1, printcharfun, 0);
1623 PRINTCHAR ('>');
1625 else if (WINDOWP (obj))
1627 strout ("#<window ", -1, -1, printcharfun, 0);
1628 sprintf (buf, "%d", XFASTINT (XWINDOW (obj)->sequence_number));
1629 strout (buf, -1, -1, printcharfun, 0);
1630 if (!NILP (XWINDOW (obj)->buffer))
1632 strout (" on ", -1, -1, printcharfun, 0);
1633 print_string (XBUFFER (XWINDOW (obj)->buffer)->name, printcharfun);
1635 PRINTCHAR ('>');
1637 else if (HASH_TABLE_P (obj))
1639 struct Lisp_Hash_Table *h = XHASH_TABLE (obj);
1640 strout ("#<hash-table", -1, -1, printcharfun, 0);
1641 if (SYMBOLP (h->test))
1643 PRINTCHAR (' ');
1644 PRINTCHAR ('\'');
1645 strout (XSYMBOL (h->test)->name->data, -1, -1, printcharfun, 0);
1646 PRINTCHAR (' ');
1647 strout (XSYMBOL (h->weak)->name->data, -1, -1, printcharfun, 0);
1648 PRINTCHAR (' ');
1649 sprintf (buf, "%d/%d", XFASTINT (h->count),
1650 XVECTOR (h->next)->size);
1651 strout (buf, -1, -1, printcharfun, 0);
1653 sprintf (buf, " 0x%lx", (unsigned long) h);
1654 strout (buf, -1, -1, printcharfun, 0);
1655 PRINTCHAR ('>');
1657 else if (BUFFERP (obj))
1659 if (NILP (XBUFFER (obj)->name))
1660 strout ("#<killed buffer>", -1, -1, printcharfun, 0);
1661 else if (escapeflag)
1663 strout ("#<buffer ", -1, -1, printcharfun, 0);
1664 print_string (XBUFFER (obj)->name, printcharfun);
1665 PRINTCHAR ('>');
1667 else
1668 print_string (XBUFFER (obj)->name, printcharfun);
1670 else if (WINDOW_CONFIGURATIONP (obj))
1672 strout ("#<window-configuration>", -1, -1, printcharfun, 0);
1674 else if (FRAMEP (obj))
1676 strout ((FRAME_LIVE_P (XFRAME (obj))
1677 ? "#<frame " : "#<dead frame "),
1678 -1, -1, printcharfun, 0);
1679 print_string (XFRAME (obj)->name, printcharfun);
1680 sprintf (buf, " 0x%lx\\ ", (unsigned long) (XFRAME (obj)));
1681 strout (buf, -1, -1, printcharfun, 0);
1682 PRINTCHAR ('>');
1684 else
1686 int size = XVECTOR (obj)->size;
1687 if (COMPILEDP (obj))
1689 PRINTCHAR ('#');
1690 size &= PSEUDOVECTOR_SIZE_MASK;
1692 if (CHAR_TABLE_P (obj))
1694 /* We print a char-table as if it were a vector,
1695 lumping the parent and default slots in with the
1696 character slots. But we add #^ as a prefix. */
1697 PRINTCHAR ('#');
1698 PRINTCHAR ('^');
1699 if (SUB_CHAR_TABLE_P (obj))
1700 PRINTCHAR ('^');
1701 size &= PSEUDOVECTOR_SIZE_MASK;
1703 if (size & PSEUDOVECTOR_FLAG)
1704 goto badtype;
1706 PRINTCHAR ('[');
1708 register int i;
1709 register Lisp_Object tem;
1711 /* Don't print more elements than the specified maximum. */
1712 if (NATNUMP (Vprint_length)
1713 && XFASTINT (Vprint_length) < size)
1714 size = XFASTINT (Vprint_length);
1716 for (i = 0; i < size; i++)
1718 if (i) PRINTCHAR (' ');
1719 tem = XVECTOR (obj)->contents[i];
1720 print_object (tem, printcharfun, escapeflag);
1723 PRINTCHAR (']');
1725 break;
1727 case Lisp_Misc:
1728 switch (XMISCTYPE (obj))
1730 case Lisp_Misc_Marker:
1731 strout ("#<marker ", -1, -1, printcharfun, 0);
1732 /* Do you think this is necessary? */
1733 if (XMARKER (obj)->insertion_type != 0)
1734 strout ("(before-insertion) ", -1, -1, printcharfun, 0);
1735 if (!(XMARKER (obj)->buffer))
1736 strout ("in no buffer", -1, -1, printcharfun, 0);
1737 else
1739 sprintf (buf, "at %d", marker_position (obj));
1740 strout (buf, -1, -1, printcharfun, 0);
1741 strout (" in ", -1, -1, printcharfun, 0);
1742 print_string (XMARKER (obj)->buffer->name, printcharfun);
1744 PRINTCHAR ('>');
1745 break;
1747 case Lisp_Misc_Overlay:
1748 strout ("#<overlay ", -1, -1, printcharfun, 0);
1749 if (!(XMARKER (OVERLAY_START (obj))->buffer))
1750 strout ("in no buffer", -1, -1, printcharfun, 0);
1751 else
1753 sprintf (buf, "from %d to %d in ",
1754 marker_position (OVERLAY_START (obj)),
1755 marker_position (OVERLAY_END (obj)));
1756 strout (buf, -1, -1, printcharfun, 0);
1757 print_string (XMARKER (OVERLAY_START (obj))->buffer->name,
1758 printcharfun);
1760 PRINTCHAR ('>');
1761 break;
1763 /* Remaining cases shouldn't happen in normal usage, but let's print
1764 them anyway for the benefit of the debugger. */
1765 case Lisp_Misc_Free:
1766 strout ("#<misc free cell>", -1, -1, printcharfun, 0);
1767 break;
1769 case Lisp_Misc_Intfwd:
1770 sprintf (buf, "#<intfwd to %d>", *XINTFWD (obj)->intvar);
1771 strout (buf, -1, -1, printcharfun, 0);
1772 break;
1774 case Lisp_Misc_Boolfwd:
1775 sprintf (buf, "#<boolfwd to %s>",
1776 (*XBOOLFWD (obj)->boolvar ? "t" : "nil"));
1777 strout (buf, -1, -1, printcharfun, 0);
1778 break;
1780 case Lisp_Misc_Objfwd:
1781 strout ("#<objfwd to ", -1, -1, printcharfun, 0);
1782 print_object (*XOBJFWD (obj)->objvar, printcharfun, escapeflag);
1783 PRINTCHAR ('>');
1784 break;
1786 case Lisp_Misc_Buffer_Objfwd:
1787 strout ("#<buffer_objfwd to ", -1, -1, printcharfun, 0);
1788 print_object (PER_BUFFER_VALUE (current_buffer,
1789 XBUFFER_OBJFWD (obj)->offset),
1790 printcharfun, escapeflag);
1791 PRINTCHAR ('>');
1792 break;
1794 case Lisp_Misc_Kboard_Objfwd:
1795 strout ("#<kboard_objfwd to ", -1, -1, printcharfun, 0);
1796 print_object (*(Lisp_Object *)((char *) current_kboard
1797 + XKBOARD_OBJFWD (obj)->offset),
1798 printcharfun, escapeflag);
1799 PRINTCHAR ('>');
1800 break;
1802 case Lisp_Misc_Buffer_Local_Value:
1803 strout ("#<buffer_local_value ", -1, -1, printcharfun, 0);
1804 goto do_buffer_local;
1805 case Lisp_Misc_Some_Buffer_Local_Value:
1806 strout ("#<some_buffer_local_value ", -1, -1, printcharfun, 0);
1807 do_buffer_local:
1808 strout ("[realvalue] ", -1, -1, printcharfun, 0);
1809 print_object (XBUFFER_LOCAL_VALUE (obj)->realvalue,
1810 printcharfun, escapeflag);
1811 if (XBUFFER_LOCAL_VALUE (obj)->found_for_buffer)
1812 strout ("[local in buffer] ", -1, -1, printcharfun, 0);
1813 else
1814 strout ("[buffer] ", -1, -1, printcharfun, 0);
1815 print_object (XBUFFER_LOCAL_VALUE (obj)->buffer,
1816 printcharfun, escapeflag);
1817 if (XBUFFER_LOCAL_VALUE (obj)->check_frame)
1819 if (XBUFFER_LOCAL_VALUE (obj)->found_for_frame)
1820 strout ("[local in frame] ", -1, -1, printcharfun, 0);
1821 else
1822 strout ("[frame] ", -1, -1, printcharfun, 0);
1823 print_object (XBUFFER_LOCAL_VALUE (obj)->frame,
1824 printcharfun, escapeflag);
1826 strout ("[alist-elt] ", -1, -1, printcharfun, 0);
1827 print_object (XCAR (XBUFFER_LOCAL_VALUE (obj)->cdr),
1828 printcharfun, escapeflag);
1829 strout ("[default-value] ", -1, -1, printcharfun, 0);
1830 print_object (XCDR (XBUFFER_LOCAL_VALUE (obj)->cdr),
1831 printcharfun, escapeflag);
1832 PRINTCHAR ('>');
1833 break;
1835 default:
1836 goto badtype;
1838 break;
1840 default:
1841 badtype:
1843 /* We're in trouble if this happens!
1844 Probably should just abort () */
1845 strout ("#<EMACS BUG: INVALID DATATYPE ", -1, -1, printcharfun, 0);
1846 if (MISCP (obj))
1847 sprintf (buf, "(MISC 0x%04x)", (int) XMISCTYPE (obj));
1848 else if (VECTORLIKEP (obj))
1849 sprintf (buf, "(PVEC 0x%08x)", (int) XVECTOR (obj)->size);
1850 else
1851 sprintf (buf, "(0x%02x)", (int) XTYPE (obj));
1852 strout (buf, -1, -1, printcharfun, 0);
1853 strout (" Save your buffers immediately and please report this bug>",
1854 -1, -1, printcharfun, 0);
1858 print_depth--;
1862 /* Print a description of INTERVAL using PRINTCHARFUN.
1863 This is part of printing a string that has text properties. */
1865 void
1866 print_interval (interval, printcharfun)
1867 INTERVAL interval;
1868 Lisp_Object printcharfun;
1870 PRINTCHAR (' ');
1871 print_object (make_number (interval->position), printcharfun, 1);
1872 PRINTCHAR (' ');
1873 print_object (make_number (interval->position + LENGTH (interval)),
1874 printcharfun, 1);
1875 PRINTCHAR (' ');
1876 print_object (interval->plist, printcharfun, 1);
1880 void
1881 syms_of_print ()
1883 Qtemp_buffer_setup_hook = intern ("temp-buffer-setup-hook");
1884 staticpro (&Qtemp_buffer_setup_hook);
1886 DEFVAR_LISP ("standard-output", &Vstandard_output,
1887 "Output stream `print' uses by default for outputting a character.\n\
1888 This may be any function of one argument.\n\
1889 It may also be a buffer (output is inserted before point)\n\
1890 or a marker (output is inserted and the marker is advanced)\n\
1891 or the symbol t (output appears in the echo area).");
1892 Vstandard_output = Qt;
1893 Qstandard_output = intern ("standard-output");
1894 staticpro (&Qstandard_output);
1896 DEFVAR_LISP ("float-output-format", &Vfloat_output_format,
1897 "The format descriptor string used to print floats.\n\
1898 This is a %-spec like those accepted by `printf' in C,\n\
1899 but with some restrictions. It must start with the two characters `%.'.\n\
1900 After that comes an integer precision specification,\n\
1901 and then a letter which controls the format.\n\
1902 The letters allowed are `e', `f' and `g'.\n\
1903 Use `e' for exponential notation \"DIG.DIGITSeEXPT\"\n\
1904 Use `f' for decimal point notation \"DIGITS.DIGITS\".\n\
1905 Use `g' to choose the shorter of those two formats for the number at hand.\n\
1906 The precision in any of these cases is the number of digits following\n\
1907 the decimal point. With `f', a precision of 0 means to omit the\n\
1908 decimal point. 0 is not allowed with `e' or `g'.\n\n\
1909 A value of nil means to use the shortest notation\n\
1910 that represents the number without losing information.");
1911 Vfloat_output_format = Qnil;
1912 Qfloat_output_format = intern ("float-output-format");
1913 staticpro (&Qfloat_output_format);
1915 DEFVAR_LISP ("print-length", &Vprint_length,
1916 "Maximum length of list to print before abbreviating.\n\
1917 A value of nil means no limit.");
1918 Vprint_length = Qnil;
1920 DEFVAR_LISP ("print-level", &Vprint_level,
1921 "Maximum depth of list nesting to print before abbreviating.\n\
1922 A value of nil means no limit.");
1923 Vprint_level = Qnil;
1925 DEFVAR_BOOL ("print-escape-newlines", &print_escape_newlines,
1926 "Non-nil means print newlines in strings as backslash-n.\n\
1927 Also print formfeeds as backslash-f.");
1928 print_escape_newlines = 0;
1930 DEFVAR_BOOL ("print-escape-nonascii", &print_escape_nonascii,
1931 "Non-nil means print unibyte non-ASCII chars in strings as \\OOO.\n\
1932 \(OOO is the octal representation of the character code.)\n\
1933 Only single-byte characters are affected, and only in `prin1'.");
1934 print_escape_nonascii = 0;
1936 DEFVAR_BOOL ("print-escape-multibyte", &print_escape_multibyte,
1937 "Non-nil means print multibyte characters in strings as \\xXXXX.\n\
1938 \(XXX is the hex representation of the character code.)\n\
1939 This affects only `prin1'.");
1940 print_escape_multibyte = 0;
1942 DEFVAR_BOOL ("print-quoted", &print_quoted,
1943 "Non-nil means print quoted forms with reader syntax.\n\
1944 I.e., (quote foo) prints as 'foo, (function foo) as #'foo, and, backquoted\n\
1945 forms print in the new syntax.");
1946 print_quoted = 0;
1948 DEFVAR_LISP ("print-gensym", &Vprint_gensym,
1949 "Non-nil means print uninterned symbols so they will read as uninterned.\n\
1950 I.e., the value of (make-symbol \"foobar\") prints as #:foobar.\n\
1951 When the uninterned symbol appears within a recursive data structure\n\
1952 and the symbol appears more than once, in addition use the #N# and #N=\n\
1953 constructs as needed, so that multiple references to the same symbol are\n\
1954 shared once again when the text is read back.");
1955 Vprint_gensym = Qnil;
1957 DEFVAR_LISP ("print-circle", &Vprint_circle,
1958 "*Non-nil means print recursive structures using #N= and #N# syntax.\n\
1959 If nil, printing proceeds recursively and may lead to\n\
1960 `max-lisp-eval-depth' being exceeded or an error may occur:\n\
1961 \"Apparently circular structure being printed.\" Also see\n\
1962 `print-length' and `print-level'.\n\
1963 If non-nil, shared substructures anywhere in the structure are printed\n\
1964 with `#N=' before the first occurrence (in the order of the print\n\
1965 representation) and `#N#' in place of each subsequent occurrence,\n\
1966 where N is a positive decimal integer.");
1967 Vprint_circle = Qnil;
1969 DEFVAR_LISP ("print-continuous-numbering", &Vprint_continuous_numbering,
1970 "*Non-nil means keep numbering between several print functions.\n\
1971 See `print-gensym' nad `print-circle'. See also `print-number-table'.");
1972 Vprint_continuous_numbering = Qnil;
1974 DEFVAR_LISP ("print-number-table", &Vprint_number_table,
1975 "A vector keeping the information of the current printed object.\n\
1976 This variable shouldn't be modified in Lisp level, but should be binded\n\
1977 with nil using let at the same position with `print-continuous-numbering',\n\
1978 so that the value of this variable can be freed after printing.");
1979 Vprint_number_table = Qnil;
1981 /* prin1_to_string_buffer initialized in init_buffer_once in buffer.c */
1982 staticpro (&Vprin1_to_string_buffer);
1984 defsubr (&Sprin1);
1985 defsubr (&Sprin1_to_string);
1986 defsubr (&Serror_message_string);
1987 defsubr (&Sprinc);
1988 defsubr (&Sprint);
1989 defsubr (&Sterpri);
1990 defsubr (&Swrite_char);
1991 defsubr (&Sexternal_debugging_output);
1993 Qexternal_debugging_output = intern ("external-debugging-output");
1994 staticpro (&Qexternal_debugging_output);
1996 Qprint_escape_newlines = intern ("print-escape-newlines");
1997 staticpro (&Qprint_escape_newlines);
1999 Qprint_escape_multibyte = intern ("print-escape-multibyte");
2000 staticpro (&Qprint_escape_multibyte);
2002 Qprint_escape_nonascii = intern ("print-escape-nonascii");
2003 staticpro (&Qprint_escape_nonascii);
2005 defsubr (&Swith_output_to_temp_buffer);