]> code.delx.au - gnu-emacs/blob - src/print.c
(print): Print internal types too, for debugging.
[gnu-emacs] / src / print.c
1 /* Lisp object printing and output streams.
2 Copyright (C) 1985, 1986, 1988, 1993, 1994 Free Software Foundation, Inc.
3
4 This file is part of GNU Emacs.
5
6 GNU Emacs is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 GNU Emacs is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20
21 #include <config.h>
22 #include <stdio.h>
23 #undef NULL
24 #include "lisp.h"
25
26 #ifndef standalone
27 #include "buffer.h"
28 #include "frame.h"
29 #include "window.h"
30 #include "process.h"
31 #include "dispextern.h"
32 #include "termchar.h"
33 #endif /* not standalone */
34
35 #ifdef USE_TEXT_PROPERTIES
36 #include "intervals.h"
37 #endif
38
39 Lisp_Object Vstandard_output, Qstandard_output;
40
41 #ifdef LISP_FLOAT_TYPE
42 Lisp_Object Vfloat_output_format, Qfloat_output_format;
43 #endif /* LISP_FLOAT_TYPE */
44
45 /* Avoid actual stack overflow in print. */
46 int print_depth;
47
48 /* Detect most circularities to print finite output. */
49 #define PRINT_CIRCLE 200
50 Lisp_Object being_printed[PRINT_CIRCLE];
51
52 /* Maximum length of list to print in full; noninteger means
53 effectively infinity */
54
55 Lisp_Object Vprint_length;
56
57 /* Maximum depth of list to print in full; noninteger means
58 effectively infinity. */
59
60 Lisp_Object Vprint_level;
61
62 /* Nonzero means print newlines in strings as \n. */
63
64 int print_escape_newlines;
65
66 Lisp_Object Qprint_escape_newlines;
67
68 /* Nonzero means print newline to stdout before next minibuffer message.
69 Defined in xdisp.c */
70
71 extern int noninteractive_need_newline;
72
73 /* Nonzero means print newline to message log before next message.
74 Defined in xdisp.c */
75
76 extern int message_log_need_newline;
77
78 #ifdef MAX_PRINT_CHARS
79 static int print_chars;
80 static int max_print;
81 #endif /* MAX_PRINT_CHARS */
82
83 void print_interval ();
84 \f
85 #if 0
86 /* Convert between chars and GLYPHs */
87
88 int
89 glyphlen (glyphs)
90 register GLYPH *glyphs;
91 {
92 register int i = 0;
93
94 while (glyphs[i])
95 i++;
96 return i;
97 }
98
99 void
100 str_to_glyph_cpy (str, glyphs)
101 char *str;
102 GLYPH *glyphs;
103 {
104 register GLYPH *gp = glyphs;
105 register char *cp = str;
106
107 while (*cp)
108 *gp++ = *cp++;
109 }
110
111 void
112 str_to_glyph_ncpy (str, glyphs, n)
113 char *str;
114 GLYPH *glyphs;
115 register int n;
116 {
117 register GLYPH *gp = glyphs;
118 register char *cp = str;
119
120 while (n-- > 0)
121 *gp++ = *cp++;
122 }
123
124 void
125 glyph_to_str_cpy (glyphs, str)
126 GLYPH *glyphs;
127 char *str;
128 {
129 register GLYPH *gp = glyphs;
130 register char *cp = str;
131
132 while (*gp)
133 *str++ = *gp++ & 0377;
134 }
135 #endif
136 \f
137 /* Low level output routines for characters and strings */
138
139 /* Lisp functions to do output using a stream
140 must have the stream in a variable called printcharfun
141 and must start with PRINTPREPARE and end with PRINTFINISH.
142 Use PRINTCHAR to output one character,
143 or call strout to output a block of characters.
144 Also, each one must have the declarations
145 struct buffer *old = current_buffer;
146 int old_point = -1, start_point;
147 Lisp_Object original;
148 */
149
150 #define PRINTPREPARE \
151 original = printcharfun; \
152 if (NILP (printcharfun)) printcharfun = Qt; \
153 if (BUFFERP (printcharfun)) \
154 { if (XBUFFER (printcharfun) != current_buffer) \
155 Fset_buffer (printcharfun); \
156 printcharfun = Qnil;} \
157 if (MARKERP (printcharfun)) \
158 { if (!(XMARKER (original)->buffer)) \
159 error ("Marker does not point anywhere"); \
160 if (XMARKER (original)->buffer != current_buffer) \
161 set_buffer_internal (XMARKER (original)->buffer); \
162 old_point = point; \
163 SET_PT (marker_position (printcharfun)); \
164 start_point = point; \
165 printcharfun = Qnil;}
166
167 #define PRINTFINISH \
168 if (MARKERP (original)) \
169 Fset_marker (original, make_number (point), Qnil); \
170 if (old_point >= 0) \
171 SET_PT (old_point + (old_point >= start_point \
172 ? point - start_point : 0)); \
173 if (old != current_buffer) \
174 set_buffer_internal (old)
175
176 #define PRINTCHAR(ch) printchar (ch, printcharfun)
177
178 /* Index of first unused element of FRAME_MESSAGE_BUF(mini_frame). */
179 static int printbufidx;
180
181 static void
182 printchar (ch, fun)
183 unsigned char ch;
184 Lisp_Object fun;
185 {
186 Lisp_Object ch1;
187
188 #ifdef MAX_PRINT_CHARS
189 if (max_print)
190 print_chars++;
191 #endif /* MAX_PRINT_CHARS */
192 #ifndef standalone
193 if (EQ (fun, Qnil))
194 {
195 QUIT;
196 insert (&ch, 1);
197 return;
198 }
199
200 if (EQ (fun, Qt))
201 {
202 FRAME_PTR mini_frame
203 = XFRAME (WINDOW_FRAME (XWINDOW (minibuf_window)));
204
205 if (noninteractive)
206 {
207 putchar (ch);
208 noninteractive_need_newline = 1;
209 return;
210 }
211
212 if (echo_area_glyphs != FRAME_MESSAGE_BUF (mini_frame)
213 || !message_buf_print)
214 {
215 if (message_log_need_newline)
216 message_dolog ("", 0, 1);
217 message_log_need_newline = 0;
218 echo_area_glyphs = FRAME_MESSAGE_BUF (mini_frame);
219 printbufidx = 0;
220 echo_area_glyphs_length = 0;
221 message_buf_print = 1;
222 }
223
224 message_dolog (&ch, 1, 0);
225 message_log_need_newline = 1;
226 if (printbufidx < FRAME_WIDTH (mini_frame) - 1)
227 FRAME_MESSAGE_BUF (mini_frame)[printbufidx++] = ch;
228 FRAME_MESSAGE_BUF (mini_frame)[printbufidx] = 0;
229 echo_area_glyphs_length = printbufidx;
230
231 return;
232 }
233 #endif /* not standalone */
234
235 XSETFASTINT (ch1, ch);
236 call1 (fun, ch1);
237 }
238
239 static void
240 strout (ptr, size, printcharfun)
241 char *ptr;
242 int size;
243 Lisp_Object printcharfun;
244 {
245 int i = 0;
246
247 if (EQ (printcharfun, Qnil))
248 {
249 insert (ptr, size >= 0 ? size : strlen (ptr));
250 #ifdef MAX_PRINT_CHARS
251 if (max_print)
252 print_chars += size >= 0 ? size : strlen(ptr);
253 #endif /* MAX_PRINT_CHARS */
254 return;
255 }
256 if (EQ (printcharfun, Qt))
257 {
258 FRAME_PTR mini_frame
259 = XFRAME (WINDOW_FRAME (XWINDOW (minibuf_window)));
260
261 i = size >= 0 ? size : strlen (ptr);
262 #ifdef MAX_PRINT_CHARS
263 if (max_print)
264 print_chars += i;
265 #endif /* MAX_PRINT_CHARS */
266
267 if (noninteractive)
268 {
269 fwrite (ptr, 1, i, stdout);
270 noninteractive_need_newline = 1;
271 return;
272 }
273
274 if (echo_area_glyphs != FRAME_MESSAGE_BUF (mini_frame)
275 || !message_buf_print)
276 {
277 if (message_log_need_newline)
278 message_dolog ("", 0, 1);
279 message_log_need_newline = 0;
280 echo_area_glyphs = FRAME_MESSAGE_BUF (mini_frame);
281 printbufidx = 0;
282 echo_area_glyphs_length = 0;
283 message_buf_print = 1;
284 }
285
286 message_dolog (ptr, i, 0);
287 message_log_need_newline = 1;
288 if (i > FRAME_WIDTH (mini_frame) - printbufidx - 1)
289 i = FRAME_WIDTH (mini_frame) - printbufidx - 1;
290 bcopy (ptr, &FRAME_MESSAGE_BUF (mini_frame) [printbufidx], i);
291 printbufidx += i;
292 echo_area_glyphs_length = printbufidx;
293 FRAME_MESSAGE_BUF (mini_frame) [printbufidx] = 0;
294
295 return;
296 }
297
298 if (size >= 0)
299 while (i < size)
300 PRINTCHAR (ptr[i++]);
301 else
302 while (ptr[i])
303 PRINTCHAR (ptr[i++]);
304 }
305
306 /* Print the contents of a string STRING using PRINTCHARFUN.
307 It isn't safe to use strout, because printing one char can relocate. */
308
309 print_string (string, printcharfun)
310 Lisp_Object string;
311 Lisp_Object printcharfun;
312 {
313 if (EQ (printcharfun, Qnil) || EQ (printcharfun, Qt))
314 /* In predictable cases, strout is safe: output to buffer or frame. */
315 strout (XSTRING (string)->data, XSTRING (string)->size, printcharfun);
316 else
317 {
318 /* Otherwise, fetch the string address for each character. */
319 int i;
320 int size = XSTRING (string)->size;
321 struct gcpro gcpro1;
322 GCPRO1 (string);
323 for (i = 0; i < size; i++)
324 PRINTCHAR (XSTRING (string)->data[i]);
325 UNGCPRO;
326 }
327 }
328 \f
329 DEFUN ("write-char", Fwrite_char, Swrite_char, 1, 2, 0,
330 "Output character CHAR to stream PRINTCHARFUN.\n\
331 PRINTCHARFUN defaults to the value of `standard-output' (which see).")
332 (ch, printcharfun)
333 Lisp_Object ch, printcharfun;
334 {
335 struct buffer *old = current_buffer;
336 int old_point = -1;
337 int start_point;
338 Lisp_Object original;
339
340 if (NILP (printcharfun))
341 printcharfun = Vstandard_output;
342 CHECK_NUMBER (ch, 0);
343 PRINTPREPARE;
344 PRINTCHAR (XINT (ch));
345 PRINTFINISH;
346 return ch;
347 }
348
349 /* Used from outside of print.c to print a block of SIZE chars at DATA
350 on the default output stream.
351 Do not use this on the contents of a Lisp string. */
352
353 write_string (data, size)
354 char *data;
355 int size;
356 {
357 struct buffer *old = current_buffer;
358 Lisp_Object printcharfun;
359 int old_point = -1;
360 int start_point;
361 Lisp_Object original;
362
363 printcharfun = Vstandard_output;
364
365 PRINTPREPARE;
366 strout (data, size, printcharfun);
367 PRINTFINISH;
368 }
369
370 /* Used from outside of print.c to print a block of SIZE chars at DATA
371 on a specified stream PRINTCHARFUN.
372 Do not use this on the contents of a Lisp string. */
373
374 write_string_1 (data, size, printcharfun)
375 char *data;
376 int size;
377 Lisp_Object printcharfun;
378 {
379 struct buffer *old = current_buffer;
380 int old_point = -1;
381 int start_point;
382 Lisp_Object original;
383
384 PRINTPREPARE;
385 strout (data, size, printcharfun);
386 PRINTFINISH;
387 }
388
389
390 #ifndef standalone
391
392 void
393 temp_output_buffer_setup (bufname)
394 char *bufname;
395 {
396 register struct buffer *old = current_buffer;
397 register Lisp_Object buf;
398
399 Fset_buffer (Fget_buffer_create (build_string (bufname)));
400
401 current_buffer->read_only = Qnil;
402 Ferase_buffer ();
403
404 XSETBUFFER (buf, current_buffer);
405 specbind (Qstandard_output, buf);
406
407 set_buffer_internal (old);
408 }
409
410 Lisp_Object
411 internal_with_output_to_temp_buffer (bufname, function, args)
412 char *bufname;
413 Lisp_Object (*function) ();
414 Lisp_Object args;
415 {
416 int count = specpdl_ptr - specpdl;
417 Lisp_Object buf, val;
418 struct gcpro gcpro1;
419
420 GCPRO1 (args);
421 record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
422 temp_output_buffer_setup (bufname);
423 buf = Vstandard_output;
424 UNGCPRO;
425
426 val = (*function) (args);
427
428 GCPRO1 (val);
429 temp_output_buffer_show (buf);
430 UNGCPRO;
431
432 return unbind_to (count, val);
433 }
434
435 DEFUN ("with-output-to-temp-buffer", Fwith_output_to_temp_buffer, Swith_output_to_temp_buffer,
436 1, UNEVALLED, 0,
437 "Bind `standard-output' to buffer BUFNAME, eval BODY, then show that buffer.\n\
438 The buffer is cleared out initially, and marked as unmodified when done.\n\
439 All output done by BODY is inserted in that buffer by default.\n\
440 The buffer is displayed in another window, but not selected.\n\
441 The value of the last form in BODY is returned.\n\
442 If BODY does not finish normally, the buffer BUFNAME is not displayed.\n\n\
443 If variable `temp-buffer-show-function' is non-nil, call it at the end\n\
444 to get the buffer displayed. It gets one argument, the buffer to display.")
445 (args)
446 Lisp_Object args;
447 {
448 struct gcpro gcpro1;
449 Lisp_Object name;
450 int count = specpdl_ptr - specpdl;
451 Lisp_Object buf, val;
452
453 GCPRO1(args);
454 name = Feval (Fcar (args));
455 UNGCPRO;
456
457 CHECK_STRING (name, 0);
458 temp_output_buffer_setup (XSTRING (name)->data);
459 buf = Vstandard_output;
460
461 val = Fprogn (Fcdr (args));
462
463 temp_output_buffer_show (buf);
464
465 return unbind_to (count, val);
466 }
467 #endif /* not standalone */
468 \f
469 static void print ();
470
471 DEFUN ("terpri", Fterpri, Sterpri, 0, 1, 0,
472 "Output a newline to stream PRINTCHARFUN.\n\
473 If PRINTCHARFUN is omitted or nil, the value of `standard-output' is used.")
474 (printcharfun)
475 Lisp_Object printcharfun;
476 {
477 struct buffer *old = current_buffer;
478 int old_point = -1;
479 int start_point;
480 Lisp_Object original;
481
482 if (NILP (printcharfun))
483 printcharfun = Vstandard_output;
484 PRINTPREPARE;
485 PRINTCHAR ('\n');
486 PRINTFINISH;
487 return Qt;
488 }
489
490 DEFUN ("prin1", Fprin1, Sprin1, 1, 2, 0,
491 "Output the printed representation of OBJECT, any Lisp object.\n\
492 Quoting characters are printed when needed to make output that `read'\n\
493 can handle, whenever this is possible.\n\
494 Output stream is PRINTCHARFUN, or value of `standard-output' (which see).")
495 (obj, printcharfun)
496 Lisp_Object obj, printcharfun;
497 {
498 struct buffer *old = current_buffer;
499 int old_point = -1;
500 int start_point;
501 Lisp_Object original;
502
503 #ifdef MAX_PRINT_CHARS
504 max_print = 0;
505 #endif /* MAX_PRINT_CHARS */
506 if (NILP (printcharfun))
507 printcharfun = Vstandard_output;
508 PRINTPREPARE;
509 print_depth = 0;
510 print (obj, printcharfun, 1);
511 PRINTFINISH;
512 return obj;
513 }
514
515 /* a buffer which is used to hold output being built by prin1-to-string */
516 Lisp_Object Vprin1_to_string_buffer;
517
518 DEFUN ("prin1-to-string", Fprin1_to_string, Sprin1_to_string, 1, 2, 0,
519 "Return a string containing the printed representation of OBJECT,\n\
520 any Lisp object. Quoting characters are used when needed to make output\n\
521 that `read' can handle, whenever this is possible, unless the optional\n\
522 second argument NOESCAPE is non-nil.")
523 (obj, noescape)
524 Lisp_Object obj, noescape;
525 {
526 struct buffer *old = current_buffer;
527 int old_point = -1;
528 int start_point;
529 Lisp_Object original, printcharfun;
530 struct gcpro gcpro1;
531
532 printcharfun = Vprin1_to_string_buffer;
533 PRINTPREPARE;
534 print_depth = 0;
535 print (obj, printcharfun, NILP (noescape));
536 /* Make Vprin1_to_string_buffer be the default buffer after PRINTFINSH */
537 PRINTFINISH;
538 set_buffer_internal (XBUFFER (Vprin1_to_string_buffer));
539 obj = Fbuffer_string ();
540
541 GCPRO1 (obj);
542 Ferase_buffer ();
543 set_buffer_internal (old);
544 UNGCPRO;
545
546 return obj;
547 }
548
549 DEFUN ("princ", Fprinc, Sprinc, 1, 2, 0,
550 "Output the printed representation of OBJECT, any Lisp object.\n\
551 No quoting characters are used; no delimiters are printed around\n\
552 the contents of strings.\n\
553 Output stream is PRINTCHARFUN, or value of standard-output (which see).")
554 (obj, printcharfun)
555 Lisp_Object obj, printcharfun;
556 {
557 struct buffer *old = current_buffer;
558 int old_point = -1;
559 int start_point;
560 Lisp_Object original;
561
562 if (NILP (printcharfun))
563 printcharfun = Vstandard_output;
564 PRINTPREPARE;
565 print_depth = 0;
566 print (obj, printcharfun, 0);
567 PRINTFINISH;
568 return obj;
569 }
570
571 DEFUN ("print", Fprint, Sprint, 1, 2, 0,
572 "Output the printed representation of OBJECT, with newlines around it.\n\
573 Quoting characters are printed when needed to make output that `read'\n\
574 can handle, whenever this is possible.\n\
575 Output stream is PRINTCHARFUN, or value of `standard-output' (which see).")
576 (obj, printcharfun)
577 Lisp_Object obj, printcharfun;
578 {
579 struct buffer *old = current_buffer;
580 int old_point = -1;
581 int start_point;
582 Lisp_Object original;
583 struct gcpro gcpro1;
584
585 #ifdef MAX_PRINT_CHARS
586 print_chars = 0;
587 max_print = MAX_PRINT_CHARS;
588 #endif /* MAX_PRINT_CHARS */
589 if (NILP (printcharfun))
590 printcharfun = Vstandard_output;
591 GCPRO1 (obj);
592 PRINTPREPARE;
593 print_depth = 0;
594 PRINTCHAR ('\n');
595 print (obj, printcharfun, 1);
596 PRINTCHAR ('\n');
597 PRINTFINISH;
598 #ifdef MAX_PRINT_CHARS
599 max_print = 0;
600 print_chars = 0;
601 #endif /* MAX_PRINT_CHARS */
602 UNGCPRO;
603 return obj;
604 }
605
606 /* The subroutine object for external-debugging-output is kept here
607 for the convenience of the debugger. */
608 Lisp_Object Qexternal_debugging_output;
609
610 DEFUN ("external-debugging-output", Fexternal_debugging_output, Sexternal_debugging_output, 1, 1, 0,
611 "Write CHARACTER to stderr.\n\
612 You can call print while debugging emacs, and pass it this function\n\
613 to make it write to the debugging output.\n")
614 (character)
615 Lisp_Object character;
616 {
617 CHECK_NUMBER (character, 0);
618 putc (XINT (character), stderr);
619
620 return character;
621 }
622
623 /* This is the interface for debugging printing. */
624
625 void
626 debug_print (arg)
627 Lisp_Object arg;
628 {
629 Fprin1 (arg, Qexternal_debugging_output);
630 }
631 \f
632 #ifdef LISP_FLOAT_TYPE
633
634 /*
635 * The buffer should be at least as large as the max string size of the
636 * largest float, printed in the biggest notation. This is undoubtably
637 * 20d float_output_format, with the negative of the C-constant "HUGE"
638 * from <math.h>.
639 *
640 * On the vax the worst case is -1e38 in 20d format which takes 61 bytes.
641 *
642 * I assume that IEEE-754 format numbers can take 329 bytes for the worst
643 * case of -1e307 in 20d float_output_format. What is one to do (short of
644 * re-writing _doprnt to be more sane)?
645 * -wsr
646 */
647
648 void
649 float_to_string (buf, data)
650 unsigned char *buf;
651 double data;
652 {
653 unsigned char *cp;
654 int width;
655
656 if (NILP (Vfloat_output_format)
657 || !STRINGP (Vfloat_output_format))
658 lose:
659 {
660 sprintf (buf, "%.17g", data);
661 width = -1;
662 }
663 else /* oink oink */
664 {
665 /* Check that the spec we have is fully valid.
666 This means not only valid for printf,
667 but meant for floats, and reasonable. */
668 cp = XSTRING (Vfloat_output_format)->data;
669
670 if (cp[0] != '%')
671 goto lose;
672 if (cp[1] != '.')
673 goto lose;
674
675 cp += 2;
676
677 /* Check the width specification. */
678 width = -1;
679 if ('0' <= *cp && *cp <= '9')
680 for (width = 0; (*cp >= '0' && *cp <= '9'); cp++)
681 width = (width * 10) + (*cp - '0');
682
683 if (*cp != 'e' && *cp != 'f' && *cp != 'g')
684 goto lose;
685
686 /* A precision of zero is valid for %f; everything else requires
687 at least one. Width may be omitted anywhere. */
688 if (width != -1
689 && (width < (*cp != 'f')
690 || width > DBL_DIG))
691 goto lose;
692
693 if (cp[1] != 0)
694 goto lose;
695
696 sprintf (buf, XSTRING (Vfloat_output_format)->data, data);
697 }
698
699 /* Make sure there is a decimal point with digit after, or an
700 exponent, so that the value is readable as a float. But don't do
701 this with "%.0f"; it's valid for that not to produce a decimal
702 point. Note that width can be 0 only for %.0f. */
703 if (width != 0)
704 {
705 for (cp = buf; *cp; cp++)
706 if ((*cp < '0' || *cp > '9') && *cp != '-')
707 break;
708
709 if (*cp == '.' && cp[1] == 0)
710 {
711 cp[1] = '0';
712 cp[2] = 0;
713 }
714
715 if (*cp == 0)
716 {
717 *cp++ = '.';
718 *cp++ = '0';
719 *cp++ = 0;
720 }
721 }
722 }
723 #endif /* LISP_FLOAT_TYPE */
724 \f
725 static void
726 print (obj, printcharfun, escapeflag)
727 Lisp_Object obj;
728 register Lisp_Object printcharfun;
729 int escapeflag;
730 {
731 char buf[30];
732
733 QUIT;
734
735 #if 1 /* I'm not sure this is really worth doing. */
736 /* Detect circularities and truncate them.
737 No need to offer any alternative--this is better than an error. */
738 if (CONSP (obj) || VECTORP (obj) || COMPILEDP (obj))
739 {
740 int i;
741 for (i = 0; i < print_depth; i++)
742 if (EQ (obj, being_printed[i]))
743 {
744 sprintf (buf, "#%d", i);
745 strout (buf, -1, printcharfun);
746 return;
747 }
748 }
749 #endif
750
751 being_printed[print_depth] = obj;
752 print_depth++;
753
754 if (print_depth > PRINT_CIRCLE)
755 error ("Apparently circular structure being printed");
756 #ifdef MAX_PRINT_CHARS
757 if (max_print && print_chars > max_print)
758 {
759 PRINTCHAR ('\n');
760 print_chars = 0;
761 }
762 #endif /* MAX_PRINT_CHARS */
763
764 switch (XGCTYPE (obj))
765 {
766 case Lisp_Int:
767 sprintf (buf, "%d", XINT (obj));
768 strout (buf, -1, printcharfun);
769 break;
770
771 #ifdef LISP_FLOAT_TYPE
772 case Lisp_Float:
773 {
774 char pigbuf[350]; /* see comments in float_to_string */
775
776 float_to_string (pigbuf, XFLOAT(obj)->data);
777 strout (pigbuf, -1, printcharfun);
778 }
779 break;
780 #endif
781
782 case Lisp_String:
783 if (!escapeflag)
784 print_string (obj, printcharfun);
785 else
786 {
787 register int i;
788 register unsigned char c;
789 struct gcpro gcpro1;
790
791 GCPRO1 (obj);
792
793 #ifdef USE_TEXT_PROPERTIES
794 if (!NULL_INTERVAL_P (XSTRING (obj)->intervals))
795 {
796 PRINTCHAR ('#');
797 PRINTCHAR ('(');
798 }
799 #endif
800
801 PRINTCHAR ('\"');
802 for (i = 0; i < XSTRING (obj)->size; i++)
803 {
804 QUIT;
805 c = XSTRING (obj)->data[i];
806 if (c == '\n' && print_escape_newlines)
807 {
808 PRINTCHAR ('\\');
809 PRINTCHAR ('n');
810 }
811 else if (c == '\f' && print_escape_newlines)
812 {
813 PRINTCHAR ('\\');
814 PRINTCHAR ('f');
815 }
816 else
817 {
818 if (c == '\"' || c == '\\')
819 PRINTCHAR ('\\');
820 PRINTCHAR (c);
821 }
822 }
823 PRINTCHAR ('\"');
824
825 #ifdef USE_TEXT_PROPERTIES
826 if (!NULL_INTERVAL_P (XSTRING (obj)->intervals))
827 {
828 traverse_intervals (XSTRING (obj)->intervals,
829 0, 0, print_interval, printcharfun);
830 PRINTCHAR (')');
831 }
832 #endif
833
834 UNGCPRO;
835 }
836 break;
837
838 case Lisp_Symbol:
839 {
840 register int confusing;
841 register unsigned char *p = XSYMBOL (obj)->name->data;
842 register unsigned char *end = p + XSYMBOL (obj)->name->size;
843 register unsigned char c;
844
845 if (p != end && (*p == '-' || *p == '+')) p++;
846 if (p == end)
847 confusing = 0;
848 else
849 {
850 while (p != end && *p >= '0' && *p <= '9')
851 p++;
852 confusing = (end == p);
853 }
854
855 p = XSYMBOL (obj)->name->data;
856 while (p != end)
857 {
858 QUIT;
859 c = *p++;
860 if (escapeflag)
861 {
862 if (c == '\"' || c == '\\' || c == '\'' || c == ';' || c == '#' ||
863 c == '(' || c == ')' || c == ',' || c =='.' || c == '`' ||
864 c == '[' || c == ']' || c == '?' || c <= 040 || confusing)
865 PRINTCHAR ('\\'), confusing = 0;
866 }
867 PRINTCHAR (c);
868 }
869 }
870 break;
871
872 case Lisp_Cons:
873 /* If deeper than spec'd depth, print placeholder. */
874 if (INTEGERP (Vprint_level)
875 && print_depth > XINT (Vprint_level))
876 strout ("...", -1, printcharfun);
877 else
878 {
879 PRINTCHAR ('(');
880 {
881 register int i = 0;
882 register int max = 0;
883
884 if (INTEGERP (Vprint_length))
885 max = XINT (Vprint_length);
886 /* Could recognize circularities in cdrs here,
887 but that would make printing of long lists quadratic.
888 It's not worth doing. */
889 while (CONSP (obj))
890 {
891 if (i++)
892 PRINTCHAR (' ');
893 if (max && i > max)
894 {
895 strout ("...", 3, printcharfun);
896 break;
897 }
898 print (Fcar (obj), printcharfun, escapeflag);
899 obj = Fcdr (obj);
900 }
901 }
902 if (!NILP (obj) && !CONSP (obj))
903 {
904 strout (" . ", 3, printcharfun);
905 print (obj, printcharfun, escapeflag);
906 }
907 PRINTCHAR (')');
908 }
909 break;
910
911 case Lisp_Vectorlike:
912 if (PROCESSP (obj))
913 {
914 if (escapeflag)
915 {
916 strout ("#<process ", -1, printcharfun);
917 print_string (XPROCESS (obj)->name, printcharfun);
918 PRINTCHAR ('>');
919 }
920 else
921 print_string (XPROCESS (obj)->name, printcharfun);
922 }
923 else if (SUBRP (obj))
924 {
925 strout ("#<subr ", -1, printcharfun);
926 strout (XSUBR (obj)->symbol_name, -1, printcharfun);
927 PRINTCHAR ('>');
928 }
929 #ifndef standalone
930 else if (WINDOWP (obj))
931 {
932 strout ("#<window ", -1, printcharfun);
933 sprintf (buf, "%d", XFASTINT (XWINDOW (obj)->sequence_number));
934 strout (buf, -1, printcharfun);
935 if (!NILP (XWINDOW (obj)->buffer))
936 {
937 strout (" on ", -1, printcharfun);
938 print_string (XBUFFER (XWINDOW (obj)->buffer)->name, printcharfun);
939 }
940 PRINTCHAR ('>');
941 }
942 else if (BUFFERP (obj))
943 {
944 if (NILP (XBUFFER (obj)->name))
945 strout ("#<killed buffer>", -1, printcharfun);
946 else if (escapeflag)
947 {
948 strout ("#<buffer ", -1, printcharfun);
949 print_string (XBUFFER (obj)->name, printcharfun);
950 PRINTCHAR ('>');
951 }
952 else
953 print_string (XBUFFER (obj)->name, printcharfun);
954 }
955 else if (WINDOW_CONFIGURATIONP (obj))
956 {
957 strout ("#<window-configuration>", -1, printcharfun);
958 }
959 #ifdef MULTI_FRAME
960 else if (FRAMEP (obj))
961 {
962 strout ((FRAME_LIVE_P (XFRAME (obj))
963 ? "#<frame " : "#<dead frame "),
964 -1, printcharfun);
965 print_string (XFRAME (obj)->name, printcharfun);
966 sprintf (buf, " 0x%lx", (unsigned long) (XFRAME (obj)));
967 strout (buf, -1, printcharfun);
968 PRINTCHAR ('>');
969 }
970 #endif
971 #endif /* not standalone */
972 else
973 {
974 int size = XVECTOR (obj)->size;
975 if (COMPILEDP (obj))
976 {
977 PRINTCHAR ('#');
978 size &= PSEUDOVECTOR_SIZE_MASK;
979 }
980 if (size & PSEUDOVECTOR_FLAG)
981 goto badtype;
982
983 PRINTCHAR ('[');
984 {
985 register int i;
986 register Lisp_Object tem;
987 for (i = 0; i < size; i++)
988 {
989 if (i) PRINTCHAR (' ');
990 tem = XVECTOR (obj)->contents[i];
991 print (tem, printcharfun, escapeflag);
992 }
993 }
994 PRINTCHAR (']');
995 }
996 break;
997
998 #ifndef standalone
999 case Lisp_Misc:
1000 switch (XMISC (obj)->type)
1001 {
1002 case Lisp_Misc_Marker:
1003 strout ("#<marker ", -1, printcharfun);
1004 if (!(XMARKER (obj)->buffer))
1005 strout ("in no buffer", -1, printcharfun);
1006 else
1007 {
1008 sprintf (buf, "at %d", marker_position (obj));
1009 strout (buf, -1, printcharfun);
1010 strout (" in ", -1, printcharfun);
1011 print_string (XMARKER (obj)->buffer->name, printcharfun);
1012 }
1013 PRINTCHAR ('>');
1014 break;
1015
1016 case Lisp_Misc_Overlay:
1017 strout ("#<overlay ", -1, printcharfun);
1018 if (!(XMARKER (OVERLAY_START (obj))->buffer))
1019 strout ("in no buffer", -1, printcharfun);
1020 else
1021 {
1022 sprintf (buf, "from %d to %d in ",
1023 marker_position (OVERLAY_START (obj)),
1024 marker_position (OVERLAY_END (obj)));
1025 strout (buf, -1, printcharfun);
1026 print_string (XMARKER (OVERLAY_START (obj))->buffer->name,
1027 printcharfun);
1028 }
1029 PRINTCHAR ('>');
1030 break;
1031
1032 /* Remaining cases shouldn't happen in normal usage, but let's print
1033 them anyway for the benefit of the debugger. */
1034 case Lisp_Misc_Free:
1035 strout ("#<misc free cell>", -1, printcharfun);
1036 break;
1037
1038 case Lisp_Misc_Intfwd:
1039 sprintf (buf, "#<intfwd to %d>", *XINTFWD (obj)->intvar);
1040 strout (buf, -1, printcharfun);
1041 break;
1042
1043 case Lisp_Misc_Boolfwd:
1044 sprintf (buf, "#<boolfwd to %s>",
1045 (*XBOOLFWD (obj)->boolvar ? "t" : "nil"));
1046 strout (buf, -1, printcharfun);
1047 break;
1048
1049 case Lisp_Misc_Objfwd:
1050 strout (buf, "#<objfwd to ", -1, printcharfun);
1051 print (*XOBJFWD (obj)->objvar, printcharfun, escapeflag);
1052 PRINTCHAR ('>');
1053 break;
1054
1055 case Lisp_Misc_Buffer_Objfwd:
1056 strout (buf, "#<buffer_objfwd to ", -1, printcharfun);
1057 print (*(Lisp_Object *)((char *)current_buffer +
1058 XBUFFER_OBJFWD (obj)->offset),
1059 printcharfun, escapeflag);
1060 PRINTCHAR ('>');
1061 break;
1062
1063 case Lisp_Misc_Buffer_Local_Value:
1064 strout ("#<buffer_local_value ", -1, printcharfun);
1065 goto do_buffer_local;
1066 case Lisp_Misc_Some_Buffer_Local_Value:
1067 strout ("#<some_buffer_local_value ", -1, printcharfun);
1068 do_buffer_local:
1069 strout ("[realvalue] ", -1, printcharfun);
1070 print (XBUFFER_LOCAL_VALUE (obj)->car, printcharfun, escapeflag);
1071 strout ("[buffer] ", -1, printcharfun);
1072 print (XCONS (XBUFFER_LOCAL_VALUE (obj)->cdr)->car,
1073 printcharfun, escapeflag);
1074 strout ("[alist-elt] ", -1, printcharfun);
1075 print (XCONS (XCONS (XBUFFER_LOCAL_VALUE (obj)->cdr)->cdr)->car,
1076 printcharfun, escapeflag);
1077 strout ("[default-value] ", -1, printcharfun);
1078 print (XCONS (XCONS (XBUFFER_LOCAL_VALUE (obj)->cdr)->cdr)->cdr,
1079 printcharfun, escapeflag);
1080 PRINTCHAR ('>');
1081 break;
1082
1083 default:
1084 goto badtype;
1085 }
1086 break;
1087 #endif /* standalone */
1088
1089 default:
1090 badtype:
1091 {
1092 /* We're in trouble if this happens!
1093 Probably should just abort () */
1094 strout ("#<EMACS BUG: INVALID DATATYPE ", -1, printcharfun);
1095 if (MISCP (obj))
1096 sprintf (buf, "(MISC 0x%04x)", (int) XMISC (obj)->type);
1097 else if (VECTORLIKEP (obj))
1098 sprintf (buf, "(PVEC 0x%08x)", (int) XVECTOR (obj)->size);
1099 else
1100 sprintf (buf, "(0x%02x)", (int) XTYPE (obj));
1101 strout (buf, -1, printcharfun);
1102 strout (" Save your buffers immediately and please report this bug>",
1103 -1, printcharfun);
1104 }
1105 }
1106
1107 print_depth--;
1108 }
1109 \f
1110 #ifdef USE_TEXT_PROPERTIES
1111
1112 /* Print a description of INTERVAL using PRINTCHARFUN.
1113 This is part of printing a string that has text properties. */
1114
1115 void
1116 print_interval (interval, printcharfun)
1117 INTERVAL interval;
1118 Lisp_Object printcharfun;
1119 {
1120 PRINTCHAR (' ');
1121 print (make_number (interval->position), printcharfun, 1);
1122 PRINTCHAR (' ');
1123 print (make_number (interval->position + LENGTH (interval)),
1124 printcharfun, 1);
1125 PRINTCHAR (' ');
1126 print (interval->plist, printcharfun, 1);
1127 }
1128
1129 #endif /* USE_TEXT_PROPERTIES */
1130 \f
1131 void
1132 syms_of_print ()
1133 {
1134 staticpro (&Qprint_escape_newlines);
1135 Qprint_escape_newlines = intern ("print-escape-newlines");
1136
1137 DEFVAR_LISP ("standard-output", &Vstandard_output,
1138 "Output stream `print' uses by default for outputting a character.\n\
1139 This may be any function of one argument.\n\
1140 It may also be a buffer (output is inserted before point)\n\
1141 or a marker (output is inserted and the marker is advanced)\n\
1142 or the symbol t (output appears in the minibuffer line).");
1143 Vstandard_output = Qt;
1144 Qstandard_output = intern ("standard-output");
1145 staticpro (&Qstandard_output);
1146
1147 #ifdef LISP_FLOAT_TYPE
1148 DEFVAR_LISP ("float-output-format", &Vfloat_output_format,
1149 "The format descriptor string used to print floats.\n\
1150 This is a %-spec like those accepted by `printf' in C,\n\
1151 but with some restrictions. It must start with the two characters `%.'.\n\
1152 After that comes an integer precision specification,\n\
1153 and then a letter which controls the format.\n\
1154 The letters allowed are `e', `f' and `g'.\n\
1155 Use `e' for exponential notation \"DIG.DIGITSeEXPT\"\n\
1156 Use `f' for decimal point notation \"DIGITS.DIGITS\".\n\
1157 Use `g' to choose the shorter of those two formats for the number at hand.\n\
1158 The precision in any of these cases is the number of digits following\n\
1159 the decimal point. With `f', a precision of 0 means to omit the\n\
1160 decimal point. 0 is not allowed with `e' or `g'.\n\n\
1161 A value of nil means to use `%.17g'.");
1162 Vfloat_output_format = Qnil;
1163 Qfloat_output_format = intern ("float-output-format");
1164 staticpro (&Qfloat_output_format);
1165 #endif /* LISP_FLOAT_TYPE */
1166
1167 DEFVAR_LISP ("print-length", &Vprint_length,
1168 "Maximum length of list to print before abbreviating.\n\
1169 A value of nil means no limit.");
1170 Vprint_length = Qnil;
1171
1172 DEFVAR_LISP ("print-level", &Vprint_level,
1173 "Maximum depth of list nesting to print before abbreviating.\n\
1174 A value of nil means no limit.");
1175 Vprint_level = Qnil;
1176
1177 DEFVAR_BOOL ("print-escape-newlines", &print_escape_newlines,
1178 "Non-nil means print newlines in strings as backslash-n.\n\
1179 Also print formfeeds as backslash-f.");
1180 print_escape_newlines = 0;
1181
1182 /* prin1_to_string_buffer initialized in init_buffer_once in buffer.c */
1183 staticpro (&Vprin1_to_string_buffer);
1184
1185 defsubr (&Sprin1);
1186 defsubr (&Sprin1_to_string);
1187 defsubr (&Sprinc);
1188 defsubr (&Sprint);
1189 defsubr (&Sterpri);
1190 defsubr (&Swrite_char);
1191 defsubr (&Sexternal_debugging_output);
1192
1193 Qexternal_debugging_output = intern ("external-debugging-output");
1194 staticpro (&Qexternal_debugging_output);
1195
1196 #ifndef standalone
1197 defsubr (&Swith_output_to_temp_buffer);
1198 #endif /* not standalone */
1199 }