Update years in copyright notice; nfc.
[bpt/emacs.git] / src / print.c
1 /* Lisp object printing and output streams.
2 Copyright (C) 1985, 1986, 1988, 1993, 1994, 1995, 1997,
3 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 2005 Free Software Foundation, Inc.
5
6 This file is part of GNU Emacs.
7
8 GNU Emacs is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
12
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs; see the file COPYING. If not, write to
20 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
22
23
24 #include <config.h>
25 #include <stdio.h>
26 #include "lisp.h"
27 #include "buffer.h"
28 #include "charset.h"
29 #include "keyboard.h"
30 #include "frame.h"
31 #include "window.h"
32 #include "process.h"
33 #include "dispextern.h"
34 #include "termchar.h"
35 #include "intervals.h"
36
37 Lisp_Object Vstandard_output, Qstandard_output;
38
39 Lisp_Object Qtemp_buffer_setup_hook;
40
41 /* These are used to print like we read. */
42 extern Lisp_Object Qbackquote, Qcomma, Qcomma_at, Qcomma_dot, Qfunction;
43
44 Lisp_Object Vfloat_output_format, Qfloat_output_format;
45
46 /* Work around a problem that happens because math.h on hpux 7
47 defines two static variables--which, in Emacs, are not really static,
48 because `static' is defined as nothing. The problem is that they are
49 defined both here and in lread.c.
50 These macros prevent the name conflict. */
51 #if defined (HPUX) && !defined (HPUX8)
52 #define _MAXLDBL print_maxldbl
53 #define _NMAXLDBL print_nmaxldbl
54 #endif
55
56 #include <math.h>
57
58 #if STDC_HEADERS
59 #include <float.h>
60 #endif
61
62 /* Default to values appropriate for IEEE floating point. */
63 #ifndef FLT_RADIX
64 #define FLT_RADIX 2
65 #endif
66 #ifndef DBL_MANT_DIG
67 #define DBL_MANT_DIG 53
68 #endif
69 #ifndef DBL_DIG
70 #define DBL_DIG 15
71 #endif
72 #ifndef DBL_MIN
73 #define DBL_MIN 2.2250738585072014e-308
74 #endif
75
76 #ifdef DBL_MIN_REPLACEMENT
77 #undef DBL_MIN
78 #define DBL_MIN DBL_MIN_REPLACEMENT
79 #endif
80
81 /* Define DOUBLE_DIGITS_BOUND, an upper bound on the number of decimal digits
82 needed to express a float without losing information.
83 The general-case formula is valid for the usual case, IEEE floating point,
84 but many compilers can't optimize the formula to an integer constant,
85 so make a special case for it. */
86 #if FLT_RADIX == 2 && DBL_MANT_DIG == 53
87 #define DOUBLE_DIGITS_BOUND 17 /* IEEE floating point */
88 #else
89 #define DOUBLE_DIGITS_BOUND ((int) ceil (log10 (pow (FLT_RADIX, DBL_MANT_DIG))))
90 #endif
91
92 /* Avoid actual stack overflow in print. */
93 int print_depth;
94
95 /* Nonzero if inside outputting backquote in old style. */
96 int old_backquote_output;
97
98 /* Detect most circularities to print finite output. */
99 #define PRINT_CIRCLE 200
100 Lisp_Object being_printed[PRINT_CIRCLE];
101
102 /* When printing into a buffer, first we put the text in this
103 block, then insert it all at once. */
104 char *print_buffer;
105
106 /* Size allocated in print_buffer. */
107 int print_buffer_size;
108 /* Chars stored in print_buffer. */
109 int print_buffer_pos;
110 /* Bytes stored in print_buffer. */
111 int print_buffer_pos_byte;
112
113 /* Maximum length of list to print in full; noninteger means
114 effectively infinity */
115
116 Lisp_Object Vprint_length;
117
118 /* Maximum depth of list to print in full; noninteger means
119 effectively infinity. */
120
121 Lisp_Object Vprint_level;
122
123 /* Nonzero means print newlines in strings as \n. */
124
125 int print_escape_newlines;
126
127 /* Nonzero means to print single-byte non-ascii characters in strings as
128 octal escapes. */
129
130 int print_escape_nonascii;
131
132 /* Nonzero means to print multibyte characters in strings as hex escapes. */
133
134 int print_escape_multibyte;
135
136 Lisp_Object Qprint_escape_newlines;
137 Lisp_Object Qprint_escape_multibyte, Qprint_escape_nonascii;
138
139 /* Nonzero means print (quote foo) forms as 'foo, etc. */
140
141 int print_quoted;
142
143 /* Non-nil means print #: before uninterned symbols. */
144
145 Lisp_Object Vprint_gensym;
146
147 /* Non-nil means print recursive structures using #n= and #n# syntax. */
148
149 Lisp_Object Vprint_circle;
150
151 /* Non-nil means keep continuous number for #n= and #n# syntax
152 between several print functions. */
153
154 Lisp_Object Vprint_continuous_numbering;
155
156 /* Vprint_number_table is a vector like [OBJ1 STAT1 OBJ2 STAT2 ...],
157 where OBJn are objects going to be printed, and STATn are their status,
158 which may be different meanings during process. See the comments of
159 the functions print and print_preprocess for details.
160 print_number_index keeps the last position the next object should be added,
161 twice of which is the actual vector position in Vprint_number_table. */
162 int print_number_index;
163 Lisp_Object Vprint_number_table;
164
165 /* PRINT_NUMBER_OBJECT returns the I'th object in Vprint_number_table TABLE.
166 PRINT_NUMBER_STATUS returns the status of the I'th object in TABLE.
167 See the comment of the variable Vprint_number_table. */
168 #define PRINT_NUMBER_OBJECT(table,i) XVECTOR ((table))->contents[(i) * 2]
169 #define PRINT_NUMBER_STATUS(table,i) XVECTOR ((table))->contents[(i) * 2 + 1]
170
171 /* Nonzero means print newline to stdout before next minibuffer message.
172 Defined in xdisp.c */
173
174 extern int noninteractive_need_newline;
175
176 extern int minibuffer_auto_raise;
177
178 #ifdef MAX_PRINT_CHARS
179 static int print_chars;
180 static int max_print;
181 #endif /* MAX_PRINT_CHARS */
182
183 void print_interval ();
184
185 \f
186 /* Low level output routines for characters and strings */
187
188 /* Lisp functions to do output using a stream
189 must have the stream in a variable called printcharfun
190 and must start with PRINTPREPARE, end with PRINTFINISH,
191 and use PRINTDECLARE to declare common variables.
192 Use PRINTCHAR to output one character,
193 or call strout to output a block of characters. */
194
195 #define PRINTDECLARE \
196 struct buffer *old = current_buffer; \
197 int old_point = -1, start_point = -1; \
198 int old_point_byte = -1, start_point_byte = -1; \
199 int specpdl_count = SPECPDL_INDEX (); \
200 int free_print_buffer = 0; \
201 int multibyte = !NILP (current_buffer->enable_multibyte_characters); \
202 Lisp_Object original
203
204 #define PRINTPREPARE \
205 original = printcharfun; \
206 if (NILP (printcharfun)) printcharfun = Qt; \
207 if (BUFFERP (printcharfun)) \
208 { \
209 if (XBUFFER (printcharfun) != current_buffer) \
210 Fset_buffer (printcharfun); \
211 printcharfun = Qnil; \
212 } \
213 if (MARKERP (printcharfun)) \
214 { \
215 EMACS_INT marker_pos; \
216 if (!(XMARKER (printcharfun)->buffer)) \
217 error ("Marker does not point anywhere"); \
218 if (XMARKER (printcharfun)->buffer != current_buffer) \
219 set_buffer_internal (XMARKER (printcharfun)->buffer); \
220 marker_pos = marker_position (printcharfun); \
221 if (marker_pos < BEGV || marker_pos > ZV) \
222 error ("Marker is outside the accessible part of the buffer"); \
223 old_point = PT; \
224 old_point_byte = PT_BYTE; \
225 SET_PT_BOTH (marker_pos, \
226 marker_byte_position (printcharfun)); \
227 start_point = PT; \
228 start_point_byte = PT_BYTE; \
229 printcharfun = Qnil; \
230 } \
231 if (NILP (printcharfun)) \
232 { \
233 Lisp_Object string; \
234 if (NILP (current_buffer->enable_multibyte_characters) \
235 && ! print_escape_multibyte) \
236 specbind (Qprint_escape_multibyte, Qt); \
237 if (! NILP (current_buffer->enable_multibyte_characters) \
238 && ! print_escape_nonascii) \
239 specbind (Qprint_escape_nonascii, Qt); \
240 if (print_buffer != 0) \
241 { \
242 string = make_string_from_bytes (print_buffer, \
243 print_buffer_pos, \
244 print_buffer_pos_byte); \
245 record_unwind_protect (print_unwind, string); \
246 } \
247 else \
248 { \
249 print_buffer_size = 1000; \
250 print_buffer = (char *) xmalloc (print_buffer_size); \
251 free_print_buffer = 1; \
252 } \
253 print_buffer_pos = 0; \
254 print_buffer_pos_byte = 0; \
255 } \
256 if (EQ (printcharfun, Qt) && ! noninteractive) \
257 setup_echo_area_for_printing (multibyte);
258
259 #define PRINTFINISH \
260 if (NILP (printcharfun)) \
261 { \
262 if (print_buffer_pos != print_buffer_pos_byte \
263 && NILP (current_buffer->enable_multibyte_characters)) \
264 { \
265 unsigned char *temp \
266 = (unsigned char *) alloca (print_buffer_pos + 1); \
267 copy_text (print_buffer, temp, print_buffer_pos_byte, \
268 1, 0); \
269 insert_1_both (temp, print_buffer_pos, \
270 print_buffer_pos, 0, 1, 0); \
271 } \
272 else \
273 insert_1_both (print_buffer, print_buffer_pos, \
274 print_buffer_pos_byte, 0, 1, 0); \
275 } \
276 if (free_print_buffer) \
277 { \
278 xfree (print_buffer); \
279 print_buffer = 0; \
280 } \
281 unbind_to (specpdl_count, Qnil); \
282 if (MARKERP (original)) \
283 set_marker_both (original, Qnil, PT, PT_BYTE); \
284 if (old_point >= 0) \
285 SET_PT_BOTH (old_point + (old_point >= start_point \
286 ? PT - start_point : 0), \
287 old_point_byte + (old_point_byte >= start_point_byte \
288 ? PT_BYTE - start_point_byte : 0)); \
289 if (old != current_buffer) \
290 set_buffer_internal (old);
291
292 #define PRINTCHAR(ch) printchar (ch, printcharfun)
293
294 /* This is used to restore the saved contents of print_buffer
295 when there is a recursive call to print. */
296
297 static Lisp_Object
298 print_unwind (saved_text)
299 Lisp_Object saved_text;
300 {
301 bcopy (SDATA (saved_text), print_buffer, SCHARS (saved_text));
302 return Qnil;
303 }
304
305
306 /* Print character CH using method FUN. FUN nil means print to
307 print_buffer. FUN t means print to echo area or stdout if
308 non-interactive. If FUN is neither nil nor t, call FUN with CH as
309 argument. */
310
311 static void
312 printchar (ch, fun)
313 unsigned int ch;
314 Lisp_Object fun;
315 {
316 #ifdef MAX_PRINT_CHARS
317 if (max_print)
318 print_chars++;
319 #endif /* MAX_PRINT_CHARS */
320
321 if (!NILP (fun) && !EQ (fun, Qt))
322 call1 (fun, make_number (ch));
323 else
324 {
325 unsigned char str[MAX_MULTIBYTE_LENGTH];
326 int len = CHAR_STRING (ch, str);
327
328 QUIT;
329
330 if (NILP (fun))
331 {
332 if (print_buffer_pos_byte + len >= print_buffer_size)
333 print_buffer = (char *) xrealloc (print_buffer,
334 print_buffer_size *= 2);
335 bcopy (str, print_buffer + print_buffer_pos_byte, len);
336 print_buffer_pos += 1;
337 print_buffer_pos_byte += len;
338 }
339 else if (noninteractive)
340 {
341 fwrite (str, 1, len, stdout);
342 noninteractive_need_newline = 1;
343 }
344 else
345 {
346 int multibyte_p
347 = !NILP (current_buffer->enable_multibyte_characters);
348
349 setup_echo_area_for_printing (multibyte_p);
350 insert_char (ch);
351 message_dolog (str, len, 0, multibyte_p);
352 }
353 }
354 }
355
356
357 /* Output SIZE characters, SIZE_BYTE bytes from string PTR using
358 method PRINTCHARFUN. If SIZE < 0, use the string length of PTR for
359 both SIZE and SIZE_BYTE. PRINTCHARFUN nil means output to
360 print_buffer. PRINTCHARFUN t means output to the echo area or to
361 stdout if non-interactive. If neither nil nor t, call Lisp
362 function PRINTCHARFUN for each character printed. MULTIBYTE
363 non-zero means PTR contains multibyte characters. */
364
365 static void
366 strout (ptr, size, size_byte, printcharfun, multibyte)
367 char *ptr;
368 int size, size_byte;
369 Lisp_Object printcharfun;
370 int multibyte;
371 {
372 if (size < 0)
373 size_byte = size = strlen (ptr);
374
375 if (NILP (printcharfun))
376 {
377 if (print_buffer_pos_byte + size_byte > print_buffer_size)
378 {
379 print_buffer_size = print_buffer_size * 2 + size_byte;
380 print_buffer = (char *) xrealloc (print_buffer,
381 print_buffer_size);
382 }
383 bcopy (ptr, print_buffer + print_buffer_pos_byte, size_byte);
384 print_buffer_pos += size;
385 print_buffer_pos_byte += size_byte;
386
387 #ifdef MAX_PRINT_CHARS
388 if (max_print)
389 print_chars += size;
390 #endif /* MAX_PRINT_CHARS */
391 }
392 else if (noninteractive && EQ (printcharfun, Qt))
393 {
394 fwrite (ptr, 1, size_byte, stdout);
395 noninteractive_need_newline = 1;
396 }
397 else if (EQ (printcharfun, Qt))
398 {
399 /* Output to echo area. We're trying to avoid a little overhead
400 here, that's the reason we don't call printchar to do the
401 job. */
402 int i;
403 int multibyte_p
404 = !NILP (current_buffer->enable_multibyte_characters);
405
406 setup_echo_area_for_printing (multibyte_p);
407 message_dolog (ptr, size_byte, 0, multibyte_p);
408
409 if (size == size_byte)
410 {
411 for (i = 0; i < size; ++i)
412 insert_char ((unsigned char )*ptr++);
413 }
414 else
415 {
416 int len;
417 for (i = 0; i < size_byte; i += len)
418 {
419 int ch = STRING_CHAR_AND_LENGTH (ptr + i, size_byte - i, len);
420 insert_char (ch);
421 }
422 }
423
424 #ifdef MAX_PRINT_CHARS
425 if (max_print)
426 print_chars += size;
427 #endif /* MAX_PRINT_CHARS */
428 }
429 else
430 {
431 /* PRINTCHARFUN is a Lisp function. */
432 int i = 0;
433
434 if (size == size_byte)
435 {
436 while (i < size_byte)
437 {
438 int ch = ptr[i++];
439 PRINTCHAR (ch);
440 }
441 }
442 else
443 {
444 while (i < size_byte)
445 {
446 /* Here, we must convert each multi-byte form to the
447 corresponding character code before handing it to
448 PRINTCHAR. */
449 int len;
450 int ch = STRING_CHAR_AND_LENGTH (ptr + i, size_byte - i, len);
451 PRINTCHAR (ch);
452 i += len;
453 }
454 }
455 }
456 }
457
458 /* Print the contents of a string STRING using PRINTCHARFUN.
459 It isn't safe to use strout in many cases,
460 because printing one char can relocate. */
461
462 static void
463 print_string (string, printcharfun)
464 Lisp_Object string;
465 Lisp_Object printcharfun;
466 {
467 if (EQ (printcharfun, Qt) || NILP (printcharfun))
468 {
469 int chars;
470
471 if (STRING_MULTIBYTE (string))
472 chars = SCHARS (string);
473 else if (EQ (printcharfun, Qt)
474 ? ! NILP (buffer_defaults.enable_multibyte_characters)
475 : ! NILP (current_buffer->enable_multibyte_characters))
476 {
477 /* If unibyte string STRING contains 8-bit codes, we must
478 convert STRING to a multibyte string containing the same
479 character codes. */
480 Lisp_Object newstr;
481 int bytes;
482
483 chars = SBYTES (string);
484 bytes = parse_str_to_multibyte (SDATA (string), chars);
485 if (chars < bytes)
486 {
487 newstr = make_uninit_multibyte_string (chars, bytes);
488 bcopy (SDATA (string), SDATA (newstr), chars);
489 str_to_multibyte (SDATA (newstr), bytes, chars);
490 string = newstr;
491 }
492 }
493 else
494 chars = SBYTES (string);
495
496 /* strout is safe for output to a frame (echo area) or to print_buffer. */
497 strout (SDATA (string),
498 chars, SBYTES (string),
499 printcharfun, STRING_MULTIBYTE (string));
500 }
501 else
502 {
503 /* Otherwise, string may be relocated by printing one char.
504 So re-fetch the string address for each character. */
505 int i;
506 int size = SCHARS (string);
507 int size_byte = SBYTES (string);
508 struct gcpro gcpro1;
509 GCPRO1 (string);
510 if (size == size_byte)
511 for (i = 0; i < size; i++)
512 PRINTCHAR (SREF (string, i));
513 else
514 for (i = 0; i < size_byte; )
515 {
516 /* Here, we must convert each multi-byte form to the
517 corresponding character code before handing it to PRINTCHAR. */
518 int len;
519 int ch = STRING_CHAR_AND_LENGTH (SDATA (string) + i,
520 size_byte - i, len);
521 if (!CHAR_VALID_P (ch, 0))
522 {
523 ch = SREF (string, i);
524 len = 1;
525 }
526 PRINTCHAR (ch);
527 i += len;
528 }
529 UNGCPRO;
530 }
531 }
532 \f
533 DEFUN ("write-char", Fwrite_char, Swrite_char, 1, 2, 0,
534 doc: /* Output character CHARACTER to stream PRINTCHARFUN.
535 PRINTCHARFUN defaults to the value of `standard-output' (which see). */)
536 (character, printcharfun)
537 Lisp_Object character, printcharfun;
538 {
539 PRINTDECLARE;
540
541 if (NILP (printcharfun))
542 printcharfun = Vstandard_output;
543 CHECK_NUMBER (character);
544 PRINTPREPARE;
545 PRINTCHAR (XINT (character));
546 PRINTFINISH;
547 return character;
548 }
549
550 /* Used from outside of print.c to print a block of SIZE
551 single-byte chars at DATA on the default output stream.
552 Do not use this on the contents of a Lisp string. */
553
554 void
555 write_string (data, size)
556 char *data;
557 int size;
558 {
559 PRINTDECLARE;
560 Lisp_Object printcharfun;
561
562 printcharfun = Vstandard_output;
563
564 PRINTPREPARE;
565 strout (data, size, size, printcharfun, 0);
566 PRINTFINISH;
567 }
568
569 /* Used from outside of print.c to print a block of SIZE
570 single-byte chars at DATA on a specified stream PRINTCHARFUN.
571 Do not use this on the contents of a Lisp string. */
572
573 void
574 write_string_1 (data, size, printcharfun)
575 char *data;
576 int size;
577 Lisp_Object printcharfun;
578 {
579 PRINTDECLARE;
580
581 PRINTPREPARE;
582 strout (data, size, size, printcharfun, 0);
583 PRINTFINISH;
584 }
585
586
587 void
588 temp_output_buffer_setup (bufname)
589 const char *bufname;
590 {
591 int count = SPECPDL_INDEX ();
592 register struct buffer *old = current_buffer;
593 register Lisp_Object buf;
594
595 record_unwind_protect (set_buffer_if_live, Fcurrent_buffer ());
596
597 Fset_buffer (Fget_buffer_create (build_string (bufname)));
598
599 Fkill_all_local_variables ();
600 delete_all_overlays (current_buffer);
601 current_buffer->directory = old->directory;
602 current_buffer->read_only = Qnil;
603 current_buffer->filename = Qnil;
604 current_buffer->undo_list = Qt;
605 eassert (current_buffer->overlays_before == NULL);
606 eassert (current_buffer->overlays_after == NULL);
607 current_buffer->enable_multibyte_characters
608 = buffer_defaults.enable_multibyte_characters;
609 specbind (Qinhibit_read_only, Qt);
610 specbind (Qinhibit_modification_hooks, Qt);
611 Ferase_buffer ();
612 XSETBUFFER (buf, current_buffer);
613
614 Frun_hooks (1, &Qtemp_buffer_setup_hook);
615
616 unbind_to (count, Qnil);
617
618 specbind (Qstandard_output, buf);
619 }
620
621 Lisp_Object
622 internal_with_output_to_temp_buffer (bufname, function, args)
623 const char *bufname;
624 Lisp_Object (*function) P_ ((Lisp_Object));
625 Lisp_Object args;
626 {
627 int count = SPECPDL_INDEX ();
628 Lisp_Object buf, val;
629 struct gcpro gcpro1;
630
631 GCPRO1 (args);
632 record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
633 temp_output_buffer_setup (bufname);
634 buf = Vstandard_output;
635 UNGCPRO;
636
637 val = (*function) (args);
638
639 GCPRO1 (val);
640 temp_output_buffer_show (buf);
641 UNGCPRO;
642
643 return unbind_to (count, val);
644 }
645
646 DEFUN ("with-output-to-temp-buffer",
647 Fwith_output_to_temp_buffer, Swith_output_to_temp_buffer,
648 1, UNEVALLED, 0,
649 doc: /* Bind `standard-output' to buffer BUFNAME, eval BODY, then show that buffer.
650 The buffer is cleared out initially, and marked as unmodified when done.
651 All output done by BODY is inserted in that buffer by default.
652 The buffer is displayed in another window, but not selected.
653 The value of the last form in BODY is returned.
654 If BODY does not finish normally, the buffer BUFNAME is not displayed.
655
656 The hook `temp-buffer-setup-hook' is run before BODY,
657 with the buffer BUFNAME temporarily current.
658 The hook `temp-buffer-show-hook' is run after the buffer is displayed,
659 with the buffer temporarily current, and the window that was used
660 to display it temporarily selected.
661
662 If variable `temp-buffer-show-function' is non-nil, call it at the end
663 to get the buffer displayed instead of just displaying the non-selected
664 buffer and calling the hook. It gets one argument, the buffer to display.
665
666 usage: (with-output-to-temp-buffer BUFNAME BODY ...) */)
667 (args)
668 Lisp_Object args;
669 {
670 struct gcpro gcpro1;
671 Lisp_Object name;
672 int count = SPECPDL_INDEX ();
673 Lisp_Object buf, val;
674
675 GCPRO1(args);
676 name = Feval (Fcar (args));
677 CHECK_STRING (name);
678 temp_output_buffer_setup (SDATA (name));
679 buf = Vstandard_output;
680 UNGCPRO;
681
682 val = Fprogn (XCDR (args));
683
684 GCPRO1 (val);
685 temp_output_buffer_show (buf);
686 UNGCPRO;
687
688 return unbind_to (count, val);
689 }
690
691 \f
692 static void print ();
693 static void print_preprocess ();
694 static void print_preprocess_string ();
695 static void print_object ();
696
697 DEFUN ("terpri", Fterpri, Sterpri, 0, 1, 0,
698 doc: /* Output a newline to stream PRINTCHARFUN.
699 If PRINTCHARFUN is omitted or nil, the value of `standard-output' is used. */)
700 (printcharfun)
701 Lisp_Object printcharfun;
702 {
703 PRINTDECLARE;
704
705 if (NILP (printcharfun))
706 printcharfun = Vstandard_output;
707 PRINTPREPARE;
708 PRINTCHAR ('\n');
709 PRINTFINISH;
710 return Qt;
711 }
712
713 DEFUN ("prin1", Fprin1, Sprin1, 1, 2, 0,
714 doc: /* Output the printed representation of OBJECT, any Lisp object.
715 Quoting characters are printed when needed to make output that `read'
716 can handle, whenever this is possible. For complex objects, the behavior
717 is controlled by `print-level' and `print-length', which see.
718
719 OBJECT is any of the Lisp data types: a number, a string, a symbol,
720 a list, a buffer, a window, a frame, etc.
721
722 A printed representation of an object is text which describes that object.
723
724 Optional argument PRINTCHARFUN is the output stream, which can be one
725 of these:
726
727 - a buffer, in which case output is inserted into that buffer at point;
728 - a marker, in which case output is inserted at marker's position;
729 - a function, in which case that function is called once for each
730 character of OBJECT's printed representation;
731 - a symbol, in which case that symbol's function definition is called; or
732 - t, in which case the output is displayed in the echo area.
733
734 If PRINTCHARFUN is omitted, the value of `standard-output' (which see)
735 is used instead. */)
736 (object, printcharfun)
737 Lisp_Object object, printcharfun;
738 {
739 PRINTDECLARE;
740
741 #ifdef MAX_PRINT_CHARS
742 max_print = 0;
743 #endif /* MAX_PRINT_CHARS */
744 if (NILP (printcharfun))
745 printcharfun = Vstandard_output;
746 PRINTPREPARE;
747 print (object, printcharfun, 1);
748 PRINTFINISH;
749 return object;
750 }
751
752 /* a buffer which is used to hold output being built by prin1-to-string */
753 Lisp_Object Vprin1_to_string_buffer;
754
755 DEFUN ("prin1-to-string", Fprin1_to_string, Sprin1_to_string, 1, 2, 0,
756 doc: /* Return a string containing the printed representation of OBJECT.
757 OBJECT can be any Lisp object. This function outputs quoting characters
758 when necessary to make output that `read' can handle, whenever possible,
759 unless the optional second argument NOESCAPE is non-nil.
760
761 OBJECT is any of the Lisp data types: a number, a string, a symbol,
762 a list, a buffer, a window, a frame, etc.
763
764 A printed representation of an object is text which describes that object. */)
765 (object, noescape)
766 Lisp_Object object, noescape;
767 {
768 Lisp_Object printcharfun;
769 /* struct gcpro gcpro1, gcpro2; */
770 Lisp_Object save_deactivate_mark;
771 int count = specpdl_ptr - specpdl;
772 struct buffer *previous;
773
774 specbind (Qinhibit_modification_hooks, Qt);
775
776 {
777 PRINTDECLARE;
778
779 /* Save and restore this--we are altering a buffer
780 but we don't want to deactivate the mark just for that.
781 No need for specbind, since errors deactivate the mark. */
782 save_deactivate_mark = Vdeactivate_mark;
783 /* GCPRO2 (object, save_deactivate_mark); */
784 abort_on_gc++;
785
786 printcharfun = Vprin1_to_string_buffer;
787 PRINTPREPARE;
788 print (object, printcharfun, NILP (noescape));
789 /* Make Vprin1_to_string_buffer be the default buffer after PRINTFINSH */
790 PRINTFINISH;
791 }
792
793 previous = current_buffer;
794 set_buffer_internal (XBUFFER (Vprin1_to_string_buffer));
795 object = Fbuffer_string ();
796 if (SBYTES (object) == SCHARS (object))
797 STRING_SET_UNIBYTE (object);
798
799 /* Note that this won't make prepare_to_modify_buffer call
800 ask-user-about-supersession-threat because this buffer
801 does not visit a file. */
802 Ferase_buffer ();
803 set_buffer_internal (previous);
804
805 Vdeactivate_mark = save_deactivate_mark;
806 /* UNGCPRO; */
807
808 abort_on_gc--;
809 return unbind_to (count, object);
810 }
811
812 DEFUN ("princ", Fprinc, Sprinc, 1, 2, 0,
813 doc: /* Output the printed representation of OBJECT, any Lisp object.
814 No quoting characters are used; no delimiters are printed around
815 the contents of strings.
816
817 OBJECT is any of the Lisp data types: a number, a string, a symbol,
818 a list, a buffer, a window, a frame, etc.
819
820 A printed representation of an object is text which describes that object.
821
822 Optional argument PRINTCHARFUN is the output stream, which can be one
823 of these:
824
825 - a buffer, in which case output is inserted into that buffer at point;
826 - a marker, in which case output is inserted at marker's position;
827 - a function, in which case that function is called once for each
828 character of OBJECT's printed representation;
829 - a symbol, in which case that symbol's function definition is called; or
830 - t, in which case the output is displayed in the echo area.
831
832 If PRINTCHARFUN is omitted, the value of `standard-output' (which see)
833 is used instead. */)
834 (object, printcharfun)
835 Lisp_Object object, printcharfun;
836 {
837 PRINTDECLARE;
838
839 if (NILP (printcharfun))
840 printcharfun = Vstandard_output;
841 PRINTPREPARE;
842 print (object, printcharfun, 0);
843 PRINTFINISH;
844 return object;
845 }
846
847 DEFUN ("print", Fprint, Sprint, 1, 2, 0,
848 doc: /* Output the printed representation of OBJECT, with newlines around it.
849 Quoting characters are printed when needed to make output that `read'
850 can handle, whenever this is possible. For complex objects, the behavior
851 is controlled by `print-level' and `print-length', which see.
852
853 OBJECT is any of the Lisp data types: a number, a string, a symbol,
854 a list, a buffer, a window, a frame, etc.
855
856 A printed representation of an object is text which describes that object.
857
858 Optional argument PRINTCHARFUN is the output stream, which can be one
859 of these:
860
861 - a buffer, in which case output is inserted into that buffer at point;
862 - a marker, in which case output is inserted at marker's position;
863 - a function, in which case that function is called once for each
864 character of OBJECT's printed representation;
865 - a symbol, in which case that symbol's function definition is called; or
866 - t, in which case the output is displayed in the echo area.
867
868 If PRINTCHARFUN is omitted, the value of `standard-output' (which see)
869 is used instead. */)
870 (object, printcharfun)
871 Lisp_Object object, printcharfun;
872 {
873 PRINTDECLARE;
874 struct gcpro gcpro1;
875
876 #ifdef MAX_PRINT_CHARS
877 print_chars = 0;
878 max_print = MAX_PRINT_CHARS;
879 #endif /* MAX_PRINT_CHARS */
880 if (NILP (printcharfun))
881 printcharfun = Vstandard_output;
882 GCPRO1 (object);
883 PRINTPREPARE;
884 PRINTCHAR ('\n');
885 print (object, printcharfun, 1);
886 PRINTCHAR ('\n');
887 PRINTFINISH;
888 #ifdef MAX_PRINT_CHARS
889 max_print = 0;
890 print_chars = 0;
891 #endif /* MAX_PRINT_CHARS */
892 UNGCPRO;
893 return object;
894 }
895
896 /* The subroutine object for external-debugging-output is kept here
897 for the convenience of the debugger. */
898 Lisp_Object Qexternal_debugging_output;
899
900 DEFUN ("external-debugging-output", Fexternal_debugging_output, Sexternal_debugging_output, 1, 1, 0,
901 doc: /* Write CHARACTER to stderr.
902 You can call print while debugging emacs, and pass it this function
903 to make it write to the debugging output. */)
904 (character)
905 Lisp_Object character;
906 {
907 CHECK_NUMBER (character);
908 putc (XINT (character), stderr);
909
910 #ifdef WINDOWSNT
911 /* Send the output to a debugger (nothing happens if there isn't one). */
912 {
913 char buf[2] = {(char) XINT (character), '\0'};
914 OutputDebugString (buf);
915 }
916 #endif
917
918 return character;
919 }
920
921
922 #if defined(GNU_LINUX)
923
924 /* This functionality is not vitally important in general, so we rely on
925 non-portable ability to use stderr as lvalue. */
926
927 #define WITH_REDIRECT_DEBUGGING_OUTPUT 1
928
929 FILE *initial_stderr_stream = NULL;
930
931 DEFUN ("redirect-debugging-output", Fredirect_debugging_output, Sredirect_debugging_output,
932 1, 2,
933 "FDebug output file: \nP",
934 doc: /* Redirect debugging output (stderr stream) to file FILE.
935 If FILE is nil, reset target to the initial stderr stream.
936 Optional arg APPEND non-nil (interactively, with prefix arg) means
937 append to existing target file. */)
938 (file, append)
939 Lisp_Object file, append;
940 {
941 if (initial_stderr_stream != NULL)
942 fclose(stderr);
943 stderr = initial_stderr_stream;
944 initial_stderr_stream = NULL;
945
946 if (STRINGP (file))
947 {
948 file = Fexpand_file_name (file, Qnil);
949 initial_stderr_stream = stderr;
950 stderr = fopen(SDATA (file), NILP (append) ? "w" : "a");
951 if (stderr == NULL)
952 {
953 stderr = initial_stderr_stream;
954 initial_stderr_stream = NULL;
955 report_file_error ("Cannot open debugging output stream",
956 Fcons (file, Qnil));
957 }
958 }
959 return Qnil;
960 }
961 #endif /* GNU_LINUX */
962
963
964 /* This is the interface for debugging printing. */
965
966 void
967 debug_print (arg)
968 Lisp_Object arg;
969 {
970 Fprin1 (arg, Qexternal_debugging_output);
971 fprintf (stderr, "\r\n");
972 }
973 \f
974 DEFUN ("error-message-string", Ferror_message_string, Serror_message_string,
975 1, 1, 0,
976 doc: /* Convert an error value (ERROR-SYMBOL . DATA) to an error message.
977 See Info anchor `(elisp)Definition of signal' for some details on how this
978 error message is constructed. */)
979 (obj)
980 Lisp_Object obj;
981 {
982 struct buffer *old = current_buffer;
983 Lisp_Object value;
984 struct gcpro gcpro1;
985
986 /* If OBJ is (error STRING), just return STRING.
987 That is not only faster, it also avoids the need to allocate
988 space here when the error is due to memory full. */
989 if (CONSP (obj) && EQ (XCAR (obj), Qerror)
990 && CONSP (XCDR (obj))
991 && STRINGP (XCAR (XCDR (obj)))
992 && NILP (XCDR (XCDR (obj))))
993 return XCAR (XCDR (obj));
994
995 print_error_message (obj, Vprin1_to_string_buffer, 0, Qnil);
996
997 set_buffer_internal (XBUFFER (Vprin1_to_string_buffer));
998 value = Fbuffer_string ();
999
1000 GCPRO1 (value);
1001 Ferase_buffer ();
1002 set_buffer_internal (old);
1003 UNGCPRO;
1004
1005 return value;
1006 }
1007
1008 /* Print an error message for the error DATA onto Lisp output stream
1009 STREAM (suitable for the print functions). */
1010
1011 void
1012 print_error_message (data, stream, context, caller)
1013 Lisp_Object data, stream;
1014 char *context;
1015 Lisp_Object caller;
1016 {
1017 Lisp_Object errname, errmsg, file_error, tail;
1018 struct gcpro gcpro1;
1019 int i;
1020
1021 if (context != 0)
1022 write_string_1 (context, -1, stream);
1023
1024 /* If we know from where the error was signaled, show it in
1025 *Messages*. */
1026 if (!NILP (caller) && SYMBOLP (caller))
1027 {
1028 const char *name = SDATA (SYMBOL_NAME (caller));
1029 message_dolog (name, strlen (name), 0, 0);
1030 message_dolog (": ", 2, 0, 0);
1031 }
1032
1033 errname = Fcar (data);
1034
1035 if (EQ (errname, Qerror))
1036 {
1037 data = Fcdr (data);
1038 if (!CONSP (data))
1039 data = Qnil;
1040 errmsg = Fcar (data);
1041 file_error = Qnil;
1042 }
1043 else
1044 {
1045 Lisp_Object error_conditions;
1046 errmsg = Fget (errname, Qerror_message);
1047 error_conditions = Fget (errname, Qerror_conditions);
1048 file_error = Fmemq (Qfile_error, error_conditions);
1049 }
1050
1051 /* Print an error message including the data items. */
1052
1053 tail = Fcdr_safe (data);
1054 GCPRO1 (tail);
1055
1056 /* For file-error, make error message by concatenating
1057 all the data items. They are all strings. */
1058 if (!NILP (file_error) && CONSP (tail))
1059 errmsg = XCAR (tail), tail = XCDR (tail);
1060
1061 if (STRINGP (errmsg))
1062 Fprinc (errmsg, stream);
1063 else
1064 write_string_1 ("peculiar error", -1, stream);
1065
1066 for (i = 0; CONSP (tail); tail = XCDR (tail), i++)
1067 {
1068 Lisp_Object obj;
1069
1070 write_string_1 (i ? ", " : ": ", 2, stream);
1071 obj = XCAR (tail);
1072 if (!NILP (file_error) || EQ (errname, Qend_of_file))
1073 Fprinc (obj, stream);
1074 else
1075 Fprin1 (obj, stream);
1076 }
1077
1078 UNGCPRO;
1079 }
1080
1081
1082 \f
1083 /*
1084 * The buffer should be at least as large as the max string size of the
1085 * largest float, printed in the biggest notation. This is undoubtedly
1086 * 20d float_output_format, with the negative of the C-constant "HUGE"
1087 * from <math.h>.
1088 *
1089 * On the vax the worst case is -1e38 in 20d format which takes 61 bytes.
1090 *
1091 * I assume that IEEE-754 format numbers can take 329 bytes for the worst
1092 * case of -1e307 in 20d float_output_format. What is one to do (short of
1093 * re-writing _doprnt to be more sane)?
1094 * -wsr
1095 */
1096
1097 void
1098 float_to_string (buf, data)
1099 unsigned char *buf;
1100 double data;
1101 {
1102 unsigned char *cp;
1103 int width;
1104
1105 /* Check for plus infinity in a way that won't lose
1106 if there is no plus infinity. */
1107 if (data == data / 2 && data > 1.0)
1108 {
1109 strcpy (buf, "1.0e+INF");
1110 return;
1111 }
1112 /* Likewise for minus infinity. */
1113 if (data == data / 2 && data < -1.0)
1114 {
1115 strcpy (buf, "-1.0e+INF");
1116 return;
1117 }
1118 /* Check for NaN in a way that won't fail if there are no NaNs. */
1119 if (! (data * 0.0 >= 0.0))
1120 {
1121 /* Prepend "-" if the NaN's sign bit is negative.
1122 The sign bit of a double is the bit that is 1 in -0.0. */
1123 int i;
1124 union { double d; char c[sizeof (double)]; } u_data, u_minus_zero;
1125 u_data.d = data;
1126 u_minus_zero.d = - 0.0;
1127 for (i = 0; i < sizeof (double); i++)
1128 if (u_data.c[i] & u_minus_zero.c[i])
1129 {
1130 *buf++ = '-';
1131 break;
1132 }
1133
1134 strcpy (buf, "0.0e+NaN");
1135 return;
1136 }
1137
1138 if (NILP (Vfloat_output_format)
1139 || !STRINGP (Vfloat_output_format))
1140 lose:
1141 {
1142 /* Generate the fewest number of digits that represent the
1143 floating point value without losing information.
1144 The following method is simple but a bit slow.
1145 For ideas about speeding things up, please see:
1146
1147 Guy L Steele Jr & Jon L White, How to print floating-point numbers
1148 accurately. SIGPLAN notices 25, 6 (June 1990), 112-126.
1149
1150 Robert G Burger & R Kent Dybvig, Printing floating point numbers
1151 quickly and accurately, SIGPLAN notices 31, 5 (May 1996), 108-116. */
1152
1153 width = fabs (data) < DBL_MIN ? 1 : DBL_DIG;
1154 do
1155 sprintf (buf, "%.*g", width, data);
1156 while (width++ < DOUBLE_DIGITS_BOUND && atof (buf) != data);
1157 }
1158 else /* oink oink */
1159 {
1160 /* Check that the spec we have is fully valid.
1161 This means not only valid for printf,
1162 but meant for floats, and reasonable. */
1163 cp = SDATA (Vfloat_output_format);
1164
1165 if (cp[0] != '%')
1166 goto lose;
1167 if (cp[1] != '.')
1168 goto lose;
1169
1170 cp += 2;
1171
1172 /* Check the width specification. */
1173 width = -1;
1174 if ('0' <= *cp && *cp <= '9')
1175 {
1176 width = 0;
1177 do
1178 width = (width * 10) + (*cp++ - '0');
1179 while (*cp >= '0' && *cp <= '9');
1180
1181 /* A precision of zero is valid only for %f. */
1182 if (width > DBL_DIG
1183 || (width == 0 && *cp != 'f'))
1184 goto lose;
1185 }
1186
1187 if (*cp != 'e' && *cp != 'f' && *cp != 'g')
1188 goto lose;
1189
1190 if (cp[1] != 0)
1191 goto lose;
1192
1193 sprintf (buf, SDATA (Vfloat_output_format), data);
1194 }
1195
1196 /* Make sure there is a decimal point with digit after, or an
1197 exponent, so that the value is readable as a float. But don't do
1198 this with "%.0f"; it's valid for that not to produce a decimal
1199 point. Note that width can be 0 only for %.0f. */
1200 if (width != 0)
1201 {
1202 for (cp = buf; *cp; cp++)
1203 if ((*cp < '0' || *cp > '9') && *cp != '-')
1204 break;
1205
1206 if (*cp == '.' && cp[1] == 0)
1207 {
1208 cp[1] = '0';
1209 cp[2] = 0;
1210 }
1211
1212 if (*cp == 0)
1213 {
1214 *cp++ = '.';
1215 *cp++ = '0';
1216 *cp++ = 0;
1217 }
1218 }
1219 }
1220
1221 \f
1222 static void
1223 print (obj, printcharfun, escapeflag)
1224 Lisp_Object obj;
1225 register Lisp_Object printcharfun;
1226 int escapeflag;
1227 {
1228 old_backquote_output = 0;
1229
1230 /* Reset print_number_index and Vprint_number_table only when
1231 the variable Vprint_continuous_numbering is nil. Otherwise,
1232 the values of these variables will be kept between several
1233 print functions. */
1234 if (NILP (Vprint_continuous_numbering))
1235 {
1236 print_number_index = 0;
1237 Vprint_number_table = Qnil;
1238 }
1239
1240 /* Construct Vprint_number_table for print-gensym and print-circle. */
1241 if (!NILP (Vprint_gensym) || !NILP (Vprint_circle))
1242 {
1243 int i, start, index;
1244 start = index = print_number_index;
1245 /* Construct Vprint_number_table.
1246 This increments print_number_index for the objects added. */
1247 print_depth = 0;
1248 print_preprocess (obj);
1249
1250 /* Remove unnecessary objects, which appear only once in OBJ;
1251 that is, whose status is Qnil. Compactify the necessary objects. */
1252 for (i = start; i < print_number_index; i++)
1253 if (!NILP (PRINT_NUMBER_STATUS (Vprint_number_table, i)))
1254 {
1255 PRINT_NUMBER_OBJECT (Vprint_number_table, index)
1256 = PRINT_NUMBER_OBJECT (Vprint_number_table, i);
1257 index++;
1258 }
1259
1260 /* Clear out objects outside the active part of the table. */
1261 for (i = index; i < print_number_index; i++)
1262 PRINT_NUMBER_OBJECT (Vprint_number_table, i) = Qnil;
1263
1264 /* Reset the status field for the next print step. Now this
1265 field means whether the object has already been printed. */
1266 for (i = start; i < print_number_index; i++)
1267 PRINT_NUMBER_STATUS (Vprint_number_table, i) = Qnil;
1268
1269 print_number_index = index;
1270 }
1271
1272 print_depth = 0;
1273 print_object (obj, printcharfun, escapeflag);
1274 }
1275
1276 /* Construct Vprint_number_table according to the structure of OBJ.
1277 OBJ itself and all its elements will be added to Vprint_number_table
1278 recursively if it is a list, vector, compiled function, char-table,
1279 string (its text properties will be traced), or a symbol that has
1280 no obarray (this is for the print-gensym feature).
1281 The status fields of Vprint_number_table mean whether each object appears
1282 more than once in OBJ: Qnil at the first time, and Qt after that . */
1283 static void
1284 print_preprocess (obj)
1285 Lisp_Object obj;
1286 {
1287 int i;
1288 EMACS_INT size;
1289 int loop_count = 0;
1290 Lisp_Object halftail;
1291
1292 /* Give up if we go so deep that print_object will get an error. */
1293 /* See similar code in print_object. */
1294 if (print_depth >= PRINT_CIRCLE)
1295 return;
1296
1297 /* Avoid infinite recursion for circular nested structure
1298 in the case where Vprint_circle is nil. */
1299 if (NILP (Vprint_circle))
1300 {
1301 for (i = 0; i < print_depth; i++)
1302 if (EQ (obj, being_printed[i]))
1303 return;
1304 being_printed[print_depth] = obj;
1305 }
1306
1307 print_depth++;
1308 halftail = obj;
1309
1310 loop:
1311 if (STRINGP (obj) || CONSP (obj) || VECTORP (obj)
1312 || COMPILEDP (obj) || CHAR_TABLE_P (obj)
1313 || (! NILP (Vprint_gensym)
1314 && SYMBOLP (obj)
1315 && !SYMBOL_INTERNED_P (obj)))
1316 {
1317 /* In case print-circle is nil and print-gensym is t,
1318 add OBJ to Vprint_number_table only when OBJ is a symbol. */
1319 if (! NILP (Vprint_circle) || SYMBOLP (obj))
1320 {
1321 for (i = 0; i < print_number_index; i++)
1322 if (EQ (PRINT_NUMBER_OBJECT (Vprint_number_table, i), obj))
1323 {
1324 /* OBJ appears more than once. Let's remember that. */
1325 PRINT_NUMBER_STATUS (Vprint_number_table, i) = Qt;
1326 return;
1327 }
1328
1329 /* OBJ is not yet recorded. Let's add to the table. */
1330 if (print_number_index == 0)
1331 {
1332 /* Initialize the table. */
1333 Vprint_number_table = Fmake_vector (make_number (40), Qnil);
1334 }
1335 else if (XVECTOR (Vprint_number_table)->size == print_number_index * 2)
1336 {
1337 /* Reallocate the table. */
1338 int i = print_number_index * 4;
1339 Lisp_Object old_table = Vprint_number_table;
1340 Vprint_number_table = Fmake_vector (make_number (i), Qnil);
1341 for (i = 0; i < print_number_index; i++)
1342 {
1343 PRINT_NUMBER_OBJECT (Vprint_number_table, i)
1344 = PRINT_NUMBER_OBJECT (old_table, i);
1345 PRINT_NUMBER_STATUS (Vprint_number_table, i)
1346 = PRINT_NUMBER_STATUS (old_table, i);
1347 }
1348 }
1349 PRINT_NUMBER_OBJECT (Vprint_number_table, print_number_index) = obj;
1350 /* If Vprint_continuous_numbering is non-nil and OBJ is a gensym,
1351 always print the gensym with a number. This is a special for
1352 the lisp function byte-compile-output-docform. */
1353 if (!NILP (Vprint_continuous_numbering)
1354 && SYMBOLP (obj)
1355 && !SYMBOL_INTERNED_P (obj))
1356 PRINT_NUMBER_STATUS (Vprint_number_table, print_number_index) = Qt;
1357 print_number_index++;
1358 }
1359
1360 switch (XGCTYPE (obj))
1361 {
1362 case Lisp_String:
1363 /* A string may have text properties, which can be circular. */
1364 traverse_intervals_noorder (STRING_INTERVALS (obj),
1365 print_preprocess_string, Qnil);
1366 break;
1367
1368 case Lisp_Cons:
1369 /* Use HALFTAIL and LOOP_COUNT to detect circular lists,
1370 just as in print_object. */
1371 if (loop_count && EQ (obj, halftail))
1372 break;
1373 print_preprocess (XCAR (obj));
1374 obj = XCDR (obj);
1375 loop_count++;
1376 if (!(loop_count & 1))
1377 halftail = XCDR (halftail);
1378 goto loop;
1379
1380 case Lisp_Vectorlike:
1381 size = XVECTOR (obj)->size;
1382 if (size & PSEUDOVECTOR_FLAG)
1383 size &= PSEUDOVECTOR_SIZE_MASK;
1384 for (i = 0; i < size; i++)
1385 print_preprocess (XVECTOR (obj)->contents[i]);
1386 break;
1387
1388 default:
1389 break;
1390 }
1391 }
1392 print_depth--;
1393 }
1394
1395 static void
1396 print_preprocess_string (interval, arg)
1397 INTERVAL interval;
1398 Lisp_Object arg;
1399 {
1400 print_preprocess (interval->plist);
1401 }
1402
1403 static void
1404 print_object (obj, printcharfun, escapeflag)
1405 Lisp_Object obj;
1406 register Lisp_Object printcharfun;
1407 int escapeflag;
1408 {
1409 char buf[40];
1410
1411 QUIT;
1412
1413 /* Detect circularities and truncate them. */
1414 if (STRINGP (obj) || CONSP (obj) || VECTORP (obj)
1415 || COMPILEDP (obj) || CHAR_TABLE_P (obj)
1416 || (! NILP (Vprint_gensym)
1417 && SYMBOLP (obj)
1418 && !SYMBOL_INTERNED_P (obj)))
1419 {
1420 if (NILP (Vprint_circle) && NILP (Vprint_gensym))
1421 {
1422 /* Simple but incomplete way. */
1423 int i;
1424 for (i = 0; i < print_depth; i++)
1425 if (EQ (obj, being_printed[i]))
1426 {
1427 sprintf (buf, "#%d", i);
1428 strout (buf, -1, -1, printcharfun, 0);
1429 return;
1430 }
1431 being_printed[print_depth] = obj;
1432 }
1433 else
1434 {
1435 /* With the print-circle feature. */
1436 int i;
1437 for (i = 0; i < print_number_index; i++)
1438 if (EQ (PRINT_NUMBER_OBJECT (Vprint_number_table, i), obj))
1439 {
1440 if (NILP (PRINT_NUMBER_STATUS (Vprint_number_table, i)))
1441 {
1442 /* Add a prefix #n= if OBJ has not yet been printed;
1443 that is, its status field is nil. */
1444 sprintf (buf, "#%d=", i + 1);
1445 strout (buf, -1, -1, printcharfun, 0);
1446 /* OBJ is going to be printed. Set the status to t. */
1447 PRINT_NUMBER_STATUS (Vprint_number_table, i) = Qt;
1448 break;
1449 }
1450 else
1451 {
1452 /* Just print #n# if OBJ has already been printed. */
1453 sprintf (buf, "#%d#", i + 1);
1454 strout (buf, -1, -1, printcharfun, 0);
1455 return;
1456 }
1457 }
1458 }
1459 }
1460
1461 print_depth++;
1462
1463 /* See similar code in print_preprocess. */
1464 if (print_depth > PRINT_CIRCLE)
1465 error ("Apparently circular structure being printed");
1466 #ifdef MAX_PRINT_CHARS
1467 if (max_print && print_chars > max_print)
1468 {
1469 PRINTCHAR ('\n');
1470 print_chars = 0;
1471 }
1472 #endif /* MAX_PRINT_CHARS */
1473
1474 switch (XGCTYPE (obj))
1475 {
1476 case Lisp_Int:
1477 if (sizeof (int) == sizeof (EMACS_INT))
1478 sprintf (buf, "%d", XINT (obj));
1479 else if (sizeof (long) == sizeof (EMACS_INT))
1480 sprintf (buf, "%ld", (long) XINT (obj));
1481 else
1482 abort ();
1483 strout (buf, -1, -1, printcharfun, 0);
1484 break;
1485
1486 case Lisp_Float:
1487 {
1488 char pigbuf[350]; /* see comments in float_to_string */
1489
1490 float_to_string (pigbuf, XFLOAT_DATA (obj));
1491 strout (pigbuf, -1, -1, printcharfun, 0);
1492 }
1493 break;
1494
1495 case Lisp_String:
1496 if (!escapeflag)
1497 print_string (obj, printcharfun);
1498 else
1499 {
1500 register int i, i_byte;
1501 struct gcpro gcpro1;
1502 unsigned char *str;
1503 int size_byte;
1504 /* 1 means we must ensure that the next character we output
1505 cannot be taken as part of a hex character escape. */
1506 int need_nonhex = 0;
1507 int multibyte = STRING_MULTIBYTE (obj);
1508
1509 GCPRO1 (obj);
1510
1511 if (!NULL_INTERVAL_P (STRING_INTERVALS (obj)))
1512 {
1513 PRINTCHAR ('#');
1514 PRINTCHAR ('(');
1515 }
1516
1517 PRINTCHAR ('\"');
1518 str = SDATA (obj);
1519 size_byte = SBYTES (obj);
1520
1521 for (i = 0, i_byte = 0; i_byte < size_byte;)
1522 {
1523 /* Here, we must convert each multi-byte form to the
1524 corresponding character code before handing it to PRINTCHAR. */
1525 int len;
1526 int c;
1527
1528 if (multibyte)
1529 {
1530 c = STRING_CHAR_AND_LENGTH (str + i_byte,
1531 size_byte - i_byte, len);
1532 if (CHAR_VALID_P (c, 0))
1533 i_byte += len;
1534 else
1535 c = str[i_byte++];
1536 }
1537 else
1538 c = str[i_byte++];
1539
1540 QUIT;
1541
1542 if (c == '\n' && print_escape_newlines)
1543 {
1544 PRINTCHAR ('\\');
1545 PRINTCHAR ('n');
1546 }
1547 else if (c == '\f' && print_escape_newlines)
1548 {
1549 PRINTCHAR ('\\');
1550 PRINTCHAR ('f');
1551 }
1552 else if (multibyte
1553 && ! ASCII_BYTE_P (c)
1554 && (SINGLE_BYTE_CHAR_P (c) || print_escape_multibyte))
1555 {
1556 /* When multibyte is disabled,
1557 print multibyte string chars using hex escapes.
1558 For a char code that could be in a unibyte string,
1559 when found in a multibyte string, always use a hex escape
1560 so it reads back as multibyte. */
1561 unsigned char outbuf[50];
1562 sprintf (outbuf, "\\x%x", c);
1563 strout (outbuf, -1, -1, printcharfun, 0);
1564 need_nonhex = 1;
1565 }
1566 else if (! multibyte
1567 && SINGLE_BYTE_CHAR_P (c) && ! ASCII_BYTE_P (c)
1568 && print_escape_nonascii)
1569 {
1570 /* When printing in a multibyte buffer
1571 or when explicitly requested,
1572 print single-byte non-ASCII string chars
1573 using octal escapes. */
1574 unsigned char outbuf[5];
1575 sprintf (outbuf, "\\%03o", c);
1576 strout (outbuf, -1, -1, printcharfun, 0);
1577 }
1578 else
1579 {
1580 /* If we just had a hex escape, and this character
1581 could be taken as part of it,
1582 output `\ ' to prevent that. */
1583 if (need_nonhex)
1584 {
1585 need_nonhex = 0;
1586 if ((c >= 'a' && c <= 'f')
1587 || (c >= 'A' && c <= 'F')
1588 || (c >= '0' && c <= '9'))
1589 strout ("\\ ", -1, -1, printcharfun, 0);
1590 }
1591
1592 if (c == '\"' || c == '\\')
1593 PRINTCHAR ('\\');
1594 PRINTCHAR (c);
1595 }
1596 }
1597 PRINTCHAR ('\"');
1598
1599 if (!NULL_INTERVAL_P (STRING_INTERVALS (obj)))
1600 {
1601 traverse_intervals (STRING_INTERVALS (obj),
1602 0, print_interval, printcharfun);
1603 PRINTCHAR (')');
1604 }
1605
1606 UNGCPRO;
1607 }
1608 break;
1609
1610 case Lisp_Symbol:
1611 {
1612 register int confusing;
1613 register unsigned char *p = SDATA (SYMBOL_NAME (obj));
1614 register unsigned char *end = p + SBYTES (SYMBOL_NAME (obj));
1615 register int c;
1616 int i, i_byte, size_byte;
1617 Lisp_Object name;
1618
1619 name = SYMBOL_NAME (obj);
1620
1621 if (p != end && (*p == '-' || *p == '+')) p++;
1622 if (p == end)
1623 confusing = 0;
1624 /* If symbol name begins with a digit, and ends with a digit,
1625 and contains nothing but digits and `e', it could be treated
1626 as a number. So set CONFUSING.
1627
1628 Symbols that contain periods could also be taken as numbers,
1629 but periods are always escaped, so we don't have to worry
1630 about them here. */
1631 else if (*p >= '0' && *p <= '9'
1632 && end[-1] >= '0' && end[-1] <= '9')
1633 {
1634 while (p != end && ((*p >= '0' && *p <= '9')
1635 /* Needed for \2e10. */
1636 || *p == 'e'))
1637 p++;
1638 confusing = (end == p);
1639 }
1640 else
1641 confusing = 0;
1642
1643 if (! NILP (Vprint_gensym) && !SYMBOL_INTERNED_P (obj))
1644 {
1645 PRINTCHAR ('#');
1646 PRINTCHAR (':');
1647 }
1648
1649 size_byte = SBYTES (name);
1650
1651 for (i = 0, i_byte = 0; i_byte < size_byte;)
1652 {
1653 /* Here, we must convert each multi-byte form to the
1654 corresponding character code before handing it to PRINTCHAR. */
1655 FETCH_STRING_CHAR_ADVANCE (c, name, i, i_byte);
1656 QUIT;
1657
1658 if (escapeflag)
1659 {
1660 if (c == '\"' || c == '\\' || c == '\''
1661 || c == ';' || c == '#' || c == '(' || c == ')'
1662 || c == ',' || c =='.' || c == '`'
1663 || c == '[' || c == ']' || c == '?' || c <= 040
1664 || confusing)
1665 PRINTCHAR ('\\'), confusing = 0;
1666 }
1667 PRINTCHAR (c);
1668 }
1669 }
1670 break;
1671
1672 case Lisp_Cons:
1673 /* If deeper than spec'd depth, print placeholder. */
1674 if (INTEGERP (Vprint_level)
1675 && print_depth > XINT (Vprint_level))
1676 strout ("...", -1, -1, printcharfun, 0);
1677 else if (print_quoted && CONSP (XCDR (obj)) && NILP (XCDR (XCDR (obj)))
1678 && (EQ (XCAR (obj), Qquote)))
1679 {
1680 PRINTCHAR ('\'');
1681 print_object (XCAR (XCDR (obj)), printcharfun, escapeflag);
1682 }
1683 else if (print_quoted && CONSP (XCDR (obj)) && NILP (XCDR (XCDR (obj)))
1684 && (EQ (XCAR (obj), Qfunction)))
1685 {
1686 PRINTCHAR ('#');
1687 PRINTCHAR ('\'');
1688 print_object (XCAR (XCDR (obj)), printcharfun, escapeflag);
1689 }
1690 else if (print_quoted && CONSP (XCDR (obj)) && NILP (XCDR (XCDR (obj)))
1691 && ! old_backquote_output
1692 && ((EQ (XCAR (obj), Qbackquote)
1693 || EQ (XCAR (obj), Qcomma)
1694 || EQ (XCAR (obj), Qcomma_at)
1695 || EQ (XCAR (obj), Qcomma_dot))))
1696 {
1697 print_object (XCAR (obj), printcharfun, 0);
1698 print_object (XCAR (XCDR (obj)), printcharfun, escapeflag);
1699 }
1700 else
1701 {
1702 PRINTCHAR ('(');
1703
1704 /* If the first element is a backquote form,
1705 print it old-style so it won't be misunderstood. */
1706 if (print_quoted && CONSP (XCAR (obj))
1707 && CONSP (XCDR (XCAR (obj)))
1708 && NILP (XCDR (XCDR (XCAR (obj))))
1709 && EQ (XCAR (XCAR (obj)), Qbackquote))
1710 {
1711 Lisp_Object tem;
1712 tem = XCAR (obj);
1713 PRINTCHAR ('(');
1714
1715 print_object (Qbackquote, printcharfun, 0);
1716 PRINTCHAR (' ');
1717
1718 ++old_backquote_output;
1719 print_object (XCAR (XCDR (tem)), printcharfun, 0);
1720 --old_backquote_output;
1721 PRINTCHAR (')');
1722
1723 obj = XCDR (obj);
1724 }
1725
1726 {
1727 int print_length, i;
1728 Lisp_Object halftail = obj;
1729
1730 /* Negative values of print-length are invalid in CL.
1731 Treat them like nil, as CMUCL does. */
1732 if (NATNUMP (Vprint_length))
1733 print_length = XFASTINT (Vprint_length);
1734 else
1735 print_length = 0;
1736
1737 i = 0;
1738 while (CONSP (obj))
1739 {
1740 /* Detect circular list. */
1741 if (NILP (Vprint_circle))
1742 {
1743 /* Simple but imcomplete way. */
1744 if (i != 0 && EQ (obj, halftail))
1745 {
1746 sprintf (buf, " . #%d", i / 2);
1747 strout (buf, -1, -1, printcharfun, 0);
1748 goto end_of_list;
1749 }
1750 }
1751 else
1752 {
1753 /* With the print-circle feature. */
1754 if (i != 0)
1755 {
1756 int i;
1757 for (i = 0; i < print_number_index; i++)
1758 if (EQ (PRINT_NUMBER_OBJECT (Vprint_number_table, i),
1759 obj))
1760 {
1761 if (NILP (PRINT_NUMBER_STATUS (Vprint_number_table, i)))
1762 {
1763 strout (" . ", 3, 3, printcharfun, 0);
1764 print_object (obj, printcharfun, escapeflag);
1765 }
1766 else
1767 {
1768 sprintf (buf, " . #%d#", i + 1);
1769 strout (buf, -1, -1, printcharfun, 0);
1770 }
1771 goto end_of_list;
1772 }
1773 }
1774 }
1775
1776 if (i++)
1777 PRINTCHAR (' ');
1778
1779 if (print_length && i > print_length)
1780 {
1781 strout ("...", 3, 3, printcharfun, 0);
1782 goto end_of_list;
1783 }
1784
1785 print_object (XCAR (obj), printcharfun, escapeflag);
1786
1787 obj = XCDR (obj);
1788 if (!(i & 1))
1789 halftail = XCDR (halftail);
1790 }
1791 }
1792
1793 /* OBJ non-nil here means it's the end of a dotted list. */
1794 if (!NILP (obj))
1795 {
1796 strout (" . ", 3, 3, printcharfun, 0);
1797 print_object (obj, printcharfun, escapeflag);
1798 }
1799
1800 end_of_list:
1801 PRINTCHAR (')');
1802 }
1803 break;
1804
1805 case Lisp_Vectorlike:
1806 if (PROCESSP (obj))
1807 {
1808 if (escapeflag)
1809 {
1810 strout ("#<process ", -1, -1, printcharfun, 0);
1811 print_string (XPROCESS (obj)->name, printcharfun);
1812 PRINTCHAR ('>');
1813 }
1814 else
1815 print_string (XPROCESS (obj)->name, printcharfun);
1816 }
1817 else if (BOOL_VECTOR_P (obj))
1818 {
1819 register int i;
1820 register unsigned char c;
1821 struct gcpro gcpro1;
1822 int size_in_chars
1823 = ((XBOOL_VECTOR (obj)->size + BOOL_VECTOR_BITS_PER_CHAR - 1)
1824 / BOOL_VECTOR_BITS_PER_CHAR);
1825
1826 GCPRO1 (obj);
1827
1828 PRINTCHAR ('#');
1829 PRINTCHAR ('&');
1830 sprintf (buf, "%ld", (long) XBOOL_VECTOR (obj)->size);
1831 strout (buf, -1, -1, printcharfun, 0);
1832 PRINTCHAR ('\"');
1833
1834 /* Don't print more characters than the specified maximum.
1835 Negative values of print-length are invalid. Treat them
1836 like a print-length of nil. */
1837 if (NATNUMP (Vprint_length)
1838 && XFASTINT (Vprint_length) < size_in_chars)
1839 size_in_chars = XFASTINT (Vprint_length);
1840
1841 for (i = 0; i < size_in_chars; i++)
1842 {
1843 QUIT;
1844 c = XBOOL_VECTOR (obj)->data[i];
1845 if (c == '\n' && print_escape_newlines)
1846 {
1847 PRINTCHAR ('\\');
1848 PRINTCHAR ('n');
1849 }
1850 else if (c == '\f' && print_escape_newlines)
1851 {
1852 PRINTCHAR ('\\');
1853 PRINTCHAR ('f');
1854 }
1855 else if (c > '\177')
1856 {
1857 /* Use octal escapes to avoid encoding issues. */
1858 PRINTCHAR ('\\');
1859 PRINTCHAR ('0' + ((c >> 6) & 3));
1860 PRINTCHAR ('0' + ((c >> 3) & 7));
1861 PRINTCHAR ('0' + (c & 7));
1862 }
1863 else
1864 {
1865 if (c == '\"' || c == '\\')
1866 PRINTCHAR ('\\');
1867 PRINTCHAR (c);
1868 }
1869 }
1870 PRINTCHAR ('\"');
1871
1872 UNGCPRO;
1873 }
1874 else if (SUBRP (obj))
1875 {
1876 strout ("#<subr ", -1, -1, printcharfun, 0);
1877 strout (XSUBR (obj)->symbol_name, -1, -1, printcharfun, 0);
1878 PRINTCHAR ('>');
1879 }
1880 else if (WINDOWP (obj))
1881 {
1882 strout ("#<window ", -1, -1, printcharfun, 0);
1883 sprintf (buf, "%ld", (long) XFASTINT (XWINDOW (obj)->sequence_number));
1884 strout (buf, -1, -1, printcharfun, 0);
1885 if (!NILP (XWINDOW (obj)->buffer))
1886 {
1887 strout (" on ", -1, -1, printcharfun, 0);
1888 print_string (XBUFFER (XWINDOW (obj)->buffer)->name, printcharfun);
1889 }
1890 PRINTCHAR ('>');
1891 }
1892 else if (HASH_TABLE_P (obj))
1893 {
1894 struct Lisp_Hash_Table *h = XHASH_TABLE (obj);
1895 strout ("#<hash-table", -1, -1, printcharfun, 0);
1896 if (SYMBOLP (h->test))
1897 {
1898 PRINTCHAR (' ');
1899 PRINTCHAR ('\'');
1900 strout (SDATA (SYMBOL_NAME (h->test)), -1, -1, printcharfun, 0);
1901 PRINTCHAR (' ');
1902 strout (SDATA (SYMBOL_NAME (h->weak)), -1, -1, printcharfun, 0);
1903 PRINTCHAR (' ');
1904 sprintf (buf, "%ld/%ld", (long) XFASTINT (h->count),
1905 (long) XVECTOR (h->next)->size);
1906 strout (buf, -1, -1, printcharfun, 0);
1907 }
1908 sprintf (buf, " 0x%lx", (unsigned long) h);
1909 strout (buf, -1, -1, printcharfun, 0);
1910 PRINTCHAR ('>');
1911 }
1912 else if (BUFFERP (obj))
1913 {
1914 if (NILP (XBUFFER (obj)->name))
1915 strout ("#<killed buffer>", -1, -1, printcharfun, 0);
1916 else if (escapeflag)
1917 {
1918 strout ("#<buffer ", -1, -1, printcharfun, 0);
1919 print_string (XBUFFER (obj)->name, printcharfun);
1920 PRINTCHAR ('>');
1921 }
1922 else
1923 print_string (XBUFFER (obj)->name, printcharfun);
1924 }
1925 else if (WINDOW_CONFIGURATIONP (obj))
1926 {
1927 strout ("#<window-configuration>", -1, -1, printcharfun, 0);
1928 }
1929 else if (FRAMEP (obj))
1930 {
1931 strout ((FRAME_LIVE_P (XFRAME (obj))
1932 ? "#<frame " : "#<dead frame "),
1933 -1, -1, printcharfun, 0);
1934 print_string (XFRAME (obj)->name, printcharfun);
1935 sprintf (buf, " 0x%lx", (unsigned long) (XFRAME (obj)));
1936 strout (buf, -1, -1, printcharfun, 0);
1937 PRINTCHAR ('>');
1938 }
1939 else
1940 {
1941 EMACS_INT size = XVECTOR (obj)->size;
1942 if (COMPILEDP (obj))
1943 {
1944 PRINTCHAR ('#');
1945 size &= PSEUDOVECTOR_SIZE_MASK;
1946 }
1947 if (CHAR_TABLE_P (obj))
1948 {
1949 /* We print a char-table as if it were a vector,
1950 lumping the parent and default slots in with the
1951 character slots. But we add #^ as a prefix. */
1952 PRINTCHAR ('#');
1953 PRINTCHAR ('^');
1954 if (SUB_CHAR_TABLE_P (obj))
1955 PRINTCHAR ('^');
1956 size &= PSEUDOVECTOR_SIZE_MASK;
1957 }
1958 if (size & PSEUDOVECTOR_FLAG)
1959 goto badtype;
1960
1961 PRINTCHAR ('[');
1962 {
1963 register int i;
1964 register Lisp_Object tem;
1965 int real_size = size;
1966
1967 /* Don't print more elements than the specified maximum. */
1968 if (NATNUMP (Vprint_length)
1969 && XFASTINT (Vprint_length) < size)
1970 size = XFASTINT (Vprint_length);
1971
1972 for (i = 0; i < size; i++)
1973 {
1974 if (i) PRINTCHAR (' ');
1975 tem = XVECTOR (obj)->contents[i];
1976 print_object (tem, printcharfun, escapeflag);
1977 }
1978 if (size < real_size)
1979 strout (" ...", 4, 4, printcharfun, 0);
1980 }
1981 PRINTCHAR (']');
1982 }
1983 break;
1984
1985 case Lisp_Misc:
1986 switch (XMISCTYPE (obj))
1987 {
1988 case Lisp_Misc_Marker:
1989 strout ("#<marker ", -1, -1, printcharfun, 0);
1990 /* Do you think this is necessary? */
1991 if (XMARKER (obj)->insertion_type != 0)
1992 strout ("(moves after insertion) ", -1, -1, printcharfun, 0);
1993 if (!(XMARKER (obj)->buffer))
1994 strout ("in no buffer", -1, -1, printcharfun, 0);
1995 else
1996 {
1997 sprintf (buf, "at %d", marker_position (obj));
1998 strout (buf, -1, -1, printcharfun, 0);
1999 strout (" in ", -1, -1, printcharfun, 0);
2000 print_string (XMARKER (obj)->buffer->name, printcharfun);
2001 }
2002 PRINTCHAR ('>');
2003 break;
2004
2005 case Lisp_Misc_Overlay:
2006 strout ("#<overlay ", -1, -1, printcharfun, 0);
2007 if (!(XMARKER (OVERLAY_START (obj))->buffer))
2008 strout ("in no buffer", -1, -1, printcharfun, 0);
2009 else
2010 {
2011 sprintf (buf, "from %d to %d in ",
2012 marker_position (OVERLAY_START (obj)),
2013 marker_position (OVERLAY_END (obj)));
2014 strout (buf, -1, -1, printcharfun, 0);
2015 print_string (XMARKER (OVERLAY_START (obj))->buffer->name,
2016 printcharfun);
2017 }
2018 PRINTCHAR ('>');
2019 break;
2020
2021 /* Remaining cases shouldn't happen in normal usage, but let's print
2022 them anyway for the benefit of the debugger. */
2023 case Lisp_Misc_Free:
2024 strout ("#<misc free cell>", -1, -1, printcharfun, 0);
2025 break;
2026
2027 case Lisp_Misc_Intfwd:
2028 sprintf (buf, "#<intfwd to %ld>", (long) *XINTFWD (obj)->intvar);
2029 strout (buf, -1, -1, printcharfun, 0);
2030 break;
2031
2032 case Lisp_Misc_Boolfwd:
2033 sprintf (buf, "#<boolfwd to %s>",
2034 (*XBOOLFWD (obj)->boolvar ? "t" : "nil"));
2035 strout (buf, -1, -1, printcharfun, 0);
2036 break;
2037
2038 case Lisp_Misc_Objfwd:
2039 strout ("#<objfwd to ", -1, -1, printcharfun, 0);
2040 print_object (*XOBJFWD (obj)->objvar, printcharfun, escapeflag);
2041 PRINTCHAR ('>');
2042 break;
2043
2044 case Lisp_Misc_Buffer_Objfwd:
2045 strout ("#<buffer_objfwd to ", -1, -1, printcharfun, 0);
2046 print_object (PER_BUFFER_VALUE (current_buffer,
2047 XBUFFER_OBJFWD (obj)->offset),
2048 printcharfun, escapeflag);
2049 PRINTCHAR ('>');
2050 break;
2051
2052 case Lisp_Misc_Kboard_Objfwd:
2053 strout ("#<kboard_objfwd to ", -1, -1, printcharfun, 0);
2054 print_object (*(Lisp_Object *)((char *) current_kboard
2055 + XKBOARD_OBJFWD (obj)->offset),
2056 printcharfun, escapeflag);
2057 PRINTCHAR ('>');
2058 break;
2059
2060 case Lisp_Misc_Buffer_Local_Value:
2061 strout ("#<buffer_local_value ", -1, -1, printcharfun, 0);
2062 goto do_buffer_local;
2063 case Lisp_Misc_Some_Buffer_Local_Value:
2064 strout ("#<some_buffer_local_value ", -1, -1, printcharfun, 0);
2065 do_buffer_local:
2066 strout ("[realvalue] ", -1, -1, printcharfun, 0);
2067 print_object (XBUFFER_LOCAL_VALUE (obj)->realvalue,
2068 printcharfun, escapeflag);
2069 if (XBUFFER_LOCAL_VALUE (obj)->found_for_buffer)
2070 strout ("[local in buffer] ", -1, -1, printcharfun, 0);
2071 else
2072 strout ("[buffer] ", -1, -1, printcharfun, 0);
2073 print_object (XBUFFER_LOCAL_VALUE (obj)->buffer,
2074 printcharfun, escapeflag);
2075 if (XBUFFER_LOCAL_VALUE (obj)->check_frame)
2076 {
2077 if (XBUFFER_LOCAL_VALUE (obj)->found_for_frame)
2078 strout ("[local in frame] ", -1, -1, printcharfun, 0);
2079 else
2080 strout ("[frame] ", -1, -1, printcharfun, 0);
2081 print_object (XBUFFER_LOCAL_VALUE (obj)->frame,
2082 printcharfun, escapeflag);
2083 }
2084 strout ("[alist-elt] ", -1, -1, printcharfun, 0);
2085 print_object (XCAR (XBUFFER_LOCAL_VALUE (obj)->cdr),
2086 printcharfun, escapeflag);
2087 strout ("[default-value] ", -1, -1, printcharfun, 0);
2088 print_object (XCDR (XBUFFER_LOCAL_VALUE (obj)->cdr),
2089 printcharfun, escapeflag);
2090 PRINTCHAR ('>');
2091 break;
2092
2093 case Lisp_Misc_Save_Value:
2094 strout ("#<save_value ", -1, -1, printcharfun, 0);
2095 sprintf(buf, "ptr=0x%08lx int=%d",
2096 (unsigned long) XSAVE_VALUE (obj)->pointer,
2097 XSAVE_VALUE (obj)->integer);
2098 strout (buf, -1, -1, printcharfun, 0);
2099 PRINTCHAR ('>');
2100 break;
2101
2102 default:
2103 goto badtype;
2104 }
2105 break;
2106
2107 default:
2108 badtype:
2109 {
2110 /* We're in trouble if this happens!
2111 Probably should just abort () */
2112 strout ("#<EMACS BUG: INVALID DATATYPE ", -1, -1, printcharfun, 0);
2113 if (MISCP (obj))
2114 sprintf (buf, "(MISC 0x%04x)", (int) XMISCTYPE (obj));
2115 else if (VECTORLIKEP (obj))
2116 sprintf (buf, "(PVEC 0x%08x)", (int) XVECTOR (obj)->size);
2117 else
2118 sprintf (buf, "(0x%02x)", (int) XTYPE (obj));
2119 strout (buf, -1, -1, printcharfun, 0);
2120 strout (" Save your buffers immediately and please report this bug>",
2121 -1, -1, printcharfun, 0);
2122 }
2123 }
2124
2125 print_depth--;
2126 }
2127 \f
2128
2129 /* Print a description of INTERVAL using PRINTCHARFUN.
2130 This is part of printing a string that has text properties. */
2131
2132 void
2133 print_interval (interval, printcharfun)
2134 INTERVAL interval;
2135 Lisp_Object printcharfun;
2136 {
2137 PRINTCHAR (' ');
2138 print_object (make_number (interval->position), printcharfun, 1);
2139 PRINTCHAR (' ');
2140 print_object (make_number (interval->position + LENGTH (interval)),
2141 printcharfun, 1);
2142 PRINTCHAR (' ');
2143 print_object (interval->plist, printcharfun, 1);
2144 }
2145
2146 \f
2147 void
2148 syms_of_print ()
2149 {
2150 Qtemp_buffer_setup_hook = intern ("temp-buffer-setup-hook");
2151 staticpro (&Qtemp_buffer_setup_hook);
2152
2153 DEFVAR_LISP ("standard-output", &Vstandard_output,
2154 doc: /* Output stream `print' uses by default for outputting a character.
2155 This may be any function of one argument.
2156 It may also be a buffer (output is inserted before point)
2157 or a marker (output is inserted and the marker is advanced)
2158 or the symbol t (output appears in the echo area). */);
2159 Vstandard_output = Qt;
2160 Qstandard_output = intern ("standard-output");
2161 staticpro (&Qstandard_output);
2162
2163 DEFVAR_LISP ("float-output-format", &Vfloat_output_format,
2164 doc: /* The format descriptor string used to print floats.
2165 This is a %-spec like those accepted by `printf' in C,
2166 but with some restrictions. It must start with the two characters `%.'.
2167 After that comes an integer precision specification,
2168 and then a letter which controls the format.
2169 The letters allowed are `e', `f' and `g'.
2170 Use `e' for exponential notation \"DIG.DIGITSeEXPT\"
2171 Use `f' for decimal point notation \"DIGITS.DIGITS\".
2172 Use `g' to choose the shorter of those two formats for the number at hand.
2173 The precision in any of these cases is the number of digits following
2174 the decimal point. With `f', a precision of 0 means to omit the
2175 decimal point. 0 is not allowed with `e' or `g'.
2176
2177 A value of nil means to use the shortest notation
2178 that represents the number without losing information. */);
2179 Vfloat_output_format = Qnil;
2180 Qfloat_output_format = intern ("float-output-format");
2181 staticpro (&Qfloat_output_format);
2182
2183 DEFVAR_LISP ("print-length", &Vprint_length,
2184 doc: /* Maximum length of list to print before abbreviating.
2185 A value of nil means no limit. See also `eval-expression-print-length'. */);
2186 Vprint_length = Qnil;
2187
2188 DEFVAR_LISP ("print-level", &Vprint_level,
2189 doc: /* Maximum depth of list nesting to print before abbreviating.
2190 A value of nil means no limit. See also `eval-expression-print-level'. */);
2191 Vprint_level = Qnil;
2192
2193 DEFVAR_BOOL ("print-escape-newlines", &print_escape_newlines,
2194 doc: /* Non-nil means print newlines in strings as `\\n'.
2195 Also print formfeeds as `\\f'. */);
2196 print_escape_newlines = 0;
2197
2198 DEFVAR_BOOL ("print-escape-nonascii", &print_escape_nonascii,
2199 doc: /* Non-nil means print unibyte non-ASCII chars in strings as \\OOO.
2200 \(OOO is the octal representation of the character code.)
2201 Only single-byte characters are affected, and only in `prin1'.
2202 When the output goes in a multibyte buffer, this feature is
2203 enabled regardless of the value of the variable. */);
2204 print_escape_nonascii = 0;
2205
2206 DEFVAR_BOOL ("print-escape-multibyte", &print_escape_multibyte,
2207 doc: /* Non-nil means print multibyte characters in strings as \\xXXXX.
2208 \(XXXX is the hex representation of the character code.)
2209 This affects only `prin1'. */);
2210 print_escape_multibyte = 0;
2211
2212 DEFVAR_BOOL ("print-quoted", &print_quoted,
2213 doc: /* Non-nil means print quoted forms with reader syntax.
2214 I.e., (quote foo) prints as 'foo, (function foo) as #'foo, and backquoted
2215 forms print as in the new syntax. */);
2216 print_quoted = 0;
2217
2218 DEFVAR_LISP ("print-gensym", &Vprint_gensym,
2219 doc: /* Non-nil means print uninterned symbols so they will read as uninterned.
2220 I.e., the value of (make-symbol \"foobar\") prints as #:foobar.
2221 When the uninterned symbol appears within a recursive data structure,
2222 and the symbol appears more than once, in addition use the #N# and #N=
2223 constructs as needed, so that multiple references to the same symbol are
2224 shared once again when the text is read back. */);
2225 Vprint_gensym = Qnil;
2226
2227 DEFVAR_LISP ("print-circle", &Vprint_circle,
2228 doc: /* *Non-nil means print recursive structures using #N= and #N# syntax.
2229 If nil, printing proceeds recursively and may lead to
2230 `max-lisp-eval-depth' being exceeded or an error may occur:
2231 \"Apparently circular structure being printed.\" Also see
2232 `print-length' and `print-level'.
2233 If non-nil, shared substructures anywhere in the structure are printed
2234 with `#N=' before the first occurrence (in the order of the print
2235 representation) and `#N#' in place of each subsequent occurrence,
2236 where N is a positive decimal integer. */);
2237 Vprint_circle = Qnil;
2238
2239 DEFVAR_LISP ("print-continuous-numbering", &Vprint_continuous_numbering,
2240 doc: /* *Non-nil means number continuously across print calls.
2241 This affects the numbers printed for #N= labels and #M# references.
2242 See also `print-circle', `print-gensym', and `print-number-table'.
2243 This variable should not be set with `setq'; bind it with a `let' instead. */);
2244 Vprint_continuous_numbering = Qnil;
2245
2246 DEFVAR_LISP ("print-number-table", &Vprint_number_table,
2247 doc: /* A vector used internally to produce `#N=' labels and `#N#' references.
2248 The Lisp printer uses this vector to detect Lisp objects referenced more
2249 than once.
2250
2251 When you bind `print-continuous-numbering' to t, you should probably
2252 also bind `print-number-table' to nil. This ensures that the value of
2253 `print-number-table' can be garbage-collected once the printing is
2254 done. If all elements of `print-number-table' are nil, it means that
2255 the printing done so far has not found any shared structure or objects
2256 that need to be recorded in the table. */);
2257 Vprint_number_table = Qnil;
2258
2259 /* prin1_to_string_buffer initialized in init_buffer_once in buffer.c */
2260 staticpro (&Vprin1_to_string_buffer);
2261
2262 defsubr (&Sprin1);
2263 defsubr (&Sprin1_to_string);
2264 defsubr (&Serror_message_string);
2265 defsubr (&Sprinc);
2266 defsubr (&Sprint);
2267 defsubr (&Sterpri);
2268 defsubr (&Swrite_char);
2269 defsubr (&Sexternal_debugging_output);
2270 #ifdef WITH_REDIRECT_DEBUGGING_OUTPUT
2271 defsubr (&Sredirect_debugging_output);
2272 #endif
2273
2274 Qexternal_debugging_output = intern ("external-debugging-output");
2275 staticpro (&Qexternal_debugging_output);
2276
2277 Qprint_escape_newlines = intern ("print-escape-newlines");
2278 staticpro (&Qprint_escape_newlines);
2279
2280 Qprint_escape_multibyte = intern ("print-escape-multibyte");
2281 staticpro (&Qprint_escape_multibyte);
2282
2283 Qprint_escape_nonascii = intern ("print-escape-nonascii");
2284 staticpro (&Qprint_escape_nonascii);
2285
2286 defsubr (&Swith_output_to_temp_buffer);
2287 }
2288
2289 /* arch-tag: bc797170-94ae-41de-86e3-75e20f8f7a39
2290 (do not change this comment) */