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