(re_search_2): Optimize regexp that starts with ^.
[emacs.git] / src / print.c
blobb6a12e7228d26e48a174d91e4b7046bec8c93fcb
1 /* Lisp object printing and output streams.
2 Copyright (C) 1985, 86, 88, 93, 94, 95 Free Software Foundation, Inc.
4 This file is part of GNU Emacs.
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.
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.
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. */
22 #include <config.h>
23 #include <stdio.h>
24 #include "lisp.h"
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 */
36 #ifdef USE_TEXT_PROPERTIES
37 #include "intervals.h"
38 #endif
40 Lisp_Object Vstandard_output, Qstandard_output;
42 /* These are used to print like we read. */
43 extern Lisp_Object Qbackquote, Qcomma, Qcomma_at, Qcomma_dot, Qfunction;
45 #ifdef LISP_FLOAT_TYPE
46 Lisp_Object Vfloat_output_format, Qfloat_output_format;
47 #endif /* LISP_FLOAT_TYPE */
49 /* Avoid actual stack overflow in print. */
50 int print_depth;
52 /* Detect most circularities to print finite output. */
53 #define PRINT_CIRCLE 200
54 Lisp_Object being_printed[PRINT_CIRCLE];
56 /* When printing into a buffer, first we put the text in this
57 block, then insert it all at once. */
58 char *print_buffer;
60 /* Size allocated in print_buffer. */
61 int print_buffer_size;
62 /* Size used in print_buffer. */
63 int print_buffer_pos;
65 /* Maximum length of list to print in full; noninteger means
66 effectively infinity */
68 Lisp_Object Vprint_length;
70 /* Maximum depth of list to print in full; noninteger means
71 effectively infinity. */
73 Lisp_Object Vprint_level;
75 /* Nonzero means print newlines in strings as \n. */
77 int print_escape_newlines;
79 Lisp_Object Qprint_escape_newlines;
81 /* Nonzero means print (quote foo) forms as 'foo, etc. */
83 int print_quoted;
85 Lisp_Object Qprint_quoted;
87 /* Nonzero means print newline to stdout before next minibuffer message.
88 Defined in xdisp.c */
90 extern int noninteractive_need_newline;
92 #ifdef MAX_PRINT_CHARS
93 static int print_chars;
94 static int max_print;
95 #endif /* MAX_PRINT_CHARS */
97 void print_interval ();
99 #if 0
100 /* Convert between chars and GLYPHs */
103 glyphlen (glyphs)
104 register GLYPH *glyphs;
106 register int i = 0;
108 while (glyphs[i])
109 i++;
110 return i;
113 void
114 str_to_glyph_cpy (str, glyphs)
115 char *str;
116 GLYPH *glyphs;
118 register GLYPH *gp = glyphs;
119 register char *cp = str;
121 while (*cp)
122 *gp++ = *cp++;
125 void
126 str_to_glyph_ncpy (str, glyphs, n)
127 char *str;
128 GLYPH *glyphs;
129 register int n;
131 register GLYPH *gp = glyphs;
132 register char *cp = str;
134 while (n-- > 0)
135 *gp++ = *cp++;
138 void
139 glyph_to_str_cpy (glyphs, str)
140 GLYPH *glyphs;
141 char *str;
143 register GLYPH *gp = glyphs;
144 register char *cp = str;
146 while (*gp)
147 *str++ = *gp++ & 0377;
149 #endif
151 /* Low level output routines for characters and strings */
153 /* Lisp functions to do output using a stream
154 must have the stream in a variable called printcharfun
155 and must start with PRINTPREPARE and end with PRINTFINISH.
156 Use PRINTCHAR to output one character,
157 or call strout to output a block of characters.
158 Also, each one must have the declarations
159 struct buffer *old = current_buffer;
160 int old_point = -1, start_point;
161 Lisp_Object original;
164 #define PRINTPREPARE \
165 original = printcharfun; \
166 if (NILP (printcharfun)) printcharfun = Qt; \
167 if (BUFFERP (printcharfun)) \
168 { if (XBUFFER (printcharfun) != current_buffer) \
169 Fset_buffer (printcharfun); \
170 printcharfun = Qnil;} \
171 if (MARKERP (printcharfun)) \
172 { if (!(XMARKER (original)->buffer)) \
173 error ("Marker does not point anywhere"); \
174 if (XMARKER (original)->buffer != current_buffer) \
175 set_buffer_internal (XMARKER (original)->buffer); \
176 old_point = point; \
177 SET_PT (marker_position (printcharfun)); \
178 start_point = point; \
179 printcharfun = Qnil;} \
180 if (NILP (printcharfun)) \
182 print_buffer_pos = 0; \
183 print_buffer_size = 1000; \
184 print_buffer = (char *) xmalloc (print_buffer_size); \
186 else \
187 print_buffer = 0;
189 #define PRINTFINISH \
190 if (NILP (printcharfun)) \
191 insert (print_buffer, print_buffer_pos); \
192 if (print_buffer) free (print_buffer); \
193 if (MARKERP (original)) \
194 Fset_marker (original, make_number (point), Qnil); \
195 if (old_point >= 0) \
196 SET_PT (old_point + (old_point >= start_point \
197 ? point - start_point : 0)); \
198 if (old != current_buffer) \
199 set_buffer_internal (old)
201 #define PRINTCHAR(ch) printchar (ch, printcharfun)
203 /* Index of first unused element of FRAME_MESSAGE_BUF(mini_frame). */
204 static int printbufidx;
206 static void
207 printchar (ch, fun)
208 unsigned char ch;
209 Lisp_Object fun;
211 Lisp_Object ch1;
213 #ifdef MAX_PRINT_CHARS
214 if (max_print)
215 print_chars++;
216 #endif /* MAX_PRINT_CHARS */
217 #ifndef standalone
218 if (EQ (fun, Qnil))
220 QUIT;
221 if (print_buffer_pos == print_buffer_size)
222 print_buffer = (char *) xrealloc (print_buffer,
223 print_buffer_size *= 2);
224 print_buffer[print_buffer_pos++] = ch;
225 return;
228 if (EQ (fun, Qt))
230 FRAME_PTR mini_frame
231 = XFRAME (WINDOW_FRAME (XWINDOW (minibuf_window)));
233 if (noninteractive)
235 putchar (ch);
236 noninteractive_need_newline = 1;
237 return;
240 if (echo_area_glyphs != FRAME_MESSAGE_BUF (mini_frame)
241 || !message_buf_print)
243 message_log_maybe_newline ();
244 echo_area_glyphs = FRAME_MESSAGE_BUF (mini_frame);
245 printbufidx = 0;
246 echo_area_glyphs_length = 0;
247 message_buf_print = 1;
250 message_dolog (&ch, 1, 0);
251 if (printbufidx < FRAME_WIDTH (mini_frame) - 1)
252 FRAME_MESSAGE_BUF (mini_frame)[printbufidx++] = ch;
253 FRAME_MESSAGE_BUF (mini_frame)[printbufidx] = 0;
254 echo_area_glyphs_length = printbufidx;
256 return;
258 #endif /* not standalone */
260 XSETFASTINT (ch1, ch);
261 call1 (fun, ch1);
264 static void
265 strout (ptr, size, printcharfun)
266 char *ptr;
267 int size;
268 Lisp_Object printcharfun;
270 int i = 0;
272 if (EQ (printcharfun, Qnil))
274 if (size < 0)
275 size = strlen (ptr);
277 if (print_buffer_pos + size > print_buffer_size)
279 print_buffer_size = print_buffer_size * 2 + size;
280 print_buffer = (char *) xrealloc (print_buffer,
281 print_buffer_size);
283 bcopy (ptr, print_buffer + print_buffer_pos, size);
284 print_buffer_pos += size;
286 #ifdef MAX_PRINT_CHARS
287 if (max_print)
288 print_chars += size;
289 #endif /* MAX_PRINT_CHARS */
290 return;
292 if (EQ (printcharfun, Qt))
294 FRAME_PTR mini_frame
295 = XFRAME (WINDOW_FRAME (XWINDOW (minibuf_window)));
297 i = size >= 0 ? size : strlen (ptr);
298 #ifdef MAX_PRINT_CHARS
299 if (max_print)
300 print_chars += i;
301 #endif /* MAX_PRINT_CHARS */
303 if (noninteractive)
305 fwrite (ptr, 1, i, stdout);
306 noninteractive_need_newline = 1;
307 return;
310 if (echo_area_glyphs != FRAME_MESSAGE_BUF (mini_frame)
311 || !message_buf_print)
313 message_log_maybe_newline ();
314 echo_area_glyphs = FRAME_MESSAGE_BUF (mini_frame);
315 printbufidx = 0;
316 echo_area_glyphs_length = 0;
317 message_buf_print = 1;
320 message_dolog (ptr, i, 0);
321 if (i > FRAME_WIDTH (mini_frame) - printbufidx - 1)
322 i = FRAME_WIDTH (mini_frame) - printbufidx - 1;
323 bcopy (ptr, &FRAME_MESSAGE_BUF (mini_frame) [printbufidx], i);
324 printbufidx += i;
325 echo_area_glyphs_length = printbufidx;
326 FRAME_MESSAGE_BUF (mini_frame) [printbufidx] = 0;
328 return;
331 if (size >= 0)
332 while (i < size)
333 PRINTCHAR (ptr[i++]);
334 else
335 while (ptr[i])
336 PRINTCHAR (ptr[i++]);
339 /* Print the contents of a string STRING using PRINTCHARFUN.
340 It isn't safe to use strout in many cases,
341 because printing one char can relocate. */
343 print_string (string, printcharfun)
344 Lisp_Object string;
345 Lisp_Object printcharfun;
347 if (EQ (printcharfun, Qt) || NILP (printcharfun))
348 /* strout is safe for output to a frame (echo area) or to print_buffer. */
349 strout (XSTRING (string)->data, XSTRING (string)->size, printcharfun);
350 else
352 /* Otherwise, fetch the string address for each character. */
353 int i;
354 int size = XSTRING (string)->size;
355 struct gcpro gcpro1;
356 GCPRO1 (string);
357 for (i = 0; i < size; i++)
358 PRINTCHAR (XSTRING (string)->data[i]);
359 UNGCPRO;
363 DEFUN ("write-char", Fwrite_char, Swrite_char, 1, 2, 0,
364 "Output character CHARACTER to stream PRINTCHARFUN.\n\
365 PRINTCHARFUN defaults to the value of `standard-output' (which see).")
366 (character, printcharfun)
367 Lisp_Object character, printcharfun;
369 struct buffer *old = current_buffer;
370 int old_point = -1;
371 int start_point;
372 Lisp_Object original;
374 if (NILP (printcharfun))
375 printcharfun = Vstandard_output;
376 CHECK_NUMBER (character, 0);
377 PRINTPREPARE;
378 PRINTCHAR (XINT (character));
379 PRINTFINISH;
380 return character;
383 /* Used from outside of print.c to print a block of SIZE chars at DATA
384 on the default output stream.
385 Do not use this on the contents of a Lisp string. */
387 write_string (data, size)
388 char *data;
389 int size;
391 struct buffer *old = current_buffer;
392 Lisp_Object printcharfun;
393 int old_point = -1;
394 int start_point;
395 Lisp_Object original;
397 printcharfun = Vstandard_output;
399 PRINTPREPARE;
400 strout (data, size, printcharfun);
401 PRINTFINISH;
404 /* Used from outside of print.c to print a block of SIZE chars at DATA
405 on a specified stream PRINTCHARFUN.
406 Do not use this on the contents of a Lisp string. */
408 write_string_1 (data, size, printcharfun)
409 char *data;
410 int size;
411 Lisp_Object printcharfun;
413 struct buffer *old = current_buffer;
414 int old_point = -1;
415 int start_point;
416 Lisp_Object original;
418 PRINTPREPARE;
419 strout (data, size, printcharfun);
420 PRINTFINISH;
424 #ifndef standalone
426 void
427 temp_output_buffer_setup (bufname)
428 char *bufname;
430 register struct buffer *old = current_buffer;
431 register Lisp_Object buf;
433 Fset_buffer (Fget_buffer_create (build_string (bufname)));
435 current_buffer->directory = old->directory;
436 current_buffer->read_only = Qnil;
437 Ferase_buffer ();
439 XSETBUFFER (buf, current_buffer);
440 specbind (Qstandard_output, buf);
442 set_buffer_internal (old);
445 Lisp_Object
446 internal_with_output_to_temp_buffer (bufname, function, args)
447 char *bufname;
448 Lisp_Object (*function) ();
449 Lisp_Object args;
451 int count = specpdl_ptr - specpdl;
452 Lisp_Object buf, val;
453 struct gcpro gcpro1;
455 GCPRO1 (args);
456 record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
457 temp_output_buffer_setup (bufname);
458 buf = Vstandard_output;
459 UNGCPRO;
461 val = (*function) (args);
463 GCPRO1 (val);
464 temp_output_buffer_show (buf);
465 UNGCPRO;
467 return unbind_to (count, val);
470 DEFUN ("with-output-to-temp-buffer", Fwith_output_to_temp_buffer, Swith_output_to_temp_buffer,
471 1, UNEVALLED, 0,
472 "Bind `standard-output' to buffer BUFNAME, eval BODY, then show that buffer.\n\
473 The buffer is cleared out initially, and marked as unmodified when done.\n\
474 All output done by BODY is inserted in that buffer by default.\n\
475 The buffer is displayed in another window, but not selected.\n\
476 The value of the last form in BODY is returned.\n\
477 If BODY does not finish normally, the buffer BUFNAME is not displayed.\n\n\
478 If variable `temp-buffer-show-function' is non-nil, call it at the end\n\
479 to get the buffer displayed. It gets one argument, the buffer to display.")
480 (args)
481 Lisp_Object args;
483 struct gcpro gcpro1;
484 Lisp_Object name;
485 int count = specpdl_ptr - specpdl;
486 Lisp_Object buf, val;
488 GCPRO1(args);
489 name = Feval (Fcar (args));
490 UNGCPRO;
492 CHECK_STRING (name, 0);
493 temp_output_buffer_setup (XSTRING (name)->data);
494 buf = Vstandard_output;
496 val = Fprogn (Fcdr (args));
498 temp_output_buffer_show (buf);
500 return unbind_to (count, val);
502 #endif /* not standalone */
504 static void print ();
506 DEFUN ("terpri", Fterpri, Sterpri, 0, 1, 0,
507 "Output a newline to stream PRINTCHARFUN.\n\
508 If PRINTCHARFUN is omitted or nil, the value of `standard-output' is used.")
509 (printcharfun)
510 Lisp_Object printcharfun;
512 struct buffer *old = current_buffer;
513 int old_point = -1;
514 int start_point;
515 Lisp_Object original;
517 if (NILP (printcharfun))
518 printcharfun = Vstandard_output;
519 PRINTPREPARE;
520 PRINTCHAR ('\n');
521 PRINTFINISH;
522 return Qt;
525 DEFUN ("prin1", Fprin1, Sprin1, 1, 2, 0,
526 "Output the printed representation of OBJECT, any Lisp object.\n\
527 Quoting characters are printed when needed to make output that `read'\n\
528 can handle, whenever this is possible.\n\
529 Output stream is PRINTCHARFUN, or value of `standard-output' (which see).")
530 (object, printcharfun)
531 Lisp_Object object, printcharfun;
533 struct buffer *old = current_buffer;
534 int old_point = -1;
535 int start_point;
536 Lisp_Object original;
538 #ifdef MAX_PRINT_CHARS
539 max_print = 0;
540 #endif /* MAX_PRINT_CHARS */
541 if (NILP (printcharfun))
542 printcharfun = Vstandard_output;
543 PRINTPREPARE;
544 print_depth = 0;
545 print (object, printcharfun, 1);
546 PRINTFINISH;
547 return object;
550 /* a buffer which is used to hold output being built by prin1-to-string */
551 Lisp_Object Vprin1_to_string_buffer;
553 DEFUN ("prin1-to-string", Fprin1_to_string, Sprin1_to_string, 1, 2, 0,
554 "Return a string containing the printed representation of OBJECT,\n\
555 any Lisp object. Quoting characters are used when needed to make output\n\
556 that `read' can handle, whenever this is possible, unless the optional\n\
557 second argument NOESCAPE is non-nil.")
558 (object, noescape)
559 Lisp_Object object, noescape;
561 struct buffer *old = current_buffer;
562 int old_point = -1;
563 int start_point;
564 Lisp_Object original, printcharfun;
565 struct gcpro gcpro1, gcpro2;
566 Lisp_Object tem;
568 /* Save and restore this--we are altering a buffer
569 but we don't want to deactivate the mark just for that.
570 No need for specbind, since errors deactivate the mark. */
571 tem = Vdeactivate_mark;
572 GCPRO2 (object, tem);
574 printcharfun = Vprin1_to_string_buffer;
575 PRINTPREPARE;
576 print_depth = 0;
577 print (object, printcharfun, NILP (noescape));
578 /* Make Vprin1_to_string_buffer be the default buffer after PRINTFINSH */
579 PRINTFINISH;
580 set_buffer_internal (XBUFFER (Vprin1_to_string_buffer));
581 object = Fbuffer_string ();
583 Ferase_buffer ();
584 set_buffer_internal (old);
586 Vdeactivate_mark = tem;
587 UNGCPRO;
589 return object;
592 DEFUN ("princ", Fprinc, Sprinc, 1, 2, 0,
593 "Output the printed representation of OBJECT, any Lisp object.\n\
594 No quoting characters are used; no delimiters are printed around\n\
595 the contents of strings.\n\
596 Output stream is PRINTCHARFUN, or value of standard-output (which see).")
597 (object, printcharfun)
598 Lisp_Object object, printcharfun;
600 struct buffer *old = current_buffer;
601 int old_point = -1;
602 int start_point;
603 Lisp_Object original;
605 if (NILP (printcharfun))
606 printcharfun = Vstandard_output;
607 PRINTPREPARE;
608 print_depth = 0;
609 print (object, printcharfun, 0);
610 PRINTFINISH;
611 return object;
614 DEFUN ("print", Fprint, Sprint, 1, 2, 0,
615 "Output the printed representation of OBJECT, with newlines around it.\n\
616 Quoting characters are printed when needed to make output that `read'\n\
617 can handle, whenever this is possible.\n\
618 Output stream is PRINTCHARFUN, or value of `standard-output' (which see).")
619 (object, printcharfun)
620 Lisp_Object object, printcharfun;
622 struct buffer *old = current_buffer;
623 int old_point = -1;
624 int start_point;
625 Lisp_Object original;
626 struct gcpro gcpro1;
628 #ifdef MAX_PRINT_CHARS
629 print_chars = 0;
630 max_print = MAX_PRINT_CHARS;
631 #endif /* MAX_PRINT_CHARS */
632 if (NILP (printcharfun))
633 printcharfun = Vstandard_output;
634 GCPRO1 (object);
635 PRINTPREPARE;
636 print_depth = 0;
637 PRINTCHAR ('\n');
638 print (object, printcharfun, 1);
639 PRINTCHAR ('\n');
640 PRINTFINISH;
641 #ifdef MAX_PRINT_CHARS
642 max_print = 0;
643 print_chars = 0;
644 #endif /* MAX_PRINT_CHARS */
645 UNGCPRO;
646 return object;
649 /* The subroutine object for external-debugging-output is kept here
650 for the convenience of the debugger. */
651 Lisp_Object Qexternal_debugging_output;
653 DEFUN ("external-debugging-output", Fexternal_debugging_output, Sexternal_debugging_output, 1, 1, 0,
654 "Write CHARACTER to stderr.\n\
655 You can call print while debugging emacs, and pass it this function\n\
656 to make it write to the debugging output.\n")
657 (character)
658 Lisp_Object character;
660 CHECK_NUMBER (character, 0);
661 putc (XINT (character), stderr);
663 return character;
666 /* This is the interface for debugging printing. */
668 void
669 debug_print (arg)
670 Lisp_Object arg;
672 Fprin1 (arg, Qexternal_debugging_output);
673 fprintf (stderr, "\r\n");
676 DEFUN ("error-message-string", Ferror_message_string, Serror_message_string,
677 1, 1, 0,
678 "Convert an error value (ERROR-SYMBOL . DATA) to an error message.")
679 (obj)
680 Lisp_Object obj;
682 struct buffer *old = current_buffer;
683 Lisp_Object original, printcharfun, value;
684 struct gcpro gcpro1;
686 print_error_message (obj, Vprin1_to_string_buffer, NULL);
688 set_buffer_internal (XBUFFER (Vprin1_to_string_buffer));
689 value = Fbuffer_string ();
691 GCPRO1 (value);
692 Ferase_buffer ();
693 set_buffer_internal (old);
694 UNGCPRO;
696 return value;
699 /* Print an error message for the error DATA
700 onto Lisp output stream STREAM (suitable for the print functions). */
702 print_error_message (data, stream)
703 Lisp_Object data, stream;
705 Lisp_Object errname, errmsg, file_error, tail;
706 struct gcpro gcpro1;
707 int i;
709 errname = Fcar (data);
711 if (EQ (errname, Qerror))
713 data = Fcdr (data);
714 if (!CONSP (data)) data = Qnil;
715 errmsg = Fcar (data);
716 file_error = Qnil;
718 else
720 errmsg = Fget (errname, Qerror_message);
721 file_error = Fmemq (Qfile_error,
722 Fget (errname, Qerror_conditions));
725 /* Print an error message including the data items. */
727 tail = Fcdr_safe (data);
728 GCPRO1 (tail);
730 /* For file-error, make error message by concatenating
731 all the data items. They are all strings. */
732 if (!NILP (file_error) && !NILP (tail))
733 errmsg = XCONS (tail)->car, tail = XCONS (tail)->cdr;
735 if (STRINGP (errmsg))
736 Fprinc (errmsg, stream);
737 else
738 write_string_1 ("peculiar error", -1, stream);
740 for (i = 0; CONSP (tail); tail = Fcdr (tail), i++)
742 write_string_1 (i ? ", " : ": ", 2, stream);
743 if (!NILP (file_error))
744 Fprinc (Fcar (tail), stream);
745 else
746 Fprin1 (Fcar (tail), stream);
748 UNGCPRO;
751 #ifdef LISP_FLOAT_TYPE
754 * The buffer should be at least as large as the max string size of the
755 * largest float, printed in the biggest notation. This is undoubtedly
756 * 20d float_output_format, with the negative of the C-constant "HUGE"
757 * from <math.h>.
759 * On the vax the worst case is -1e38 in 20d format which takes 61 bytes.
761 * I assume that IEEE-754 format numbers can take 329 bytes for the worst
762 * case of -1e307 in 20d float_output_format. What is one to do (short of
763 * re-writing _doprnt to be more sane)?
764 * -wsr
767 void
768 float_to_string (buf, data)
769 unsigned char *buf;
770 double data;
772 unsigned char *cp;
773 int width;
775 if (NILP (Vfloat_output_format)
776 || !STRINGP (Vfloat_output_format))
777 lose:
779 sprintf (buf, "%.17g", data);
780 width = -1;
782 else /* oink oink */
784 /* Check that the spec we have is fully valid.
785 This means not only valid for printf,
786 but meant for floats, and reasonable. */
787 cp = XSTRING (Vfloat_output_format)->data;
789 if (cp[0] != '%')
790 goto lose;
791 if (cp[1] != '.')
792 goto lose;
794 cp += 2;
796 /* Check the width specification. */
797 width = -1;
798 if ('0' <= *cp && *cp <= '9')
800 width = 0;
802 width = (width * 10) + (*cp++ - '0');
803 while (*cp >= '0' && *cp <= '9');
805 /* A precision of zero is valid only for %f. */
806 if (width > DBL_DIG
807 || (width == 0 && *cp != 'f'))
808 goto lose;
811 if (*cp != 'e' && *cp != 'f' && *cp != 'g')
812 goto lose;
814 if (cp[1] != 0)
815 goto lose;
817 sprintf (buf, XSTRING (Vfloat_output_format)->data, data);
820 /* Make sure there is a decimal point with digit after, or an
821 exponent, so that the value is readable as a float. But don't do
822 this with "%.0f"; it's valid for that not to produce a decimal
823 point. Note that width can be 0 only for %.0f. */
824 if (width != 0)
826 for (cp = buf; *cp; cp++)
827 if ((*cp < '0' || *cp > '9') && *cp != '-')
828 break;
830 if (*cp == '.' && cp[1] == 0)
832 cp[1] = '0';
833 cp[2] = 0;
836 if (*cp == 0)
838 *cp++ = '.';
839 *cp++ = '0';
840 *cp++ = 0;
844 #endif /* LISP_FLOAT_TYPE */
846 static void
847 print (obj, printcharfun, escapeflag)
848 Lisp_Object obj;
849 register Lisp_Object printcharfun;
850 int escapeflag;
852 char buf[30];
854 QUIT;
856 #if 1 /* I'm not sure this is really worth doing. */
857 /* Detect circularities and truncate them.
858 No need to offer any alternative--this is better than an error. */
859 if (CONSP (obj) || VECTORP (obj) || COMPILEDP (obj))
861 int i;
862 for (i = 0; i < print_depth; i++)
863 if (EQ (obj, being_printed[i]))
865 sprintf (buf, "#%d", i);
866 strout (buf, -1, printcharfun);
867 return;
870 #endif
872 being_printed[print_depth] = obj;
873 print_depth++;
875 if (print_depth > PRINT_CIRCLE)
876 error ("Apparently circular structure being printed");
877 #ifdef MAX_PRINT_CHARS
878 if (max_print && print_chars > max_print)
880 PRINTCHAR ('\n');
881 print_chars = 0;
883 #endif /* MAX_PRINT_CHARS */
885 switch (XGCTYPE (obj))
887 case Lisp_Int:
888 if (sizeof (int) == sizeof (EMACS_INT))
889 sprintf (buf, "%d", XINT (obj));
890 else if (sizeof (long) == sizeof (EMACS_INT))
891 sprintf (buf, "%ld", XINT (obj));
892 else
893 abort ();
894 strout (buf, -1, printcharfun);
895 break;
897 #ifdef LISP_FLOAT_TYPE
898 case Lisp_Float:
900 char pigbuf[350]; /* see comments in float_to_string */
902 float_to_string (pigbuf, XFLOAT(obj)->data);
903 strout (pigbuf, -1, printcharfun);
905 break;
906 #endif
908 case Lisp_String:
909 if (!escapeflag)
910 print_string (obj, printcharfun);
911 else
913 register int i;
914 register unsigned char c;
915 struct gcpro gcpro1;
917 GCPRO1 (obj);
919 #ifdef USE_TEXT_PROPERTIES
920 if (!NULL_INTERVAL_P (XSTRING (obj)->intervals))
922 PRINTCHAR ('#');
923 PRINTCHAR ('(');
925 #endif
927 PRINTCHAR ('\"');
928 for (i = 0; i < XSTRING (obj)->size; i++)
930 QUIT;
931 c = XSTRING (obj)->data[i];
932 if (c == '\n' && print_escape_newlines)
934 PRINTCHAR ('\\');
935 PRINTCHAR ('n');
937 else if (c == '\f' && print_escape_newlines)
939 PRINTCHAR ('\\');
940 PRINTCHAR ('f');
942 else
944 if (c == '\"' || c == '\\')
945 PRINTCHAR ('\\');
946 PRINTCHAR (c);
949 PRINTCHAR ('\"');
951 #ifdef USE_TEXT_PROPERTIES
952 if (!NULL_INTERVAL_P (XSTRING (obj)->intervals))
954 traverse_intervals (XSTRING (obj)->intervals,
955 0, 0, print_interval, printcharfun);
956 PRINTCHAR (')');
958 #endif
960 UNGCPRO;
962 break;
964 case Lisp_Symbol:
966 register int confusing;
967 register unsigned char *p = XSYMBOL (obj)->name->data;
968 register unsigned char *end = p + XSYMBOL (obj)->name->size;
969 register unsigned char c;
971 if (p != end && (*p == '-' || *p == '+')) p++;
972 if (p == end)
973 confusing = 0;
974 else
976 while (p != end && *p >= '0' && *p <= '9')
977 p++;
978 confusing = (end == p);
981 p = XSYMBOL (obj)->name->data;
982 while (p != end)
984 QUIT;
985 c = *p++;
986 if (escapeflag)
988 if (c == '\"' || c == '\\' || c == '\'' || c == ';' || c == '#' ||
989 c == '(' || c == ')' || c == ',' || c =='.' || c == '`' ||
990 c == '[' || c == ']' || c == '?' || c <= 040 || confusing)
991 PRINTCHAR ('\\'), confusing = 0;
993 PRINTCHAR (c);
996 break;
998 case Lisp_Cons:
999 /* If deeper than spec'd depth, print placeholder. */
1000 if (INTEGERP (Vprint_level)
1001 && print_depth > XINT (Vprint_level))
1002 strout ("...", -1, printcharfun);
1003 else if (print_quoted && CONSP (XCDR (obj)) && NILP (XCDR (XCDR (obj)))
1004 && (EQ (XCAR (obj), Qquote)))
1006 PRINTCHAR ('\'');
1007 print (XCAR (XCDR (obj)), printcharfun, escapeflag);
1009 else if (print_quoted && CONSP (XCDR (obj)) && NILP (XCDR (XCDR (obj)))
1010 && (EQ (XCAR (obj), Qfunction)))
1012 PRINTCHAR ('#');
1013 PRINTCHAR ('\'');
1014 print (XCAR (XCDR (obj)), printcharfun, escapeflag);
1016 else if (print_quoted && CONSP (XCDR (obj)) && NILP (XCDR (XCDR (obj)))
1017 && ((EQ (XCAR (obj), Qbackquote)
1018 || EQ (XCAR (obj), Qcomma)
1019 || EQ (XCAR (obj), Qcomma_at)
1020 || EQ (XCAR (obj), Qcomma_dot))))
1022 print (XCAR (obj), printcharfun, 0);
1023 print (XCAR (XCDR (obj)), printcharfun, escapeflag);
1025 else
1027 PRINTCHAR ('(');
1029 register int i = 0;
1030 register int max = 0;
1032 if (INTEGERP (Vprint_length))
1033 max = XINT (Vprint_length);
1034 /* Could recognize circularities in cdrs here,
1035 but that would make printing of long lists quadratic.
1036 It's not worth doing. */
1037 while (CONSP (obj))
1039 if (i++)
1040 PRINTCHAR (' ');
1041 if (max && i > max)
1043 strout ("...", 3, printcharfun);
1044 break;
1046 print (XCAR (obj), printcharfun, escapeflag);
1047 obj = XCDR (obj);
1050 if (!NILP (obj))
1052 strout (" . ", 3, printcharfun);
1053 print (obj, printcharfun, escapeflag);
1055 PRINTCHAR (')');
1057 break;
1059 case Lisp_Vectorlike:
1060 if (PROCESSP (obj))
1062 if (escapeflag)
1064 strout ("#<process ", -1, printcharfun);
1065 print_string (XPROCESS (obj)->name, printcharfun);
1066 PRINTCHAR ('>');
1068 else
1069 print_string (XPROCESS (obj)->name, printcharfun);
1071 else if (BOOL_VECTOR_P (obj))
1073 register int i;
1074 register unsigned char c;
1075 struct gcpro gcpro1;
1076 int size_in_chars
1077 = (XBOOL_VECTOR (obj)->size + BITS_PER_CHAR) / BITS_PER_CHAR;
1079 GCPRO1 (obj);
1081 PRINTCHAR ('#');
1082 PRINTCHAR ('&');
1083 sprintf (buf, "%d", XBOOL_VECTOR (obj)->size);
1084 strout (buf, -1, printcharfun);
1085 PRINTCHAR ('\"');
1087 /* Don't print more characters than the specified maximum. */
1088 if (INTEGERP (Vprint_length)
1089 && XINT (Vprint_length) < size_in_chars)
1090 size_in_chars = XINT (Vprint_length);
1092 for (i = 0; i < size_in_chars; i++)
1094 QUIT;
1095 c = XBOOL_VECTOR (obj)->data[i];
1096 if (c == '\n' && print_escape_newlines)
1098 PRINTCHAR ('\\');
1099 PRINTCHAR ('n');
1101 else if (c == '\f' && print_escape_newlines)
1103 PRINTCHAR ('\\');
1104 PRINTCHAR ('f');
1106 else
1108 if (c == '\"' || c == '\\')
1109 PRINTCHAR ('\\');
1110 PRINTCHAR (c);
1113 PRINTCHAR ('\"');
1115 UNGCPRO;
1117 else if (SUBRP (obj))
1119 strout ("#<subr ", -1, printcharfun);
1120 strout (XSUBR (obj)->symbol_name, -1, printcharfun);
1121 PRINTCHAR ('>');
1123 #ifndef standalone
1124 else if (WINDOWP (obj))
1126 strout ("#<window ", -1, printcharfun);
1127 sprintf (buf, "%d", XFASTINT (XWINDOW (obj)->sequence_number));
1128 strout (buf, -1, printcharfun);
1129 if (!NILP (XWINDOW (obj)->buffer))
1131 strout (" on ", -1, printcharfun);
1132 print_string (XBUFFER (XWINDOW (obj)->buffer)->name, printcharfun);
1134 PRINTCHAR ('>');
1136 else if (BUFFERP (obj))
1138 if (NILP (XBUFFER (obj)->name))
1139 strout ("#<killed buffer>", -1, printcharfun);
1140 else if (escapeflag)
1142 strout ("#<buffer ", -1, printcharfun);
1143 print_string (XBUFFER (obj)->name, printcharfun);
1144 PRINTCHAR ('>');
1146 else
1147 print_string (XBUFFER (obj)->name, printcharfun);
1149 else if (WINDOW_CONFIGURATIONP (obj))
1151 strout ("#<window-configuration>", -1, printcharfun);
1153 #ifdef MULTI_FRAME
1154 else if (FRAMEP (obj))
1156 strout ((FRAME_LIVE_P (XFRAME (obj))
1157 ? "#<frame " : "#<dead frame "),
1158 -1, printcharfun);
1159 print_string (XFRAME (obj)->name, printcharfun);
1160 sprintf (buf, " 0x%lx", (unsigned long) (XFRAME (obj)));
1161 strout (buf, -1, printcharfun);
1162 PRINTCHAR ('>');
1164 #endif
1165 #endif /* not standalone */
1166 else
1168 int size = XVECTOR (obj)->size;
1169 if (COMPILEDP (obj))
1171 PRINTCHAR ('#');
1172 size &= PSEUDOVECTOR_SIZE_MASK;
1174 if (CHAR_TABLE_P (obj))
1176 /* We print a char-table as if it were a vector,
1177 lumping the parent and default slots in with the
1178 character slots. But we add #^ as a prefix. */
1179 PRINTCHAR ('#');
1180 PRINTCHAR ('^');
1181 size &= PSEUDOVECTOR_SIZE_MASK;
1183 if (size & PSEUDOVECTOR_FLAG)
1184 goto badtype;
1186 PRINTCHAR ('[');
1188 register int i;
1189 register Lisp_Object tem;
1191 /* Don't print more elements than the specified maximum. */
1192 if (INTEGERP (Vprint_length)
1193 && XINT (Vprint_length) < size)
1194 size = XINT (Vprint_length);
1196 for (i = 0; i < size; i++)
1198 if (i) PRINTCHAR (' ');
1199 tem = XVECTOR (obj)->contents[i];
1200 print (tem, printcharfun, escapeflag);
1203 PRINTCHAR (']');
1205 break;
1207 #ifndef standalone
1208 case Lisp_Misc:
1209 switch (XMISCTYPE (obj))
1211 case Lisp_Misc_Marker:
1212 strout ("#<marker ", -1, printcharfun);
1213 if (!(XMARKER (obj)->buffer))
1214 strout ("in no buffer", -1, printcharfun);
1215 else
1217 sprintf (buf, "at %d", marker_position (obj));
1218 strout (buf, -1, printcharfun);
1219 strout (" in ", -1, printcharfun);
1220 print_string (XMARKER (obj)->buffer->name, printcharfun);
1222 PRINTCHAR ('>');
1223 break;
1225 case Lisp_Misc_Overlay:
1226 strout ("#<overlay ", -1, printcharfun);
1227 if (!(XMARKER (OVERLAY_START (obj))->buffer))
1228 strout ("in no buffer", -1, printcharfun);
1229 else
1231 sprintf (buf, "from %d to %d in ",
1232 marker_position (OVERLAY_START (obj)),
1233 marker_position (OVERLAY_END (obj)));
1234 strout (buf, -1, printcharfun);
1235 print_string (XMARKER (OVERLAY_START (obj))->buffer->name,
1236 printcharfun);
1238 PRINTCHAR ('>');
1239 break;
1241 /* Remaining cases shouldn't happen in normal usage, but let's print
1242 them anyway for the benefit of the debugger. */
1243 case Lisp_Misc_Free:
1244 strout ("#<misc free cell>", -1, printcharfun);
1245 break;
1247 case Lisp_Misc_Intfwd:
1248 sprintf (buf, "#<intfwd to %d>", *XINTFWD (obj)->intvar);
1249 strout (buf, -1, printcharfun);
1250 break;
1252 case Lisp_Misc_Boolfwd:
1253 sprintf (buf, "#<boolfwd to %s>",
1254 (*XBOOLFWD (obj)->boolvar ? "t" : "nil"));
1255 strout (buf, -1, printcharfun);
1256 break;
1258 case Lisp_Misc_Objfwd:
1259 strout ("#<objfwd to ", -1, printcharfun);
1260 print (*XOBJFWD (obj)->objvar, printcharfun, escapeflag);
1261 PRINTCHAR ('>');
1262 break;
1264 case Lisp_Misc_Buffer_Objfwd:
1265 strout ("#<buffer_objfwd to ", -1, printcharfun);
1266 print (*(Lisp_Object *)((char *)current_buffer
1267 + XBUFFER_OBJFWD (obj)->offset),
1268 printcharfun, escapeflag);
1269 PRINTCHAR ('>');
1270 break;
1272 case Lisp_Misc_Kboard_Objfwd:
1273 strout ("#<kboard_objfwd to ", -1, printcharfun);
1274 print (*(Lisp_Object *)((char *) current_kboard
1275 + XKBOARD_OBJFWD (obj)->offset),
1276 printcharfun, escapeflag);
1277 PRINTCHAR ('>');
1278 break;
1280 case Lisp_Misc_Buffer_Local_Value:
1281 strout ("#<buffer_local_value ", -1, printcharfun);
1282 goto do_buffer_local;
1283 case Lisp_Misc_Some_Buffer_Local_Value:
1284 strout ("#<some_buffer_local_value ", -1, printcharfun);
1285 do_buffer_local:
1286 strout ("[realvalue] ", -1, printcharfun);
1287 print (XBUFFER_LOCAL_VALUE (obj)->car, printcharfun, escapeflag);
1288 strout ("[buffer] ", -1, printcharfun);
1289 print (XCONS (XBUFFER_LOCAL_VALUE (obj)->cdr)->car,
1290 printcharfun, escapeflag);
1291 strout ("[alist-elt] ", -1, printcharfun);
1292 print (XCONS (XCONS (XBUFFER_LOCAL_VALUE (obj)->cdr)->cdr)->car,
1293 printcharfun, escapeflag);
1294 strout ("[default-value] ", -1, printcharfun);
1295 print (XCONS (XCONS (XBUFFER_LOCAL_VALUE (obj)->cdr)->cdr)->cdr,
1296 printcharfun, escapeflag);
1297 PRINTCHAR ('>');
1298 break;
1300 default:
1301 goto badtype;
1303 break;
1304 #endif /* standalone */
1306 default:
1307 badtype:
1309 /* We're in trouble if this happens!
1310 Probably should just abort () */
1311 strout ("#<EMACS BUG: INVALID DATATYPE ", -1, printcharfun);
1312 if (MISCP (obj))
1313 sprintf (buf, "(MISC 0x%04x)", (int) XMISCTYPE (obj));
1314 else if (VECTORLIKEP (obj))
1315 sprintf (buf, "(PVEC 0x%08x)", (int) XVECTOR (obj)->size);
1316 else
1317 sprintf (buf, "(0x%02x)", (int) XTYPE (obj));
1318 strout (buf, -1, printcharfun);
1319 strout (" Save your buffers immediately and please report this bug>",
1320 -1, printcharfun);
1324 print_depth--;
1327 #ifdef USE_TEXT_PROPERTIES
1329 /* Print a description of INTERVAL using PRINTCHARFUN.
1330 This is part of printing a string that has text properties. */
1332 void
1333 print_interval (interval, printcharfun)
1334 INTERVAL interval;
1335 Lisp_Object printcharfun;
1337 PRINTCHAR (' ');
1338 print (make_number (interval->position), printcharfun, 1);
1339 PRINTCHAR (' ');
1340 print (make_number (interval->position + LENGTH (interval)),
1341 printcharfun, 1);
1342 PRINTCHAR (' ');
1343 print (interval->plist, printcharfun, 1);
1346 #endif /* USE_TEXT_PROPERTIES */
1348 void
1349 syms_of_print ()
1351 DEFVAR_LISP ("standard-output", &Vstandard_output,
1352 "Output stream `print' uses by default for outputting a character.\n\
1353 This may be any function of one argument.\n\
1354 It may also be a buffer (output is inserted before point)\n\
1355 or a marker (output is inserted and the marker is advanced)\n\
1356 or the symbol t (output appears in the echo area).");
1357 Vstandard_output = Qt;
1358 Qstandard_output = intern ("standard-output");
1359 staticpro (&Qstandard_output);
1361 #ifdef LISP_FLOAT_TYPE
1362 DEFVAR_LISP ("float-output-format", &Vfloat_output_format,
1363 "The format descriptor string used to print floats.\n\
1364 This is a %-spec like those accepted by `printf' in C,\n\
1365 but with some restrictions. It must start with the two characters `%.'.\n\
1366 After that comes an integer precision specification,\n\
1367 and then a letter which controls the format.\n\
1368 The letters allowed are `e', `f' and `g'.\n\
1369 Use `e' for exponential notation \"DIG.DIGITSeEXPT\"\n\
1370 Use `f' for decimal point notation \"DIGITS.DIGITS\".\n\
1371 Use `g' to choose the shorter of those two formats for the number at hand.\n\
1372 The precision in any of these cases is the number of digits following\n\
1373 the decimal point. With `f', a precision of 0 means to omit the\n\
1374 decimal point. 0 is not allowed with `e' or `g'.\n\n\
1375 A value of nil means to use `%.17g'.");
1376 Vfloat_output_format = Qnil;
1377 Qfloat_output_format = intern ("float-output-format");
1378 staticpro (&Qfloat_output_format);
1379 #endif /* LISP_FLOAT_TYPE */
1381 DEFVAR_LISP ("print-length", &Vprint_length,
1382 "Maximum length of list to print before abbreviating.\n\
1383 A value of nil means no limit.");
1384 Vprint_length = Qnil;
1386 DEFVAR_LISP ("print-level", &Vprint_level,
1387 "Maximum depth of list nesting to print before abbreviating.\n\
1388 A value of nil means no limit.");
1389 Vprint_level = Qnil;
1391 DEFVAR_BOOL ("print-escape-newlines", &print_escape_newlines,
1392 "Non-nil means print newlines in strings as backslash-n.\n\
1393 Also print formfeeds as backslash-f.");
1394 print_escape_newlines = 0;
1396 DEFVAR_BOOL ("print-quoted", &print_quoted,
1397 "Non-nil means print quoted forms with reader syntax.\n\
1398 I.e., (quote foo) prints as 'foo, (function foo) as #'foo, and, backquoted\n\
1399 forms print in the new syntax.");
1400 print_quoted = 0;
1402 /* prin1_to_string_buffer initialized in init_buffer_once in buffer.c */
1403 staticpro (&Vprin1_to_string_buffer);
1405 defsubr (&Sprin1);
1406 defsubr (&Sprin1_to_string);
1407 defsubr (&Serror_message_string);
1408 defsubr (&Sprinc);
1409 defsubr (&Sprint);
1410 defsubr (&Sterpri);
1411 defsubr (&Swrite_char);
1412 defsubr (&Sexternal_debugging_output);
1414 Qexternal_debugging_output = intern ("external-debugging-output");
1415 staticpro (&Qexternal_debugging_output);
1417 Qprint_escape_newlines = intern ("print-escape-newlines");
1418 staticpro (&Qprint_escape_newlines);
1420 Qprint_quoted = intern ("print-quoted");
1421 staticpro (&Qprint_quoted);
1423 #ifndef standalone
1424 defsubr (&Swith_output_to_temp_buffer);
1425 #endif /* not standalone */