(Qtemp_buffer_setup_hook): New variable.
[bpt/emacs.git] / src / print.c
1 /* Lisp object printing and output streams.
2 Copyright (C) 1985, 86, 88, 93, 94, 95, 97, 1998
3 Free Software Foundation, Inc.
4
5 This file is part of GNU Emacs.
6
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.
11
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.
16
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. */
21
22
23 #include <config.h>
24 #include <stdio.h>
25 #include "lisp.h"
26
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 */
37
38 #ifdef USE_TEXT_PROPERTIES
39 #include "intervals.h"
40 #endif
41
42 Lisp_Object Vstandard_output, Qstandard_output;
43
44 Lisp_Object Qtemp_buffer_setup_hook;
45
46 /* These are used to print like we read. */
47 extern Lisp_Object Qbackquote, Qcomma, Qcomma_at, Qcomma_dot, Qfunction;
48
49 #ifdef LISP_FLOAT_TYPE
50 Lisp_Object Vfloat_output_format, Qfloat_output_format;
51
52 /* Work around a problem that happens because math.h on hpux 7
53 defines two static variables--which, in Emacs, are not really static,
54 because `static' is defined as nothing. The problem is that they are
55 defined both here and in lread.c.
56 These macros prevent the name conflict. */
57 #if defined (HPUX) && !defined (HPUX8)
58 #define _MAXLDBL print_maxldbl
59 #define _NMAXLDBL print_nmaxldbl
60 #endif
61
62 #include <math.h>
63
64 #if STDC_HEADERS
65 #include <float.h>
66 #include <stdlib.h>
67 #endif
68
69 /* Default to values appropriate for IEEE floating point. */
70 #ifndef FLT_RADIX
71 #define FLT_RADIX 2
72 #endif
73 #ifndef DBL_MANT_DIG
74 #define DBL_MANT_DIG 53
75 #endif
76 #ifndef DBL_DIG
77 #define DBL_DIG 15
78 #endif
79 #ifndef DBL_MIN
80 #define DBL_MIN 2.2250738585072014e-308
81 #endif
82
83 #ifdef DBL_MIN_REPLACEMENT
84 #undef DBL_MIN
85 #define DBL_MIN DBL_MIN_REPLACEMENT
86 #endif
87
88 /* Define DOUBLE_DIGITS_BOUND, an upper bound on the number of decimal digits
89 needed to express a float without losing information.
90 The general-case formula is valid for the usual case, IEEE floating point,
91 but many compilers can't optimize the formula to an integer constant,
92 so make a special case for it. */
93 #if FLT_RADIX == 2 && DBL_MANT_DIG == 53
94 #define DOUBLE_DIGITS_BOUND 17 /* IEEE floating point */
95 #else
96 #define DOUBLE_DIGITS_BOUND ((int) ceil (log10 (pow (FLT_RADIX, DBL_MANT_DIG))))
97 #endif
98
99 #endif /* LISP_FLOAT_TYPE */
100
101 /* Avoid actual stack overflow in print. */
102 int print_depth;
103
104 /* Detect most circularities to print finite output. */
105 #define PRINT_CIRCLE 200
106 Lisp_Object being_printed[PRINT_CIRCLE];
107
108 /* When printing into a buffer, first we put the text in this
109 block, then insert it all at once. */
110 char *print_buffer;
111
112 /* Size allocated in print_buffer. */
113 int print_buffer_size;
114 /* Chars stored in print_buffer. */
115 int print_buffer_pos;
116 /* Bytes stored in print_buffer. */
117 int print_buffer_pos_byte;
118
119 /* Maximum length of list to print in full; noninteger means
120 effectively infinity */
121
122 Lisp_Object Vprint_length;
123
124 /* Maximum depth of list to print in full; noninteger means
125 effectively infinity. */
126
127 Lisp_Object Vprint_level;
128
129 /* Nonzero means print newlines in strings as \n. */
130
131 int print_escape_newlines;
132
133 /* Nonzero means to print single-byte non-ascii characters in strings as
134 octal escapes. */
135
136 int print_escape_nonascii;
137
138 /* Nonzero means to print multibyte characters in strings as hex escapes. */
139
140 int print_escape_multibyte;
141
142 Lisp_Object Qprint_escape_newlines;
143 Lisp_Object Qprint_escape_multibyte, Qprint_escape_nonascii;
144
145 /* Nonzero means print (quote foo) forms as 'foo, etc. */
146
147 int print_quoted;
148
149 /* Non-nil means print #: before uninterned symbols.
150 Neither t nor nil means so that and don't clear Vprint_gensym_alist
151 on entry to and exit from print functions. */
152
153 Lisp_Object Vprint_gensym;
154
155 /* Association list of certain objects that are `eq' in the form being
156 printed and which should be `eq' when read back in, using the #n=object
157 and #n# reader forms. Each element has the form (object . n). */
158
159 Lisp_Object Vprint_gensym_alist;
160
161 /* Nonzero means print newline to stdout before next minibuffer message.
162 Defined in xdisp.c */
163
164 extern int noninteractive_need_newline;
165
166 extern int minibuffer_auto_raise;
167
168 #ifdef MAX_PRINT_CHARS
169 static int print_chars;
170 static int max_print;
171 #endif /* MAX_PRINT_CHARS */
172
173 void print_interval ();
174 \f
175 #if 0
176 /* Convert between chars and GLYPHs */
177
178 int
179 glyphlen (glyphs)
180 register GLYPH *glyphs;
181 {
182 register int i = 0;
183
184 while (glyphs[i])
185 i++;
186 return i;
187 }
188
189 void
190 str_to_glyph_cpy (str, glyphs)
191 char *str;
192 GLYPH *glyphs;
193 {
194 register GLYPH *gp = glyphs;
195 register char *cp = str;
196
197 while (*cp)
198 *gp++ = *cp++;
199 }
200
201 void
202 str_to_glyph_ncpy (str, glyphs, n)
203 char *str;
204 GLYPH *glyphs;
205 register int n;
206 {
207 register GLYPH *gp = glyphs;
208 register char *cp = str;
209
210 while (n-- > 0)
211 *gp++ = *cp++;
212 }
213
214 void
215 glyph_to_str_cpy (glyphs, str)
216 GLYPH *glyphs;
217 char *str;
218 {
219 register GLYPH *gp = glyphs;
220 register char *cp = str;
221
222 while (*gp)
223 *str++ = *gp++ & 0377;
224 }
225 #endif
226 \f
227 /* Low level output routines for characters and strings */
228
229 /* Lisp functions to do output using a stream
230 must have the stream in a variable called printcharfun
231 and must start with PRINTPREPARE, end with PRINTFINISH,
232 and use PRINTDECLARE to declare common variables.
233 Use PRINTCHAR to output one character,
234 or call strout to output a block of characters.
235 */
236
237 #define PRINTDECLARE \
238 struct buffer *old = current_buffer; \
239 int old_point = -1, start_point; \
240 int old_point_byte, start_point_byte; \
241 int specpdl_count = specpdl_ptr - specpdl; \
242 int free_print_buffer = 0; \
243 Lisp_Object original
244
245 #define PRINTPREPARE \
246 original = printcharfun; \
247 if (NILP (printcharfun)) printcharfun = Qt; \
248 if (BUFFERP (printcharfun)) \
249 { \
250 if (XBUFFER (printcharfun) != current_buffer) \
251 Fset_buffer (printcharfun); \
252 printcharfun = Qnil; \
253 } \
254 if (MARKERP (printcharfun)) \
255 { \
256 if (!(XMARKER (original)->buffer)) \
257 error ("Marker does not point anywhere"); \
258 if (XMARKER (original)->buffer != current_buffer) \
259 set_buffer_internal (XMARKER (original)->buffer); \
260 old_point = PT; \
261 old_point_byte = PT_BYTE; \
262 SET_PT_BOTH (marker_position (printcharfun), \
263 marker_byte_position (printcharfun)); \
264 start_point = PT; \
265 start_point_byte = PT_BYTE; \
266 printcharfun = Qnil; \
267 } \
268 if (NILP (printcharfun)) \
269 { \
270 Lisp_Object string; \
271 if (NILP (current_buffer->enable_multibyte_characters) \
272 && ! print_escape_multibyte) \
273 specbind (Qprint_escape_multibyte, Qt); \
274 if (! NILP (current_buffer->enable_multibyte_characters) \
275 && ! print_escape_nonascii) \
276 specbind (Qprint_escape_nonascii, Qt); \
277 if (print_buffer != 0) \
278 { \
279 string = make_string_from_bytes (print_buffer, \
280 print_buffer_pos, \
281 print_buffer_pos_byte); \
282 record_unwind_protect (print_unwind, string); \
283 } \
284 else \
285 { \
286 print_buffer_size = 1000; \
287 print_buffer = (char *) xmalloc (print_buffer_size); \
288 free_print_buffer = 1; \
289 } \
290 print_buffer_pos = 0; \
291 print_buffer_pos_byte = 0; \
292 } \
293 if (!CONSP (Vprint_gensym)) \
294 Vprint_gensym_alist = Qnil
295
296 #define PRINTFINISH \
297 if (NILP (printcharfun)) \
298 { \
299 if (print_buffer_pos != print_buffer_pos_byte \
300 && NILP (current_buffer->enable_multibyte_characters)) \
301 { \
302 unsigned char *temp \
303 = (unsigned char *) alloca (print_buffer_pos + 1); \
304 copy_text (print_buffer, temp, print_buffer_pos_byte, \
305 1, 0); \
306 insert_1_both (temp, print_buffer_pos, \
307 print_buffer_pos, 0, 1, 0); \
308 } \
309 else \
310 insert_1_both (print_buffer, print_buffer_pos, \
311 print_buffer_pos_byte, 0, 1, 0); \
312 } \
313 if (free_print_buffer) \
314 { \
315 xfree (print_buffer); \
316 print_buffer = 0; \
317 } \
318 unbind_to (specpdl_count, Qnil); \
319 if (MARKERP (original)) \
320 set_marker_both (original, Qnil, PT, PT_BYTE); \
321 if (old_point >= 0) \
322 SET_PT_BOTH (old_point + (old_point >= start_point \
323 ? PT - start_point : 0), \
324 old_point_byte + (old_point_byte >= start_point_byte \
325 ? PT_BYTE - start_point_byte : 0)); \
326 if (old != current_buffer) \
327 set_buffer_internal (old); \
328 if (!CONSP (Vprint_gensym)) \
329 Vprint_gensym_alist = Qnil
330
331 #define PRINTCHAR(ch) printchar (ch, printcharfun)
332
333 /* Nonzero if there is no room to print any more characters
334 so print might as well return right away. */
335
336 #define PRINTFULLP() \
337 (EQ (printcharfun, Qt) && !noninteractive \
338 && printbufidx >= FRAME_WIDTH (XFRAME (WINDOW_FRAME (XWINDOW (minibuf_window)))))
339
340 /* This is used to restore the saved contents of print_buffer
341 when there is a recursive call to print. */
342 static Lisp_Object
343 print_unwind (saved_text)
344 Lisp_Object saved_text;
345 {
346 bcopy (XSTRING (saved_text)->data, print_buffer, XSTRING (saved_text)->size);
347 }
348
349 /* Index of first unused element of FRAME_MESSAGE_BUF (mini_frame). */
350 static int printbufidx;
351
352 static void
353 printchar (ch, fun)
354 unsigned int ch;
355 Lisp_Object fun;
356 {
357 Lisp_Object ch1;
358
359 #ifdef MAX_PRINT_CHARS
360 if (max_print)
361 print_chars++;
362 #endif /* MAX_PRINT_CHARS */
363 #ifndef standalone
364 if (EQ (fun, Qnil))
365 {
366 int len;
367 unsigned char work[4], *str;
368
369 QUIT;
370 len = CHAR_STRING (ch, work, str);
371 if (print_buffer_pos_byte + len >= print_buffer_size)
372 print_buffer = (char *) xrealloc (print_buffer,
373 print_buffer_size *= 2);
374 bcopy (str, print_buffer + print_buffer_pos_byte, len);
375 print_buffer_pos += 1;
376 print_buffer_pos_byte += len;
377 return;
378 }
379
380 if (EQ (fun, Qt))
381 {
382 FRAME_PTR mini_frame
383 = XFRAME (WINDOW_FRAME (XWINDOW (minibuf_window)));
384 unsigned char work[4], *str;
385 int len = CHAR_STRING (ch, work, str);
386
387 QUIT;
388
389 if (noninteractive)
390 {
391 while (len--)
392 putchar (*str), str++;
393 noninteractive_need_newline = 1;
394 return;
395 }
396
397 if (echo_area_glyphs != FRAME_MESSAGE_BUF (mini_frame)
398 || !message_buf_print)
399 {
400 message_log_maybe_newline ();
401 echo_area_glyphs = FRAME_MESSAGE_BUF (mini_frame);
402 printbufidx = 0;
403 echo_area_glyphs_length = 0;
404 message_buf_print = 1;
405
406 if (minibuffer_auto_raise)
407 {
408 Lisp_Object mini_window;
409
410 /* Get the frame containing the minibuffer
411 that the selected frame is using. */
412 mini_window = FRAME_MINIBUF_WINDOW (selected_frame);
413
414 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
415 }
416 }
417
418 if (len == 1
419 && ! NILP (current_buffer->enable_multibyte_characters)
420 && ! CHAR_HEAD_P (*str))
421 {
422 /* Convert the unibyte character to multibyte. */
423 unsigned char c = *str;
424
425 len = count_size_as_multibyte (&c, 1);
426 copy_text (&c, work, 1, 0, 1);
427 str = work;
428 }
429
430 message_dolog (str, len, 0, len > 1);
431
432 if (! NILP (current_buffer->enable_multibyte_characters)
433 && ! message_enable_multibyte)
434 {
435 /* Record that the message buffer is multibyte. */
436 message_enable_multibyte = 1;
437
438 /* If we have already had some message text in the messsage
439 buffer, we convert it to multibyte. */
440 if (printbufidx > 0)
441 {
442 int size
443 = count_size_as_multibyte (FRAME_MESSAGE_BUF (mini_frame),
444 printbufidx);
445 unsigned char *tembuf = (unsigned char *) alloca (size + 1);
446 copy_text (FRAME_MESSAGE_BUF (mini_frame), tembuf, printbufidx,
447 0, 1);
448 printbufidx = size;
449 if (printbufidx > FRAME_MESSAGE_BUF_SIZE (mini_frame))
450 {
451 printbufidx = FRAME_MESSAGE_BUF_SIZE (mini_frame);
452 /* Rewind incomplete multi-byte form. */
453 while (printbufidx > 0 && tembuf[printbufidx] >= 0xA0)
454 printbufidx--;
455 }
456 bcopy (tembuf, FRAME_MESSAGE_BUF (mini_frame), printbufidx);
457 }
458 }
459
460 if (printbufidx < FRAME_MESSAGE_BUF_SIZE (mini_frame) - len)
461 {
462 bcopy (str, &FRAME_MESSAGE_BUF (mini_frame)[printbufidx], len);
463 printbufidx += len;
464 }
465 FRAME_MESSAGE_BUF (mini_frame)[printbufidx] = 0;
466 echo_area_glyphs_length = printbufidx;
467
468 return;
469 }
470 #endif /* not standalone */
471
472 XSETFASTINT (ch1, ch);
473 call1 (fun, ch1);
474 }
475
476 static void
477 strout (ptr, size, size_byte, printcharfun, multibyte)
478 char *ptr;
479 int size, size_byte;
480 Lisp_Object printcharfun;
481 int multibyte;
482 {
483 int i = 0;
484
485 if (size < 0)
486 size_byte = size = strlen (ptr);
487
488 if (EQ (printcharfun, Qnil))
489 {
490 if (print_buffer_pos_byte + size_byte > print_buffer_size)
491 {
492 print_buffer_size = print_buffer_size * 2 + size_byte;
493 print_buffer = (char *) xrealloc (print_buffer,
494 print_buffer_size);
495 }
496 bcopy (ptr, print_buffer + print_buffer_pos_byte, size_byte);
497 print_buffer_pos += size;
498 print_buffer_pos_byte += size_byte;
499
500 #ifdef MAX_PRINT_CHARS
501 if (max_print)
502 print_chars += size;
503 #endif /* MAX_PRINT_CHARS */
504 return;
505 }
506 if (EQ (printcharfun, Qt))
507 {
508 FRAME_PTR mini_frame
509 = XFRAME (WINDOW_FRAME (XWINDOW (minibuf_window)));
510
511 QUIT;
512
513 #ifdef MAX_PRINT_CHARS
514 if (max_print)
515 print_chars += size;
516 #endif /* MAX_PRINT_CHARS */
517
518 if (noninteractive)
519 {
520 fwrite (ptr, 1, size_byte, stdout);
521 noninteractive_need_newline = 1;
522 return;
523 }
524
525 if (echo_area_glyphs != FRAME_MESSAGE_BUF (mini_frame)
526 || !message_buf_print)
527 {
528 message_log_maybe_newline ();
529 echo_area_glyphs = FRAME_MESSAGE_BUF (mini_frame);
530 printbufidx = 0;
531 echo_area_glyphs_length = 0;
532 message_buf_print = 1;
533
534 if (minibuffer_auto_raise)
535 {
536 Lisp_Object mini_window;
537
538 /* Get the frame containing the minibuffer
539 that the selected frame is using. */
540 mini_window = FRAME_MINIBUF_WINDOW (selected_frame);
541
542 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
543 }
544 }
545
546 message_dolog (ptr, size_byte, 0, multibyte);
547
548 /* Convert message to multibyte if we are now adding multibyte text. */
549 if (multibyte
550 && ! message_enable_multibyte
551 && printbufidx > 0)
552 {
553 int size = count_size_as_multibyte (FRAME_MESSAGE_BUF (mini_frame),
554 printbufidx);
555 unsigned char *tembuf = (unsigned char *) alloca (size + 1);
556 copy_text (FRAME_MESSAGE_BUF (mini_frame), tembuf, printbufidx,
557 0, 1);
558 printbufidx = size;
559 if (printbufidx > FRAME_MESSAGE_BUF_SIZE (mini_frame))
560 {
561 printbufidx = FRAME_MESSAGE_BUF_SIZE (mini_frame);
562 /* Rewind incomplete multi-byte form. */
563 while (printbufidx > 0 && tembuf[printbufidx] >= 0xA0)
564 printbufidx--;
565 }
566
567 bcopy (tembuf, FRAME_MESSAGE_BUF (mini_frame), printbufidx);
568 }
569
570 if (multibyte)
571 message_enable_multibyte = 1;
572
573 /* Compute how much of the new text will fit there. */
574 if (size_byte > FRAME_MESSAGE_BUF_SIZE (mini_frame) - printbufidx - 1)
575 {
576 size_byte = FRAME_MESSAGE_BUF_SIZE (mini_frame) - printbufidx - 1;
577 /* Rewind incomplete multi-byte form. */
578 while (size_byte && (unsigned char) ptr[size_byte] >= 0xA0)
579 size_byte--;
580 }
581
582 /* Put that part of the new text in. */
583 bcopy (ptr, &FRAME_MESSAGE_BUF (mini_frame) [printbufidx], size_byte);
584 printbufidx += size_byte;
585 FRAME_MESSAGE_BUF (mini_frame) [printbufidx] = 0;
586 echo_area_glyphs_length = printbufidx;
587
588 return;
589 }
590
591 i = 0;
592 if (size == size_byte)
593 while (i < size_byte)
594 {
595 int ch = ptr[i++];
596
597 PRINTCHAR (ch);
598 }
599 else
600 while (i < size_byte)
601 {
602 /* Here, we must convert each multi-byte form to the
603 corresponding character code before handing it to PRINTCHAR. */
604 int len;
605 int ch = STRING_CHAR_AND_LENGTH (ptr + i, size_byte - i, len);
606
607 PRINTCHAR (ch);
608 i += len;
609 }
610 }
611
612 /* Print the contents of a string STRING using PRINTCHARFUN.
613 It isn't safe to use strout in many cases,
614 because printing one char can relocate. */
615
616 static void
617 print_string (string, printcharfun)
618 Lisp_Object string;
619 Lisp_Object printcharfun;
620 {
621 if (EQ (printcharfun, Qt) || NILP (printcharfun))
622 {
623 int chars;
624
625 if (STRING_MULTIBYTE (string))
626 chars = XSTRING (string)->size;
627 else if (EQ (printcharfun, Qt)
628 ? ! NILP (buffer_defaults.enable_multibyte_characters)
629 : ! NILP (current_buffer->enable_multibyte_characters))
630 chars = multibyte_chars_in_text (XSTRING (string)->data,
631 STRING_BYTES (XSTRING (string)));
632 else
633 chars = STRING_BYTES (XSTRING (string));
634
635 /* strout is safe for output to a frame (echo area) or to print_buffer. */
636 strout (XSTRING (string)->data,
637 chars, STRING_BYTES (XSTRING (string)),
638 printcharfun, STRING_MULTIBYTE (string));
639 }
640 else
641 {
642 /* Otherwise, string may be relocated by printing one char.
643 So re-fetch the string address for each character. */
644 int i;
645 int size = XSTRING (string)->size;
646 int size_byte = STRING_BYTES (XSTRING (string));
647 struct gcpro gcpro1;
648 GCPRO1 (string);
649 if (size == size_byte)
650 for (i = 0; i < size; i++)
651 PRINTCHAR (XSTRING (string)->data[i]);
652 else
653 for (i = 0; i < size_byte; i++)
654 {
655 /* Here, we must convert each multi-byte form to the
656 corresponding character code before handing it to PRINTCHAR. */
657 int len;
658 int ch = STRING_CHAR_AND_CHAR_LENGTH (XSTRING (string)->data + i,
659 size_byte - i, len);
660 if (!CHAR_VALID_P (ch, 0))
661 {
662 ch = XSTRING (string)->data[i];
663 len = 1;
664 }
665 PRINTCHAR (ch);
666 i += len;
667 }
668 UNGCPRO;
669 }
670 }
671 \f
672 DEFUN ("write-char", Fwrite_char, Swrite_char, 1, 2, 0,
673 "Output character CHARACTER to stream PRINTCHARFUN.\n\
674 PRINTCHARFUN defaults to the value of `standard-output' (which see).")
675 (character, printcharfun)
676 Lisp_Object character, printcharfun;
677 {
678 PRINTDECLARE;
679
680 if (NILP (printcharfun))
681 printcharfun = Vstandard_output;
682 CHECK_NUMBER (character, 0);
683 PRINTPREPARE;
684 PRINTCHAR (XINT (character));
685 PRINTFINISH;
686 return character;
687 }
688
689 /* Used from outside of print.c to print a block of SIZE
690 single-byte chars at DATA on the default output stream.
691 Do not use this on the contents of a Lisp string. */
692
693 void
694 write_string (data, size)
695 char *data;
696 int size;
697 {
698 PRINTDECLARE;
699 Lisp_Object printcharfun;
700
701 printcharfun = Vstandard_output;
702
703 PRINTPREPARE;
704 strout (data, size, size, printcharfun, 0);
705 PRINTFINISH;
706 }
707
708 /* Used from outside of print.c to print a block of SIZE
709 single-byte chars at DATA on a specified stream PRINTCHARFUN.
710 Do not use this on the contents of a Lisp string. */
711
712 void
713 write_string_1 (data, size, printcharfun)
714 char *data;
715 int size;
716 Lisp_Object printcharfun;
717 {
718 PRINTDECLARE;
719
720 PRINTPREPARE;
721 strout (data, size, size, printcharfun, 0);
722 PRINTFINISH;
723 }
724
725
726 #ifndef standalone
727
728 void
729 temp_output_buffer_setup (bufname)
730 char *bufname;
731 {
732 int count = specpdl_ptr - specpdl;
733 register struct buffer *old = current_buffer;
734 register Lisp_Object buf;
735
736 record_unwind_protect (set_buffer_if_live, Fcurrent_buffer ());
737
738 Fset_buffer (Fget_buffer_create (build_string (bufname)));
739
740 current_buffer->directory = old->directory;
741 current_buffer->read_only = Qnil;
742 current_buffer->filename = Qnil;
743 current_buffer->undo_list = Qt;
744 current_buffer->overlays_before = Qnil;
745 current_buffer->overlays_after = Qnil;
746 current_buffer->enable_multibyte_characters
747 = buffer_defaults.enable_multibyte_characters;
748 Ferase_buffer ();
749 XSETBUFFER (buf, current_buffer);
750
751 call1 (Vrun_hooks, Qtemp_buffer_setup_hook);
752
753 unbind_to (count, Qnil);
754
755 specbind (Qstandard_output, buf);
756 }
757
758 Lisp_Object
759 internal_with_output_to_temp_buffer (bufname, function, args)
760 char *bufname;
761 Lisp_Object (*function) P_ ((Lisp_Object));
762 Lisp_Object args;
763 {
764 int count = specpdl_ptr - specpdl;
765 Lisp_Object buf, val;
766 struct gcpro gcpro1;
767
768 GCPRO1 (args);
769 record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
770 temp_output_buffer_setup (bufname);
771 buf = Vstandard_output;
772 UNGCPRO;
773
774 val = (*function) (args);
775
776 GCPRO1 (val);
777 temp_output_buffer_show (buf);
778 UNGCPRO;
779
780 return unbind_to (count, val);
781 }
782
783 DEFUN ("with-output-to-temp-buffer", Fwith_output_to_temp_buffer, Swith_output_to_temp_buffer,
784 1, UNEVALLED, 0,
785 "Bind `standard-output' to buffer BUFNAME, eval BODY, then show that buffer.\n\
786 The buffer is cleared out initially, and marked as unmodified when done.\n\
787 All output done by BODY is inserted in that buffer by default.\n\
788 The buffer is displayed in another window, but not selected.\n\
789 The value of the last form in BODY is returned.\n\
790 If BODY does not finish normally, the buffer BUFNAME is not displayed.\n\
791 \n\
792 The hook `temp-buffer-setup-hook' is run before BODY,\n\
793 with the buffer BUFNAME temporarily current.\n\
794 The hook `temp-buffer-show-hook' is run after the buffer is displayed,\n\
795 with the buffer temporarily current, and the window that was used\n\
796 to display it temporarily selected.\n\
797 \n\
798 If variable `temp-buffer-show-function' is non-nil, call it at the end\n\
799 to get the buffer displayed instead of just displaying the non-selected\n\
800 buffer and calling the hook. It gets one argument, the buffer to display.")
801 (args)
802 Lisp_Object args;
803 {
804 struct gcpro gcpro1;
805 Lisp_Object name;
806 int count = specpdl_ptr - specpdl;
807 Lisp_Object buf, val;
808
809 GCPRO1(args);
810 name = Feval (Fcar (args));
811 UNGCPRO;
812
813 CHECK_STRING (name, 0);
814 temp_output_buffer_setup (XSTRING (name)->data);
815 buf = Vstandard_output;
816
817 val = Fprogn (Fcdr (args));
818
819 temp_output_buffer_show (buf);
820
821 return unbind_to (count, val);
822 }
823 #endif /* not standalone */
824 \f
825 static void print ();
826
827 DEFUN ("terpri", Fterpri, Sterpri, 0, 1, 0,
828 "Output a newline to stream PRINTCHARFUN.\n\
829 If PRINTCHARFUN is omitted or nil, the value of `standard-output' is used.")
830 (printcharfun)
831 Lisp_Object printcharfun;
832 {
833 PRINTDECLARE;
834
835 if (NILP (printcharfun))
836 printcharfun = Vstandard_output;
837 PRINTPREPARE;
838 PRINTCHAR ('\n');
839 PRINTFINISH;
840 return Qt;
841 }
842
843 DEFUN ("prin1", Fprin1, Sprin1, 1, 2, 0,
844 "Output the printed representation of OBJECT, any Lisp object.\n\
845 Quoting characters are printed when needed to make output that `read'\n\
846 can handle, whenever this is possible.\n\
847 Output stream is PRINTCHARFUN, or value of `standard-output' (which see).")
848 (object, printcharfun)
849 Lisp_Object object, printcharfun;
850 {
851 PRINTDECLARE;
852
853 #ifdef MAX_PRINT_CHARS
854 max_print = 0;
855 #endif /* MAX_PRINT_CHARS */
856 if (NILP (printcharfun))
857 printcharfun = Vstandard_output;
858 PRINTPREPARE;
859 print_depth = 0;
860 print (object, printcharfun, 1);
861 PRINTFINISH;
862 return object;
863 }
864
865 /* a buffer which is used to hold output being built by prin1-to-string */
866 Lisp_Object Vprin1_to_string_buffer;
867
868 DEFUN ("prin1-to-string", Fprin1_to_string, Sprin1_to_string, 1, 2, 0,
869 "Return a string containing the printed representation of OBJECT,\n\
870 any Lisp object. Quoting characters are used when needed to make output\n\
871 that `read' can handle, whenever this is possible, unless the optional\n\
872 second argument NOESCAPE is non-nil.")
873 (object, noescape)
874 Lisp_Object object, noescape;
875 {
876 PRINTDECLARE;
877 Lisp_Object printcharfun;
878 struct gcpro gcpro1, gcpro2;
879 Lisp_Object tem;
880
881 /* Save and restore this--we are altering a buffer
882 but we don't want to deactivate the mark just for that.
883 No need for specbind, since errors deactivate the mark. */
884 tem = Vdeactivate_mark;
885 GCPRO2 (object, tem);
886
887 printcharfun = Vprin1_to_string_buffer;
888 PRINTPREPARE;
889 print_depth = 0;
890 print (object, printcharfun, NILP (noescape));
891 /* Make Vprin1_to_string_buffer be the default buffer after PRINTFINSH */
892 PRINTFINISH;
893 set_buffer_internal (XBUFFER (Vprin1_to_string_buffer));
894 object = Fbuffer_string ();
895
896 Ferase_buffer ();
897 set_buffer_internal (old);
898
899 Vdeactivate_mark = tem;
900 UNGCPRO;
901
902 return object;
903 }
904
905 DEFUN ("princ", Fprinc, Sprinc, 1, 2, 0,
906 "Output the printed representation of OBJECT, any Lisp object.\n\
907 No quoting characters are used; no delimiters are printed around\n\
908 the contents of strings.\n\
909 Output stream is PRINTCHARFUN, or value of standard-output (which see).")
910 (object, printcharfun)
911 Lisp_Object object, printcharfun;
912 {
913 PRINTDECLARE;
914
915 if (NILP (printcharfun))
916 printcharfun = Vstandard_output;
917 PRINTPREPARE;
918 print_depth = 0;
919 print (object, printcharfun, 0);
920 PRINTFINISH;
921 return object;
922 }
923
924 DEFUN ("print", Fprint, Sprint, 1, 2, 0,
925 "Output the printed representation of OBJECT, with newlines around it.\n\
926 Quoting characters are printed when needed to make output that `read'\n\
927 can handle, whenever this is possible.\n\
928 Output stream is PRINTCHARFUN, or value of `standard-output' (which see).")
929 (object, printcharfun)
930 Lisp_Object object, printcharfun;
931 {
932 PRINTDECLARE;
933 struct gcpro gcpro1;
934
935 #ifdef MAX_PRINT_CHARS
936 print_chars = 0;
937 max_print = MAX_PRINT_CHARS;
938 #endif /* MAX_PRINT_CHARS */
939 if (NILP (printcharfun))
940 printcharfun = Vstandard_output;
941 GCPRO1 (object);
942 PRINTPREPARE;
943 print_depth = 0;
944 PRINTCHAR ('\n');
945 print (object, printcharfun, 1);
946 PRINTCHAR ('\n');
947 PRINTFINISH;
948 #ifdef MAX_PRINT_CHARS
949 max_print = 0;
950 print_chars = 0;
951 #endif /* MAX_PRINT_CHARS */
952 UNGCPRO;
953 return object;
954 }
955
956 /* The subroutine object for external-debugging-output is kept here
957 for the convenience of the debugger. */
958 Lisp_Object Qexternal_debugging_output;
959
960 DEFUN ("external-debugging-output", Fexternal_debugging_output, Sexternal_debugging_output, 1, 1, 0,
961 "Write CHARACTER to stderr.\n\
962 You can call print while debugging emacs, and pass it this function\n\
963 to make it write to the debugging output.\n")
964 (character)
965 Lisp_Object character;
966 {
967 CHECK_NUMBER (character, 0);
968 putc (XINT (character), stderr);
969
970 #ifdef WINDOWSNT
971 /* Send the output to a debugger (nothing happens if there isn't one). */
972 {
973 char buf[2] = {(char) XINT (character), '\0'};
974 OutputDebugString (buf);
975 }
976 #endif
977
978 return character;
979 }
980
981 /* This is the interface for debugging printing. */
982
983 void
984 debug_print (arg)
985 Lisp_Object arg;
986 {
987 Fprin1 (arg, Qexternal_debugging_output);
988 fprintf (stderr, "\r\n");
989 }
990 \f
991 DEFUN ("error-message-string", Ferror_message_string, Serror_message_string,
992 1, 1, 0,
993 "Convert an error value (ERROR-SYMBOL . DATA) to an error message.")
994 (obj)
995 Lisp_Object obj;
996 {
997 struct buffer *old = current_buffer;
998 Lisp_Object original, printcharfun, value;
999 struct gcpro gcpro1;
1000
1001 /* If OBJ is (error STRING), just return STRING.
1002 That is not only faster, it also avoids the need to allocate
1003 space here when the error is due to memory full. */
1004 if (CONSP (obj) && EQ (XCONS (obj)->car, Qerror)
1005 && CONSP (XCONS (obj)->cdr)
1006 && STRINGP (XCONS (XCONS (obj)->cdr)->car)
1007 && NILP (XCONS (XCONS (obj)->cdr)->cdr))
1008 return XCONS (XCONS (obj)->cdr)->car;
1009
1010 print_error_message (obj, Vprin1_to_string_buffer);
1011
1012 set_buffer_internal (XBUFFER (Vprin1_to_string_buffer));
1013 value = Fbuffer_string ();
1014
1015 GCPRO1 (value);
1016 Ferase_buffer ();
1017 set_buffer_internal (old);
1018 UNGCPRO;
1019
1020 return value;
1021 }
1022
1023 /* Print an error message for the error DATA
1024 onto Lisp output stream STREAM (suitable for the print functions). */
1025
1026 void
1027 print_error_message (data, stream)
1028 Lisp_Object data, stream;
1029 {
1030 Lisp_Object errname, errmsg, file_error, tail;
1031 struct gcpro gcpro1;
1032 int i;
1033
1034 errname = Fcar (data);
1035
1036 if (EQ (errname, Qerror))
1037 {
1038 data = Fcdr (data);
1039 if (!CONSP (data)) data = Qnil;
1040 errmsg = Fcar (data);
1041 file_error = Qnil;
1042 }
1043 else
1044 {
1045 errmsg = Fget (errname, Qerror_message);
1046 file_error = Fmemq (Qfile_error,
1047 Fget (errname, Qerror_conditions));
1048 }
1049
1050 /* Print an error message including the data items. */
1051
1052 tail = Fcdr_safe (data);
1053 GCPRO1 (tail);
1054
1055 /* For file-error, make error message by concatenating
1056 all the data items. They are all strings. */
1057 if (!NILP (file_error) && !NILP (tail))
1058 errmsg = XCONS (tail)->car, tail = XCONS (tail)->cdr;
1059
1060 if (STRINGP (errmsg))
1061 Fprinc (errmsg, stream);
1062 else
1063 write_string_1 ("peculiar error", -1, stream);
1064
1065 for (i = 0; CONSP (tail); tail = Fcdr (tail), i++)
1066 {
1067 write_string_1 (i ? ", " : ": ", 2, stream);
1068 if (!NILP (file_error))
1069 Fprinc (Fcar (tail), stream);
1070 else
1071 Fprin1 (Fcar (tail), stream);
1072 }
1073 UNGCPRO;
1074 }
1075 \f
1076 #ifdef LISP_FLOAT_TYPE
1077
1078 /*
1079 * The buffer should be at least as large as the max string size of the
1080 * largest float, printed in the biggest notation. This is undoubtedly
1081 * 20d float_output_format, with the negative of the C-constant "HUGE"
1082 * from <math.h>.
1083 *
1084 * On the vax the worst case is -1e38 in 20d format which takes 61 bytes.
1085 *
1086 * I assume that IEEE-754 format numbers can take 329 bytes for the worst
1087 * case of -1e307 in 20d float_output_format. What is one to do (short of
1088 * re-writing _doprnt to be more sane)?
1089 * -wsr
1090 */
1091
1092 void
1093 float_to_string (buf, data)
1094 unsigned char *buf;
1095 double data;
1096 {
1097 unsigned char *cp;
1098 int width;
1099
1100 /* Check for plus infinity in a way that won't lose
1101 if there is no plus infinity. */
1102 if (data == data / 2 && data > 1.0)
1103 {
1104 strcpy (buf, "1.0e+INF");
1105 return;
1106 }
1107 /* Likewise for minus infinity. */
1108 if (data == data / 2 && data < -1.0)
1109 {
1110 strcpy (buf, "-1.0e+INF");
1111 return;
1112 }
1113 /* Check for NaN in a way that won't fail if there are no NaNs. */
1114 if (! (data * 0.0 >= 0.0))
1115 {
1116 strcpy (buf, "0.0e+NaN");
1117 return;
1118 }
1119
1120 if (NILP (Vfloat_output_format)
1121 || !STRINGP (Vfloat_output_format))
1122 lose:
1123 {
1124 /* Generate the fewest number of digits that represent the
1125 floating point value without losing information.
1126 The following method is simple but a bit slow.
1127 For ideas about speeding things up, please see:
1128
1129 Guy L Steele Jr & Jon L White, How to print floating-point numbers
1130 accurately. SIGPLAN notices 25, 6 (June 1990), 112-126.
1131
1132 Robert G Burger & R Kent Dybvig, Printing floating point numbers
1133 quickly and accurately, SIGPLAN notices 31, 5 (May 1996), 108-116. */
1134
1135 width = fabs (data) < DBL_MIN ? 1 : DBL_DIG;
1136 do
1137 sprintf (buf, "%.*g", width, data);
1138 while (width++ < DOUBLE_DIGITS_BOUND && atof (buf) != data);
1139 }
1140 else /* oink oink */
1141 {
1142 /* Check that the spec we have is fully valid.
1143 This means not only valid for printf,
1144 but meant for floats, and reasonable. */
1145 cp = XSTRING (Vfloat_output_format)->data;
1146
1147 if (cp[0] != '%')
1148 goto lose;
1149 if (cp[1] != '.')
1150 goto lose;
1151
1152 cp += 2;
1153
1154 /* Check the width specification. */
1155 width = -1;
1156 if ('0' <= *cp && *cp <= '9')
1157 {
1158 width = 0;
1159 do
1160 width = (width * 10) + (*cp++ - '0');
1161 while (*cp >= '0' && *cp <= '9');
1162
1163 /* A precision of zero is valid only for %f. */
1164 if (width > DBL_DIG
1165 || (width == 0 && *cp != 'f'))
1166 goto lose;
1167 }
1168
1169 if (*cp != 'e' && *cp != 'f' && *cp != 'g')
1170 goto lose;
1171
1172 if (cp[1] != 0)
1173 goto lose;
1174
1175 sprintf (buf, XSTRING (Vfloat_output_format)->data, data);
1176 }
1177
1178 /* Make sure there is a decimal point with digit after, or an
1179 exponent, so that the value is readable as a float. But don't do
1180 this with "%.0f"; it's valid for that not to produce a decimal
1181 point. Note that width can be 0 only for %.0f. */
1182 if (width != 0)
1183 {
1184 for (cp = buf; *cp; cp++)
1185 if ((*cp < '0' || *cp > '9') && *cp != '-')
1186 break;
1187
1188 if (*cp == '.' && cp[1] == 0)
1189 {
1190 cp[1] = '0';
1191 cp[2] = 0;
1192 }
1193
1194 if (*cp == 0)
1195 {
1196 *cp++ = '.';
1197 *cp++ = '0';
1198 *cp++ = 0;
1199 }
1200 }
1201 }
1202 #endif /* LISP_FLOAT_TYPE */
1203 \f
1204 static void
1205 print (obj, printcharfun, escapeflag)
1206 Lisp_Object obj;
1207 register Lisp_Object printcharfun;
1208 int escapeflag;
1209 {
1210 char buf[30];
1211
1212 QUIT;
1213
1214 #if 1 /* I'm not sure this is really worth doing. */
1215 /* Detect circularities and truncate them.
1216 No need to offer any alternative--this is better than an error. */
1217 if (CONSP (obj) || VECTORP (obj) || COMPILEDP (obj))
1218 {
1219 int i;
1220 for (i = 0; i < print_depth; i++)
1221 if (EQ (obj, being_printed[i]))
1222 {
1223 sprintf (buf, "#%d", i);
1224 strout (buf, -1, -1, printcharfun, 0);
1225 return;
1226 }
1227 }
1228 #endif
1229
1230 being_printed[print_depth] = obj;
1231 print_depth++;
1232
1233 if (print_depth > PRINT_CIRCLE)
1234 error ("Apparently circular structure being printed");
1235 #ifdef MAX_PRINT_CHARS
1236 if (max_print && print_chars > max_print)
1237 {
1238 PRINTCHAR ('\n');
1239 print_chars = 0;
1240 }
1241 #endif /* MAX_PRINT_CHARS */
1242
1243 switch (XGCTYPE (obj))
1244 {
1245 case Lisp_Int:
1246 if (sizeof (int) == sizeof (EMACS_INT))
1247 sprintf (buf, "%d", XINT (obj));
1248 else if (sizeof (long) == sizeof (EMACS_INT))
1249 sprintf (buf, "%ld", XINT (obj));
1250 else
1251 abort ();
1252 strout (buf, -1, -1, printcharfun, 0);
1253 break;
1254
1255 #ifdef LISP_FLOAT_TYPE
1256 case Lisp_Float:
1257 {
1258 char pigbuf[350]; /* see comments in float_to_string */
1259
1260 float_to_string (pigbuf, XFLOAT(obj)->data);
1261 strout (pigbuf, -1, -1, printcharfun, 0);
1262 }
1263 break;
1264 #endif
1265
1266 case Lisp_String:
1267 if (!escapeflag)
1268 print_string (obj, printcharfun);
1269 else
1270 {
1271 register int i, i_byte;
1272 register unsigned char c;
1273 struct gcpro gcpro1;
1274 unsigned char *str;
1275 int size_byte;
1276 /* 1 means we must ensure that the next character we output
1277 cannot be taken as part of a hex character escape. */
1278 int need_nonhex = 0;
1279
1280 GCPRO1 (obj);
1281
1282 #ifdef USE_TEXT_PROPERTIES
1283 if (!NULL_INTERVAL_P (XSTRING (obj)->intervals))
1284 {
1285 PRINTCHAR ('#');
1286 PRINTCHAR ('(');
1287 }
1288 #endif
1289
1290 PRINTCHAR ('\"');
1291 str = XSTRING (obj)->data;
1292 size_byte = STRING_BYTES (XSTRING (obj));
1293
1294 for (i = 0, i_byte = 0; i_byte < size_byte;)
1295 {
1296 /* Here, we must convert each multi-byte form to the
1297 corresponding character code before handing it to PRINTCHAR. */
1298 int len;
1299 int c;
1300
1301 if (STRING_MULTIBYTE (obj))
1302 {
1303 c = STRING_CHAR_AND_CHAR_LENGTH (str + i_byte,
1304 size_byte - i_byte, len);
1305 if (CHAR_VALID_P (c, 0))
1306 i_byte += len;
1307 else
1308 c = str[i_byte++];
1309 }
1310 else
1311 c = str[i_byte++];
1312
1313 QUIT;
1314
1315 if (c == '\n' && print_escape_newlines)
1316 {
1317 PRINTCHAR ('\\');
1318 PRINTCHAR ('n');
1319 }
1320 else if (c == '\f' && print_escape_newlines)
1321 {
1322 PRINTCHAR ('\\');
1323 PRINTCHAR ('f');
1324 }
1325 else if (! SINGLE_BYTE_CHAR_P (c) && print_escape_multibyte)
1326 {
1327 /* When multibyte is disabled,
1328 print multibyte string chars using hex escapes. */
1329 unsigned char outbuf[50];
1330 sprintf (outbuf, "\\x%x", c);
1331 strout (outbuf, -1, -1, printcharfun, 0);
1332 need_nonhex = 1;
1333 }
1334 else if (SINGLE_BYTE_CHAR_P (c) && ! ASCII_BYTE_P (c)
1335 && print_escape_nonascii)
1336 {
1337 /* When printing in a multibyte buffer
1338 or when explicitly requested,
1339 print single-byte non-ASCII string chars
1340 using octal escapes. */
1341 unsigned char outbuf[5];
1342 sprintf (outbuf, "\\%03o", c);
1343 strout (outbuf, -1, -1, printcharfun, 0);
1344 }
1345 else
1346 {
1347 /* If we just had a hex escape, and this character
1348 could be taken as part of it,
1349 output `\ ' to prevent that. */
1350 if (need_nonhex)
1351 {
1352 need_nonhex = 0;
1353 if ((c >= 'a' && c <= 'f')
1354 || (c >= 'A' && c <= 'F')
1355 || (c >= '0' && c <= '9'))
1356 strout ("\\ ", -1, -1, printcharfun, 0);
1357 }
1358
1359 if (c == '\"' || c == '\\')
1360 PRINTCHAR ('\\');
1361 PRINTCHAR (c);
1362 }
1363 }
1364 PRINTCHAR ('\"');
1365
1366 #ifdef USE_TEXT_PROPERTIES
1367 if (!NULL_INTERVAL_P (XSTRING (obj)->intervals))
1368 {
1369 traverse_intervals (XSTRING (obj)->intervals,
1370 0, 0, print_interval, printcharfun);
1371 PRINTCHAR (')');
1372 }
1373 #endif
1374
1375 UNGCPRO;
1376 }
1377 break;
1378
1379 case Lisp_Symbol:
1380 {
1381 register int confusing;
1382 register unsigned char *p = XSYMBOL (obj)->name->data;
1383 register unsigned char *end = p + STRING_BYTES (XSYMBOL (obj)->name);
1384 register int c;
1385 int i, i_byte, size_byte;
1386 Lisp_Object name;
1387
1388 XSETSTRING (name, XSYMBOL (obj)->name);
1389
1390 if (p != end && (*p == '-' || *p == '+')) p++;
1391 if (p == end)
1392 confusing = 0;
1393 /* If symbol name begins with a digit, and ends with a digit,
1394 and contains nothing but digits and `e', it could be treated
1395 as a number. So set CONFUSING.
1396
1397 Symbols that contain periods could also be taken as numbers,
1398 but periods are always escaped, so we don't have to worry
1399 about them here. */
1400 else if (*p >= '0' && *p <= '9'
1401 && end[-1] >= '0' && end[-1] <= '9')
1402 {
1403 while (p != end && ((*p >= '0' && *p <= '9')
1404 /* Needed for \2e10. */
1405 || *p == 'e'))
1406 p++;
1407 confusing = (end == p);
1408 }
1409 else
1410 confusing = 0;
1411
1412 /* If we print an uninterned symbol as part of a complex object and
1413 the flag print-gensym is non-nil, prefix it with #n= to read the
1414 object back with the #n# reader syntax later if needed. */
1415 if (! NILP (Vprint_gensym) && NILP (XSYMBOL (obj)->obarray))
1416 {
1417 if (print_depth > 1)
1418 {
1419 Lisp_Object tem;
1420 tem = Fassq (obj, Vprint_gensym_alist);
1421 if (CONSP (tem))
1422 {
1423 PRINTCHAR ('#');
1424 print (XCDR (tem), printcharfun, escapeflag);
1425 PRINTCHAR ('#');
1426 break;
1427 }
1428 else
1429 {
1430 if (CONSP (Vprint_gensym_alist))
1431 XSETFASTINT (tem, XFASTINT (XCDR (XCAR (Vprint_gensym_alist))) + 1);
1432 else
1433 XSETFASTINT (tem, 1);
1434 Vprint_gensym_alist = Fcons (Fcons (obj, tem), Vprint_gensym_alist);
1435
1436 PRINTCHAR ('#');
1437 print (tem, printcharfun, escapeflag);
1438 PRINTCHAR ('=');
1439 }
1440 }
1441 PRINTCHAR ('#');
1442 PRINTCHAR (':');
1443 }
1444
1445 size_byte = STRING_BYTES (XSTRING (name));
1446
1447 for (i = 0, i_byte = 0; i_byte < size_byte;)
1448 {
1449 /* Here, we must convert each multi-byte form to the
1450 corresponding character code before handing it to PRINTCHAR. */
1451
1452 if (STRING_MULTIBYTE (name))
1453 FETCH_STRING_CHAR_ADVANCE (c, name, i, i_byte);
1454 else
1455 c = XSTRING (name)->data[i_byte++];
1456
1457 QUIT;
1458
1459 if (escapeflag)
1460 {
1461 if (c == '\"' || c == '\\' || c == '\''
1462 || c == ';' || c == '#' || c == '(' || c == ')'
1463 || c == ',' || c =='.' || c == '`'
1464 || c == '[' || c == ']' || c == '?' || c <= 040
1465 || confusing)
1466 PRINTCHAR ('\\'), confusing = 0;
1467 }
1468 PRINTCHAR (c);
1469 }
1470 }
1471 break;
1472
1473 case Lisp_Cons:
1474 /* If deeper than spec'd depth, print placeholder. */
1475 if (INTEGERP (Vprint_level)
1476 && print_depth > XINT (Vprint_level))
1477 strout ("...", -1, -1, printcharfun, 0);
1478 else if (print_quoted && CONSP (XCDR (obj)) && NILP (XCDR (XCDR (obj)))
1479 && (EQ (XCAR (obj), Qquote)))
1480 {
1481 PRINTCHAR ('\'');
1482 print (XCAR (XCDR (obj)), printcharfun, escapeflag);
1483 }
1484 else if (print_quoted && CONSP (XCDR (obj)) && NILP (XCDR (XCDR (obj)))
1485 && (EQ (XCAR (obj), Qfunction)))
1486 {
1487 PRINTCHAR ('#');
1488 PRINTCHAR ('\'');
1489 print (XCAR (XCDR (obj)), printcharfun, escapeflag);
1490 }
1491 else if (print_quoted && CONSP (XCDR (obj)) && NILP (XCDR (XCDR (obj)))
1492 && ((EQ (XCAR (obj), Qbackquote)
1493 || EQ (XCAR (obj), Qcomma)
1494 || EQ (XCAR (obj), Qcomma_at)
1495 || EQ (XCAR (obj), Qcomma_dot))))
1496 {
1497 print (XCAR (obj), printcharfun, 0);
1498 print (XCAR (XCDR (obj)), printcharfun, escapeflag);
1499 }
1500 else
1501 {
1502 PRINTCHAR ('(');
1503 {
1504 register int i = 0;
1505 register int print_length = 0;
1506 Lisp_Object halftail = obj;
1507
1508 if (INTEGERP (Vprint_length))
1509 print_length = XINT (Vprint_length);
1510 while (CONSP (obj))
1511 {
1512 /* Detect circular list. */
1513 if (i != 0 && EQ (obj, halftail))
1514 {
1515 sprintf (buf, " . #%d", i / 2);
1516 strout (buf, -1, -1, printcharfun, 0);
1517 obj = Qnil;
1518 break;
1519 }
1520 if (i++)
1521 PRINTCHAR (' ');
1522 if (print_length && i > print_length)
1523 {
1524 strout ("...", 3, 3, printcharfun, 0);
1525 break;
1526 }
1527 print (XCAR (obj), printcharfun, escapeflag);
1528 obj = XCDR (obj);
1529 if (!(i & 1))
1530 halftail = XCDR (halftail);
1531 }
1532 }
1533 if (!NILP (obj))
1534 {
1535 strout (" . ", 3, 3, printcharfun, 0);
1536 print (obj, printcharfun, escapeflag);
1537 }
1538 PRINTCHAR (')');
1539 }
1540 break;
1541
1542 case Lisp_Vectorlike:
1543 if (PROCESSP (obj))
1544 {
1545 if (escapeflag)
1546 {
1547 strout ("#<process ", -1, -1, printcharfun, 0);
1548 print_string (XPROCESS (obj)->name, printcharfun);
1549 PRINTCHAR ('>');
1550 }
1551 else
1552 print_string (XPROCESS (obj)->name, printcharfun);
1553 }
1554 else if (BOOL_VECTOR_P (obj))
1555 {
1556 register int i;
1557 register unsigned char c;
1558 struct gcpro gcpro1;
1559 int size_in_chars
1560 = (XBOOL_VECTOR (obj)->size + BITS_PER_CHAR - 1) / BITS_PER_CHAR;
1561
1562 GCPRO1 (obj);
1563
1564 PRINTCHAR ('#');
1565 PRINTCHAR ('&');
1566 sprintf (buf, "%d", XBOOL_VECTOR (obj)->size);
1567 strout (buf, -1, -1, printcharfun, 0);
1568 PRINTCHAR ('\"');
1569
1570 /* Don't print more characters than the specified maximum. */
1571 if (INTEGERP (Vprint_length)
1572 && XINT (Vprint_length) < size_in_chars)
1573 size_in_chars = XINT (Vprint_length);
1574
1575 for (i = 0; i < size_in_chars; i++)
1576 {
1577 QUIT;
1578 c = XBOOL_VECTOR (obj)->data[i];
1579 if (c == '\n' && print_escape_newlines)
1580 {
1581 PRINTCHAR ('\\');
1582 PRINTCHAR ('n');
1583 }
1584 else if (c == '\f' && print_escape_newlines)
1585 {
1586 PRINTCHAR ('\\');
1587 PRINTCHAR ('f');
1588 }
1589 else
1590 {
1591 if (c == '\"' || c == '\\')
1592 PRINTCHAR ('\\');
1593 PRINTCHAR (c);
1594 }
1595 }
1596 PRINTCHAR ('\"');
1597
1598 UNGCPRO;
1599 }
1600 else if (SUBRP (obj))
1601 {
1602 strout ("#<subr ", -1, -1, printcharfun, 0);
1603 strout (XSUBR (obj)->symbol_name, -1, -1, printcharfun, 0);
1604 PRINTCHAR ('>');
1605 }
1606 #ifndef standalone
1607 else if (WINDOWP (obj))
1608 {
1609 strout ("#<window ", -1, -1, printcharfun, 0);
1610 sprintf (buf, "%d", XFASTINT (XWINDOW (obj)->sequence_number));
1611 strout (buf, -1, -1, printcharfun, 0);
1612 if (!NILP (XWINDOW (obj)->buffer))
1613 {
1614 strout (" on ", -1, -1, printcharfun, 0);
1615 print_string (XBUFFER (XWINDOW (obj)->buffer)->name, printcharfun);
1616 }
1617 PRINTCHAR ('>');
1618 }
1619 else if (BUFFERP (obj))
1620 {
1621 if (NILP (XBUFFER (obj)->name))
1622 strout ("#<killed buffer>", -1, -1, printcharfun, 0);
1623 else if (escapeflag)
1624 {
1625 strout ("#<buffer ", -1, -1, printcharfun, 0);
1626 print_string (XBUFFER (obj)->name, printcharfun);
1627 PRINTCHAR ('>');
1628 }
1629 else
1630 print_string (XBUFFER (obj)->name, printcharfun);
1631 }
1632 else if (WINDOW_CONFIGURATIONP (obj))
1633 {
1634 strout ("#<window-configuration>", -1, -1, printcharfun, 0);
1635 }
1636 else if (FRAMEP (obj))
1637 {
1638 strout ((FRAME_LIVE_P (XFRAME (obj))
1639 ? "#<frame " : "#<dead frame "),
1640 -1, -1, printcharfun, 0);
1641 print_string (XFRAME (obj)->name, printcharfun);
1642 sprintf (buf, " 0x%lx\\ ", (unsigned long) (XFRAME (obj)));
1643 strout (buf, -1, -1, printcharfun, 0);
1644 PRINTCHAR ('>');
1645 }
1646 #endif /* not standalone */
1647 else
1648 {
1649 int size = XVECTOR (obj)->size;
1650 if (COMPILEDP (obj))
1651 {
1652 PRINTCHAR ('#');
1653 size &= PSEUDOVECTOR_SIZE_MASK;
1654 }
1655 if (CHAR_TABLE_P (obj))
1656 {
1657 /* We print a char-table as if it were a vector,
1658 lumping the parent and default slots in with the
1659 character slots. But we add #^ as a prefix. */
1660 PRINTCHAR ('#');
1661 PRINTCHAR ('^');
1662 if (SUB_CHAR_TABLE_P (obj))
1663 PRINTCHAR ('^');
1664 size &= PSEUDOVECTOR_SIZE_MASK;
1665 }
1666 if (size & PSEUDOVECTOR_FLAG)
1667 goto badtype;
1668
1669 PRINTCHAR ('[');
1670 {
1671 register int i;
1672 register Lisp_Object tem;
1673
1674 /* Don't print more elements than the specified maximum. */
1675 if (INTEGERP (Vprint_length)
1676 && XINT (Vprint_length) < size)
1677 size = XINT (Vprint_length);
1678
1679 for (i = 0; i < size; i++)
1680 {
1681 if (i) PRINTCHAR (' ');
1682 tem = XVECTOR (obj)->contents[i];
1683 print (tem, printcharfun, escapeflag);
1684 }
1685 }
1686 PRINTCHAR (']');
1687 }
1688 break;
1689
1690 #ifndef standalone
1691 case Lisp_Misc:
1692 switch (XMISCTYPE (obj))
1693 {
1694 case Lisp_Misc_Marker:
1695 strout ("#<marker ", -1, -1, printcharfun, 0);
1696 /* Do you think this is necessary? */
1697 if (XMARKER (obj)->insertion_type != 0)
1698 strout ("(before-insertion) ", -1, -1, printcharfun, 0);
1699 if (!(XMARKER (obj)->buffer))
1700 strout ("in no buffer", -1, -1, printcharfun, 0);
1701 else
1702 {
1703 sprintf (buf, "at %d", marker_position (obj));
1704 strout (buf, -1, -1, printcharfun, 0);
1705 strout (" in ", -1, -1, printcharfun, 0);
1706 print_string (XMARKER (obj)->buffer->name, printcharfun);
1707 }
1708 PRINTCHAR ('>');
1709 break;
1710
1711 case Lisp_Misc_Overlay:
1712 strout ("#<overlay ", -1, -1, printcharfun, 0);
1713 if (!(XMARKER (OVERLAY_START (obj))->buffer))
1714 strout ("in no buffer", -1, -1, printcharfun, 0);
1715 else
1716 {
1717 sprintf (buf, "from %d to %d in ",
1718 marker_position (OVERLAY_START (obj)),
1719 marker_position (OVERLAY_END (obj)));
1720 strout (buf, -1, -1, printcharfun, 0);
1721 print_string (XMARKER (OVERLAY_START (obj))->buffer->name,
1722 printcharfun);
1723 }
1724 PRINTCHAR ('>');
1725 break;
1726
1727 /* Remaining cases shouldn't happen in normal usage, but let's print
1728 them anyway for the benefit of the debugger. */
1729 case Lisp_Misc_Free:
1730 strout ("#<misc free cell>", -1, -1, printcharfun, 0);
1731 break;
1732
1733 case Lisp_Misc_Intfwd:
1734 sprintf (buf, "#<intfwd to %d>", *XINTFWD (obj)->intvar);
1735 strout (buf, -1, -1, printcharfun, 0);
1736 break;
1737
1738 case Lisp_Misc_Boolfwd:
1739 sprintf (buf, "#<boolfwd to %s>",
1740 (*XBOOLFWD (obj)->boolvar ? "t" : "nil"));
1741 strout (buf, -1, -1, printcharfun, 0);
1742 break;
1743
1744 case Lisp_Misc_Objfwd:
1745 strout ("#<objfwd to ", -1, -1, printcharfun, 0);
1746 print (*XOBJFWD (obj)->objvar, printcharfun, escapeflag);
1747 PRINTCHAR ('>');
1748 break;
1749
1750 case Lisp_Misc_Buffer_Objfwd:
1751 strout ("#<buffer_objfwd to ", -1, -1, printcharfun, 0);
1752 print (*(Lisp_Object *)((char *)current_buffer
1753 + XBUFFER_OBJFWD (obj)->offset),
1754 printcharfun, escapeflag);
1755 PRINTCHAR ('>');
1756 break;
1757
1758 case Lisp_Misc_Kboard_Objfwd:
1759 strout ("#<kboard_objfwd to ", -1, -1, printcharfun, 0);
1760 print (*(Lisp_Object *)((char *) current_kboard
1761 + XKBOARD_OBJFWD (obj)->offset),
1762 printcharfun, escapeflag);
1763 PRINTCHAR ('>');
1764 break;
1765
1766 case Lisp_Misc_Buffer_Local_Value:
1767 strout ("#<buffer_local_value ", -1, -1, printcharfun, 0);
1768 goto do_buffer_local;
1769 case Lisp_Misc_Some_Buffer_Local_Value:
1770 strout ("#<some_buffer_local_value ", -1, -1, printcharfun, 0);
1771 do_buffer_local:
1772 strout ("[realvalue] ", -1, -1, printcharfun, 0);
1773 print (XBUFFER_LOCAL_VALUE (obj)->realvalue, printcharfun, escapeflag);
1774 if (XBUFFER_LOCAL_VALUE (obj)->found_for_buffer)
1775 strout ("[local in buffer] ", -1, -1, printcharfun, 0);
1776 else
1777 strout ("[buffer] ", -1, -1, printcharfun, 0);
1778 print (XBUFFER_LOCAL_VALUE (obj)->buffer,
1779 printcharfun, escapeflag);
1780 if (XBUFFER_LOCAL_VALUE (obj)->check_frame)
1781 {
1782 if (XBUFFER_LOCAL_VALUE (obj)->found_for_frame)
1783 strout ("[local in frame] ", -1, -1, printcharfun, 0);
1784 else
1785 strout ("[frame] ", -1, -1, printcharfun, 0);
1786 print (XBUFFER_LOCAL_VALUE (obj)->frame,
1787 printcharfun, escapeflag);
1788 }
1789 strout ("[alist-elt] ", -1, -1, printcharfun, 0);
1790 print (XCONS (XBUFFER_LOCAL_VALUE (obj)->cdr)->car,
1791 printcharfun, escapeflag);
1792 strout ("[default-value] ", -1, -1, printcharfun, 0);
1793 print (XCONS (XBUFFER_LOCAL_VALUE (obj)->cdr)->cdr,
1794 printcharfun, escapeflag);
1795 PRINTCHAR ('>');
1796 break;
1797
1798 default:
1799 goto badtype;
1800 }
1801 break;
1802 #endif /* standalone */
1803
1804 default:
1805 badtype:
1806 {
1807 /* We're in trouble if this happens!
1808 Probably should just abort () */
1809 strout ("#<EMACS BUG: INVALID DATATYPE ", -1, -1, printcharfun, 0);
1810 if (MISCP (obj))
1811 sprintf (buf, "(MISC 0x%04x)", (int) XMISCTYPE (obj));
1812 else if (VECTORLIKEP (obj))
1813 sprintf (buf, "(PVEC 0x%08x)", (int) XVECTOR (obj)->size);
1814 else
1815 sprintf (buf, "(0x%02x)", (int) XTYPE (obj));
1816 strout (buf, -1, -1, printcharfun, 0);
1817 strout (" Save your buffers immediately and please report this bug>",
1818 -1, -1, printcharfun, 0);
1819 }
1820 }
1821
1822 print_depth--;
1823 }
1824 \f
1825 #ifdef USE_TEXT_PROPERTIES
1826
1827 /* Print a description of INTERVAL using PRINTCHARFUN.
1828 This is part of printing a string that has text properties. */
1829
1830 void
1831 print_interval (interval, printcharfun)
1832 INTERVAL interval;
1833 Lisp_Object printcharfun;
1834 {
1835 PRINTCHAR (' ');
1836 print (make_number (interval->position), printcharfun, 1);
1837 PRINTCHAR (' ');
1838 print (make_number (interval->position + LENGTH (interval)),
1839 printcharfun, 1);
1840 PRINTCHAR (' ');
1841 print (interval->plist, printcharfun, 1);
1842 }
1843
1844 #endif /* USE_TEXT_PROPERTIES */
1845 \f
1846 void
1847 syms_of_print ()
1848 {
1849 Qtemp_buffer_setup_hook = intern ("temp-buffer-setup-hook");
1850 staticpro (&Qtemp_buffer_setup_hook);
1851
1852 DEFVAR_LISP ("standard-output", &Vstandard_output,
1853 "Output stream `print' uses by default for outputting a character.\n\
1854 This may be any function of one argument.\n\
1855 It may also be a buffer (output is inserted before point)\n\
1856 or a marker (output is inserted and the marker is advanced)\n\
1857 or the symbol t (output appears in the echo area).");
1858 Vstandard_output = Qt;
1859 Qstandard_output = intern ("standard-output");
1860 staticpro (&Qstandard_output);
1861
1862 #ifdef LISP_FLOAT_TYPE
1863 DEFVAR_LISP ("float-output-format", &Vfloat_output_format,
1864 "The format descriptor string used to print floats.\n\
1865 This is a %-spec like those accepted by `printf' in C,\n\
1866 but with some restrictions. It must start with the two characters `%.'.\n\
1867 After that comes an integer precision specification,\n\
1868 and then a letter which controls the format.\n\
1869 The letters allowed are `e', `f' and `g'.\n\
1870 Use `e' for exponential notation \"DIG.DIGITSeEXPT\"\n\
1871 Use `f' for decimal point notation \"DIGITS.DIGITS\".\n\
1872 Use `g' to choose the shorter of those two formats for the number at hand.\n\
1873 The precision in any of these cases is the number of digits following\n\
1874 the decimal point. With `f', a precision of 0 means to omit the\n\
1875 decimal point. 0 is not allowed with `e' or `g'.\n\n\
1876 A value of nil means to use the shortest notation\n\
1877 that represents the number without losing information.");
1878 Vfloat_output_format = Qnil;
1879 Qfloat_output_format = intern ("float-output-format");
1880 staticpro (&Qfloat_output_format);
1881 #endif /* LISP_FLOAT_TYPE */
1882
1883 DEFVAR_LISP ("print-length", &Vprint_length,
1884 "Maximum length of list to print before abbreviating.\n\
1885 A value of nil means no limit.");
1886 Vprint_length = Qnil;
1887
1888 DEFVAR_LISP ("print-level", &Vprint_level,
1889 "Maximum depth of list nesting to print before abbreviating.\n\
1890 A value of nil means no limit.");
1891 Vprint_level = Qnil;
1892
1893 DEFVAR_BOOL ("print-escape-newlines", &print_escape_newlines,
1894 "Non-nil means print newlines in strings as backslash-n.\n\
1895 Also print formfeeds as backslash-f.");
1896 print_escape_newlines = 0;
1897
1898 DEFVAR_BOOL ("print-escape-nonascii", &print_escape_nonascii,
1899 "Non-nil means print unibyte non-ASCII chars in strings as \\OOO.\n\
1900 \(OOO is the octal representation of the character code.)\n\
1901 Only single-byte characters are affected, and only in `prin1'.");
1902 print_escape_nonascii = 0;
1903
1904 DEFVAR_BOOL ("print-escape-multibyte", &print_escape_multibyte,
1905 "Non-nil means print multibyte characters in strings as \\xXXXX.\n\
1906 \(XXX is the hex representation of the character code.)\n\
1907 This affects only `prin1'.");
1908 print_escape_multibyte = 0;
1909
1910 DEFVAR_BOOL ("print-quoted", &print_quoted,
1911 "Non-nil means print quoted forms with reader syntax.\n\
1912 I.e., (quote foo) prints as 'foo, (function foo) as #'foo, and, backquoted\n\
1913 forms print in the new syntax.");
1914 print_quoted = 0;
1915
1916 DEFVAR_LISP ("print-gensym", &Vprint_gensym,
1917 "Non-nil means print uninterned symbols so they will read as uninterned.\n\
1918 I.e., the value of (make-symbol \"foobar\") prints as #:foobar.\n\
1919 When the uninterned symbol appears within a larger data structure,\n\
1920 in addition use the #...# and #...= constructs as needed,\n\
1921 so that multiple references to the same symbol are shared once again\n\
1922 when the text is read back.\n\
1923 \n\
1924 If the value of `print-gensym' is a cons cell, then in addition refrain from\n\
1925 clearing `print-gensym-alist' on entry to and exit from printing functions,\n\
1926 so that the use of #...# and #...= can carry over for several separately\n\
1927 printed objects.");
1928 Vprint_gensym = Qnil;
1929
1930 DEFVAR_LISP ("print-gensym-alist", &Vprint_gensym_alist,
1931 "Association list of elements (GENSYM . N) to guide use of #N# and #N=.\n\
1932 In each element, GENSYM is an uninterned symbol that has been associated\n\
1933 with #N= for the specified value of N.");
1934 Vprint_gensym_alist = Qnil;
1935
1936 /* prin1_to_string_buffer initialized in init_buffer_once in buffer.c */
1937 staticpro (&Vprin1_to_string_buffer);
1938
1939 defsubr (&Sprin1);
1940 defsubr (&Sprin1_to_string);
1941 defsubr (&Serror_message_string);
1942 defsubr (&Sprinc);
1943 defsubr (&Sprint);
1944 defsubr (&Sterpri);
1945 defsubr (&Swrite_char);
1946 defsubr (&Sexternal_debugging_output);
1947
1948 Qexternal_debugging_output = intern ("external-debugging-output");
1949 staticpro (&Qexternal_debugging_output);
1950
1951 Qprint_escape_newlines = intern ("print-escape-newlines");
1952 staticpro (&Qprint_escape_newlines);
1953
1954 Qprint_escape_multibyte = intern ("print-escape-multibyte");
1955 staticpro (&Qprint_escape_multibyte);
1956
1957 Qprint_escape_nonascii = intern ("print-escape-nonascii");
1958 staticpro (&Qprint_escape_nonascii);
1959
1960 #ifndef standalone
1961 defsubr (&Swith_output_to_temp_buffer);
1962 #endif /* not standalone */
1963 }