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