]> code.delx.au - gnu-emacs/blob - src/print.c
(Fmake_marker): Initialize marker's bytepos and charpos.
[gnu-emacs] / src / print.c
1 /* Lisp object printing and output streams.
2 Copyright (C) 1985, 86, 88, 93, 94, 95, 97 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 "charset.h"
29 #include "frame.h"
30 #include "window.h"
31 #include "process.h"
32 #include "dispextern.h"
33 #include "termchar.h"
34 #include "keyboard.h"
35 #endif /* not standalone */
36
37 #ifdef USE_TEXT_PROPERTIES
38 #include "intervals.h"
39 #endif
40
41 Lisp_Object Vstandard_output, Qstandard_output;
42
43 /* These are used to print like we read. */
44 extern Lisp_Object Qbackquote, Qcomma, Qcomma_at, Qcomma_dot, Qfunction;
45
46 #ifdef LISP_FLOAT_TYPE
47 Lisp_Object Vfloat_output_format, Qfloat_output_format;
48
49 /* Work around a problem that happens because math.h on hpux 7
50 defines two static variables--which, in Emacs, are not really static,
51 because `static' is defined as nothing. The problem is that they are
52 defined both here and in lread.c.
53 These macros prevent the name conflict. */
54 #if defined (HPUX) && !defined (HPUX8)
55 #define _MAXLDBL print_maxldbl
56 #define _NMAXLDBL print_nmaxldbl
57 #endif
58
59 #include <math.h>
60
61 #if STDC_HEADERS
62 #include <float.h>
63 #include <stdlib.h>
64 #endif
65
66 /* Default to values appropriate for IEEE floating point. */
67 #ifndef FLT_RADIX
68 #define FLT_RADIX 2
69 #endif
70 #ifndef DBL_MANT_DIG
71 #define DBL_MANT_DIG 53
72 #endif
73 #ifndef DBL_DIG
74 #define DBL_DIG 15
75 #endif
76 #ifndef DBL_MIN
77 #define DBL_MIN 2.2250738585072014e-308
78 #endif
79
80 #ifdef DBL_MIN_REPLACEMENT
81 #undef DBL_MIN
82 #define DBL_MIN DBL_MIN_REPLACEMENT
83 #endif
84
85 /* Define DOUBLE_DIGITS_BOUND, an upper bound on the number of decimal digits
86 needed to express a float without losing information.
87 The general-case formula is valid for the usual case, IEEE floating point,
88 but many compilers can't optimize the formula to an integer constant,
89 so make a special case for it. */
90 #if FLT_RADIX == 2 && DBL_MANT_DIG == 53
91 #define DOUBLE_DIGITS_BOUND 17 /* IEEE floating point */
92 #else
93 #define DOUBLE_DIGITS_BOUND ((int) ceil (log10 (pow (FLT_RADIX, DBL_MANT_DIG))))
94 #endif
95
96 #endif /* LISP_FLOAT_TYPE */
97
98 /* Avoid actual stack overflow in print. */
99 int print_depth;
100
101 /* Detect most circularities to print finite output. */
102 #define PRINT_CIRCLE 200
103 Lisp_Object being_printed[PRINT_CIRCLE];
104
105 /* When printing into a buffer, first we put the text in this
106 block, then insert it all at once. */
107 char *print_buffer;
108
109 /* Size allocated in print_buffer. */
110 int print_buffer_size;
111 /* Size used in print_buffer. */
112 int print_buffer_pos;
113
114 /* Maximum length of list to print in full; noninteger means
115 effectively infinity */
116
117 Lisp_Object Vprint_length;
118
119 /* Maximum depth of list to print in full; noninteger means
120 effectively infinity. */
121
122 Lisp_Object Vprint_level;
123
124 /* Nonzero means print newlines in strings as \n. */
125
126 int print_escape_newlines;
127
128 Lisp_Object Qprint_escape_newlines;
129
130 /* Nonzero means print (quote foo) forms as 'foo, etc. */
131
132 int print_quoted;
133
134 /* Non-nil means print #: before uninterned symbols.
135 Neither t nor nil means so that and don't clear Vprint_gensym_alist
136 on entry to and exit from print functions. */
137
138 Lisp_Object Vprint_gensym;
139
140 /* Association list of certain objects that are `eq' in the form being
141 printed and which should be `eq' when read back in, using the #n=object
142 and #n# reader forms. Each element has the form (object . n). */
143
144 Lisp_Object Vprint_gensym_alist;
145
146 /* Nonzero means print newline to stdout before next minibuffer message.
147 Defined in xdisp.c */
148
149 extern int noninteractive_need_newline;
150
151 extern int minibuffer_auto_raise;
152
153 #ifdef MAX_PRINT_CHARS
154 static int print_chars;
155 static int max_print;
156 #endif /* MAX_PRINT_CHARS */
157
158 void print_interval ();
159 \f
160 #if 0
161 /* Convert between chars and GLYPHs */
162
163 int
164 glyphlen (glyphs)
165 register GLYPH *glyphs;
166 {
167 register int i = 0;
168
169 while (glyphs[i])
170 i++;
171 return i;
172 }
173
174 void
175 str_to_glyph_cpy (str, glyphs)
176 char *str;
177 GLYPH *glyphs;
178 {
179 register GLYPH *gp = glyphs;
180 register char *cp = str;
181
182 while (*cp)
183 *gp++ = *cp++;
184 }
185
186 void
187 str_to_glyph_ncpy (str, glyphs, n)
188 char *str;
189 GLYPH *glyphs;
190 register int n;
191 {
192 register GLYPH *gp = glyphs;
193 register char *cp = str;
194
195 while (n-- > 0)
196 *gp++ = *cp++;
197 }
198
199 void
200 glyph_to_str_cpy (glyphs, str)
201 GLYPH *glyphs;
202 char *str;
203 {
204 register GLYPH *gp = glyphs;
205 register char *cp = str;
206
207 while (*gp)
208 *str++ = *gp++ & 0377;
209 }
210 #endif
211 \f
212 /* Low level output routines for characters and strings */
213
214 /* Lisp functions to do output using a stream
215 must have the stream in a variable called printcharfun
216 and must start with PRINTPREPARE, end with PRINTFINISH,
217 and use PRINTDECLARE to declare common variables.
218 Use PRINTCHAR to output one character,
219 or call strout to output a block of characters.
220 */
221
222 #define PRINTDECLARE \
223 struct buffer *old = current_buffer; \
224 int old_point = -1, start_point; \
225 int old_point_byte, start_point_byte; \
226 int specpdl_count = specpdl_ptr - specpdl; \
227 int free_print_buffer = 0; \
228 Lisp_Object original
229
230 #define PRINTPREPARE \
231 original = printcharfun; \
232 if (NILP (printcharfun)) printcharfun = Qt; \
233 if (BUFFERP (printcharfun)) \
234 { \
235 if (XBUFFER (printcharfun) != current_buffer) \
236 Fset_buffer (printcharfun); \
237 printcharfun = Qnil; \
238 } \
239 if (MARKERP (printcharfun)) \
240 { \
241 if (!(XMARKER (original)->buffer)) \
242 error ("Marker does not point anywhere"); \
243 if (XMARKER (original)->buffer != current_buffer) \
244 set_buffer_internal (XMARKER (original)->buffer); \
245 old_point = PT; \
246 old_point_byte = PT_BYTE; \
247 SET_PT_BOTH (marker_position (printcharfun), \
248 marker_byte_position (printcharfun)); \
249 start_point = PT; \
250 start_point_byte = PT_BYTE; \
251 printcharfun = Qnil; \
252 } \
253 if (NILP (printcharfun)) \
254 { \
255 if (print_buffer != 0) \
256 record_unwind_protect (print_unwind, \
257 make_string (print_buffer, \
258 print_buffer_pos)); \
259 else \
260 { \
261 print_buffer_size = 1000; \
262 print_buffer = (char *) xmalloc (print_buffer_size); \
263 free_print_buffer = 1; \
264 } \
265 print_buffer_pos = 0; \
266 } \
267 if (!CONSP (Vprint_gensym)) \
268 Vprint_gensym_alist = Qnil
269
270 #define PRINTFINISH \
271 if (NILP (printcharfun)) \
272 insert (print_buffer, print_buffer_pos); \
273 if (free_print_buffer) \
274 { \
275 xfree (print_buffer); \
276 print_buffer = 0; \
277 } \
278 unbind_to (specpdl_count, Qnil); \
279 if (MARKERP (original)) \
280 set_marker_both (original, Qnil, PT, PT_BYTE); \
281 if (old_point >= 0) \
282 SET_PT_BOTH (old_point + (old_point >= start_point \
283 ? PT - start_point : 0), \
284 old_point_byte + (old_point_byte >= start_point_byte \
285 ? PT_BYTE - start_point_byte : 0)); \
286 if (old != current_buffer) \
287 set_buffer_internal (old); \
288 if (!CONSP (Vprint_gensym)) \
289 Vprint_gensym_alist = Qnil
290
291 #define PRINTCHAR(ch) printchar (ch, printcharfun)
292
293 /* Nonzero if there is no room to print any more characters
294 so print might as well return right away. */
295
296 #define PRINTFULLP() \
297 (EQ (printcharfun, Qt) && !noninteractive \
298 && printbufidx >= FRAME_WIDTH (XFRAME (WINDOW_FRAME (XWINDOW (minibuf_window)))))
299
300 /* This is used to restore the saved contents of print_buffer
301 when there is a recursive call to print. */
302 static Lisp_Object
303 print_unwind (saved_text)
304 Lisp_Object saved_text;
305 {
306 bcopy (XSTRING (saved_text)->data, print_buffer, XSTRING (saved_text)->size);
307 }
308
309 /* Index of first unused element of FRAME_MESSAGE_BUF (mini_frame). */
310 static int printbufidx;
311
312 static void
313 printchar (ch, fun)
314 unsigned int ch;
315 Lisp_Object fun;
316 {
317 Lisp_Object ch1;
318
319 #ifdef MAX_PRINT_CHARS
320 if (max_print)
321 print_chars++;
322 #endif /* MAX_PRINT_CHARS */
323 #ifndef standalone
324 if (EQ (fun, Qnil))
325 {
326 int len;
327 unsigned char work[4], *str;
328
329 QUIT;
330 len = CHAR_STRING (ch, work, str);
331 if (print_buffer_pos + len >= print_buffer_size)
332 print_buffer = (char *) xrealloc (print_buffer,
333 print_buffer_size *= 2);
334 bcopy (str, print_buffer + print_buffer_pos, len);
335 print_buffer_pos += len;
336 return;
337 }
338
339 if (EQ (fun, Qt))
340 {
341 FRAME_PTR mini_frame
342 = XFRAME (WINDOW_FRAME (XWINDOW (minibuf_window)));
343 unsigned char work[4], *str;
344 int len = CHAR_STRING (ch, work, str);
345
346 QUIT;
347
348 if (noninteractive)
349 {
350 while (len--)
351 putchar (*str), str++;
352 noninteractive_need_newline = 1;
353 return;
354 }
355
356 if (echo_area_glyphs != FRAME_MESSAGE_BUF (mini_frame)
357 || !message_buf_print)
358 {
359 message_log_maybe_newline ();
360 echo_area_glyphs = FRAME_MESSAGE_BUF (mini_frame);
361 printbufidx = 0;
362 echo_area_glyphs_length = 0;
363 message_buf_print = 1;
364
365 if (minibuffer_auto_raise)
366 {
367 Lisp_Object mini_window;
368
369 /* Get the frame containing the minibuffer
370 that the selected frame is using. */
371 mini_window = FRAME_MINIBUF_WINDOW (selected_frame);
372
373 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
374 }
375 }
376
377 message_dolog (str, len, 0);
378 if (printbufidx < FRAME_MESSAGE_BUF_SIZE (mini_frame) - len)
379 bcopy (str, &FRAME_MESSAGE_BUF (mini_frame)[printbufidx], len),
380 printbufidx += len;
381 FRAME_MESSAGE_BUF (mini_frame)[printbufidx] = 0;
382 echo_area_glyphs_length = printbufidx;
383
384 return;
385 }
386 #endif /* not standalone */
387
388 XSETFASTINT (ch1, ch);
389 call1 (fun, ch1);
390 }
391
392 static void
393 strout (ptr, size, printcharfun)
394 char *ptr;
395 int size;
396 Lisp_Object printcharfun;
397 {
398 int i = 0;
399
400 if (size < 0)
401 size = strlen (ptr);
402
403 if (EQ (printcharfun, Qnil))
404 {
405 if (print_buffer_pos + size > print_buffer_size)
406 {
407 print_buffer_size = print_buffer_size * 2 + size;
408 print_buffer = (char *) xrealloc (print_buffer,
409 print_buffer_size);
410 }
411 bcopy (ptr, print_buffer + print_buffer_pos, size);
412 print_buffer_pos += size;
413
414 #ifdef MAX_PRINT_CHARS
415 if (max_print)
416 print_chars += size;
417 #endif /* MAX_PRINT_CHARS */
418 return;
419 }
420 if (EQ (printcharfun, Qt))
421 {
422 FRAME_PTR mini_frame
423 = XFRAME (WINDOW_FRAME (XWINDOW (minibuf_window)));
424
425 QUIT;
426
427 #ifdef MAX_PRINT_CHARS
428 if (max_print)
429 print_chars += size;
430 #endif /* MAX_PRINT_CHARS */
431
432 if (noninteractive)
433 {
434 fwrite (ptr, 1, size, stdout);
435 noninteractive_need_newline = 1;
436 return;
437 }
438
439 if (echo_area_glyphs != FRAME_MESSAGE_BUF (mini_frame)
440 || !message_buf_print)
441 {
442 message_log_maybe_newline ();
443 echo_area_glyphs = FRAME_MESSAGE_BUF (mini_frame);
444 printbufidx = 0;
445 echo_area_glyphs_length = 0;
446 message_buf_print = 1;
447
448 if (minibuffer_auto_raise)
449 {
450 Lisp_Object mini_window;
451
452 /* Get the frame containing the minibuffer
453 that the selected frame is using. */
454 mini_window = FRAME_MINIBUF_WINDOW (selected_frame);
455
456 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
457 }
458 }
459
460 message_dolog (ptr, size, 0);
461 if (size > FRAME_MESSAGE_BUF_SIZE (mini_frame) - printbufidx - 1)
462 {
463 size = FRAME_MESSAGE_BUF_SIZE (mini_frame) - printbufidx - 1;
464 /* Rewind incomplete multi-byte form. */
465 while (size && (unsigned char) ptr[size] >= 0xA0) size--;
466 }
467 bcopy (ptr, &FRAME_MESSAGE_BUF (mini_frame) [printbufidx], size);
468 printbufidx += size;
469 echo_area_glyphs_length = printbufidx;
470 FRAME_MESSAGE_BUF (mini_frame) [printbufidx] = 0;
471
472 return;
473 }
474
475 i = 0;
476 while (i < size)
477 {
478 /* Here, we must convert each multi-byte form to the
479 corresponding character code before handing it to PRINTCHAR. */
480 int len;
481 int ch = STRING_CHAR_AND_LENGTH (ptr + i, size - i, len);
482
483 PRINTCHAR (ch);
484 i += len;
485 }
486 }
487
488 /* Print the contents of a string STRING using PRINTCHARFUN.
489 It isn't safe to use strout in many cases,
490 because printing one char can relocate. */
491
492 void
493 print_string (string, printcharfun)
494 Lisp_Object string;
495 Lisp_Object printcharfun;
496 {
497 if (EQ (printcharfun, Qt) || NILP (printcharfun))
498 /* strout is safe for output to a frame (echo area) or to print_buffer. */
499 strout (XSTRING (string)->data, XSTRING (string)->size, printcharfun);
500 else
501 {
502 /* Otherwise, fetch the string address for each character. */
503 int i;
504 int size = XSTRING (string)->size;
505 struct gcpro gcpro1;
506 GCPRO1 (string);
507 for (i = 0; i < size; i++)
508 PRINTCHAR (XSTRING (string)->data[i]);
509 UNGCPRO;
510 }
511 }
512 \f
513 DEFUN ("write-char", Fwrite_char, Swrite_char, 1, 2, 0,
514 "Output character CHARACTER to stream PRINTCHARFUN.\n\
515 PRINTCHARFUN defaults to the value of `standard-output' (which see).")
516 (character, printcharfun)
517 Lisp_Object character, printcharfun;
518 {
519 PRINTDECLARE;
520
521 if (NILP (printcharfun))
522 printcharfun = Vstandard_output;
523 CHECK_NUMBER (character, 0);
524 PRINTPREPARE;
525 PRINTCHAR (XINT (character));
526 PRINTFINISH;
527 return character;
528 }
529
530 /* Used from outside of print.c to print a block of SIZE chars at DATA
531 on the default output stream.
532 Do not use this on the contents of a Lisp string. */
533
534 void
535 write_string (data, size)
536 char *data;
537 int size;
538 {
539 PRINTDECLARE;
540 Lisp_Object printcharfun;
541
542 printcharfun = Vstandard_output;
543
544 PRINTPREPARE;
545 strout (data, size, printcharfun);
546 PRINTFINISH;
547 }
548
549 /* Used from outside of print.c to print a block of SIZE chars at DATA
550 on a specified stream PRINTCHARFUN.
551 Do not use this on the contents of a Lisp string. */
552
553 void
554 write_string_1 (data, size, printcharfun)
555 char *data;
556 int size;
557 Lisp_Object printcharfun;
558 {
559 PRINTDECLARE;
560
561 PRINTPREPARE;
562 strout (data, size, printcharfun);
563 PRINTFINISH;
564 }
565
566
567 #ifndef standalone
568
569 void
570 temp_output_buffer_setup (bufname)
571 char *bufname;
572 {
573 register struct buffer *old = current_buffer;
574 register Lisp_Object buf;
575
576 Fset_buffer (Fget_buffer_create (build_string (bufname)));
577
578 current_buffer->directory = old->directory;
579 current_buffer->read_only = Qnil;
580 Ferase_buffer ();
581
582 XSETBUFFER (buf, current_buffer);
583 specbind (Qstandard_output, buf);
584
585 set_buffer_internal (old);
586 }
587
588 Lisp_Object
589 internal_with_output_to_temp_buffer (bufname, function, args)
590 char *bufname;
591 Lisp_Object (*function) ();
592 Lisp_Object args;
593 {
594 int count = specpdl_ptr - specpdl;
595 Lisp_Object buf, val;
596 struct gcpro gcpro1;
597
598 GCPRO1 (args);
599 record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
600 temp_output_buffer_setup (bufname);
601 buf = Vstandard_output;
602 UNGCPRO;
603
604 val = (*function) (args);
605
606 GCPRO1 (val);
607 temp_output_buffer_show (buf);
608 UNGCPRO;
609
610 return unbind_to (count, val);
611 }
612
613 DEFUN ("with-output-to-temp-buffer", Fwith_output_to_temp_buffer, Swith_output_to_temp_buffer,
614 1, UNEVALLED, 0,
615 "Bind `standard-output' to buffer BUFNAME, eval BODY, then show that buffer.\n\
616 The buffer is cleared out initially, and marked as unmodified when done.\n\
617 All output done by BODY is inserted in that buffer by default.\n\
618 The buffer is displayed in another window, but not selected.\n\
619 The value of the last form in BODY is returned.\n\
620 If BODY does not finish normally, the buffer BUFNAME is not displayed.\n\n\
621 If variable `temp-buffer-show-function' is non-nil, call it at the end\n\
622 to get the buffer displayed. It gets one argument, the buffer to display.")
623 (args)
624 Lisp_Object args;
625 {
626 struct gcpro gcpro1;
627 Lisp_Object name;
628 int count = specpdl_ptr - specpdl;
629 Lisp_Object buf, val;
630
631 GCPRO1(args);
632 name = Feval (Fcar (args));
633 UNGCPRO;
634
635 CHECK_STRING (name, 0);
636 temp_output_buffer_setup (XSTRING (name)->data);
637 buf = Vstandard_output;
638
639 val = Fprogn (Fcdr (args));
640
641 temp_output_buffer_show (buf);
642
643 return unbind_to (count, val);
644 }
645 #endif /* not standalone */
646 \f
647 static void print ();
648
649 DEFUN ("terpri", Fterpri, Sterpri, 0, 1, 0,
650 "Output a newline to stream PRINTCHARFUN.\n\
651 If PRINTCHARFUN is omitted or nil, the value of `standard-output' is used.")
652 (printcharfun)
653 Lisp_Object printcharfun;
654 {
655 PRINTDECLARE;
656
657 if (NILP (printcharfun))
658 printcharfun = Vstandard_output;
659 PRINTPREPARE;
660 PRINTCHAR ('\n');
661 PRINTFINISH;
662 return Qt;
663 }
664
665 DEFUN ("prin1", Fprin1, Sprin1, 1, 2, 0,
666 "Output the printed representation of OBJECT, any Lisp object.\n\
667 Quoting characters are printed when needed to make output that `read'\n\
668 can handle, whenever this is possible.\n\
669 Output stream is PRINTCHARFUN, or value of `standard-output' (which see).")
670 (object, printcharfun)
671 Lisp_Object object, printcharfun;
672 {
673 PRINTDECLARE;
674
675 #ifdef MAX_PRINT_CHARS
676 max_print = 0;
677 #endif /* MAX_PRINT_CHARS */
678 if (NILP (printcharfun))
679 printcharfun = Vstandard_output;
680 PRINTPREPARE;
681 print_depth = 0;
682 print (object, printcharfun, 1);
683 PRINTFINISH;
684 return object;
685 }
686
687 /* a buffer which is used to hold output being built by prin1-to-string */
688 Lisp_Object Vprin1_to_string_buffer;
689
690 DEFUN ("prin1-to-string", Fprin1_to_string, Sprin1_to_string, 1, 2, 0,
691 "Return a string containing the printed representation of OBJECT,\n\
692 any Lisp object. Quoting characters are used when needed to make output\n\
693 that `read' can handle, whenever this is possible, unless the optional\n\
694 second argument NOESCAPE is non-nil.")
695 (object, noescape)
696 Lisp_Object object, noescape;
697 {
698 PRINTDECLARE;
699 Lisp_Object printcharfun;
700 struct gcpro gcpro1, gcpro2;
701 Lisp_Object tem;
702
703 /* Save and restore this--we are altering a buffer
704 but we don't want to deactivate the mark just for that.
705 No need for specbind, since errors deactivate the mark. */
706 tem = Vdeactivate_mark;
707 GCPRO2 (object, tem);
708
709 printcharfun = Vprin1_to_string_buffer;
710 PRINTPREPARE;
711 print_depth = 0;
712 print (object, printcharfun, NILP (noescape));
713 /* Make Vprin1_to_string_buffer be the default buffer after PRINTFINSH */
714 PRINTFINISH;
715 set_buffer_internal (XBUFFER (Vprin1_to_string_buffer));
716 object = Fbuffer_string ();
717
718 Ferase_buffer ();
719 set_buffer_internal (old);
720
721 Vdeactivate_mark = tem;
722 UNGCPRO;
723
724 return object;
725 }
726
727 DEFUN ("princ", Fprinc, Sprinc, 1, 2, 0,
728 "Output the printed representation of OBJECT, any Lisp object.\n\
729 No quoting characters are used; no delimiters are printed around\n\
730 the contents of strings.\n\
731 Output stream is PRINTCHARFUN, or value of standard-output (which see).")
732 (object, printcharfun)
733 Lisp_Object object, printcharfun;
734 {
735 PRINTDECLARE;
736
737 if (NILP (printcharfun))
738 printcharfun = Vstandard_output;
739 PRINTPREPARE;
740 print_depth = 0;
741 print (object, printcharfun, 0);
742 PRINTFINISH;
743 return object;
744 }
745
746 DEFUN ("print", Fprint, Sprint, 1, 2, 0,
747 "Output the printed representation of OBJECT, with newlines around it.\n\
748 Quoting characters are printed when needed to make output that `read'\n\
749 can handle, whenever this is possible.\n\
750 Output stream is PRINTCHARFUN, or value of `standard-output' (which see).")
751 (object, printcharfun)
752 Lisp_Object object, printcharfun;
753 {
754 PRINTDECLARE;
755 struct gcpro gcpro1;
756
757 #ifdef MAX_PRINT_CHARS
758 print_chars = 0;
759 max_print = MAX_PRINT_CHARS;
760 #endif /* MAX_PRINT_CHARS */
761 if (NILP (printcharfun))
762 printcharfun = Vstandard_output;
763 GCPRO1 (object);
764 PRINTPREPARE;
765 print_depth = 0;
766 PRINTCHAR ('\n');
767 print (object, printcharfun, 1);
768 PRINTCHAR ('\n');
769 PRINTFINISH;
770 #ifdef MAX_PRINT_CHARS
771 max_print = 0;
772 print_chars = 0;
773 #endif /* MAX_PRINT_CHARS */
774 UNGCPRO;
775 return object;
776 }
777
778 /* The subroutine object for external-debugging-output is kept here
779 for the convenience of the debugger. */
780 Lisp_Object Qexternal_debugging_output;
781
782 DEFUN ("external-debugging-output", Fexternal_debugging_output, Sexternal_debugging_output, 1, 1, 0,
783 "Write CHARACTER to stderr.\n\
784 You can call print while debugging emacs, and pass it this function\n\
785 to make it write to the debugging output.\n")
786 (character)
787 Lisp_Object character;
788 {
789 CHECK_NUMBER (character, 0);
790 putc (XINT (character), stderr);
791
792 #ifdef WINDOWSNT
793 /* Send the output to a debugger (nothing happens if there isn't one). */
794 {
795 char buf[2] = {(char) XINT (character), '\0'};
796 OutputDebugString (buf);
797 }
798 #endif
799
800 return character;
801 }
802
803 /* This is the interface for debugging printing. */
804
805 void
806 debug_print (arg)
807 Lisp_Object arg;
808 {
809 Fprin1 (arg, Qexternal_debugging_output);
810 fprintf (stderr, "\r\n");
811 }
812 \f
813 DEFUN ("error-message-string", Ferror_message_string, Serror_message_string,
814 1, 1, 0,
815 "Convert an error value (ERROR-SYMBOL . DATA) to an error message.")
816 (obj)
817 Lisp_Object obj;
818 {
819 struct buffer *old = current_buffer;
820 Lisp_Object original, printcharfun, value;
821 struct gcpro gcpro1;
822
823 /* If OBJ is (error STRING), just return STRING.
824 That is not only faster, it also avoids the need to allocate
825 space here when the error is due to memory full. */
826 if (CONSP (obj) && EQ (XCONS (obj)->car, Qerror)
827 && CONSP (XCONS (obj)->cdr)
828 && STRINGP (XCONS (XCONS (obj)->cdr)->car)
829 && NILP (XCONS (XCONS (obj)->cdr)->cdr))
830 return XCONS (XCONS (obj)->cdr)->car;
831
832 print_error_message (obj, Vprin1_to_string_buffer);
833
834 set_buffer_internal (XBUFFER (Vprin1_to_string_buffer));
835 value = Fbuffer_string ();
836
837 GCPRO1 (value);
838 Ferase_buffer ();
839 set_buffer_internal (old);
840 UNGCPRO;
841
842 return value;
843 }
844
845 /* Print an error message for the error DATA
846 onto Lisp output stream STREAM (suitable for the print functions). */
847
848 void
849 print_error_message (data, stream)
850 Lisp_Object data, stream;
851 {
852 Lisp_Object errname, errmsg, file_error, tail;
853 struct gcpro gcpro1;
854 int i;
855
856 errname = Fcar (data);
857
858 if (EQ (errname, Qerror))
859 {
860 data = Fcdr (data);
861 if (!CONSP (data)) data = Qnil;
862 errmsg = Fcar (data);
863 file_error = Qnil;
864 }
865 else
866 {
867 errmsg = Fget (errname, Qerror_message);
868 file_error = Fmemq (Qfile_error,
869 Fget (errname, Qerror_conditions));
870 }
871
872 /* Print an error message including the data items. */
873
874 tail = Fcdr_safe (data);
875 GCPRO1 (tail);
876
877 /* For file-error, make error message by concatenating
878 all the data items. They are all strings. */
879 if (!NILP (file_error) && !NILP (tail))
880 errmsg = XCONS (tail)->car, tail = XCONS (tail)->cdr;
881
882 if (STRINGP (errmsg))
883 Fprinc (errmsg, stream);
884 else
885 write_string_1 ("peculiar error", -1, stream);
886
887 for (i = 0; CONSP (tail); tail = Fcdr (tail), i++)
888 {
889 write_string_1 (i ? ", " : ": ", 2, stream);
890 if (!NILP (file_error))
891 Fprinc (Fcar (tail), stream);
892 else
893 Fprin1 (Fcar (tail), stream);
894 }
895 UNGCPRO;
896 }
897 \f
898 #ifdef LISP_FLOAT_TYPE
899
900 /*
901 * The buffer should be at least as large as the max string size of the
902 * largest float, printed in the biggest notation. This is undoubtedly
903 * 20d float_output_format, with the negative of the C-constant "HUGE"
904 * from <math.h>.
905 *
906 * On the vax the worst case is -1e38 in 20d format which takes 61 bytes.
907 *
908 * I assume that IEEE-754 format numbers can take 329 bytes for the worst
909 * case of -1e307 in 20d float_output_format. What is one to do (short of
910 * re-writing _doprnt to be more sane)?
911 * -wsr
912 */
913
914 void
915 float_to_string (buf, data)
916 unsigned char *buf;
917 double data;
918 {
919 unsigned char *cp;
920 int width;
921
922 if (NILP (Vfloat_output_format)
923 || !STRINGP (Vfloat_output_format))
924 lose:
925 {
926 /* Generate the fewest number of digits that represent the
927 floating point value without losing information.
928 The following method is simple but a bit slow.
929 For ideas about speeding things up, please see:
930
931 Guy L Steele Jr & Jon L White, How to print floating-point numbers
932 accurately. SIGPLAN notices 25, 6 (June 1990), 112-126.
933
934 Robert G Burger & R Kent Dybvig, Printing floating point numbers
935 quickly and accurately, SIGPLAN notices 31, 5 (May 1996), 108-116. */
936
937 width = fabs (data) < DBL_MIN ? 1 : DBL_DIG;
938 do
939 sprintf (buf, "%.*g", width, data);
940 while (width++ < DOUBLE_DIGITS_BOUND && atof (buf) != data);
941 }
942 else /* oink oink */
943 {
944 /* Check that the spec we have is fully valid.
945 This means not only valid for printf,
946 but meant for floats, and reasonable. */
947 cp = XSTRING (Vfloat_output_format)->data;
948
949 if (cp[0] != '%')
950 goto lose;
951 if (cp[1] != '.')
952 goto lose;
953
954 cp += 2;
955
956 /* Check the width specification. */
957 width = -1;
958 if ('0' <= *cp && *cp <= '9')
959 {
960 width = 0;
961 do
962 width = (width * 10) + (*cp++ - '0');
963 while (*cp >= '0' && *cp <= '9');
964
965 /* A precision of zero is valid only for %f. */
966 if (width > DBL_DIG
967 || (width == 0 && *cp != 'f'))
968 goto lose;
969 }
970
971 if (*cp != 'e' && *cp != 'f' && *cp != 'g')
972 goto lose;
973
974 if (cp[1] != 0)
975 goto lose;
976
977 sprintf (buf, XSTRING (Vfloat_output_format)->data, data);
978 }
979
980 /* Make sure there is a decimal point with digit after, or an
981 exponent, so that the value is readable as a float. But don't do
982 this with "%.0f"; it's valid for that not to produce a decimal
983 point. Note that width can be 0 only for %.0f. */
984 if (width != 0)
985 {
986 for (cp = buf; *cp; cp++)
987 if ((*cp < '0' || *cp > '9') && *cp != '-')
988 break;
989
990 if (*cp == '.' && cp[1] == 0)
991 {
992 cp[1] = '0';
993 cp[2] = 0;
994 }
995
996 if (*cp == 0)
997 {
998 *cp++ = '.';
999 *cp++ = '0';
1000 *cp++ = 0;
1001 }
1002 }
1003 }
1004 #endif /* LISP_FLOAT_TYPE */
1005 \f
1006 static void
1007 print (obj, printcharfun, escapeflag)
1008 Lisp_Object obj;
1009 register Lisp_Object printcharfun;
1010 int escapeflag;
1011 {
1012 char buf[30];
1013
1014 QUIT;
1015
1016 #if 1 /* I'm not sure this is really worth doing. */
1017 /* Detect circularities and truncate them.
1018 No need to offer any alternative--this is better than an error. */
1019 if (CONSP (obj) || VECTORP (obj) || COMPILEDP (obj))
1020 {
1021 int i;
1022 for (i = 0; i < print_depth; i++)
1023 if (EQ (obj, being_printed[i]))
1024 {
1025 sprintf (buf, "#%d", i);
1026 strout (buf, -1, printcharfun);
1027 return;
1028 }
1029 }
1030 #endif
1031
1032 being_printed[print_depth] = obj;
1033 print_depth++;
1034
1035 if (print_depth > PRINT_CIRCLE)
1036 error ("Apparently circular structure being printed");
1037 #ifdef MAX_PRINT_CHARS
1038 if (max_print && print_chars > max_print)
1039 {
1040 PRINTCHAR ('\n');
1041 print_chars = 0;
1042 }
1043 #endif /* MAX_PRINT_CHARS */
1044
1045 switch (XGCTYPE (obj))
1046 {
1047 case Lisp_Int:
1048 if (sizeof (int) == sizeof (EMACS_INT))
1049 sprintf (buf, "%d", XINT (obj));
1050 else if (sizeof (long) == sizeof (EMACS_INT))
1051 sprintf (buf, "%ld", XINT (obj));
1052 else
1053 abort ();
1054 strout (buf, -1, printcharfun);
1055 break;
1056
1057 #ifdef LISP_FLOAT_TYPE
1058 case Lisp_Float:
1059 {
1060 char pigbuf[350]; /* see comments in float_to_string */
1061
1062 float_to_string (pigbuf, XFLOAT(obj)->data);
1063 strout (pigbuf, -1, printcharfun);
1064 }
1065 break;
1066 #endif
1067
1068 case Lisp_String:
1069 if (!escapeflag)
1070 print_string (obj, printcharfun);
1071 else
1072 {
1073 register int i;
1074 register unsigned char c;
1075 struct gcpro gcpro1;
1076 int size;
1077
1078 GCPRO1 (obj);
1079
1080 #ifdef USE_TEXT_PROPERTIES
1081 if (!NULL_INTERVAL_P (XSTRING (obj)->intervals))
1082 {
1083 PRINTCHAR ('#');
1084 PRINTCHAR ('(');
1085 }
1086 #endif
1087
1088 PRINTCHAR ('\"');
1089 size = XSTRING (obj)->size;
1090 for (i = 0; i < size;)
1091 {
1092 /* Here, we must convert each multi-byte form to the
1093 corresponding character code before handing it to PRINTCHAR. */
1094 int len;
1095 int c = STRING_CHAR_AND_LENGTH (&XSTRING (obj)->data[i],
1096 size - i, len);
1097 i += len;
1098 QUIT;
1099
1100 if (c == '\n' && print_escape_newlines)
1101 {
1102 PRINTCHAR ('\\');
1103 PRINTCHAR ('n');
1104 }
1105 else if (c == '\f' && print_escape_newlines)
1106 {
1107 PRINTCHAR ('\\');
1108 PRINTCHAR ('f');
1109 }
1110 else
1111 {
1112 if (c == '\"' || c == '\\')
1113 PRINTCHAR ('\\');
1114 PRINTCHAR (c);
1115 }
1116 }
1117 PRINTCHAR ('\"');
1118
1119 #ifdef USE_TEXT_PROPERTIES
1120 if (!NULL_INTERVAL_P (XSTRING (obj)->intervals))
1121 {
1122 traverse_intervals (XSTRING (obj)->intervals,
1123 0, 0, print_interval, printcharfun);
1124 PRINTCHAR (')');
1125 }
1126 #endif
1127
1128 UNGCPRO;
1129 }
1130 break;
1131
1132 case Lisp_Symbol:
1133 {
1134 register int confusing;
1135 register unsigned char *p = XSYMBOL (obj)->name->data;
1136 register unsigned char *end = p + XSYMBOL (obj)->name->size;
1137 register unsigned char c;
1138 int i, size;
1139
1140 if (p != end && (*p == '-' || *p == '+')) p++;
1141 if (p == end)
1142 confusing = 0;
1143 /* If symbol name begins with a digit, and ends with a digit,
1144 and contains nothing but digits and `e', it could be treated
1145 as a number. So set CONFUSING.
1146
1147 Symbols that contain periods could also be taken as numbers,
1148 but periods are always escaped, so we don't have to worry
1149 about them here. */
1150 else if (*p >= '0' && *p <= '9'
1151 && end[-1] >= '0' && end[-1] <= '9')
1152 {
1153 while (p != end && ((*p >= '0' && *p <= '9')
1154 /* Needed for \2e10. */
1155 || *p == 'e'))
1156 p++;
1157 confusing = (end == p);
1158 }
1159 else
1160 confusing = 0;
1161
1162 /* If we print an uninterned symbol as part of a complex object and
1163 the flag print-gensym is non-nil, prefix it with #n= to read the
1164 object back with the #n# reader syntax later if needed. */
1165 if (! NILP (Vprint_gensym) && NILP (XSYMBOL (obj)->obarray))
1166 {
1167 if (print_depth > 1)
1168 {
1169 Lisp_Object tem;
1170 tem = Fassq (obj, Vprint_gensym_alist);
1171 if (CONSP (tem))
1172 {
1173 PRINTCHAR ('#');
1174 print (XCDR (tem), printcharfun, escapeflag);
1175 PRINTCHAR ('#');
1176 break;
1177 }
1178 else
1179 {
1180 if (CONSP (Vprint_gensym_alist))
1181 XSETFASTINT (tem, XFASTINT (XCDR (XCAR (Vprint_gensym_alist))) + 1);
1182 else
1183 XSETFASTINT (tem, 1);
1184 Vprint_gensym_alist = Fcons (Fcons (obj, tem), Vprint_gensym_alist);
1185
1186 PRINTCHAR ('#');
1187 print (tem, printcharfun, escapeflag);
1188 PRINTCHAR ('=');
1189 }
1190 }
1191 PRINTCHAR ('#');
1192 PRINTCHAR (':');
1193 }
1194
1195 size = XSYMBOL (obj)->name->size;
1196 for (i = 0; i < size;)
1197 {
1198 /* Here, we must convert each multi-byte form to the
1199 corresponding character code before handing it to PRINTCHAR. */
1200 int len;
1201 int c = STRING_CHAR_AND_LENGTH (&XSYMBOL (obj)->name->data[i],
1202 size - i, len);
1203 i += len;
1204 QUIT;
1205
1206 if (escapeflag)
1207 {
1208 if (c == '\"' || c == '\\' || c == '\''
1209 || c == ';' || c == '#' || c == '(' || c == ')'
1210 || c == ',' || c =='.' || c == '`'
1211 || c == '[' || c == ']' || c == '?' || c <= 040
1212 || confusing)
1213 PRINTCHAR ('\\'), confusing = 0;
1214 }
1215 PRINTCHAR (c);
1216 }
1217 }
1218 break;
1219
1220 case Lisp_Cons:
1221 /* If deeper than spec'd depth, print placeholder. */
1222 if (INTEGERP (Vprint_level)
1223 && print_depth > XINT (Vprint_level))
1224 strout ("...", -1, printcharfun);
1225 else if (print_quoted && CONSP (XCDR (obj)) && NILP (XCDR (XCDR (obj)))
1226 && (EQ (XCAR (obj), Qquote)))
1227 {
1228 PRINTCHAR ('\'');
1229 print (XCAR (XCDR (obj)), printcharfun, escapeflag);
1230 }
1231 else if (print_quoted && CONSP (XCDR (obj)) && NILP (XCDR (XCDR (obj)))
1232 && (EQ (XCAR (obj), Qfunction)))
1233 {
1234 PRINTCHAR ('#');
1235 PRINTCHAR ('\'');
1236 print (XCAR (XCDR (obj)), printcharfun, escapeflag);
1237 }
1238 else if (print_quoted && CONSP (XCDR (obj)) && NILP (XCDR (XCDR (obj)))
1239 && ((EQ (XCAR (obj), Qbackquote)
1240 || EQ (XCAR (obj), Qcomma)
1241 || EQ (XCAR (obj), Qcomma_at)
1242 || EQ (XCAR (obj), Qcomma_dot))))
1243 {
1244 print (XCAR (obj), printcharfun, 0);
1245 print (XCAR (XCDR (obj)), printcharfun, escapeflag);
1246 }
1247 else
1248 {
1249 PRINTCHAR ('(');
1250 {
1251 register int i = 0;
1252 register int max = 0;
1253
1254 if (INTEGERP (Vprint_length))
1255 max = XINT (Vprint_length);
1256 /* Could recognize circularities in cdrs here,
1257 but that would make printing of long lists quadratic.
1258 It's not worth doing. */
1259 while (CONSP (obj))
1260 {
1261 if (i++)
1262 PRINTCHAR (' ');
1263 if (max && i > max)
1264 {
1265 strout ("...", 3, printcharfun);
1266 break;
1267 }
1268 print (XCAR (obj), printcharfun, escapeflag);
1269 obj = XCDR (obj);
1270 }
1271 }
1272 if (!NILP (obj))
1273 {
1274 strout (" . ", 3, printcharfun);
1275 print (obj, printcharfun, escapeflag);
1276 }
1277 PRINTCHAR (')');
1278 }
1279 break;
1280
1281 case Lisp_Vectorlike:
1282 if (PROCESSP (obj))
1283 {
1284 if (escapeflag)
1285 {
1286 strout ("#<process ", -1, printcharfun);
1287 print_string (XPROCESS (obj)->name, printcharfun);
1288 PRINTCHAR ('>');
1289 }
1290 else
1291 print_string (XPROCESS (obj)->name, printcharfun);
1292 }
1293 else if (BOOL_VECTOR_P (obj))
1294 {
1295 register int i;
1296 register unsigned char c;
1297 struct gcpro gcpro1;
1298 int size_in_chars
1299 = (XBOOL_VECTOR (obj)->size + BITS_PER_CHAR - 1) / BITS_PER_CHAR;
1300
1301 GCPRO1 (obj);
1302
1303 PRINTCHAR ('#');
1304 PRINTCHAR ('&');
1305 sprintf (buf, "%d", XBOOL_VECTOR (obj)->size);
1306 strout (buf, -1, printcharfun);
1307 PRINTCHAR ('\"');
1308
1309 /* Don't print more characters than the specified maximum. */
1310 if (INTEGERP (Vprint_length)
1311 && XINT (Vprint_length) < size_in_chars)
1312 size_in_chars = XINT (Vprint_length);
1313
1314 for (i = 0; i < size_in_chars; i++)
1315 {
1316 QUIT;
1317 c = XBOOL_VECTOR (obj)->data[i];
1318 if (c == '\n' && print_escape_newlines)
1319 {
1320 PRINTCHAR ('\\');
1321 PRINTCHAR ('n');
1322 }
1323 else if (c == '\f' && print_escape_newlines)
1324 {
1325 PRINTCHAR ('\\');
1326 PRINTCHAR ('f');
1327 }
1328 else
1329 {
1330 if (c == '\"' || c == '\\')
1331 PRINTCHAR ('\\');
1332 PRINTCHAR (c);
1333 }
1334 }
1335 PRINTCHAR ('\"');
1336
1337 UNGCPRO;
1338 }
1339 else if (SUBRP (obj))
1340 {
1341 strout ("#<subr ", -1, printcharfun);
1342 strout (XSUBR (obj)->symbol_name, -1, printcharfun);
1343 PRINTCHAR ('>');
1344 }
1345 #ifndef standalone
1346 else if (WINDOWP (obj))
1347 {
1348 strout ("#<window ", -1, printcharfun);
1349 sprintf (buf, "%d", XFASTINT (XWINDOW (obj)->sequence_number));
1350 strout (buf, -1, printcharfun);
1351 if (!NILP (XWINDOW (obj)->buffer))
1352 {
1353 strout (" on ", -1, printcharfun);
1354 print_string (XBUFFER (XWINDOW (obj)->buffer)->name, printcharfun);
1355 }
1356 PRINTCHAR ('>');
1357 }
1358 else if (BUFFERP (obj))
1359 {
1360 if (NILP (XBUFFER (obj)->name))
1361 strout ("#<killed buffer>", -1, printcharfun);
1362 else if (escapeflag)
1363 {
1364 strout ("#<buffer ", -1, printcharfun);
1365 print_string (XBUFFER (obj)->name, printcharfun);
1366 PRINTCHAR ('>');
1367 }
1368 else
1369 print_string (XBUFFER (obj)->name, printcharfun);
1370 }
1371 else if (WINDOW_CONFIGURATIONP (obj))
1372 {
1373 strout ("#<window-configuration>", -1, printcharfun);
1374 }
1375 else if (FRAMEP (obj))
1376 {
1377 strout ((FRAME_LIVE_P (XFRAME (obj))
1378 ? "#<frame " : "#<dead frame "),
1379 -1, printcharfun);
1380 print_string (XFRAME (obj)->name, printcharfun);
1381 sprintf (buf, " 0x%lx", (unsigned long) (XFRAME (obj)));
1382 strout (buf, -1, printcharfun);
1383 PRINTCHAR ('>');
1384 }
1385 #endif /* not standalone */
1386 else
1387 {
1388 int size = XVECTOR (obj)->size;
1389 if (COMPILEDP (obj))
1390 {
1391 PRINTCHAR ('#');
1392 size &= PSEUDOVECTOR_SIZE_MASK;
1393 }
1394 if (CHAR_TABLE_P (obj))
1395 {
1396 /* We print a char-table as if it were a vector,
1397 lumping the parent and default slots in with the
1398 character slots. But we add #^ as a prefix. */
1399 PRINTCHAR ('#');
1400 PRINTCHAR ('^');
1401 if (SUB_CHAR_TABLE_P (obj))
1402 PRINTCHAR ('^');
1403 size &= PSEUDOVECTOR_SIZE_MASK;
1404 }
1405 if (size & PSEUDOVECTOR_FLAG)
1406 goto badtype;
1407
1408 PRINTCHAR ('[');
1409 {
1410 register int i;
1411 register Lisp_Object tem;
1412
1413 /* Don't print more elements than the specified maximum. */
1414 if (INTEGERP (Vprint_length)
1415 && XINT (Vprint_length) < size)
1416 size = XINT (Vprint_length);
1417
1418 for (i = 0; i < size; i++)
1419 {
1420 if (i) PRINTCHAR (' ');
1421 tem = XVECTOR (obj)->contents[i];
1422 print (tem, printcharfun, escapeflag);
1423 }
1424 }
1425 PRINTCHAR (']');
1426 }
1427 break;
1428
1429 #ifndef standalone
1430 case Lisp_Misc:
1431 switch (XMISCTYPE (obj))
1432 {
1433 case Lisp_Misc_Marker:
1434 strout ("#<marker ", -1, printcharfun);
1435 #if 0
1436 /* Do you think this is necessary? */
1437 if (XMARKER (obj)->insertion_type != 0)
1438 strout ("(before-insertion) ", -1, printcharfun);
1439 #endif /* 0 */
1440 if (!(XMARKER (obj)->buffer))
1441 strout ("in no buffer", -1, printcharfun);
1442 else
1443 {
1444 sprintf (buf, "at %d", marker_position (obj));
1445 strout (buf, -1, printcharfun);
1446 strout (" in ", -1, printcharfun);
1447 print_string (XMARKER (obj)->buffer->name, printcharfun);
1448 }
1449 PRINTCHAR ('>');
1450 break;
1451
1452 case Lisp_Misc_Overlay:
1453 strout ("#<overlay ", -1, printcharfun);
1454 if (!(XMARKER (OVERLAY_START (obj))->buffer))
1455 strout ("in no buffer", -1, printcharfun);
1456 else
1457 {
1458 sprintf (buf, "from %d to %d in ",
1459 marker_position (OVERLAY_START (obj)),
1460 marker_position (OVERLAY_END (obj)));
1461 strout (buf, -1, printcharfun);
1462 print_string (XMARKER (OVERLAY_START (obj))->buffer->name,
1463 printcharfun);
1464 }
1465 PRINTCHAR ('>');
1466 break;
1467
1468 /* Remaining cases shouldn't happen in normal usage, but let's print
1469 them anyway for the benefit of the debugger. */
1470 case Lisp_Misc_Free:
1471 strout ("#<misc free cell>", -1, printcharfun);
1472 break;
1473
1474 case Lisp_Misc_Intfwd:
1475 sprintf (buf, "#<intfwd to %d>", *XINTFWD (obj)->intvar);
1476 strout (buf, -1, printcharfun);
1477 break;
1478
1479 case Lisp_Misc_Boolfwd:
1480 sprintf (buf, "#<boolfwd to %s>",
1481 (*XBOOLFWD (obj)->boolvar ? "t" : "nil"));
1482 strout (buf, -1, printcharfun);
1483 break;
1484
1485 case Lisp_Misc_Objfwd:
1486 strout ("#<objfwd to ", -1, printcharfun);
1487 print (*XOBJFWD (obj)->objvar, printcharfun, escapeflag);
1488 PRINTCHAR ('>');
1489 break;
1490
1491 case Lisp_Misc_Buffer_Objfwd:
1492 strout ("#<buffer_objfwd to ", -1, printcharfun);
1493 print (*(Lisp_Object *)((char *)current_buffer
1494 + XBUFFER_OBJFWD (obj)->offset),
1495 printcharfun, escapeflag);
1496 PRINTCHAR ('>');
1497 break;
1498
1499 case Lisp_Misc_Kboard_Objfwd:
1500 strout ("#<kboard_objfwd to ", -1, printcharfun);
1501 print (*(Lisp_Object *)((char *) current_kboard
1502 + XKBOARD_OBJFWD (obj)->offset),
1503 printcharfun, escapeflag);
1504 PRINTCHAR ('>');
1505 break;
1506
1507 case Lisp_Misc_Buffer_Local_Value:
1508 strout ("#<buffer_local_value ", -1, printcharfun);
1509 goto do_buffer_local;
1510 case Lisp_Misc_Some_Buffer_Local_Value:
1511 strout ("#<some_buffer_local_value ", -1, printcharfun);
1512 do_buffer_local:
1513 strout ("[realvalue] ", -1, printcharfun);
1514 print (XBUFFER_LOCAL_VALUE (obj)->car, printcharfun, escapeflag);
1515 strout ("[buffer] ", -1, printcharfun);
1516 print (XCONS (XBUFFER_LOCAL_VALUE (obj)->cdr)->car,
1517 printcharfun, escapeflag);
1518 strout ("[alist-elt] ", -1, printcharfun);
1519 print (XCONS (XCONS (XBUFFER_LOCAL_VALUE (obj)->cdr)->cdr)->car,
1520 printcharfun, escapeflag);
1521 strout ("[default-value] ", -1, printcharfun);
1522 print (XCONS (XCONS (XBUFFER_LOCAL_VALUE (obj)->cdr)->cdr)->cdr,
1523 printcharfun, escapeflag);
1524 PRINTCHAR ('>');
1525 break;
1526
1527 default:
1528 goto badtype;
1529 }
1530 break;
1531 #endif /* standalone */
1532
1533 default:
1534 badtype:
1535 {
1536 /* We're in trouble if this happens!
1537 Probably should just abort () */
1538 strout ("#<EMACS BUG: INVALID DATATYPE ", -1, printcharfun);
1539 if (MISCP (obj))
1540 sprintf (buf, "(MISC 0x%04x)", (int) XMISCTYPE (obj));
1541 else if (VECTORLIKEP (obj))
1542 sprintf (buf, "(PVEC 0x%08x)", (int) XVECTOR (obj)->size);
1543 else
1544 sprintf (buf, "(0x%02x)", (int) XTYPE (obj));
1545 strout (buf, -1, printcharfun);
1546 strout (" Save your buffers immediately and please report this bug>",
1547 -1, printcharfun);
1548 }
1549 }
1550
1551 print_depth--;
1552 }
1553 \f
1554 #ifdef USE_TEXT_PROPERTIES
1555
1556 /* Print a description of INTERVAL using PRINTCHARFUN.
1557 This is part of printing a string that has text properties. */
1558
1559 void
1560 print_interval (interval, printcharfun)
1561 INTERVAL interval;
1562 Lisp_Object printcharfun;
1563 {
1564 PRINTCHAR (' ');
1565 print (make_number (interval->position), printcharfun, 1);
1566 PRINTCHAR (' ');
1567 print (make_number (interval->position + LENGTH (interval)),
1568 printcharfun, 1);
1569 PRINTCHAR (' ');
1570 print (interval->plist, printcharfun, 1);
1571 }
1572
1573 #endif /* USE_TEXT_PROPERTIES */
1574 \f
1575 void
1576 syms_of_print ()
1577 {
1578 DEFVAR_LISP ("standard-output", &Vstandard_output,
1579 "Output stream `print' uses by default for outputting a character.\n\
1580 This may be any function of one argument.\n\
1581 It may also be a buffer (output is inserted before point)\n\
1582 or a marker (output is inserted and the marker is advanced)\n\
1583 or the symbol t (output appears in the echo area).");
1584 Vstandard_output = Qt;
1585 Qstandard_output = intern ("standard-output");
1586 staticpro (&Qstandard_output);
1587
1588 #ifdef LISP_FLOAT_TYPE
1589 DEFVAR_LISP ("float-output-format", &Vfloat_output_format,
1590 "The format descriptor string used to print floats.\n\
1591 This is a %-spec like those accepted by `printf' in C,\n\
1592 but with some restrictions. It must start with the two characters `%.'.\n\
1593 After that comes an integer precision specification,\n\
1594 and then a letter which controls the format.\n\
1595 The letters allowed are `e', `f' and `g'.\n\
1596 Use `e' for exponential notation \"DIG.DIGITSeEXPT\"\n\
1597 Use `f' for decimal point notation \"DIGITS.DIGITS\".\n\
1598 Use `g' to choose the shorter of those two formats for the number at hand.\n\
1599 The precision in any of these cases is the number of digits following\n\
1600 the decimal point. With `f', a precision of 0 means to omit the\n\
1601 decimal point. 0 is not allowed with `e' or `g'.\n\n\
1602 A value of nil means to use the shortest notation\n\
1603 that represents the number without losing information.");
1604 Vfloat_output_format = Qnil;
1605 Qfloat_output_format = intern ("float-output-format");
1606 staticpro (&Qfloat_output_format);
1607 #endif /* LISP_FLOAT_TYPE */
1608
1609 DEFVAR_LISP ("print-length", &Vprint_length,
1610 "Maximum length of list to print before abbreviating.\n\
1611 A value of nil means no limit.");
1612 Vprint_length = Qnil;
1613
1614 DEFVAR_LISP ("print-level", &Vprint_level,
1615 "Maximum depth of list nesting to print before abbreviating.\n\
1616 A value of nil means no limit.");
1617 Vprint_level = Qnil;
1618
1619 DEFVAR_BOOL ("print-escape-newlines", &print_escape_newlines,
1620 "Non-nil means print newlines in strings as backslash-n.\n\
1621 Also print formfeeds as backslash-f.");
1622 print_escape_newlines = 0;
1623
1624 DEFVAR_BOOL ("print-quoted", &print_quoted,
1625 "Non-nil means print quoted forms with reader syntax.\n\
1626 I.e., (quote foo) prints as 'foo, (function foo) as #'foo, and, backquoted\n\
1627 forms print in the new syntax.");
1628 print_quoted = 0;
1629
1630 DEFVAR_LISP ("print-gensym", &Vprint_gensym,
1631 "Non-nil means print uninterned symbols so they will read as uninterned.\n\
1632 I.e., the value of (make-symbol \"foobar\") prints as #:foobar.\n\
1633 When the uninterned symbol appears within a larger data structure,\n\
1634 in addition use the #...# and #...= constructs as needed,\n\
1635 so that multiple references to the same symbol are shared once again\n\
1636 when the text is read back.\n\
1637 \n\
1638 If the value of `print-gensym' is a cons cell, then in addition refrain from\n\
1639 clearing `print-gensym-alist' on entry to and exit from printing functions,\n\
1640 so that the use of #...# and #...= can carry over for several separately\n\
1641 printed objects.");
1642 Vprint_gensym = Qnil;
1643
1644 DEFVAR_LISP ("print-gensym-alist", &Vprint_gensym_alist,
1645 "Association list of elements (GENSYM . N) to guide use of #N# and #N=.\n\
1646 In each element, GENSYM is an uninterned symbol that has been associated\n\
1647 with #N= for the specified value of N.");
1648 Vprint_gensym_alist = Qnil;
1649
1650 /* prin1_to_string_buffer initialized in init_buffer_once in buffer.c */
1651 staticpro (&Vprin1_to_string_buffer);
1652
1653 defsubr (&Sprin1);
1654 defsubr (&Sprin1_to_string);
1655 defsubr (&Serror_message_string);
1656 defsubr (&Sprinc);
1657 defsubr (&Sprint);
1658 defsubr (&Sterpri);
1659 defsubr (&Swrite_char);
1660 defsubr (&Sexternal_debugging_output);
1661
1662 Qexternal_debugging_output = intern ("external-debugging-output");
1663 staticpro (&Qexternal_debugging_output);
1664
1665 Qprint_escape_newlines = intern ("print-escape-newlines");
1666 staticpro (&Qprint_escape_newlines);
1667
1668 #ifndef standalone
1669 defsubr (&Swith_output_to_temp_buffer);
1670 #endif /* not standalone */
1671 }