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