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