(speedbar-update-current-file): Added call to
[emacs.git] / src / print.c
blob9cb8b1b960c6e401b0ac0cf4c4230701b2c7ee46
1 /* Lisp object printing and output streams.
2 Copyright (C) 1985, 86, 88, 93, 94, 95, 97, 1998
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"
27 #ifndef standalone
28 #include "buffer.h"
29 #include "charset.h"
30 #include "frame.h"
31 #include "window.h"
32 #include "process.h"
33 #include "dispextern.h"
34 #include "termchar.h"
35 #include "keyboard.h"
36 #endif /* not standalone */
38 #ifdef USE_TEXT_PROPERTIES
39 #include "intervals.h"
40 #endif
42 Lisp_Object Vstandard_output, Qstandard_output;
44 /* These are used to print like we read. */
45 extern Lisp_Object Qbackquote, Qcomma, Qcomma_at, Qcomma_dot, Qfunction;
47 #ifdef LISP_FLOAT_TYPE
48 Lisp_Object Vfloat_output_format, Qfloat_output_format;
50 /* Work around a problem that happens because math.h on hpux 7
51 defines two static variables--which, in Emacs, are not really static,
52 because `static' is defined as nothing. The problem is that they are
53 defined both here and in lread.c.
54 These macros prevent the name conflict. */
55 #if defined (HPUX) && !defined (HPUX8)
56 #define _MAXLDBL print_maxldbl
57 #define _NMAXLDBL print_nmaxldbl
58 #endif
60 #include <math.h>
62 #if STDC_HEADERS
63 #include <float.h>
64 #include <stdlib.h>
65 #endif
67 /* Default to values appropriate for IEEE floating point. */
68 #ifndef FLT_RADIX
69 #define FLT_RADIX 2
70 #endif
71 #ifndef DBL_MANT_DIG
72 #define DBL_MANT_DIG 53
73 #endif
74 #ifndef DBL_DIG
75 #define DBL_DIG 15
76 #endif
77 #ifndef DBL_MIN
78 #define DBL_MIN 2.2250738585072014e-308
79 #endif
81 #ifdef DBL_MIN_REPLACEMENT
82 #undef DBL_MIN
83 #define DBL_MIN DBL_MIN_REPLACEMENT
84 #endif
86 /* Define DOUBLE_DIGITS_BOUND, an upper bound on the number of decimal digits
87 needed to express a float without losing information.
88 The general-case formula is valid for the usual case, IEEE floating point,
89 but many compilers can't optimize the formula to an integer constant,
90 so make a special case for it. */
91 #if FLT_RADIX == 2 && DBL_MANT_DIG == 53
92 #define DOUBLE_DIGITS_BOUND 17 /* IEEE floating point */
93 #else
94 #define DOUBLE_DIGITS_BOUND ((int) ceil (log10 (pow (FLT_RADIX, DBL_MANT_DIG))))
95 #endif
97 #endif /* LISP_FLOAT_TYPE */
99 /* Avoid actual stack overflow in print. */
100 int print_depth;
102 /* Detect most circularities to print finite output. */
103 #define PRINT_CIRCLE 200
104 Lisp_Object being_printed[PRINT_CIRCLE];
106 /* When printing into a buffer, first we put the text in this
107 block, then insert it all at once. */
108 char *print_buffer;
110 /* Size allocated in print_buffer. */
111 int print_buffer_size;
112 /* Chars stored in print_buffer. */
113 int print_buffer_pos;
114 /* Bytes stored in print_buffer. */
115 int print_buffer_pos_byte;
117 /* Maximum length of list to print in full; noninteger means
118 effectively infinity */
120 Lisp_Object Vprint_length;
122 /* Maximum depth of list to print in full; noninteger means
123 effectively infinity. */
125 Lisp_Object Vprint_level;
127 /* Nonzero means print newlines in strings as \n. */
129 int print_escape_newlines;
131 Lisp_Object Qprint_escape_newlines;
133 /* Nonzero means to print single-byte non-ascii characters in strings as
134 octal escapes. */
136 int print_escape_nonascii;
138 /* Nonzero means print (quote foo) forms as 'foo, etc. */
140 int print_quoted;
142 /* Non-nil means print #: before uninterned symbols.
143 Neither t nor nil means so that and don't clear Vprint_gensym_alist
144 on entry to and exit from print functions. */
146 Lisp_Object Vprint_gensym;
148 /* Association list of certain objects that are `eq' in the form being
149 printed and which should be `eq' when read back in, using the #n=object
150 and #n# reader forms. Each element has the form (object . n). */
152 Lisp_Object Vprint_gensym_alist;
154 /* Nonzero means print newline to stdout before next minibuffer message.
155 Defined in xdisp.c */
157 extern int noninteractive_need_newline;
159 extern int minibuffer_auto_raise;
161 #ifdef MAX_PRINT_CHARS
162 static int print_chars;
163 static int max_print;
164 #endif /* MAX_PRINT_CHARS */
166 void print_interval ();
168 #if 0
169 /* Convert between chars and GLYPHs */
172 glyphlen (glyphs)
173 register GLYPH *glyphs;
175 register int i = 0;
177 while (glyphs[i])
178 i++;
179 return i;
182 void
183 str_to_glyph_cpy (str, glyphs)
184 char *str;
185 GLYPH *glyphs;
187 register GLYPH *gp = glyphs;
188 register char *cp = str;
190 while (*cp)
191 *gp++ = *cp++;
194 void
195 str_to_glyph_ncpy (str, glyphs, n)
196 char *str;
197 GLYPH *glyphs;
198 register int n;
200 register GLYPH *gp = glyphs;
201 register char *cp = str;
203 while (n-- > 0)
204 *gp++ = *cp++;
207 void
208 glyph_to_str_cpy (glyphs, str)
209 GLYPH *glyphs;
210 char *str;
212 register GLYPH *gp = glyphs;
213 register char *cp = str;
215 while (*gp)
216 *str++ = *gp++ & 0377;
218 #endif
220 /* Low level output routines for characters and strings */
222 /* Lisp functions to do output using a stream
223 must have the stream in a variable called printcharfun
224 and must start with PRINTPREPARE, end with PRINTFINISH,
225 and use PRINTDECLARE to declare common variables.
226 Use PRINTCHAR to output one character,
227 or call strout to output a block of characters.
230 #define PRINTDECLARE \
231 struct buffer *old = current_buffer; \
232 int old_point = -1, start_point; \
233 int old_point_byte, start_point_byte; \
234 int specpdl_count = specpdl_ptr - specpdl; \
235 int free_print_buffer = 0; \
236 Lisp_Object original
238 #define PRINTPREPARE \
239 original = printcharfun; \
240 if (NILP (printcharfun)) printcharfun = Qt; \
241 if (BUFFERP (printcharfun)) \
243 if (XBUFFER (printcharfun) != current_buffer) \
244 Fset_buffer (printcharfun); \
245 printcharfun = Qnil; \
247 if (MARKERP (printcharfun)) \
249 if (!(XMARKER (original)->buffer)) \
250 error ("Marker does not point anywhere"); \
251 if (XMARKER (original)->buffer != current_buffer) \
252 set_buffer_internal (XMARKER (original)->buffer); \
253 old_point = PT; \
254 old_point_byte = PT_BYTE; \
255 SET_PT_BOTH (marker_position (printcharfun), \
256 marker_byte_position (printcharfun)); \
257 start_point = PT; \
258 start_point_byte = PT_BYTE; \
259 printcharfun = Qnil; \
261 if (NILP (printcharfun)) \
263 Lisp_Object string; \
264 if (print_buffer != 0) \
266 string = make_string_from_bytes (print_buffer, \
267 print_buffer_pos, \
268 print_buffer_pos_byte); \
269 record_unwind_protect (print_unwind, string); \
271 else \
273 print_buffer_size = 1000; \
274 print_buffer = (char *) xmalloc (print_buffer_size); \
275 free_print_buffer = 1; \
277 print_buffer_pos = 0; \
278 print_buffer_pos_byte = 0; \
280 if (!CONSP (Vprint_gensym)) \
281 Vprint_gensym_alist = Qnil
283 #define PRINTFINISH \
284 if (NILP (printcharfun)) \
286 if (print_buffer_pos != print_buffer_pos_byte \
287 && NILP (current_buffer->enable_multibyte_characters)) \
289 unsigned char *temp \
290 = (unsigned char *) alloca (print_buffer_pos + 1); \
291 copy_text (print_buffer, temp, print_buffer_pos_byte, \
292 1, 0); \
293 insert_1_both (temp, print_buffer_pos, \
294 print_buffer_pos, 0, 1, 0); \
296 else \
297 insert_1_both (print_buffer, print_buffer_pos, \
298 print_buffer_pos_byte, 0, 1, 0); \
300 if (free_print_buffer) \
302 xfree (print_buffer); \
303 print_buffer = 0; \
305 unbind_to (specpdl_count, Qnil); \
306 if (MARKERP (original)) \
307 set_marker_both (original, Qnil, PT, PT_BYTE); \
308 if (old_point >= 0) \
309 SET_PT_BOTH (old_point + (old_point >= start_point \
310 ? PT - start_point : 0), \
311 old_point_byte + (old_point_byte >= start_point_byte \
312 ? PT_BYTE - start_point_byte : 0)); \
313 if (old != current_buffer) \
314 set_buffer_internal (old); \
315 if (!CONSP (Vprint_gensym)) \
316 Vprint_gensym_alist = Qnil
318 #define PRINTCHAR(ch) printchar (ch, printcharfun)
320 /* Nonzero if there is no room to print any more characters
321 so print might as well return right away. */
323 #define PRINTFULLP() \
324 (EQ (printcharfun, Qt) && !noninteractive \
325 && printbufidx >= FRAME_WIDTH (XFRAME (WINDOW_FRAME (XWINDOW (minibuf_window)))))
327 /* This is used to restore the saved contents of print_buffer
328 when there is a recursive call to print. */
329 static Lisp_Object
330 print_unwind (saved_text)
331 Lisp_Object saved_text;
333 bcopy (XSTRING (saved_text)->data, print_buffer, XSTRING (saved_text)->size);
336 /* Index of first unused element of FRAME_MESSAGE_BUF (mini_frame). */
337 static int printbufidx;
339 static void
340 printchar (ch, fun)
341 unsigned int ch;
342 Lisp_Object fun;
344 Lisp_Object ch1;
346 #ifdef MAX_PRINT_CHARS
347 if (max_print)
348 print_chars++;
349 #endif /* MAX_PRINT_CHARS */
350 #ifndef standalone
351 if (EQ (fun, Qnil))
353 int len;
354 unsigned char work[4], *str;
356 QUIT;
357 len = CHAR_STRING (ch, work, str);
358 if (print_buffer_pos_byte + len >= print_buffer_size)
359 print_buffer = (char *) xrealloc (print_buffer,
360 print_buffer_size *= 2);
361 bcopy (str, print_buffer + print_buffer_pos_byte, len);
362 print_buffer_pos += 1;
363 print_buffer_pos_byte += len;
364 return;
367 if (EQ (fun, Qt))
369 FRAME_PTR mini_frame
370 = XFRAME (WINDOW_FRAME (XWINDOW (minibuf_window)));
371 unsigned char work[4], *str;
372 int len = CHAR_STRING (ch, work, str);
374 QUIT;
376 if (noninteractive)
378 while (len--)
379 putchar (*str), str++;
380 noninteractive_need_newline = 1;
381 return;
384 if (echo_area_glyphs != FRAME_MESSAGE_BUF (mini_frame)
385 || !message_buf_print)
387 message_log_maybe_newline ();
388 echo_area_glyphs = FRAME_MESSAGE_BUF (mini_frame);
389 printbufidx = 0;
390 echo_area_glyphs_length = 0;
391 message_buf_print = 1;
393 if (minibuffer_auto_raise)
395 Lisp_Object mini_window;
397 /* Get the frame containing the minibuffer
398 that the selected frame is using. */
399 mini_window = FRAME_MINIBUF_WINDOW (selected_frame);
401 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
405 message_dolog (str, len, 0, len > 1);
407 /* Convert message to multibyte if we are now adding multibyte text. */
408 if (! NILP (current_buffer->enable_multibyte_characters)
409 && ! message_enable_multibyte
410 && printbufidx > 0)
412 int size = count_size_as_multibyte (FRAME_MESSAGE_BUF (mini_frame),
413 printbufidx);
414 unsigned char *tembuf = (unsigned char *) alloca (size + 1);
415 copy_text (FRAME_MESSAGE_BUF (mini_frame), tembuf, printbufidx,
416 0, 1);
417 printbufidx = size;
418 if (printbufidx > FRAME_MESSAGE_BUF_SIZE (mini_frame))
420 printbufidx = FRAME_MESSAGE_BUF_SIZE (mini_frame);
421 /* Rewind incomplete multi-byte form. */
422 while (printbufidx > 0 && tembuf[printbufidx] >= 0xA0)
423 printbufidx--;
425 bcopy (tembuf, FRAME_MESSAGE_BUF (mini_frame), printbufidx);
426 message_enable_multibyte = 1;
429 if (printbufidx < FRAME_MESSAGE_BUF_SIZE (mini_frame) - len)
430 bcopy (str, &FRAME_MESSAGE_BUF (mini_frame)[printbufidx], len),
431 printbufidx += len;
432 FRAME_MESSAGE_BUF (mini_frame)[printbufidx] = 0;
433 echo_area_glyphs_length = printbufidx;
435 return;
437 #endif /* not standalone */
439 XSETFASTINT (ch1, ch);
440 call1 (fun, ch1);
443 static void
444 strout (ptr, size, size_byte, printcharfun, multibyte)
445 char *ptr;
446 int size, size_byte;
447 Lisp_Object printcharfun;
448 int multibyte;
450 int i = 0;
452 if (size < 0)
453 size_byte = size = strlen (ptr);
455 if (EQ (printcharfun, Qnil))
457 if (print_buffer_pos_byte + size_byte > print_buffer_size)
459 print_buffer_size = print_buffer_size * 2 + size_byte;
460 print_buffer = (char *) xrealloc (print_buffer,
461 print_buffer_size);
463 bcopy (ptr, print_buffer + print_buffer_pos_byte, size_byte);
464 print_buffer_pos += size;
465 print_buffer_pos_byte += size_byte;
467 #ifdef MAX_PRINT_CHARS
468 if (max_print)
469 print_chars += size;
470 #endif /* MAX_PRINT_CHARS */
471 return;
473 if (EQ (printcharfun, Qt))
475 FRAME_PTR mini_frame
476 = XFRAME (WINDOW_FRAME (XWINDOW (minibuf_window)));
478 QUIT;
480 #ifdef MAX_PRINT_CHARS
481 if (max_print)
482 print_chars += size;
483 #endif /* MAX_PRINT_CHARS */
485 if (noninteractive)
487 fwrite (ptr, 1, size_byte, stdout);
488 noninteractive_need_newline = 1;
489 return;
492 if (echo_area_glyphs != FRAME_MESSAGE_BUF (mini_frame)
493 || !message_buf_print)
495 message_log_maybe_newline ();
496 echo_area_glyphs = FRAME_MESSAGE_BUF (mini_frame);
497 printbufidx = 0;
498 echo_area_glyphs_length = 0;
499 message_buf_print = 1;
501 if (minibuffer_auto_raise)
503 Lisp_Object mini_window;
505 /* Get the frame containing the minibuffer
506 that the selected frame is using. */
507 mini_window = FRAME_MINIBUF_WINDOW (selected_frame);
509 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
513 message_dolog (ptr, size_byte, 0, multibyte);
515 /* Convert message to multibyte if we are now adding multibyte text. */
516 if (multibyte
517 && ! message_enable_multibyte
518 && printbufidx > 0)
520 int size = count_size_as_multibyte (FRAME_MESSAGE_BUF (mini_frame),
521 printbufidx);
522 unsigned char *tembuf = (unsigned char *) alloca (size + 1);
523 copy_text (FRAME_MESSAGE_BUF (mini_frame), tembuf, printbufidx,
524 0, 1);
525 printbufidx = size;
526 if (printbufidx > FRAME_MESSAGE_BUF_SIZE (mini_frame))
528 printbufidx = FRAME_MESSAGE_BUF_SIZE (mini_frame);
529 /* Rewind incomplete multi-byte form. */
530 while (printbufidx > 0 && tembuf[printbufidx] >= 0xA0)
531 printbufidx--;
534 bcopy (tembuf, FRAME_MESSAGE_BUF (mini_frame), printbufidx);
537 if (multibyte)
538 message_enable_multibyte = 1;
540 /* Compute how much of the new text will fit there. */
541 if (size_byte > FRAME_MESSAGE_BUF_SIZE (mini_frame) - printbufidx - 1)
543 size_byte = FRAME_MESSAGE_BUF_SIZE (mini_frame) - printbufidx - 1;
544 /* Rewind incomplete multi-byte form. */
545 while (size_byte && (unsigned char) ptr[size_byte] >= 0xA0)
546 size_byte--;
549 /* Put that part of the new text in. */
550 bcopy (ptr, &FRAME_MESSAGE_BUF (mini_frame) [printbufidx], size_byte);
551 printbufidx += size_byte;
552 FRAME_MESSAGE_BUF (mini_frame) [printbufidx] = 0;
553 echo_area_glyphs_length = printbufidx;
555 return;
558 i = 0;
559 if (size == size_byte)
560 while (i < size_byte)
562 int ch = ptr[i++];
564 PRINTCHAR (ch);
566 else
567 while (i < size_byte)
569 /* Here, we must convert each multi-byte form to the
570 corresponding character code before handing it to PRINTCHAR. */
571 int len;
572 int ch = STRING_CHAR_AND_LENGTH (ptr + i, size_byte - i, len);
574 PRINTCHAR (ch);
575 i += len;
579 /* Print the contents of a string STRING using PRINTCHARFUN.
580 It isn't safe to use strout in many cases,
581 because printing one char can relocate. */
583 static void
584 print_string (string, printcharfun)
585 Lisp_Object string;
586 Lisp_Object printcharfun;
588 if (EQ (printcharfun, Qt) || NILP (printcharfun))
590 int chars;
592 if (STRING_MULTIBYTE (string))
593 chars = XSTRING (string)->size;
594 else if (EQ (printcharfun, Qt)
595 ? ! NILP (buffer_defaults.enable_multibyte_characters)
596 : ! NILP (current_buffer->enable_multibyte_characters))
597 chars = multibyte_chars_in_text (XSTRING (string)->data,
598 STRING_BYTES (XSTRING (string)));
599 else
600 chars = STRING_BYTES (XSTRING (string));
602 /* strout is safe for output to a frame (echo area) or to print_buffer. */
603 strout (XSTRING (string)->data,
604 chars, STRING_BYTES (XSTRING (string)),
605 printcharfun, STRING_MULTIBYTE (string));
607 else
609 /* Otherwise, string may be relocated by printing one char.
610 So re-fetch the string address for each character. */
611 int i;
612 int size = XSTRING (string)->size;
613 int size_byte = STRING_BYTES (XSTRING (string));
614 struct gcpro gcpro1;
615 GCPRO1 (string);
616 if (size == size_byte)
617 for (i = 0; i < size; i++)
618 PRINTCHAR (XSTRING (string)->data[i]);
619 else
620 for (i = 0; i < size_byte; i++)
622 /* Here, we must convert each multi-byte form to the
623 corresponding character code before handing it to PRINTCHAR. */
624 int len;
625 int ch = STRING_CHAR_AND_CHAR_LENGTH (XSTRING (string)->data + i,
626 size_byte - i, len);
628 PRINTCHAR (ch);
629 i += len;
631 UNGCPRO;
635 DEFUN ("write-char", Fwrite_char, Swrite_char, 1, 2, 0,
636 "Output character CHARACTER to stream PRINTCHARFUN.\n\
637 PRINTCHARFUN defaults to the value of `standard-output' (which see).")
638 (character, printcharfun)
639 Lisp_Object character, printcharfun;
641 PRINTDECLARE;
643 if (NILP (printcharfun))
644 printcharfun = Vstandard_output;
645 CHECK_NUMBER (character, 0);
646 PRINTPREPARE;
647 PRINTCHAR (XINT (character));
648 PRINTFINISH;
649 return character;
652 /* Used from outside of print.c to print a block of SIZE
653 single-byte chars at DATA on the default output stream.
654 Do not use this on the contents of a Lisp string. */
656 void
657 write_string (data, size)
658 char *data;
659 int size;
661 PRINTDECLARE;
662 Lisp_Object printcharfun;
664 printcharfun = Vstandard_output;
666 PRINTPREPARE;
667 strout (data, size, size, printcharfun, 0);
668 PRINTFINISH;
671 /* Used from outside of print.c to print a block of SIZE
672 single-byte chars at DATA on a specified stream PRINTCHARFUN.
673 Do not use this on the contents of a Lisp string. */
675 void
676 write_string_1 (data, size, printcharfun)
677 char *data;
678 int size;
679 Lisp_Object printcharfun;
681 PRINTDECLARE;
683 PRINTPREPARE;
684 strout (data, size, size, printcharfun, 0);
685 PRINTFINISH;
689 #ifndef standalone
691 void
692 temp_output_buffer_setup (bufname)
693 char *bufname;
695 register struct buffer *old = current_buffer;
696 register Lisp_Object buf;
698 Fset_buffer (Fget_buffer_create (build_string (bufname)));
700 current_buffer->directory = old->directory;
701 current_buffer->read_only = Qnil;
702 current_buffer->filename = Qnil;
703 current_buffer->undo_list = Qt;
704 current_buffer->overlays_before = Qnil;
705 current_buffer->overlays_after = Qnil;
706 current_buffer->enable_multibyte_characters
707 = buffer_defaults.enable_multibyte_characters;
708 Ferase_buffer ();
710 XSETBUFFER (buf, current_buffer);
711 specbind (Qstandard_output, buf);
713 set_buffer_internal (old);
716 Lisp_Object
717 internal_with_output_to_temp_buffer (bufname, function, args)
718 char *bufname;
719 Lisp_Object (*function) P_ ((Lisp_Object));
720 Lisp_Object args;
722 int count = specpdl_ptr - specpdl;
723 Lisp_Object buf, val;
724 struct gcpro gcpro1;
726 GCPRO1 (args);
727 record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
728 temp_output_buffer_setup (bufname);
729 buf = Vstandard_output;
730 UNGCPRO;
732 val = (*function) (args);
734 GCPRO1 (val);
735 temp_output_buffer_show (buf);
736 UNGCPRO;
738 return unbind_to (count, val);
741 DEFUN ("with-output-to-temp-buffer", Fwith_output_to_temp_buffer, Swith_output_to_temp_buffer,
742 1, UNEVALLED, 0,
743 "Bind `standard-output' to buffer BUFNAME, eval BODY, then show that buffer.\n\
744 The buffer is cleared out initially, and marked as unmodified when done.\n\
745 All output done by BODY is inserted in that buffer by default.\n\
746 The buffer is displayed in another window, but not selected.\n\
747 The value of the last form in BODY is returned.\n\
748 If BODY does not finish normally, the buffer BUFNAME is not displayed.\n\n\
749 If variable `temp-buffer-show-function' is non-nil, call it at the end\n\
750 to get the buffer displayed. It gets one argument, the buffer to display.")
751 (args)
752 Lisp_Object args;
754 struct gcpro gcpro1;
755 Lisp_Object name;
756 int count = specpdl_ptr - specpdl;
757 Lisp_Object buf, val;
759 GCPRO1(args);
760 name = Feval (Fcar (args));
761 UNGCPRO;
763 CHECK_STRING (name, 0);
764 temp_output_buffer_setup (XSTRING (name)->data);
765 buf = Vstandard_output;
767 val = Fprogn (Fcdr (args));
769 temp_output_buffer_show (buf);
771 return unbind_to (count, val);
773 #endif /* not standalone */
775 static void print ();
777 DEFUN ("terpri", Fterpri, Sterpri, 0, 1, 0,
778 "Output a newline to stream PRINTCHARFUN.\n\
779 If PRINTCHARFUN is omitted or nil, the value of `standard-output' is used.")
780 (printcharfun)
781 Lisp_Object printcharfun;
783 PRINTDECLARE;
785 if (NILP (printcharfun))
786 printcharfun = Vstandard_output;
787 PRINTPREPARE;
788 PRINTCHAR ('\n');
789 PRINTFINISH;
790 return Qt;
793 DEFUN ("prin1", Fprin1, Sprin1, 1, 2, 0,
794 "Output the printed representation of OBJECT, any Lisp object.\n\
795 Quoting characters are printed when needed to make output that `read'\n\
796 can handle, whenever this is possible.\n\
797 Output stream is PRINTCHARFUN, or value of `standard-output' (which see).")
798 (object, printcharfun)
799 Lisp_Object object, printcharfun;
801 PRINTDECLARE;
803 #ifdef MAX_PRINT_CHARS
804 max_print = 0;
805 #endif /* MAX_PRINT_CHARS */
806 if (NILP (printcharfun))
807 printcharfun = Vstandard_output;
808 PRINTPREPARE;
809 print_depth = 0;
810 print (object, printcharfun, 1);
811 PRINTFINISH;
812 return object;
815 /* a buffer which is used to hold output being built by prin1-to-string */
816 Lisp_Object Vprin1_to_string_buffer;
818 DEFUN ("prin1-to-string", Fprin1_to_string, Sprin1_to_string, 1, 2, 0,
819 "Return a string containing the printed representation of OBJECT,\n\
820 any Lisp object. Quoting characters are used when needed to make output\n\
821 that `read' can handle, whenever this is possible, unless the optional\n\
822 second argument NOESCAPE is non-nil.")
823 (object, noescape)
824 Lisp_Object object, noescape;
826 PRINTDECLARE;
827 Lisp_Object printcharfun;
828 struct gcpro gcpro1, gcpro2;
829 Lisp_Object tem;
831 /* Save and restore this--we are altering a buffer
832 but we don't want to deactivate the mark just for that.
833 No need for specbind, since errors deactivate the mark. */
834 tem = Vdeactivate_mark;
835 GCPRO2 (object, tem);
837 printcharfun = Vprin1_to_string_buffer;
838 PRINTPREPARE;
839 print_depth = 0;
840 print (object, printcharfun, NILP (noescape));
841 /* Make Vprin1_to_string_buffer be the default buffer after PRINTFINSH */
842 PRINTFINISH;
843 set_buffer_internal (XBUFFER (Vprin1_to_string_buffer));
844 object = Fbuffer_string ();
846 Ferase_buffer ();
847 set_buffer_internal (old);
849 Vdeactivate_mark = tem;
850 UNGCPRO;
852 return object;
855 DEFUN ("princ", Fprinc, Sprinc, 1, 2, 0,
856 "Output the printed representation of OBJECT, any Lisp object.\n\
857 No quoting characters are used; no delimiters are printed around\n\
858 the contents of strings.\n\
859 Output stream is PRINTCHARFUN, or value of standard-output (which see).")
860 (object, printcharfun)
861 Lisp_Object object, printcharfun;
863 PRINTDECLARE;
865 if (NILP (printcharfun))
866 printcharfun = Vstandard_output;
867 PRINTPREPARE;
868 print_depth = 0;
869 print (object, printcharfun, 0);
870 PRINTFINISH;
871 return object;
874 DEFUN ("print", Fprint, Sprint, 1, 2, 0,
875 "Output the printed representation of OBJECT, with newlines around it.\n\
876 Quoting characters are printed when needed to make output that `read'\n\
877 can handle, whenever this is possible.\n\
878 Output stream is PRINTCHARFUN, or value of `standard-output' (which see).")
879 (object, printcharfun)
880 Lisp_Object object, printcharfun;
882 PRINTDECLARE;
883 struct gcpro gcpro1;
885 #ifdef MAX_PRINT_CHARS
886 print_chars = 0;
887 max_print = MAX_PRINT_CHARS;
888 #endif /* MAX_PRINT_CHARS */
889 if (NILP (printcharfun))
890 printcharfun = Vstandard_output;
891 GCPRO1 (object);
892 PRINTPREPARE;
893 print_depth = 0;
894 PRINTCHAR ('\n');
895 print (object, printcharfun, 1);
896 PRINTCHAR ('\n');
897 PRINTFINISH;
898 #ifdef MAX_PRINT_CHARS
899 max_print = 0;
900 print_chars = 0;
901 #endif /* MAX_PRINT_CHARS */
902 UNGCPRO;
903 return object;
906 /* The subroutine object for external-debugging-output is kept here
907 for the convenience of the debugger. */
908 Lisp_Object Qexternal_debugging_output;
910 DEFUN ("external-debugging-output", Fexternal_debugging_output, Sexternal_debugging_output, 1, 1, 0,
911 "Write CHARACTER to stderr.\n\
912 You can call print while debugging emacs, and pass it this function\n\
913 to make it write to the debugging output.\n")
914 (character)
915 Lisp_Object character;
917 CHECK_NUMBER (character, 0);
918 putc (XINT (character), stderr);
920 #ifdef WINDOWSNT
921 /* Send the output to a debugger (nothing happens if there isn't one). */
923 char buf[2] = {(char) XINT (character), '\0'};
924 OutputDebugString (buf);
926 #endif
928 return character;
931 /* This is the interface for debugging printing. */
933 void
934 debug_print (arg)
935 Lisp_Object arg;
937 Fprin1 (arg, Qexternal_debugging_output);
938 fprintf (stderr, "\r\n");
941 DEFUN ("error-message-string", Ferror_message_string, Serror_message_string,
942 1, 1, 0,
943 "Convert an error value (ERROR-SYMBOL . DATA) to an error message.")
944 (obj)
945 Lisp_Object obj;
947 struct buffer *old = current_buffer;
948 Lisp_Object original, printcharfun, value;
949 struct gcpro gcpro1;
951 /* If OBJ is (error STRING), just return STRING.
952 That is not only faster, it also avoids the need to allocate
953 space here when the error is due to memory full. */
954 if (CONSP (obj) && EQ (XCONS (obj)->car, Qerror)
955 && CONSP (XCONS (obj)->cdr)
956 && STRINGP (XCONS (XCONS (obj)->cdr)->car)
957 && NILP (XCONS (XCONS (obj)->cdr)->cdr))
958 return XCONS (XCONS (obj)->cdr)->car;
960 print_error_message (obj, Vprin1_to_string_buffer);
962 set_buffer_internal (XBUFFER (Vprin1_to_string_buffer));
963 value = Fbuffer_string ();
965 GCPRO1 (value);
966 Ferase_buffer ();
967 set_buffer_internal (old);
968 UNGCPRO;
970 return value;
973 /* Print an error message for the error DATA
974 onto Lisp output stream STREAM (suitable for the print functions). */
976 void
977 print_error_message (data, stream)
978 Lisp_Object data, stream;
980 Lisp_Object errname, errmsg, file_error, tail;
981 struct gcpro gcpro1;
982 int i;
984 errname = Fcar (data);
986 if (EQ (errname, Qerror))
988 data = Fcdr (data);
989 if (!CONSP (data)) data = Qnil;
990 errmsg = Fcar (data);
991 file_error = Qnil;
993 else
995 errmsg = Fget (errname, Qerror_message);
996 file_error = Fmemq (Qfile_error,
997 Fget (errname, Qerror_conditions));
1000 /* Print an error message including the data items. */
1002 tail = Fcdr_safe (data);
1003 GCPRO1 (tail);
1005 /* For file-error, make error message by concatenating
1006 all the data items. They are all strings. */
1007 if (!NILP (file_error) && !NILP (tail))
1008 errmsg = XCONS (tail)->car, tail = XCONS (tail)->cdr;
1010 if (STRINGP (errmsg))
1011 Fprinc (errmsg, stream);
1012 else
1013 write_string_1 ("peculiar error", -1, stream);
1015 for (i = 0; CONSP (tail); tail = Fcdr (tail), i++)
1017 write_string_1 (i ? ", " : ": ", 2, stream);
1018 if (!NILP (file_error))
1019 Fprinc (Fcar (tail), stream);
1020 else
1021 Fprin1 (Fcar (tail), stream);
1023 UNGCPRO;
1026 #ifdef LISP_FLOAT_TYPE
1029 * The buffer should be at least as large as the max string size of the
1030 * largest float, printed in the biggest notation. This is undoubtedly
1031 * 20d float_output_format, with the negative of the C-constant "HUGE"
1032 * from <math.h>.
1034 * On the vax the worst case is -1e38 in 20d format which takes 61 bytes.
1036 * I assume that IEEE-754 format numbers can take 329 bytes for the worst
1037 * case of -1e307 in 20d float_output_format. What is one to do (short of
1038 * re-writing _doprnt to be more sane)?
1039 * -wsr
1042 void
1043 float_to_string (buf, data)
1044 unsigned char *buf;
1045 double data;
1047 unsigned char *cp;
1048 int width;
1050 /* Check for plus infinity in a way that won't lose
1051 if there is no plus infinity. */
1052 if (data == data / 2 && data > 1.0)
1054 strcpy (buf, "1.0e+INF");
1055 return;
1057 /* Likewise for minus infinity. */
1058 if (data == data / 2 && data < -1.0)
1060 strcpy (buf, "-1.0e+INF");
1061 return;
1063 /* Check for NaN in a way that won't fail if there are no NaNs. */
1064 if (! (data * 0.0 >= 0.0))
1066 strcpy (buf, "0.0e+NaN");
1067 return;
1070 if (NILP (Vfloat_output_format)
1071 || !STRINGP (Vfloat_output_format))
1072 lose:
1074 /* Generate the fewest number of digits that represent the
1075 floating point value without losing information.
1076 The following method is simple but a bit slow.
1077 For ideas about speeding things up, please see:
1079 Guy L Steele Jr & Jon L White, How to print floating-point numbers
1080 accurately. SIGPLAN notices 25, 6 (June 1990), 112-126.
1082 Robert G Burger & R Kent Dybvig, Printing floating point numbers
1083 quickly and accurately, SIGPLAN notices 31, 5 (May 1996), 108-116. */
1085 width = fabs (data) < DBL_MIN ? 1 : DBL_DIG;
1087 sprintf (buf, "%.*g", width, data);
1088 while (width++ < DOUBLE_DIGITS_BOUND && atof (buf) != data);
1090 else /* oink oink */
1092 /* Check that the spec we have is fully valid.
1093 This means not only valid for printf,
1094 but meant for floats, and reasonable. */
1095 cp = XSTRING (Vfloat_output_format)->data;
1097 if (cp[0] != '%')
1098 goto lose;
1099 if (cp[1] != '.')
1100 goto lose;
1102 cp += 2;
1104 /* Check the width specification. */
1105 width = -1;
1106 if ('0' <= *cp && *cp <= '9')
1108 width = 0;
1110 width = (width * 10) + (*cp++ - '0');
1111 while (*cp >= '0' && *cp <= '9');
1113 /* A precision of zero is valid only for %f. */
1114 if (width > DBL_DIG
1115 || (width == 0 && *cp != 'f'))
1116 goto lose;
1119 if (*cp != 'e' && *cp != 'f' && *cp != 'g')
1120 goto lose;
1122 if (cp[1] != 0)
1123 goto lose;
1125 sprintf (buf, XSTRING (Vfloat_output_format)->data, data);
1128 /* Make sure there is a decimal point with digit after, or an
1129 exponent, so that the value is readable as a float. But don't do
1130 this with "%.0f"; it's valid for that not to produce a decimal
1131 point. Note that width can be 0 only for %.0f. */
1132 if (width != 0)
1134 for (cp = buf; *cp; cp++)
1135 if ((*cp < '0' || *cp > '9') && *cp != '-')
1136 break;
1138 if (*cp == '.' && cp[1] == 0)
1140 cp[1] = '0';
1141 cp[2] = 0;
1144 if (*cp == 0)
1146 *cp++ = '.';
1147 *cp++ = '0';
1148 *cp++ = 0;
1152 #endif /* LISP_FLOAT_TYPE */
1154 static void
1155 print (obj, printcharfun, escapeflag)
1156 Lisp_Object obj;
1157 register Lisp_Object printcharfun;
1158 int escapeflag;
1160 char buf[30];
1162 QUIT;
1164 #if 1 /* I'm not sure this is really worth doing. */
1165 /* Detect circularities and truncate them.
1166 No need to offer any alternative--this is better than an error. */
1167 if (CONSP (obj) || VECTORP (obj) || COMPILEDP (obj))
1169 int i;
1170 for (i = 0; i < print_depth; i++)
1171 if (EQ (obj, being_printed[i]))
1173 sprintf (buf, "#%d", i);
1174 strout (buf, -1, -1, printcharfun, 0);
1175 return;
1178 #endif
1180 being_printed[print_depth] = obj;
1181 print_depth++;
1183 if (print_depth > PRINT_CIRCLE)
1184 error ("Apparently circular structure being printed");
1185 #ifdef MAX_PRINT_CHARS
1186 if (max_print && print_chars > max_print)
1188 PRINTCHAR ('\n');
1189 print_chars = 0;
1191 #endif /* MAX_PRINT_CHARS */
1193 switch (XGCTYPE (obj))
1195 case Lisp_Int:
1196 if (sizeof (int) == sizeof (EMACS_INT))
1197 sprintf (buf, "%d", XINT (obj));
1198 else if (sizeof (long) == sizeof (EMACS_INT))
1199 sprintf (buf, "%ld", XINT (obj));
1200 else
1201 abort ();
1202 strout (buf, -1, -1, printcharfun, 0);
1203 break;
1205 #ifdef LISP_FLOAT_TYPE
1206 case Lisp_Float:
1208 char pigbuf[350]; /* see comments in float_to_string */
1210 float_to_string (pigbuf, XFLOAT(obj)->data);
1211 strout (pigbuf, -1, -1, printcharfun, 0);
1213 break;
1214 #endif
1216 case Lisp_String:
1217 if (!escapeflag)
1218 print_string (obj, printcharfun);
1219 else
1221 register int i, i_byte;
1222 register unsigned char c;
1223 struct gcpro gcpro1;
1224 unsigned char *str;
1225 int size_byte;
1226 /* 1 means we must ensure that the next character we output
1227 cannot be taken as part of a hex character escape. */
1228 int need_nonhex = 0;
1230 GCPRO1 (obj);
1232 #ifdef USE_TEXT_PROPERTIES
1233 if (!NULL_INTERVAL_P (XSTRING (obj)->intervals))
1235 PRINTCHAR ('#');
1236 PRINTCHAR ('(');
1238 #endif
1240 PRINTCHAR ('\"');
1241 str = XSTRING (obj)->data;
1242 size_byte = STRING_BYTES (XSTRING (obj));
1244 for (i = 0, i_byte = 0; i_byte < size_byte;)
1246 /* Here, we must convert each multi-byte form to the
1247 corresponding character code before handing it to PRINTCHAR. */
1248 int len;
1249 int c;
1251 if (STRING_MULTIBYTE (obj))
1253 c = STRING_CHAR_AND_CHAR_LENGTH (str + i_byte,
1254 size_byte - i_byte, len);
1255 i_byte += len;
1257 else
1258 c = str[i_byte++];
1260 QUIT;
1262 if (c == '\n' && print_escape_newlines)
1264 PRINTCHAR ('\\');
1265 PRINTCHAR ('n');
1267 else if (c == '\f' && print_escape_newlines)
1269 PRINTCHAR ('\\');
1270 PRINTCHAR ('f');
1272 else if ((! SINGLE_BYTE_CHAR_P (c)
1273 && NILP (current_buffer->enable_multibyte_characters)))
1275 /* When multibyte is disabled,
1276 print multibyte string chars using hex escapes. */
1277 unsigned char outbuf[50];
1278 sprintf (outbuf, "\\x%x", c);
1279 strout (outbuf, -1, -1, printcharfun, 0);
1280 need_nonhex = 1;
1282 else if (SINGLE_BYTE_CHAR_P (c)
1283 && ! ASCII_BYTE_P (c)
1284 && (! NILP (current_buffer->enable_multibyte_characters)
1285 || print_escape_nonascii))
1287 /* When multibyte is enabled or when explicitly requested,
1288 print single-byte non-ASCII string chars
1289 using octal escapes. */
1290 unsigned char outbuf[5];
1291 sprintf (outbuf, "\\%03o", c);
1292 strout (outbuf, -1, -1, printcharfun, 0);
1294 else
1296 /* If we just had a hex escape, and this character
1297 could be taken as part of it,
1298 output `\ ' to prevent that. */
1299 if (need_nonhex)
1301 need_nonhex = 0;
1302 if ((c >= 'a' && c <= 'f')
1303 || (c >= 'A' && c <= 'F')
1304 || (c >= '0' && c <= '9'))
1305 strout ("\\ ", -1, -1, printcharfun, 0);
1308 if (c == '\"' || c == '\\')
1309 PRINTCHAR ('\\');
1310 PRINTCHAR (c);
1313 PRINTCHAR ('\"');
1315 #ifdef USE_TEXT_PROPERTIES
1316 if (!NULL_INTERVAL_P (XSTRING (obj)->intervals))
1318 traverse_intervals (XSTRING (obj)->intervals,
1319 0, 0, print_interval, printcharfun);
1320 PRINTCHAR (')');
1322 #endif
1324 UNGCPRO;
1326 break;
1328 case Lisp_Symbol:
1330 register int confusing;
1331 register unsigned char *p = XSYMBOL (obj)->name->data;
1332 register unsigned char *end = p + STRING_BYTES (XSYMBOL (obj)->name);
1333 register int c;
1334 int i, i_byte, size_byte;
1335 Lisp_Object name;
1337 XSETSTRING (name, XSYMBOL (obj)->name);
1339 if (p != end && (*p == '-' || *p == '+')) p++;
1340 if (p == end)
1341 confusing = 0;
1342 /* If symbol name begins with a digit, and ends with a digit,
1343 and contains nothing but digits and `e', it could be treated
1344 as a number. So set CONFUSING.
1346 Symbols that contain periods could also be taken as numbers,
1347 but periods are always escaped, so we don't have to worry
1348 about them here. */
1349 else if (*p >= '0' && *p <= '9'
1350 && end[-1] >= '0' && end[-1] <= '9')
1352 while (p != end && ((*p >= '0' && *p <= '9')
1353 /* Needed for \2e10. */
1354 || *p == 'e'))
1355 p++;
1356 confusing = (end == p);
1358 else
1359 confusing = 0;
1361 /* If we print an uninterned symbol as part of a complex object and
1362 the flag print-gensym is non-nil, prefix it with #n= to read the
1363 object back with the #n# reader syntax later if needed. */
1364 if (! NILP (Vprint_gensym) && NILP (XSYMBOL (obj)->obarray))
1366 if (print_depth > 1)
1368 Lisp_Object tem;
1369 tem = Fassq (obj, Vprint_gensym_alist);
1370 if (CONSP (tem))
1372 PRINTCHAR ('#');
1373 print (XCDR (tem), printcharfun, escapeflag);
1374 PRINTCHAR ('#');
1375 break;
1377 else
1379 if (CONSP (Vprint_gensym_alist))
1380 XSETFASTINT (tem, XFASTINT (XCDR (XCAR (Vprint_gensym_alist))) + 1);
1381 else
1382 XSETFASTINT (tem, 1);
1383 Vprint_gensym_alist = Fcons (Fcons (obj, tem), Vprint_gensym_alist);
1385 PRINTCHAR ('#');
1386 print (tem, printcharfun, escapeflag);
1387 PRINTCHAR ('=');
1390 PRINTCHAR ('#');
1391 PRINTCHAR (':');
1394 size_byte = STRING_BYTES (XSTRING (name));
1396 for (i = 0, i_byte = 0; i_byte < size_byte;)
1398 /* Here, we must convert each multi-byte form to the
1399 corresponding character code before handing it to PRINTCHAR. */
1401 if (STRING_MULTIBYTE (name))
1402 FETCH_STRING_CHAR_ADVANCE (c, name, i, i_byte);
1403 else
1404 c = XSTRING (name)->data[i_byte++];
1406 QUIT;
1408 if (escapeflag)
1410 if (c == '\"' || c == '\\' || c == '\''
1411 || c == ';' || c == '#' || c == '(' || c == ')'
1412 || c == ',' || c =='.' || c == '`'
1413 || c == '[' || c == ']' || c == '?' || c <= 040
1414 || confusing)
1415 PRINTCHAR ('\\'), confusing = 0;
1417 PRINTCHAR (c);
1420 break;
1422 case Lisp_Cons:
1423 /* If deeper than spec'd depth, print placeholder. */
1424 if (INTEGERP (Vprint_level)
1425 && print_depth > XINT (Vprint_level))
1426 strout ("...", -1, -1, printcharfun, 0);
1427 else if (print_quoted && CONSP (XCDR (obj)) && NILP (XCDR (XCDR (obj)))
1428 && (EQ (XCAR (obj), Qquote)))
1430 PRINTCHAR ('\'');
1431 print (XCAR (XCDR (obj)), printcharfun, escapeflag);
1433 else if (print_quoted && CONSP (XCDR (obj)) && NILP (XCDR (XCDR (obj)))
1434 && (EQ (XCAR (obj), Qfunction)))
1436 PRINTCHAR ('#');
1437 PRINTCHAR ('\'');
1438 print (XCAR (XCDR (obj)), printcharfun, escapeflag);
1440 else if (print_quoted && CONSP (XCDR (obj)) && NILP (XCDR (XCDR (obj)))
1441 && ((EQ (XCAR (obj), Qbackquote)
1442 || EQ (XCAR (obj), Qcomma)
1443 || EQ (XCAR (obj), Qcomma_at)
1444 || EQ (XCAR (obj), Qcomma_dot))))
1446 print (XCAR (obj), printcharfun, 0);
1447 print (XCAR (XCDR (obj)), printcharfun, escapeflag);
1449 else
1451 PRINTCHAR ('(');
1453 register int i = 0;
1454 register int print_length = 0;
1455 Lisp_Object halftail = obj;
1457 if (INTEGERP (Vprint_length))
1458 print_length = XINT (Vprint_length);
1459 while (CONSP (obj))
1461 /* Detect circular list. */
1462 if (i != 0 && EQ (obj, halftail))
1464 sprintf (buf, " . #%d", i / 2);
1465 strout (buf, -1, -1, printcharfun, 0);
1466 obj = Qnil;
1467 break;
1469 if (i++)
1470 PRINTCHAR (' ');
1471 if (print_length && i > print_length)
1473 strout ("...", 3, 3, printcharfun, 0);
1474 break;
1476 print (XCAR (obj), printcharfun, escapeflag);
1477 obj = XCDR (obj);
1478 if (!(i & 1))
1479 halftail = XCDR (halftail);
1482 if (!NILP (obj))
1484 strout (" . ", 3, 3, printcharfun, 0);
1485 print (obj, printcharfun, escapeflag);
1487 PRINTCHAR (')');
1489 break;
1491 case Lisp_Vectorlike:
1492 if (PROCESSP (obj))
1494 if (escapeflag)
1496 strout ("#<process ", -1, -1, printcharfun, 0);
1497 print_string (XPROCESS (obj)->name, printcharfun);
1498 PRINTCHAR ('>');
1500 else
1501 print_string (XPROCESS (obj)->name, printcharfun);
1503 else if (BOOL_VECTOR_P (obj))
1505 register int i;
1506 register unsigned char c;
1507 struct gcpro gcpro1;
1508 int size_in_chars
1509 = (XBOOL_VECTOR (obj)->size + BITS_PER_CHAR - 1) / BITS_PER_CHAR;
1511 GCPRO1 (obj);
1513 PRINTCHAR ('#');
1514 PRINTCHAR ('&');
1515 sprintf (buf, "%d", XBOOL_VECTOR (obj)->size);
1516 strout (buf, -1, -1, printcharfun, 0);
1517 PRINTCHAR ('\"');
1519 /* Don't print more characters than the specified maximum. */
1520 if (INTEGERP (Vprint_length)
1521 && XINT (Vprint_length) < size_in_chars)
1522 size_in_chars = XINT (Vprint_length);
1524 for (i = 0; i < size_in_chars; i++)
1526 QUIT;
1527 c = XBOOL_VECTOR (obj)->data[i];
1528 if (c == '\n' && print_escape_newlines)
1530 PRINTCHAR ('\\');
1531 PRINTCHAR ('n');
1533 else if (c == '\f' && print_escape_newlines)
1535 PRINTCHAR ('\\');
1536 PRINTCHAR ('f');
1538 else
1540 if (c == '\"' || c == '\\')
1541 PRINTCHAR ('\\');
1542 PRINTCHAR (c);
1545 PRINTCHAR ('\"');
1547 UNGCPRO;
1549 else if (SUBRP (obj))
1551 strout ("#<subr ", -1, -1, printcharfun, 0);
1552 strout (XSUBR (obj)->symbol_name, -1, -1, printcharfun, 0);
1553 PRINTCHAR ('>');
1555 #ifndef standalone
1556 else if (WINDOWP (obj))
1558 strout ("#<window ", -1, -1, printcharfun, 0);
1559 sprintf (buf, "%d", XFASTINT (XWINDOW (obj)->sequence_number));
1560 strout (buf, -1, -1, printcharfun, 0);
1561 if (!NILP (XWINDOW (obj)->buffer))
1563 strout (" on ", -1, -1, printcharfun, 0);
1564 print_string (XBUFFER (XWINDOW (obj)->buffer)->name, printcharfun);
1566 PRINTCHAR ('>');
1568 else if (BUFFERP (obj))
1570 if (NILP (XBUFFER (obj)->name))
1571 strout ("#<killed buffer>", -1, -1, printcharfun, 0);
1572 else if (escapeflag)
1574 strout ("#<buffer ", -1, -1, printcharfun, 0);
1575 print_string (XBUFFER (obj)->name, printcharfun);
1576 PRINTCHAR ('>');
1578 else
1579 print_string (XBUFFER (obj)->name, printcharfun);
1581 else if (WINDOW_CONFIGURATIONP (obj))
1583 strout ("#<window-configuration>", -1, -1, printcharfun, 0);
1585 else if (FRAMEP (obj))
1587 strout ((FRAME_LIVE_P (XFRAME (obj))
1588 ? "#<frame " : "#<dead frame "),
1589 -1, -1, printcharfun, 0);
1590 print_string (XFRAME (obj)->name, printcharfun);
1591 sprintf (buf, " 0x%lx\\ ", (unsigned long) (XFRAME (obj)));
1592 strout (buf, -1, -1, printcharfun, 0);
1593 PRINTCHAR ('>');
1595 #endif /* not standalone */
1596 else
1598 int size = XVECTOR (obj)->size;
1599 if (COMPILEDP (obj))
1601 PRINTCHAR ('#');
1602 size &= PSEUDOVECTOR_SIZE_MASK;
1604 if (CHAR_TABLE_P (obj))
1606 /* We print a char-table as if it were a vector,
1607 lumping the parent and default slots in with the
1608 character slots. But we add #^ as a prefix. */
1609 PRINTCHAR ('#');
1610 PRINTCHAR ('^');
1611 if (SUB_CHAR_TABLE_P (obj))
1612 PRINTCHAR ('^');
1613 size &= PSEUDOVECTOR_SIZE_MASK;
1615 if (size & PSEUDOVECTOR_FLAG)
1616 goto badtype;
1618 PRINTCHAR ('[');
1620 register int i;
1621 register Lisp_Object tem;
1623 /* Don't print more elements than the specified maximum. */
1624 if (INTEGERP (Vprint_length)
1625 && XINT (Vprint_length) < size)
1626 size = XINT (Vprint_length);
1628 for (i = 0; i < size; i++)
1630 if (i) PRINTCHAR (' ');
1631 tem = XVECTOR (obj)->contents[i];
1632 print (tem, printcharfun, escapeflag);
1635 PRINTCHAR (']');
1637 break;
1639 #ifndef standalone
1640 case Lisp_Misc:
1641 switch (XMISCTYPE (obj))
1643 case Lisp_Misc_Marker:
1644 strout ("#<marker ", -1, -1, printcharfun, 0);
1645 /* Do you think this is necessary? */
1646 if (XMARKER (obj)->insertion_type != 0)
1647 strout ("(before-insertion) ", -1, -1, printcharfun, 0);
1648 if (!(XMARKER (obj)->buffer))
1649 strout ("in no buffer", -1, -1, printcharfun, 0);
1650 else
1652 sprintf (buf, "at %d", marker_position (obj));
1653 strout (buf, -1, -1, printcharfun, 0);
1654 strout (" in ", -1, -1, printcharfun, 0);
1655 print_string (XMARKER (obj)->buffer->name, printcharfun);
1657 PRINTCHAR ('>');
1658 break;
1660 case Lisp_Misc_Overlay:
1661 strout ("#<overlay ", -1, -1, printcharfun, 0);
1662 if (!(XMARKER (OVERLAY_START (obj))->buffer))
1663 strout ("in no buffer", -1, -1, printcharfun, 0);
1664 else
1666 sprintf (buf, "from %d to %d in ",
1667 marker_position (OVERLAY_START (obj)),
1668 marker_position (OVERLAY_END (obj)));
1669 strout (buf, -1, -1, printcharfun, 0);
1670 print_string (XMARKER (OVERLAY_START (obj))->buffer->name,
1671 printcharfun);
1673 PRINTCHAR ('>');
1674 break;
1676 /* Remaining cases shouldn't happen in normal usage, but let's print
1677 them anyway for the benefit of the debugger. */
1678 case Lisp_Misc_Free:
1679 strout ("#<misc free cell>", -1, -1, printcharfun, 0);
1680 break;
1682 case Lisp_Misc_Intfwd:
1683 sprintf (buf, "#<intfwd to %d>", *XINTFWD (obj)->intvar);
1684 strout (buf, -1, -1, printcharfun, 0);
1685 break;
1687 case Lisp_Misc_Boolfwd:
1688 sprintf (buf, "#<boolfwd to %s>",
1689 (*XBOOLFWD (obj)->boolvar ? "t" : "nil"));
1690 strout (buf, -1, -1, printcharfun, 0);
1691 break;
1693 case Lisp_Misc_Objfwd:
1694 strout ("#<objfwd to ", -1, -1, printcharfun, 0);
1695 print (*XOBJFWD (obj)->objvar, printcharfun, escapeflag);
1696 PRINTCHAR ('>');
1697 break;
1699 case Lisp_Misc_Buffer_Objfwd:
1700 strout ("#<buffer_objfwd to ", -1, -1, printcharfun, 0);
1701 print (*(Lisp_Object *)((char *)current_buffer
1702 + XBUFFER_OBJFWD (obj)->offset),
1703 printcharfun, escapeflag);
1704 PRINTCHAR ('>');
1705 break;
1707 case Lisp_Misc_Kboard_Objfwd:
1708 strout ("#<kboard_objfwd to ", -1, -1, printcharfun, 0);
1709 print (*(Lisp_Object *)((char *) current_kboard
1710 + XKBOARD_OBJFWD (obj)->offset),
1711 printcharfun, escapeflag);
1712 PRINTCHAR ('>');
1713 break;
1715 case Lisp_Misc_Buffer_Local_Value:
1716 strout ("#<buffer_local_value ", -1, -1, printcharfun, 0);
1717 goto do_buffer_local;
1718 case Lisp_Misc_Some_Buffer_Local_Value:
1719 strout ("#<some_buffer_local_value ", -1, -1, printcharfun, 0);
1720 do_buffer_local:
1721 strout ("[realvalue] ", -1, -1, printcharfun, 0);
1722 print (XBUFFER_LOCAL_VALUE (obj)->realvalue, printcharfun, escapeflag);
1723 if (XBUFFER_LOCAL_VALUE (obj)->found_for_buffer)
1724 strout ("[local in buffer] ", -1, -1, printcharfun, 0);
1725 else
1726 strout ("[buffer] ", -1, -1, printcharfun, 0);
1727 print (XBUFFER_LOCAL_VALUE (obj)->buffer,
1728 printcharfun, escapeflag);
1729 if (XBUFFER_LOCAL_VALUE (obj)->check_frame)
1731 if (XBUFFER_LOCAL_VALUE (obj)->found_for_frame)
1732 strout ("[local in frame] ", -1, -1, printcharfun, 0);
1733 else
1734 strout ("[frame] ", -1, -1, printcharfun, 0);
1735 print (XBUFFER_LOCAL_VALUE (obj)->frame,
1736 printcharfun, escapeflag);
1738 strout ("[alist-elt] ", -1, -1, printcharfun, 0);
1739 print (XCONS (XBUFFER_LOCAL_VALUE (obj)->cdr)->car,
1740 printcharfun, escapeflag);
1741 strout ("[default-value] ", -1, -1, printcharfun, 0);
1742 print (XCONS (XBUFFER_LOCAL_VALUE (obj)->cdr)->cdr,
1743 printcharfun, escapeflag);
1744 PRINTCHAR ('>');
1745 break;
1747 default:
1748 goto badtype;
1750 break;
1751 #endif /* standalone */
1753 default:
1754 badtype:
1756 /* We're in trouble if this happens!
1757 Probably should just abort () */
1758 strout ("#<EMACS BUG: INVALID DATATYPE ", -1, -1, printcharfun, 0);
1759 if (MISCP (obj))
1760 sprintf (buf, "(MISC 0x%04x)", (int) XMISCTYPE (obj));
1761 else if (VECTORLIKEP (obj))
1762 sprintf (buf, "(PVEC 0x%08x)", (int) XVECTOR (obj)->size);
1763 else
1764 sprintf (buf, "(0x%02x)", (int) XTYPE (obj));
1765 strout (buf, -1, -1, printcharfun, 0);
1766 strout (" Save your buffers immediately and please report this bug>",
1767 -1, -1, printcharfun, 0);
1771 print_depth--;
1774 #ifdef USE_TEXT_PROPERTIES
1776 /* Print a description of INTERVAL using PRINTCHARFUN.
1777 This is part of printing a string that has text properties. */
1779 void
1780 print_interval (interval, printcharfun)
1781 INTERVAL interval;
1782 Lisp_Object printcharfun;
1784 PRINTCHAR (' ');
1785 print (make_number (interval->position), printcharfun, 1);
1786 PRINTCHAR (' ');
1787 print (make_number (interval->position + LENGTH (interval)),
1788 printcharfun, 1);
1789 PRINTCHAR (' ');
1790 print (interval->plist, printcharfun, 1);
1793 #endif /* USE_TEXT_PROPERTIES */
1795 void
1796 syms_of_print ()
1798 DEFVAR_LISP ("standard-output", &Vstandard_output,
1799 "Output stream `print' uses by default for outputting a character.\n\
1800 This may be any function of one argument.\n\
1801 It may also be a buffer (output is inserted before point)\n\
1802 or a marker (output is inserted and the marker is advanced)\n\
1803 or the symbol t (output appears in the echo area).");
1804 Vstandard_output = Qt;
1805 Qstandard_output = intern ("standard-output");
1806 staticpro (&Qstandard_output);
1808 #ifdef LISP_FLOAT_TYPE
1809 DEFVAR_LISP ("float-output-format", &Vfloat_output_format,
1810 "The format descriptor string used to print floats.\n\
1811 This is a %-spec like those accepted by `printf' in C,\n\
1812 but with some restrictions. It must start with the two characters `%.'.\n\
1813 After that comes an integer precision specification,\n\
1814 and then a letter which controls the format.\n\
1815 The letters allowed are `e', `f' and `g'.\n\
1816 Use `e' for exponential notation \"DIG.DIGITSeEXPT\"\n\
1817 Use `f' for decimal point notation \"DIGITS.DIGITS\".\n\
1818 Use `g' to choose the shorter of those two formats for the number at hand.\n\
1819 The precision in any of these cases is the number of digits following\n\
1820 the decimal point. With `f', a precision of 0 means to omit the\n\
1821 decimal point. 0 is not allowed with `e' or `g'.\n\n\
1822 A value of nil means to use the shortest notation\n\
1823 that represents the number without losing information.");
1824 Vfloat_output_format = Qnil;
1825 Qfloat_output_format = intern ("float-output-format");
1826 staticpro (&Qfloat_output_format);
1827 #endif /* LISP_FLOAT_TYPE */
1829 DEFVAR_LISP ("print-length", &Vprint_length,
1830 "Maximum length of list to print before abbreviating.\n\
1831 A value of nil means no limit.");
1832 Vprint_length = Qnil;
1834 DEFVAR_LISP ("print-level", &Vprint_level,
1835 "Maximum depth of list nesting to print before abbreviating.\n\
1836 A value of nil means no limit.");
1837 Vprint_level = Qnil;
1839 DEFVAR_BOOL ("print-escape-newlines", &print_escape_newlines,
1840 "Non-nil means print newlines in strings as backslash-n.\n\
1841 Also print formfeeds as backslash-f.");
1842 print_escape_newlines = 0;
1844 DEFVAR_BOOL ("print-escape-nonascii", &print_escape_nonascii,
1845 "Non-nil means print non-ASCII characters in strings as backslash-NNN.\n\
1846 NNN is the octal representation of the character's value.\n\
1847 Only single-byte characters are affected, and only in `prin1'.");
1848 print_escape_nonascii = 0;
1850 DEFVAR_BOOL ("print-quoted", &print_quoted,
1851 "Non-nil means print quoted forms with reader syntax.\n\
1852 I.e., (quote foo) prints as 'foo, (function foo) as #'foo, and, backquoted\n\
1853 forms print in the new syntax.");
1854 print_quoted = 0;
1856 DEFVAR_LISP ("print-gensym", &Vprint_gensym,
1857 "Non-nil means print uninterned symbols so they will read as uninterned.\n\
1858 I.e., the value of (make-symbol \"foobar\") prints as #:foobar.\n\
1859 When the uninterned symbol appears within a larger data structure,\n\
1860 in addition use the #...# and #...= constructs as needed,\n\
1861 so that multiple references to the same symbol are shared once again\n\
1862 when the text is read back.\n\
1864 If the value of `print-gensym' is a cons cell, then in addition refrain from\n\
1865 clearing `print-gensym-alist' on entry to and exit from printing functions,\n\
1866 so that the use of #...# and #...= can carry over for several separately\n\
1867 printed objects.");
1868 Vprint_gensym = Qnil;
1870 DEFVAR_LISP ("print-gensym-alist", &Vprint_gensym_alist,
1871 "Association list of elements (GENSYM . N) to guide use of #N# and #N=.\n\
1872 In each element, GENSYM is an uninterned symbol that has been associated\n\
1873 with #N= for the specified value of N.");
1874 Vprint_gensym_alist = Qnil;
1876 /* prin1_to_string_buffer initialized in init_buffer_once in buffer.c */
1877 staticpro (&Vprin1_to_string_buffer);
1879 defsubr (&Sprin1);
1880 defsubr (&Sprin1_to_string);
1881 defsubr (&Serror_message_string);
1882 defsubr (&Sprinc);
1883 defsubr (&Sprint);
1884 defsubr (&Sterpri);
1885 defsubr (&Swrite_char);
1886 defsubr (&Sexternal_debugging_output);
1888 Qexternal_debugging_output = intern ("external-debugging-output");
1889 staticpro (&Qexternal_debugging_output);
1891 Qprint_escape_newlines = intern ("print-escape-newlines");
1892 staticpro (&Qprint_escape_newlines);
1894 #ifndef standalone
1895 defsubr (&Swith_output_to_temp_buffer);
1896 #endif /* not standalone */