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