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