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