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