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