Merge from origin/emacs-24
[emacs.git] / src / print.c
blob7723b98348a7711d6a1f5d11359f157baf31e322
1 /* Lisp object printing and output streams.
3 Copyright (C) 1985-1986, 1988, 1993-1995, 1997-2014 Free Software
4 Foundation, Inc.
6 This file is part of GNU Emacs.
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.
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.
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/>. */
22 #include <config.h>
23 #include "sysstdio.h"
25 #include "lisp.h"
26 #include "character.h"
27 #include "buffer.h"
28 #include "charset.h"
29 #include "keyboard.h"
30 #include "frame.h"
31 #include "window.h"
32 #include "process.h"
33 #include "dispextern.h"
34 #include "termchar.h"
35 #include "intervals.h"
36 #include "blockinput.h"
37 #include "termhooks.h" /* For struct terminal. */
38 #include "font.h"
40 Lisp_Object Qstandard_output;
42 static Lisp_Object Qtemp_buffer_setup_hook;
44 /* These are used to print like we read. */
46 static Lisp_Object Qfloat_output_format;
48 #include <float.h>
49 #include <ftoastr.h>
51 /* Avoid actual stack overflow in print. */
52 static ptrdiff_t print_depth;
54 /* Level of nesting inside outputting backquote in new style. */
55 static ptrdiff_t new_backquote_output;
57 /* Detect most circularities to print finite output. */
58 #define PRINT_CIRCLE 200
59 static Lisp_Object being_printed[PRINT_CIRCLE];
61 /* Last char printed to stdout by printchar. */
62 static unsigned int printchar_stdout_last;
64 /* When printing into a buffer, first we put the text in this
65 block, then insert it all at once. */
66 static char *print_buffer;
68 /* Size allocated in print_buffer. */
69 static ptrdiff_t print_buffer_size;
70 /* Chars stored in print_buffer. */
71 static ptrdiff_t print_buffer_pos;
72 /* Bytes stored in print_buffer. */
73 static ptrdiff_t print_buffer_pos_byte;
75 Lisp_Object Qprint_escape_newlines;
76 static Lisp_Object Qprint_escape_multibyte, Qprint_escape_nonascii;
78 /* Vprint_number_table is a table, that keeps objects that are going to
79 be printed, to allow use of #n= and #n# to express sharing.
80 For any given object, the table can give the following values:
81 t the object will be printed only once.
82 -N the object will be printed several times and will take number N.
83 N the object has been printed so we can refer to it as #N#.
84 print_number_index holds the largest N already used.
85 N has to be striclty larger than 0 since we need to distinguish -N. */
86 static ptrdiff_t print_number_index;
87 static void print_interval (INTERVAL interval, Lisp_Object printcharfun);
89 /* GDB resets this to zero on W32 to disable OutputDebugString calls. */
90 bool print_output_debug_flag EXTERNALLY_VISIBLE = 1;
93 /* Low level output routines for characters and strings. */
95 /* Lisp functions to do output using a stream
96 must have the stream in a variable called printcharfun
97 and must start with PRINTPREPARE, end with PRINTFINISH,
98 and use PRINTDECLARE to declare common variables.
99 Use PRINTCHAR to output one character,
100 or call strout to output a block of characters. */
102 #define PRINTDECLARE \
103 struct buffer *old = current_buffer; \
104 ptrdiff_t old_point = -1, start_point = -1; \
105 ptrdiff_t old_point_byte = -1, start_point_byte = -1; \
106 ptrdiff_t specpdl_count = SPECPDL_INDEX (); \
107 bool free_print_buffer = 0; \
108 bool multibyte \
109 = !NILP (BVAR (current_buffer, enable_multibyte_characters)); \
110 Lisp_Object original
112 #define PRINTPREPARE \
113 original = printcharfun; \
114 if (NILP (printcharfun)) printcharfun = Qt; \
115 if (BUFFERP (printcharfun)) \
117 if (XBUFFER (printcharfun) != current_buffer) \
118 Fset_buffer (printcharfun); \
119 printcharfun = Qnil; \
121 if (MARKERP (printcharfun)) \
123 ptrdiff_t marker_pos; \
124 if (! XMARKER (printcharfun)->buffer) \
125 error ("Marker does not point anywhere"); \
126 if (XMARKER (printcharfun)->buffer != current_buffer) \
127 set_buffer_internal (XMARKER (printcharfun)->buffer); \
128 marker_pos = marker_position (printcharfun); \
129 if (marker_pos < BEGV || marker_pos > ZV) \
130 signal_error ("Marker is outside the accessible " \
131 "part of the buffer", printcharfun); \
132 old_point = PT; \
133 old_point_byte = PT_BYTE; \
134 SET_PT_BOTH (marker_pos, \
135 marker_byte_position (printcharfun)); \
136 start_point = PT; \
137 start_point_byte = PT_BYTE; \
138 printcharfun = Qnil; \
140 if (NILP (printcharfun)) \
142 Lisp_Object string; \
143 if (NILP (BVAR (current_buffer, enable_multibyte_characters)) \
144 && ! print_escape_multibyte) \
145 specbind (Qprint_escape_multibyte, Qt); \
146 if (! NILP (BVAR (current_buffer, enable_multibyte_characters)) \
147 && ! print_escape_nonascii) \
148 specbind (Qprint_escape_nonascii, Qt); \
149 if (print_buffer != 0) \
151 string = make_string_from_bytes (print_buffer, \
152 print_buffer_pos, \
153 print_buffer_pos_byte); \
154 record_unwind_protect (print_unwind, string); \
156 else \
158 int new_size = 1000; \
159 print_buffer = xmalloc (new_size); \
160 print_buffer_size = new_size; \
161 free_print_buffer = 1; \
163 print_buffer_pos = 0; \
164 print_buffer_pos_byte = 0; \
166 if (EQ (printcharfun, Qt) && ! noninteractive) \
167 setup_echo_area_for_printing (multibyte);
169 #define PRINTFINISH \
170 if (NILP (printcharfun)) \
172 if (print_buffer_pos != print_buffer_pos_byte \
173 && NILP (BVAR (current_buffer, enable_multibyte_characters)))\
175 USE_SAFE_ALLOCA; \
176 unsigned char *temp = SAFE_ALLOCA (print_buffer_pos + 1); \
177 copy_text ((unsigned char *) print_buffer, temp, \
178 print_buffer_pos_byte, 1, 0); \
179 insert_1_both ((char *) temp, print_buffer_pos, \
180 print_buffer_pos, 0, 1, 0); \
181 SAFE_FREE (); \
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);\
188 if (free_print_buffer) \
190 xfree (print_buffer); \
191 print_buffer = 0; \
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 set_buffer_internal (old);
203 #define PRINTCHAR(ch) printchar (ch, printcharfun)
205 /* This is used to restore the saved contents of print_buffer
206 when there is a recursive call to print. */
208 static void
209 print_unwind (Lisp_Object saved_text)
211 memcpy (print_buffer, SDATA (saved_text), SCHARS (saved_text));
215 /* Print character CH using method FUN. FUN nil means print to
216 print_buffer. FUN t means print to echo area or stdout if
217 non-interactive. If FUN is neither nil nor t, call FUN with CH as
218 argument. */
220 static void
221 printchar (unsigned int ch, Lisp_Object fun)
223 if (!NILP (fun) && !EQ (fun, Qt))
224 call1 (fun, make_number (ch));
225 else
227 unsigned char str[MAX_MULTIBYTE_LENGTH];
228 int len = CHAR_STRING (ch, str);
230 QUIT;
232 if (NILP (fun))
234 ptrdiff_t incr = len - (print_buffer_size - print_buffer_pos_byte);
235 if (incr > 0)
236 print_buffer = xpalloc (print_buffer, &print_buffer_size,
237 incr, -1, 1);
238 memcpy (print_buffer + print_buffer_pos_byte, str, len);
239 print_buffer_pos += 1;
240 print_buffer_pos_byte += len;
242 else if (noninteractive)
244 printchar_stdout_last = ch;
245 fwrite (str, 1, len, stdout);
246 noninteractive_need_newline = 1;
248 else
250 bool multibyte_p
251 = !NILP (BVAR (current_buffer, enable_multibyte_characters));
253 setup_echo_area_for_printing (multibyte_p);
254 insert_char (ch);
255 message_dolog ((char *) str, len, 0, multibyte_p);
261 /* Output SIZE characters, SIZE_BYTE bytes from string PTR using
262 method PRINTCHARFUN. If SIZE < 0, use the string length of PTR for
263 both SIZE and SIZE_BYTE. PRINTCHARFUN nil means output to
264 print_buffer. PRINTCHARFUN t means output to the echo area or to
265 stdout if non-interactive. If neither nil nor t, call Lisp
266 function PRINTCHARFUN for each character printed. MULTIBYTE
267 non-zero means PTR contains multibyte characters.
269 In the case where PRINTCHARFUN is nil, it is safe for PTR to point
270 to data in a Lisp string. Otherwise that is not safe. */
272 static void
273 strout (const char *ptr, ptrdiff_t size, ptrdiff_t size_byte,
274 Lisp_Object printcharfun)
276 if (size < 0)
277 size_byte = size = strlen (ptr);
279 if (NILP (printcharfun))
281 ptrdiff_t incr = size_byte - (print_buffer_size - print_buffer_pos_byte);
282 if (incr > 0)
283 print_buffer = xpalloc (print_buffer, &print_buffer_size, incr, -1, 1);
284 memcpy (print_buffer + print_buffer_pos_byte, ptr, size_byte);
285 print_buffer_pos += size;
286 print_buffer_pos_byte += size_byte;
288 else if (noninteractive && EQ (printcharfun, Qt))
290 fwrite (ptr, 1, size_byte, stdout);
291 noninteractive_need_newline = 1;
293 else if (EQ (printcharfun, Qt))
295 /* Output to echo area. We're trying to avoid a little overhead
296 here, that's the reason we don't call printchar to do the
297 job. */
298 int i;
299 bool multibyte_p
300 = !NILP (BVAR (current_buffer, enable_multibyte_characters));
302 setup_echo_area_for_printing (multibyte_p);
303 message_dolog (ptr, size_byte, 0, multibyte_p);
305 if (size == size_byte)
307 for (i = 0; i < size; ++i)
308 insert_char ((unsigned char) *ptr++);
310 else
312 int len;
313 for (i = 0; i < size_byte; i += len)
315 int ch = STRING_CHAR_AND_LENGTH ((const unsigned char *) ptr + i,
316 len);
317 insert_char (ch);
321 else
323 /* PRINTCHARFUN is a Lisp function. */
324 ptrdiff_t i = 0;
326 if (size == size_byte)
328 while (i < size_byte)
330 int ch = ptr[i++];
331 PRINTCHAR (ch);
334 else
336 while (i < size_byte)
338 /* Here, we must convert each multi-byte form to the
339 corresponding character code before handing it to
340 PRINTCHAR. */
341 int len;
342 int ch = STRING_CHAR_AND_LENGTH ((const unsigned char *) ptr + i,
343 len);
344 PRINTCHAR (ch);
345 i += len;
351 /* Print the contents of a string STRING using PRINTCHARFUN.
352 It isn't safe to use strout in many cases,
353 because printing one char can relocate. */
355 static void
356 print_string (Lisp_Object string, Lisp_Object printcharfun)
358 if (EQ (printcharfun, Qt) || NILP (printcharfun))
360 ptrdiff_t chars;
362 if (print_escape_nonascii)
363 string = string_escape_byte8 (string);
365 if (STRING_MULTIBYTE (string))
366 chars = SCHARS (string);
367 else if (! print_escape_nonascii
368 && (EQ (printcharfun, Qt)
369 ? ! NILP (BVAR (&buffer_defaults, enable_multibyte_characters))
370 : ! NILP (BVAR (current_buffer, enable_multibyte_characters))))
372 /* If unibyte string STRING contains 8-bit codes, we must
373 convert STRING to a multibyte string containing the same
374 character codes. */
375 Lisp_Object newstr;
376 ptrdiff_t bytes;
378 chars = SBYTES (string);
379 bytes = count_size_as_multibyte (SDATA (string), chars);
380 if (chars < bytes)
382 newstr = make_uninit_multibyte_string (chars, bytes);
383 memcpy (SDATA (newstr), SDATA (string), chars);
384 str_to_multibyte (SDATA (newstr), bytes, chars);
385 string = newstr;
388 else
389 chars = SBYTES (string);
391 if (EQ (printcharfun, Qt))
393 /* Output to echo area. */
394 ptrdiff_t nbytes = SBYTES (string);
396 /* Copy the string contents so that relocation of STRING by
397 GC does not cause trouble. */
398 USE_SAFE_ALLOCA;
399 char *buffer = SAFE_ALLOCA (nbytes);
400 memcpy (buffer, SDATA (string), nbytes);
402 strout (buffer, chars, nbytes, printcharfun);
404 SAFE_FREE ();
406 else
407 /* No need to copy, since output to print_buffer can't GC. */
408 strout (SSDATA (string), chars, SBYTES (string), printcharfun);
410 else
412 /* Otherwise, string may be relocated by printing one char.
413 So re-fetch the string address for each character. */
414 ptrdiff_t i;
415 ptrdiff_t size = SCHARS (string);
416 ptrdiff_t size_byte = SBYTES (string);
417 struct gcpro gcpro1;
418 GCPRO1 (string);
419 if (size == size_byte)
420 for (i = 0; i < size; i++)
421 PRINTCHAR (SREF (string, i));
422 else
423 for (i = 0; i < size_byte; )
425 /* Here, we must convert each multi-byte form to the
426 corresponding character code before handing it to PRINTCHAR. */
427 int len;
428 int ch = STRING_CHAR_AND_LENGTH (SDATA (string) + i, len);
429 PRINTCHAR (ch);
430 i += len;
432 UNGCPRO;
436 DEFUN ("write-char", Fwrite_char, Swrite_char, 1, 2, 0,
437 doc: /* Output character CHARACTER to stream PRINTCHARFUN.
438 PRINTCHARFUN defaults to the value of `standard-output' (which see). */)
439 (Lisp_Object character, Lisp_Object printcharfun)
441 PRINTDECLARE;
443 if (NILP (printcharfun))
444 printcharfun = Vstandard_output;
445 CHECK_NUMBER (character);
446 PRINTPREPARE;
447 PRINTCHAR (XINT (character));
448 PRINTFINISH;
449 return character;
452 /* Used from outside of print.c to print a block of SIZE
453 single-byte chars at DATA on the default output stream.
454 Do not use this on the contents of a Lisp string. */
456 void
457 write_string (const char *data, int size)
459 PRINTDECLARE;
460 Lisp_Object printcharfun;
462 printcharfun = Vstandard_output;
464 PRINTPREPARE;
465 strout (data, size, size, printcharfun);
466 PRINTFINISH;
469 /* Used to print a block of SIZE single-byte chars at DATA on a
470 specified stream PRINTCHARFUN.
471 Do not use this on the contents of a Lisp string. */
473 static void
474 write_string_1 (const char *data, int size, Lisp_Object printcharfun)
476 PRINTDECLARE;
478 PRINTPREPARE;
479 strout (data, size, size, printcharfun);
480 PRINTFINISH;
484 void
485 temp_output_buffer_setup (const char *bufname)
487 ptrdiff_t count = SPECPDL_INDEX ();
488 register struct buffer *old = current_buffer;
489 register Lisp_Object buf;
491 record_unwind_current_buffer ();
493 Fset_buffer (Fget_buffer_create (build_string (bufname)));
495 Fkill_all_local_variables ();
496 delete_all_overlays (current_buffer);
497 bset_directory (current_buffer, BVAR (old, directory));
498 bset_read_only (current_buffer, Qnil);
499 bset_filename (current_buffer, Qnil);
500 bset_undo_list (current_buffer, Qt);
501 eassert (current_buffer->overlays_before == NULL);
502 eassert (current_buffer->overlays_after == NULL);
503 bset_enable_multibyte_characters
504 (current_buffer, BVAR (&buffer_defaults, enable_multibyte_characters));
505 specbind (Qinhibit_read_only, Qt);
506 specbind (Qinhibit_modification_hooks, Qt);
507 Ferase_buffer ();
508 XSETBUFFER (buf, current_buffer);
510 Frun_hooks (1, &Qtemp_buffer_setup_hook);
512 unbind_to (count, Qnil);
514 specbind (Qstandard_output, buf);
517 static void print (Lisp_Object, Lisp_Object, bool);
518 static void print_preprocess (Lisp_Object);
519 static void print_preprocess_string (INTERVAL, Lisp_Object);
520 static void print_object (Lisp_Object, Lisp_Object, bool);
522 DEFUN ("terpri", Fterpri, Sterpri, 0, 2, 0,
523 doc: /* Output a newline to stream PRINTCHARFUN.
524 If ENSURE is non-nil only output a newline if not already at the
525 beginning of a line. Value is non-nil if a newline is printed.
526 If PRINTCHARFUN is omitted or nil, the value of `standard-output' is used. */)
527 (Lisp_Object printcharfun, Lisp_Object ensure)
529 Lisp_Object val = Qnil;
531 PRINTDECLARE;
532 if (NILP (printcharfun))
533 printcharfun = Vstandard_output;
534 PRINTPREPARE;
536 if (NILP (ensure))
537 val = Qt;
538 /* Difficult to check if at line beginning so abort. */
539 else if (FUNCTIONP (printcharfun))
540 signal_error ("Unsupported function argument", printcharfun);
541 else if (noninteractive && !NILP (printcharfun))
542 val = printchar_stdout_last == 10 ? Qnil : Qt;
543 else if (NILP (Fbolp ()))
544 val = Qt;
546 if (!NILP (val)) PRINTCHAR ('\n');
547 PRINTFINISH;
548 return val;
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.
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.
560 A printed representation of an object is text which describes that object.
562 Optional argument PRINTCHARFUN is the output stream, which can be one
563 of these:
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.
572 If PRINTCHARFUN is omitted, the value of `standard-output' (which see)
573 is used instead. */)
574 (Lisp_Object object, Lisp_Object printcharfun)
576 PRINTDECLARE;
578 if (NILP (printcharfun))
579 printcharfun = Vstandard_output;
580 PRINTPREPARE;
581 print (object, printcharfun, 1);
582 PRINTFINISH;
583 return object;
586 /* a buffer which is used to hold output being built by prin1-to-string */
587 Lisp_Object Vprin1_to_string_buffer;
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.
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.
599 A printed representation of an object is text which describes that object. */)
600 (Lisp_Object object, Lisp_Object noescape)
602 Lisp_Object printcharfun;
603 bool prev_abort_on_gc;
604 Lisp_Object save_deactivate_mark;
605 ptrdiff_t count = SPECPDL_INDEX ();
606 struct buffer *previous;
608 specbind (Qinhibit_modification_hooks, Qt);
611 PRINTDECLARE;
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 prev_abort_on_gc = abort_on_gc;
618 abort_on_gc = 1;
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;
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);
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);
639 Vdeactivate_mark = save_deactivate_mark;
641 abort_on_gc = prev_abort_on_gc;
642 return unbind_to (count, object);
645 DEFUN ("princ", Fprinc, Sprinc, 1, 2, 0,
646 doc: /* Output the printed representation of OBJECT, any Lisp object.
647 No quoting characters are used; no delimiters are printed around
648 the contents of strings.
650 OBJECT is any of the Lisp data types: a number, a string, a symbol,
651 a list, a buffer, a window, a frame, etc.
653 A printed representation of an object is text which describes that object.
655 Optional argument PRINTCHARFUN is the output stream, which can be one
656 of these:
658 - a buffer, in which case output is inserted into that buffer at point;
659 - a marker, in which case output is inserted at marker's position;
660 - a function, in which case that function is called once for each
661 character of OBJECT's printed representation;
662 - a symbol, in which case that symbol's function definition is called; or
663 - t, in which case the output is displayed in the echo area.
665 If PRINTCHARFUN is omitted, the value of `standard-output' (which see)
666 is used instead. */)
667 (Lisp_Object object, Lisp_Object printcharfun)
669 PRINTDECLARE;
671 if (NILP (printcharfun))
672 printcharfun = Vstandard_output;
673 PRINTPREPARE;
674 print (object, printcharfun, 0);
675 PRINTFINISH;
676 return object;
679 DEFUN ("print", Fprint, Sprint, 1, 2, 0,
680 doc: /* Output the printed representation of OBJECT, with newlines around it.
681 Quoting characters are printed when needed to make output that `read'
682 can handle, whenever this is possible. For complex objects, the behavior
683 is controlled by `print-level' and `print-length', which see.
685 OBJECT is any of the Lisp data types: a number, a string, a symbol,
686 a list, a buffer, a window, a frame, etc.
688 A printed representation of an object is text which describes that object.
690 Optional argument PRINTCHARFUN is the output stream, which can be one
691 of these:
693 - a buffer, in which case output is inserted into that buffer at point;
694 - a marker, in which case output is inserted at marker's position;
695 - a function, in which case that function is called once for each
696 character of OBJECT's printed representation;
697 - a symbol, in which case that symbol's function definition is called; or
698 - t, in which case the output is displayed in the echo area.
700 If PRINTCHARFUN is omitted, the value of `standard-output' (which see)
701 is used instead. */)
702 (Lisp_Object object, Lisp_Object printcharfun)
704 PRINTDECLARE;
705 struct gcpro gcpro1;
707 if (NILP (printcharfun))
708 printcharfun = Vstandard_output;
709 GCPRO1 (object);
710 PRINTPREPARE;
711 PRINTCHAR ('\n');
712 print (object, printcharfun, 1);
713 PRINTCHAR ('\n');
714 PRINTFINISH;
715 UNGCPRO;
716 return object;
719 /* The subroutine object for external-debugging-output is kept here
720 for the convenience of the debugger. */
721 Lisp_Object Qexternal_debugging_output;
723 DEFUN ("external-debugging-output", Fexternal_debugging_output, Sexternal_debugging_output, 1, 1, 0,
724 doc: /* Write CHARACTER to stderr.
725 You can call print while debugging emacs, and pass it this function
726 to make it write to the debugging output. */)
727 (Lisp_Object character)
729 unsigned int ch;
731 CHECK_NUMBER (character);
732 ch = XINT (character);
733 if (ASCII_CHAR_P (ch))
735 putc (ch, stderr);
736 #ifdef WINDOWSNT
737 /* Send the output to a debugger (nothing happens if there isn't
738 one). */
739 if (print_output_debug_flag)
741 char buf[2] = {(char) XINT (character), '\0'};
742 OutputDebugString (buf);
744 #endif
746 else
748 unsigned char mbstr[MAX_MULTIBYTE_LENGTH];
749 ptrdiff_t len = CHAR_STRING (ch, mbstr);
750 Lisp_Object encoded_ch =
751 ENCODE_SYSTEM (make_multibyte_string ((char *) mbstr, 1, len));
753 fwrite (SSDATA (encoded_ch), SBYTES (encoded_ch), 1, stderr);
754 #ifdef WINDOWSNT
755 if (print_output_debug_flag)
756 OutputDebugString (SSDATA (encoded_ch));
757 #endif
760 return character;
763 /* This function is never called. Its purpose is to prevent
764 print_output_debug_flag from being optimized away. */
766 extern void debug_output_compilation_hack (bool) EXTERNALLY_VISIBLE;
767 void
768 debug_output_compilation_hack (bool x)
770 print_output_debug_flag = x;
773 #if defined (GNU_LINUX)
775 /* This functionality is not vitally important in general, so we rely on
776 non-portable ability to use stderr as lvalue. */
778 #define WITH_REDIRECT_DEBUGGING_OUTPUT 1
780 static FILE *initial_stderr_stream = NULL;
782 DEFUN ("redirect-debugging-output", Fredirect_debugging_output, Sredirect_debugging_output,
783 1, 2,
784 "FDebug output file: \nP",
785 doc: /* Redirect debugging output (stderr stream) to file FILE.
786 If FILE is nil, reset target to the initial stderr stream.
787 Optional arg APPEND non-nil (interactively, with prefix arg) means
788 append to existing target file. */)
789 (Lisp_Object file, Lisp_Object append)
791 if (initial_stderr_stream != NULL)
793 block_input ();
794 fclose (stderr);
795 unblock_input ();
797 stderr = initial_stderr_stream;
798 initial_stderr_stream = NULL;
800 if (STRINGP (file))
802 file = Fexpand_file_name (file, Qnil);
803 initial_stderr_stream = stderr;
804 stderr = emacs_fopen (SSDATA (file), NILP (append) ? "w" : "a");
805 if (stderr == NULL)
807 stderr = initial_stderr_stream;
808 initial_stderr_stream = NULL;
809 report_file_error ("Cannot open debugging output stream", file);
812 return Qnil;
814 #endif /* GNU_LINUX */
817 /* This is the interface for debugging printing. */
819 void
820 debug_print (Lisp_Object arg)
822 Fprin1 (arg, Qexternal_debugging_output);
823 fprintf (stderr, "\r\n");
826 void safe_debug_print (Lisp_Object) EXTERNALLY_VISIBLE;
827 void
828 safe_debug_print (Lisp_Object arg)
830 int valid = valid_lisp_object_p (arg);
832 if (valid > 0)
833 debug_print (arg);
834 else
835 fprintf (stderr, "#<%s_LISP_OBJECT 0x%08"pI"x>\r\n",
836 !valid ? "INVALID" : "SOME",
837 XLI (arg));
841 DEFUN ("error-message-string", Ferror_message_string, Serror_message_string,
842 1, 1, 0,
843 doc: /* Convert an error value (ERROR-SYMBOL . DATA) to an error message.
844 See Info anchor `(elisp)Definition of signal' for some details on how this
845 error message is constructed. */)
846 (Lisp_Object obj)
848 struct buffer *old = current_buffer;
849 Lisp_Object value;
850 struct gcpro gcpro1;
852 /* If OBJ is (error STRING), just return STRING.
853 That is not only faster, it also avoids the need to allocate
854 space here when the error is due to memory full. */
855 if (CONSP (obj) && EQ (XCAR (obj), Qerror)
856 && CONSP (XCDR (obj))
857 && STRINGP (XCAR (XCDR (obj)))
858 && NILP (XCDR (XCDR (obj))))
859 return XCAR (XCDR (obj));
861 print_error_message (obj, Vprin1_to_string_buffer, 0, Qnil);
863 set_buffer_internal (XBUFFER (Vprin1_to_string_buffer));
864 value = Fbuffer_string ();
866 GCPRO1 (value);
867 Ferase_buffer ();
868 set_buffer_internal (old);
869 UNGCPRO;
871 return value;
874 /* Print an error message for the error DATA onto Lisp output stream
875 STREAM (suitable for the print functions).
876 CONTEXT is a C string describing the context of the error.
877 CALLER is the Lisp function inside which the error was signaled. */
879 void
880 print_error_message (Lisp_Object data, Lisp_Object stream, const char *context,
881 Lisp_Object caller)
883 Lisp_Object errname, errmsg, file_error, tail;
884 struct gcpro gcpro1;
886 if (context != 0)
887 write_string_1 (context, -1, stream);
889 /* If we know from where the error was signaled, show it in
890 *Messages*. */
891 if (!NILP (caller) && SYMBOLP (caller))
893 Lisp_Object cname = SYMBOL_NAME (caller);
894 ptrdiff_t cnamelen = SBYTES (cname);
895 USE_SAFE_ALLOCA;
896 char *name = SAFE_ALLOCA (cnamelen);
897 memcpy (name, SDATA (cname), cnamelen);
898 message_dolog (name, cnamelen, 0, 0);
899 message_dolog (": ", 2, 0, 0);
900 SAFE_FREE ();
903 errname = Fcar (data);
905 if (EQ (errname, Qerror))
907 data = Fcdr (data);
908 if (!CONSP (data))
909 data = Qnil;
910 errmsg = Fcar (data);
911 file_error = Qnil;
913 else
915 Lisp_Object error_conditions = Fget (errname, Qerror_conditions);
916 errmsg = Fget (errname, Qerror_message);
917 file_error = Fmemq (Qfile_error, error_conditions);
920 /* Print an error message including the data items. */
922 tail = Fcdr_safe (data);
923 GCPRO1 (tail);
925 /* For file-error, make error message by concatenating
926 all the data items. They are all strings. */
927 if (!NILP (file_error) && CONSP (tail))
928 errmsg = XCAR (tail), tail = XCDR (tail);
931 const char *sep = ": ";
933 if (!STRINGP (errmsg))
934 write_string_1 ("peculiar error", -1, stream);
935 else if (SCHARS (errmsg))
936 Fprinc (errmsg, stream);
937 else
938 sep = NULL;
940 for (; CONSP (tail); tail = XCDR (tail), sep = ", ")
942 Lisp_Object obj;
944 if (sep)
945 write_string_1 (sep, 2, stream);
946 obj = XCAR (tail);
947 if (!NILP (file_error)
948 || EQ (errname, Qend_of_file) || EQ (errname, Quser_error))
949 Fprinc (obj, stream);
950 else
951 Fprin1 (obj, stream);
955 UNGCPRO;
961 * The buffer should be at least as large as the max string size of the
962 * largest float, printed in the biggest notation. This is undoubtedly
963 * 20d float_output_format, with the negative of the C-constant "HUGE"
964 * from <math.h>.
966 * On the vax the worst case is -1e38 in 20d format which takes 61 bytes.
968 * I assume that IEEE-754 format numbers can take 329 bytes for the worst
969 * case of -1e307 in 20d float_output_format. What is one to do (short of
970 * re-writing _doprnt to be more sane)?
971 * -wsr
972 * Given the above, the buffer must be least FLOAT_TO_STRING_BUFSIZE bytes.
976 float_to_string (char *buf, double data)
978 char *cp;
979 int width;
980 int len;
982 /* Check for plus infinity in a way that won't lose
983 if there is no plus infinity. */
984 if (data == data / 2 && data > 1.0)
986 static char const infinity_string[] = "1.0e+INF";
987 strcpy (buf, infinity_string);
988 return sizeof infinity_string - 1;
990 /* Likewise for minus infinity. */
991 if (data == data / 2 && data < -1.0)
993 static char const minus_infinity_string[] = "-1.0e+INF";
994 strcpy (buf, minus_infinity_string);
995 return sizeof minus_infinity_string - 1;
997 /* Check for NaN in a way that won't fail if there are no NaNs. */
998 if (! (data * 0.0 >= 0.0))
1000 /* Prepend "-" if the NaN's sign bit is negative.
1001 The sign bit of a double is the bit that is 1 in -0.0. */
1002 static char const NaN_string[] = "0.0e+NaN";
1003 int i;
1004 union { double d; char c[sizeof (double)]; } u_data, u_minus_zero;
1005 bool negative = 0;
1006 u_data.d = data;
1007 u_minus_zero.d = - 0.0;
1008 for (i = 0; i < sizeof (double); i++)
1009 if (u_data.c[i] & u_minus_zero.c[i])
1011 *buf = '-';
1012 negative = 1;
1013 break;
1016 strcpy (buf + negative, NaN_string);
1017 return negative + sizeof NaN_string - 1;
1020 if (NILP (Vfloat_output_format)
1021 || !STRINGP (Vfloat_output_format))
1022 lose:
1024 /* Generate the fewest number of digits that represent the
1025 floating point value without losing information. */
1026 len = dtoastr (buf, FLOAT_TO_STRING_BUFSIZE - 2, 0, 0, data);
1027 /* The decimal point must be printed, or the byte compiler can
1028 get confused (Bug#8033). */
1029 width = 1;
1031 else /* oink oink */
1033 /* Check that the spec we have is fully valid.
1034 This means not only valid for printf,
1035 but meant for floats, and reasonable. */
1036 cp = SSDATA (Vfloat_output_format);
1038 if (cp[0] != '%')
1039 goto lose;
1040 if (cp[1] != '.')
1041 goto lose;
1043 cp += 2;
1045 /* Check the width specification. */
1046 width = -1;
1047 if ('0' <= *cp && *cp <= '9')
1049 width = 0;
1052 width = (width * 10) + (*cp++ - '0');
1053 if (DBL_DIG < width)
1054 goto lose;
1056 while (*cp >= '0' && *cp <= '9');
1058 /* A precision of zero is valid only for %f. */
1059 if (width == 0 && *cp != 'f')
1060 goto lose;
1063 if (*cp != 'e' && *cp != 'f' && *cp != 'g')
1064 goto lose;
1066 if (cp[1] != 0)
1067 goto lose;
1069 len = sprintf (buf, SSDATA (Vfloat_output_format), data);
1072 /* Make sure there is a decimal point with digit after, or an
1073 exponent, so that the value is readable as a float. But don't do
1074 this with "%.0f"; it's valid for that not to produce a decimal
1075 point. Note that width can be 0 only for %.0f. */
1076 if (width != 0)
1078 for (cp = buf; *cp; cp++)
1079 if ((*cp < '0' || *cp > '9') && *cp != '-')
1080 break;
1082 if (*cp == '.' && cp[1] == 0)
1084 cp[1] = '0';
1085 cp[2] = 0;
1086 len++;
1088 else if (*cp == 0)
1090 *cp++ = '.';
1091 *cp++ = '0';
1092 *cp++ = 0;
1093 len += 2;
1097 return len;
1101 static void
1102 print (Lisp_Object obj, Lisp_Object printcharfun, bool escapeflag)
1104 new_backquote_output = 0;
1106 /* Reset print_number_index and Vprint_number_table only when
1107 the variable Vprint_continuous_numbering is nil. Otherwise,
1108 the values of these variables will be kept between several
1109 print functions. */
1110 if (NILP (Vprint_continuous_numbering)
1111 || NILP (Vprint_number_table))
1113 print_number_index = 0;
1114 Vprint_number_table = Qnil;
1117 /* Construct Vprint_number_table for print-gensym and print-circle. */
1118 if (!NILP (Vprint_gensym) || !NILP (Vprint_circle))
1120 /* Construct Vprint_number_table.
1121 This increments print_number_index for the objects added. */
1122 print_depth = 0;
1123 print_preprocess (obj);
1125 if (HASH_TABLE_P (Vprint_number_table))
1126 { /* Remove unnecessary objects, which appear only once in OBJ;
1127 that is, whose status is Qt. */
1128 struct Lisp_Hash_Table *h = XHASH_TABLE (Vprint_number_table);
1129 ptrdiff_t i;
1131 for (i = 0; i < HASH_TABLE_SIZE (h); ++i)
1132 if (!NILP (HASH_HASH (h, i))
1133 && EQ (HASH_VALUE (h, i), Qt))
1134 Fremhash (HASH_KEY (h, i), Vprint_number_table);
1138 print_depth = 0;
1139 print_object (obj, printcharfun, escapeflag);
1142 #define PRINT_CIRCLE_CANDIDATE_P(obj) \
1143 (STRINGP (obj) || CONSP (obj) \
1144 || (VECTORLIKEP (obj) \
1145 && (VECTORP (obj) || COMPILEDP (obj) \
1146 || CHAR_TABLE_P (obj) || SUB_CHAR_TABLE_P (obj) \
1147 || HASH_TABLE_P (obj) || FONTP (obj))) \
1148 || (! NILP (Vprint_gensym) \
1149 && SYMBOLP (obj) \
1150 && !SYMBOL_INTERNED_P (obj)))
1152 /* Construct Vprint_number_table according to the structure of OBJ.
1153 OBJ itself and all its elements will be added to Vprint_number_table
1154 recursively if it is a list, vector, compiled function, char-table,
1155 string (its text properties will be traced), or a symbol that has
1156 no obarray (this is for the print-gensym feature).
1157 The status fields of Vprint_number_table mean whether each object appears
1158 more than once in OBJ: Qnil at the first time, and Qt after that. */
1159 static void
1160 print_preprocess (Lisp_Object obj)
1162 int i;
1163 ptrdiff_t size;
1164 int loop_count = 0;
1165 Lisp_Object halftail;
1167 /* Avoid infinite recursion for circular nested structure
1168 in the case where Vprint_circle is nil. */
1169 if (NILP (Vprint_circle))
1171 /* Give up if we go so deep that print_object will get an error. */
1172 /* See similar code in print_object. */
1173 if (print_depth >= PRINT_CIRCLE)
1174 error ("Apparently circular structure being printed");
1176 for (i = 0; i < print_depth; i++)
1177 if (EQ (obj, being_printed[i]))
1178 return;
1179 being_printed[print_depth] = obj;
1182 print_depth++;
1183 halftail = obj;
1185 loop:
1186 if (PRINT_CIRCLE_CANDIDATE_P (obj))
1188 if (!HASH_TABLE_P (Vprint_number_table))
1190 Lisp_Object args[2];
1191 args[0] = QCtest;
1192 args[1] = Qeq;
1193 Vprint_number_table = Fmake_hash_table (2, args);
1196 /* In case print-circle is nil and print-gensym is t,
1197 add OBJ to Vprint_number_table only when OBJ is a symbol. */
1198 if (! NILP (Vprint_circle) || SYMBOLP (obj))
1200 Lisp_Object num = Fgethash (obj, Vprint_number_table, Qnil);
1201 if (!NILP (num)
1202 /* If Vprint_continuous_numbering is non-nil and OBJ is a gensym,
1203 always print the gensym with a number. This is a special for
1204 the lisp function byte-compile-output-docform. */
1205 || (!NILP (Vprint_continuous_numbering)
1206 && SYMBOLP (obj)
1207 && !SYMBOL_INTERNED_P (obj)))
1208 { /* OBJ appears more than once. Let's remember that. */
1209 if (!INTEGERP (num))
1211 print_number_index++;
1212 /* Negative number indicates it hasn't been printed yet. */
1213 Fputhash (obj, make_number (- print_number_index),
1214 Vprint_number_table);
1216 print_depth--;
1217 return;
1219 else
1220 /* OBJ is not yet recorded. Let's add to the table. */
1221 Fputhash (obj, Qt, Vprint_number_table);
1224 switch (XTYPE (obj))
1226 case Lisp_String:
1227 /* A string may have text properties, which can be circular. */
1228 traverse_intervals_noorder (string_intervals (obj),
1229 print_preprocess_string, Qnil);
1230 break;
1232 case Lisp_Cons:
1233 /* Use HALFTAIL and LOOP_COUNT to detect circular lists,
1234 just as in print_object. */
1235 if (loop_count && EQ (obj, halftail))
1236 break;
1237 print_preprocess (XCAR (obj));
1238 obj = XCDR (obj);
1239 loop_count++;
1240 if (!(loop_count & 1))
1241 halftail = XCDR (halftail);
1242 goto loop;
1244 case Lisp_Vectorlike:
1245 size = ASIZE (obj);
1246 if (size & PSEUDOVECTOR_FLAG)
1247 size &= PSEUDOVECTOR_SIZE_MASK;
1248 for (i = (SUB_CHAR_TABLE_P (obj)
1249 ? SUB_CHAR_TABLE_OFFSET : 0); i < size; i++)
1250 print_preprocess (AREF (obj, i));
1251 if (HASH_TABLE_P (obj))
1252 { /* For hash tables, the key_and_value slot is past
1253 `size' because it needs to be marked specially in case
1254 the table is weak. */
1255 struct Lisp_Hash_Table *h = XHASH_TABLE (obj);
1256 print_preprocess (h->key_and_value);
1258 break;
1260 default:
1261 break;
1264 print_depth--;
1267 static void
1268 print_preprocess_string (INTERVAL interval, Lisp_Object arg)
1270 print_preprocess (interval->plist);
1273 static void print_check_string_charset_prop (INTERVAL interval, Lisp_Object string);
1275 #define PRINT_STRING_NON_CHARSET_FOUND 1
1276 #define PRINT_STRING_UNSAFE_CHARSET_FOUND 2
1278 /* Bitwise or of the above macros. */
1279 static int print_check_string_result;
1281 static void
1282 print_check_string_charset_prop (INTERVAL interval, Lisp_Object string)
1284 Lisp_Object val;
1286 if (NILP (interval->plist)
1287 || (print_check_string_result == (PRINT_STRING_NON_CHARSET_FOUND
1288 | PRINT_STRING_UNSAFE_CHARSET_FOUND)))
1289 return;
1290 for (val = interval->plist; CONSP (val) && ! EQ (XCAR (val), Qcharset);
1291 val = XCDR (XCDR (val)));
1292 if (! CONSP (val))
1294 print_check_string_result |= PRINT_STRING_NON_CHARSET_FOUND;
1295 return;
1297 if (! (print_check_string_result & PRINT_STRING_NON_CHARSET_FOUND))
1299 if (! EQ (val, interval->plist)
1300 || CONSP (XCDR (XCDR (val))))
1301 print_check_string_result |= PRINT_STRING_NON_CHARSET_FOUND;
1303 if (NILP (Vprint_charset_text_property)
1304 || ! (print_check_string_result & PRINT_STRING_UNSAFE_CHARSET_FOUND))
1306 int i, c;
1307 ptrdiff_t charpos = interval->position;
1308 ptrdiff_t bytepos = string_char_to_byte (string, charpos);
1309 Lisp_Object charset;
1311 charset = XCAR (XCDR (val));
1312 for (i = 0; i < LENGTH (interval); i++)
1314 FETCH_STRING_CHAR_ADVANCE (c, string, charpos, bytepos);
1315 if (! ASCII_CHAR_P (c)
1316 && ! EQ (CHARSET_NAME (CHAR_CHARSET (c)), charset))
1318 print_check_string_result |= PRINT_STRING_UNSAFE_CHARSET_FOUND;
1319 break;
1325 /* The value is (charset . nil). */
1326 static Lisp_Object print_prune_charset_plist;
1328 static Lisp_Object
1329 print_prune_string_charset (Lisp_Object string)
1331 print_check_string_result = 0;
1332 traverse_intervals (string_intervals (string), 0,
1333 print_check_string_charset_prop, string);
1334 if (! (print_check_string_result & PRINT_STRING_UNSAFE_CHARSET_FOUND))
1336 string = Fcopy_sequence (string);
1337 if (print_check_string_result & PRINT_STRING_NON_CHARSET_FOUND)
1339 if (NILP (print_prune_charset_plist))
1340 print_prune_charset_plist = list1 (Qcharset);
1341 Fremove_text_properties (make_number (0),
1342 make_number (SCHARS (string)),
1343 print_prune_charset_plist, string);
1345 else
1346 Fset_text_properties (make_number (0), make_number (SCHARS (string)),
1347 Qnil, string);
1349 return string;
1352 static void
1353 print_object (Lisp_Object obj, Lisp_Object printcharfun, bool escapeflag)
1355 char buf[max (sizeof "from..to..in " + 2 * INT_STRLEN_BOUND (EMACS_INT),
1356 max (sizeof " . #" + INT_STRLEN_BOUND (printmax_t),
1357 40))];
1359 QUIT;
1361 /* Detect circularities and truncate them. */
1362 if (NILP (Vprint_circle))
1364 /* Simple but incomplete way. */
1365 int i;
1367 /* See similar code in print_preprocess. */
1368 if (print_depth >= PRINT_CIRCLE)
1369 error ("Apparently circular structure being printed");
1371 for (i = 0; i < print_depth; i++)
1372 if (EQ (obj, being_printed[i]))
1374 int len = sprintf (buf, "#%d", i);
1375 strout (buf, len, len, printcharfun);
1376 return;
1378 being_printed[print_depth] = obj;
1380 else if (PRINT_CIRCLE_CANDIDATE_P (obj))
1382 /* With the print-circle feature. */
1383 Lisp_Object num = Fgethash (obj, Vprint_number_table, Qnil);
1384 if (INTEGERP (num))
1386 EMACS_INT n = XINT (num);
1387 if (n < 0)
1388 { /* Add a prefix #n= if OBJ has not yet been printed;
1389 that is, its status field is nil. */
1390 int len = sprintf (buf, "#%"pI"d=", -n);
1391 strout (buf, len, len, printcharfun);
1392 /* OBJ is going to be printed. Remember that fact. */
1393 Fputhash (obj, make_number (- n), Vprint_number_table);
1395 else
1397 /* Just print #n# if OBJ has already been printed. */
1398 int len = sprintf (buf, "#%"pI"d#", n);
1399 strout (buf, len, len, printcharfun);
1400 return;
1405 print_depth++;
1407 switch (XTYPE (obj))
1409 case_Lisp_Int:
1411 int len = sprintf (buf, "%"pI"d", XINT (obj));
1412 strout (buf, len, len, printcharfun);
1414 break;
1416 case Lisp_Float:
1418 char pigbuf[FLOAT_TO_STRING_BUFSIZE];
1419 int len = float_to_string (pigbuf, XFLOAT_DATA (obj));
1420 strout (pigbuf, len, len, printcharfun);
1422 break;
1424 case Lisp_String:
1425 if (!escapeflag)
1426 print_string (obj, printcharfun);
1427 else
1429 register ptrdiff_t i, i_byte;
1430 struct gcpro gcpro1;
1431 ptrdiff_t size_byte;
1432 /* 1 means we must ensure that the next character we output
1433 cannot be taken as part of a hex character escape. */
1434 bool need_nonhex = 0;
1435 bool multibyte = STRING_MULTIBYTE (obj);
1437 GCPRO1 (obj);
1439 if (! EQ (Vprint_charset_text_property, Qt))
1440 obj = print_prune_string_charset (obj);
1442 if (string_intervals (obj))
1444 PRINTCHAR ('#');
1445 PRINTCHAR ('(');
1448 PRINTCHAR ('\"');
1449 size_byte = SBYTES (obj);
1451 for (i = 0, i_byte = 0; i_byte < size_byte;)
1453 /* Here, we must convert each multi-byte form to the
1454 corresponding character code before handing it to PRINTCHAR. */
1455 int c;
1457 FETCH_STRING_CHAR_ADVANCE (c, obj, i, i_byte);
1459 QUIT;
1461 if (c == '\n' && print_escape_newlines)
1463 PRINTCHAR ('\\');
1464 PRINTCHAR ('n');
1466 else if (c == '\f' && print_escape_newlines)
1468 PRINTCHAR ('\\');
1469 PRINTCHAR ('f');
1471 else if (multibyte
1472 && (CHAR_BYTE8_P (c)
1473 || (! ASCII_CHAR_P (c) && print_escape_multibyte)))
1475 /* When multibyte is disabled,
1476 print multibyte string chars using hex escapes.
1477 For a char code that could be in a unibyte string,
1478 when found in a multibyte string, always use a hex escape
1479 so it reads back as multibyte. */
1480 char outbuf[50];
1481 int len;
1483 if (CHAR_BYTE8_P (c))
1484 len = sprintf (outbuf, "\\%03o", CHAR_TO_BYTE8 (c));
1485 else
1487 len = sprintf (outbuf, "\\x%04x", c);
1488 need_nonhex = 1;
1490 strout (outbuf, len, len, printcharfun);
1492 else if (! multibyte
1493 && SINGLE_BYTE_CHAR_P (c) && ! ASCII_CHAR_P (c)
1494 && print_escape_nonascii)
1496 /* When printing in a multibyte buffer
1497 or when explicitly requested,
1498 print single-byte non-ASCII string chars
1499 using octal escapes. */
1500 char outbuf[5];
1501 int len = sprintf (outbuf, "\\%03o", c);
1502 strout (outbuf, len, len, printcharfun);
1504 else
1506 /* If we just had a hex escape, and this character
1507 could be taken as part of it,
1508 output `\ ' to prevent that. */
1509 if (need_nonhex)
1511 need_nonhex = 0;
1512 if ((c >= 'a' && c <= 'f')
1513 || (c >= 'A' && c <= 'F')
1514 || (c >= '0' && c <= '9'))
1515 strout ("\\ ", -1, -1, printcharfun);
1518 if (c == '\"' || c == '\\')
1519 PRINTCHAR ('\\');
1520 PRINTCHAR (c);
1523 PRINTCHAR ('\"');
1525 if (string_intervals (obj))
1527 traverse_intervals (string_intervals (obj),
1528 0, print_interval, printcharfun);
1529 PRINTCHAR (')');
1532 UNGCPRO;
1534 break;
1536 case Lisp_Symbol:
1538 bool confusing;
1539 unsigned char *p = SDATA (SYMBOL_NAME (obj));
1540 unsigned char *end = p + SBYTES (SYMBOL_NAME (obj));
1541 int c;
1542 ptrdiff_t i, i_byte;
1543 ptrdiff_t size_byte;
1544 Lisp_Object name;
1546 name = SYMBOL_NAME (obj);
1548 if (p != end && (*p == '-' || *p == '+')) p++;
1549 if (p == end)
1550 confusing = 0;
1551 /* If symbol name begins with a digit, and ends with a digit,
1552 and contains nothing but digits and `e', it could be treated
1553 as a number. So set CONFUSING.
1555 Symbols that contain periods could also be taken as numbers,
1556 but periods are always escaped, so we don't have to worry
1557 about them here. */
1558 else if (*p >= '0' && *p <= '9'
1559 && end[-1] >= '0' && end[-1] <= '9')
1561 while (p != end && ((*p >= '0' && *p <= '9')
1562 /* Needed for \2e10. */
1563 || *p == 'e' || *p == 'E'))
1564 p++;
1565 confusing = (end == p);
1567 else
1568 confusing = 0;
1570 size_byte = SBYTES (name);
1572 if (! NILP (Vprint_gensym) && !SYMBOL_INTERNED_P (obj))
1574 PRINTCHAR ('#');
1575 PRINTCHAR (':');
1577 else if (size_byte == 0)
1579 PRINTCHAR ('#');
1580 PRINTCHAR ('#');
1581 break;
1584 for (i = 0, i_byte = 0; i_byte < size_byte;)
1586 /* Here, we must convert each multi-byte form to the
1587 corresponding character code before handing it to PRINTCHAR. */
1588 FETCH_STRING_CHAR_ADVANCE (c, name, i, i_byte);
1589 QUIT;
1591 if (escapeflag)
1593 if (c == '\"' || c == '\\' || c == '\''
1594 || c == ';' || c == '#' || c == '(' || c == ')'
1595 || c == ',' || c == '.' || c == '`'
1596 || c == '[' || c == ']' || c == '?' || c <= 040
1597 || confusing)
1598 PRINTCHAR ('\\'), confusing = 0;
1600 PRINTCHAR (c);
1603 break;
1605 case Lisp_Cons:
1606 /* If deeper than spec'd depth, print placeholder. */
1607 if (INTEGERP (Vprint_level)
1608 && print_depth > XINT (Vprint_level))
1609 strout ("...", -1, -1, printcharfun);
1610 else if (print_quoted && CONSP (XCDR (obj)) && NILP (XCDR (XCDR (obj)))
1611 && (EQ (XCAR (obj), Qquote)))
1613 PRINTCHAR ('\'');
1614 print_object (XCAR (XCDR (obj)), printcharfun, escapeflag);
1616 else if (print_quoted && CONSP (XCDR (obj)) && NILP (XCDR (XCDR (obj)))
1617 && (EQ (XCAR (obj), Qfunction)))
1619 PRINTCHAR ('#');
1620 PRINTCHAR ('\'');
1621 print_object (XCAR (XCDR (obj)), printcharfun, escapeflag);
1623 else if (print_quoted && CONSP (XCDR (obj)) && NILP (XCDR (XCDR (obj)))
1624 && ((EQ (XCAR (obj), Qbackquote))))
1626 print_object (XCAR (obj), printcharfun, 0);
1627 new_backquote_output++;
1628 print_object (XCAR (XCDR (obj)), printcharfun, escapeflag);
1629 new_backquote_output--;
1631 else if (print_quoted && CONSP (XCDR (obj)) && NILP (XCDR (XCDR (obj)))
1632 && new_backquote_output
1633 && ((EQ (XCAR (obj), Qbackquote)
1634 || EQ (XCAR (obj), Qcomma)
1635 || EQ (XCAR (obj), Qcomma_at)
1636 || EQ (XCAR (obj), Qcomma_dot))))
1638 print_object (XCAR (obj), printcharfun, 0);
1639 new_backquote_output--;
1640 print_object (XCAR (XCDR (obj)), printcharfun, escapeflag);
1641 new_backquote_output++;
1643 else
1645 PRINTCHAR ('(');
1648 printmax_t i, print_length;
1649 Lisp_Object halftail = obj;
1651 /* Negative values of print-length are invalid in CL.
1652 Treat them like nil, as CMUCL does. */
1653 if (NATNUMP (Vprint_length))
1654 print_length = XFASTINT (Vprint_length);
1655 else
1656 print_length = TYPE_MAXIMUM (printmax_t);
1658 i = 0;
1659 while (CONSP (obj))
1661 /* Detect circular list. */
1662 if (NILP (Vprint_circle))
1664 /* Simple but incomplete way. */
1665 if (i != 0 && EQ (obj, halftail))
1667 int len = sprintf (buf, " . #%"pMd, i / 2);
1668 strout (buf, len, len, printcharfun);
1669 goto end_of_list;
1672 else
1674 /* With the print-circle feature. */
1675 if (i != 0)
1677 Lisp_Object num = Fgethash (obj, Vprint_number_table, Qnil);
1678 if (INTEGERP (num))
1680 strout (" . ", 3, 3, printcharfun);
1681 print_object (obj, printcharfun, escapeflag);
1682 goto end_of_list;
1687 if (i)
1688 PRINTCHAR (' ');
1690 if (print_length <= i)
1692 strout ("...", 3, 3, printcharfun);
1693 goto end_of_list;
1696 i++;
1697 print_object (XCAR (obj), printcharfun, escapeflag);
1699 obj = XCDR (obj);
1700 if (!(i & 1))
1701 halftail = XCDR (halftail);
1705 /* OBJ non-nil here means it's the end of a dotted list. */
1706 if (!NILP (obj))
1708 strout (" . ", 3, 3, printcharfun);
1709 print_object (obj, printcharfun, escapeflag);
1712 end_of_list:
1713 PRINTCHAR (')');
1715 break;
1717 case Lisp_Vectorlike:
1718 if (PROCESSP (obj))
1720 if (escapeflag)
1722 strout ("#<process ", -1, -1, printcharfun);
1723 print_string (XPROCESS (obj)->name, printcharfun);
1724 PRINTCHAR ('>');
1726 else
1727 print_string (XPROCESS (obj)->name, printcharfun);
1729 else if (BOOL_VECTOR_P (obj))
1731 ptrdiff_t i;
1732 int len;
1733 unsigned char c;
1734 struct gcpro gcpro1;
1735 EMACS_INT size = bool_vector_size (obj);
1736 ptrdiff_t size_in_chars = bool_vector_bytes (size);
1737 ptrdiff_t real_size_in_chars = size_in_chars;
1738 GCPRO1 (obj);
1740 PRINTCHAR ('#');
1741 PRINTCHAR ('&');
1742 len = sprintf (buf, "%"pI"d", size);
1743 strout (buf, len, len, printcharfun);
1744 PRINTCHAR ('\"');
1746 /* Don't print more characters than the specified maximum.
1747 Negative values of print-length are invalid. Treat them
1748 like a print-length of nil. */
1749 if (NATNUMP (Vprint_length)
1750 && XFASTINT (Vprint_length) < size_in_chars)
1751 size_in_chars = XFASTINT (Vprint_length);
1753 for (i = 0; i < size_in_chars; i++)
1755 QUIT;
1756 c = bool_vector_uchar_data (obj)[i];
1757 if (c == '\n' && print_escape_newlines)
1759 PRINTCHAR ('\\');
1760 PRINTCHAR ('n');
1762 else if (c == '\f' && print_escape_newlines)
1764 PRINTCHAR ('\\');
1765 PRINTCHAR ('f');
1767 else if (c > '\177')
1769 /* Use octal escapes to avoid encoding issues. */
1770 PRINTCHAR ('\\');
1771 PRINTCHAR ('0' + ((c >> 6) & 3));
1772 PRINTCHAR ('0' + ((c >> 3) & 7));
1773 PRINTCHAR ('0' + (c & 7));
1775 else
1777 if (c == '\"' || c == '\\')
1778 PRINTCHAR ('\\');
1779 PRINTCHAR (c);
1783 if (size_in_chars < real_size_in_chars)
1784 strout (" ...", 4, 4, printcharfun);
1785 PRINTCHAR ('\"');
1787 UNGCPRO;
1789 else if (SUBRP (obj))
1791 strout ("#<subr ", -1, -1, printcharfun);
1792 strout (XSUBR (obj)->symbol_name, -1, -1, printcharfun);
1793 PRINTCHAR ('>');
1795 else if (WINDOWP (obj))
1797 int len;
1798 strout ("#<window ", -1, -1, printcharfun);
1799 len = sprintf (buf, "%d", XWINDOW (obj)->sequence_number);
1800 strout (buf, len, len, printcharfun);
1801 if (BUFFERP (XWINDOW (obj)->contents))
1803 strout (" on ", -1, -1, printcharfun);
1804 print_string (BVAR (XBUFFER (XWINDOW (obj)->contents), name),
1805 printcharfun);
1807 PRINTCHAR ('>');
1809 else if (TERMINALP (obj))
1811 int len;
1812 struct terminal *t = XTERMINAL (obj);
1813 strout ("#<terminal ", -1, -1, printcharfun);
1814 len = sprintf (buf, "%d", t->id);
1815 strout (buf, len, len, printcharfun);
1816 if (t->name)
1818 strout (" on ", -1, -1, printcharfun);
1819 strout (t->name, -1, -1, printcharfun);
1821 PRINTCHAR ('>');
1823 else if (HASH_TABLE_P (obj))
1825 struct Lisp_Hash_Table *h = XHASH_TABLE (obj);
1826 ptrdiff_t i;
1827 ptrdiff_t real_size, size;
1828 int len;
1829 #if 0
1830 void *ptr = h;
1831 strout ("#<hash-table", -1, -1, printcharfun);
1832 if (SYMBOLP (h->test))
1834 PRINTCHAR (' ');
1835 PRINTCHAR ('\'');
1836 strout (SDATA (SYMBOL_NAME (h->test)), -1, -1, printcharfun);
1837 PRINTCHAR (' ');
1838 strout (SDATA (SYMBOL_NAME (h->weak)), -1, -1, printcharfun);
1839 PRINTCHAR (' ');
1840 len = sprintf (buf, "%"pD"d/%"pD"d", h->count, ASIZE (h->next));
1841 strout (buf, len, len, printcharfun);
1843 len = sprintf (buf, " %p>", ptr);
1844 strout (buf, len, len, printcharfun);
1845 #endif
1846 /* Implement a readable output, e.g.:
1847 #s(hash-table size 2 test equal data (k1 v1 k2 v2)) */
1848 /* Always print the size. */
1849 len = sprintf (buf, "#s(hash-table size %"pD"d", ASIZE (h->next));
1850 strout (buf, len, len, printcharfun);
1852 if (!NILP (h->test.name))
1854 strout (" test ", -1, -1, printcharfun);
1855 print_object (h->test.name, printcharfun, escapeflag);
1858 if (!NILP (h->weak))
1860 strout (" weakness ", -1, -1, printcharfun);
1861 print_object (h->weak, printcharfun, escapeflag);
1864 if (!NILP (h->rehash_size))
1866 strout (" rehash-size ", -1, -1, printcharfun);
1867 print_object (h->rehash_size, printcharfun, escapeflag);
1870 if (!NILP (h->rehash_threshold))
1872 strout (" rehash-threshold ", -1, -1, printcharfun);
1873 print_object (h->rehash_threshold, printcharfun, escapeflag);
1876 strout (" data ", -1, -1, printcharfun);
1878 /* Print the data here as a plist. */
1879 real_size = HASH_TABLE_SIZE (h);
1880 size = real_size;
1882 /* Don't print more elements than the specified maximum. */
1883 if (NATNUMP (Vprint_length)
1884 && XFASTINT (Vprint_length) < size)
1885 size = XFASTINT (Vprint_length);
1887 PRINTCHAR ('(');
1888 for (i = 0; i < size; i++)
1889 if (!NILP (HASH_HASH (h, i)))
1891 if (i) PRINTCHAR (' ');
1892 print_object (HASH_KEY (h, i), printcharfun, escapeflag);
1893 PRINTCHAR (' ');
1894 print_object (HASH_VALUE (h, i), printcharfun, escapeflag);
1897 if (size < real_size)
1898 strout (" ...", 4, 4, printcharfun);
1900 PRINTCHAR (')');
1901 PRINTCHAR (')');
1904 else if (BUFFERP (obj))
1906 if (!BUFFER_LIVE_P (XBUFFER (obj)))
1907 strout ("#<killed buffer>", -1, -1, printcharfun);
1908 else if (escapeflag)
1910 strout ("#<buffer ", -1, -1, printcharfun);
1911 print_string (BVAR (XBUFFER (obj), name), printcharfun);
1912 PRINTCHAR ('>');
1914 else
1915 print_string (BVAR (XBUFFER (obj), name), printcharfun);
1917 else if (WINDOW_CONFIGURATIONP (obj))
1919 strout ("#<window-configuration>", -1, -1, printcharfun);
1921 else if (FRAMEP (obj))
1923 int len;
1924 void *ptr = XFRAME (obj);
1925 Lisp_Object frame_name = XFRAME (obj)->name;
1927 strout ((FRAME_LIVE_P (XFRAME (obj))
1928 ? "#<frame " : "#<dead frame "),
1929 -1, -1, printcharfun);
1930 if (!STRINGP (frame_name))
1932 /* A frame could be too young and have no name yet;
1933 don't crash. */
1934 if (SYMBOLP (frame_name))
1935 frame_name = Fsymbol_name (frame_name);
1936 else /* can't happen: name should be either nil or string */
1937 frame_name = build_string ("*INVALID*FRAME*NAME*");
1939 print_string (frame_name, printcharfun);
1940 len = sprintf (buf, " %p>", ptr);
1941 strout (buf, len, len, printcharfun);
1943 else if (FONTP (obj))
1945 int i;
1947 if (! FONT_OBJECT_P (obj))
1949 if (FONT_SPEC_P (obj))
1950 strout ("#<font-spec", -1, -1, printcharfun);
1951 else
1952 strout ("#<font-entity", -1, -1, printcharfun);
1953 for (i = 0; i < FONT_SPEC_MAX; i++)
1955 PRINTCHAR (' ');
1956 if (i < FONT_WEIGHT_INDEX || i > FONT_WIDTH_INDEX)
1957 print_object (AREF (obj, i), printcharfun, escapeflag);
1958 else
1959 print_object (font_style_symbolic (obj, i, 0),
1960 printcharfun, escapeflag);
1963 else
1965 strout ("#<font-object ", -1, -1, printcharfun);
1966 print_object (AREF (obj, FONT_NAME_INDEX), printcharfun,
1967 escapeflag);
1969 PRINTCHAR ('>');
1971 else
1973 ptrdiff_t size = ASIZE (obj);
1974 if (COMPILEDP (obj))
1976 PRINTCHAR ('#');
1977 size &= PSEUDOVECTOR_SIZE_MASK;
1979 if (CHAR_TABLE_P (obj) || SUB_CHAR_TABLE_P (obj))
1981 /* We print a char-table as if it were a vector,
1982 lumping the parent and default slots in with the
1983 character slots. But we add #^ as a prefix. */
1985 /* Make each lowest sub_char_table start a new line.
1986 Otherwise we'll make a line extremely long, which
1987 results in slow redisplay. */
1988 if (SUB_CHAR_TABLE_P (obj)
1989 && XSUB_CHAR_TABLE (obj)->depth == 3)
1990 PRINTCHAR ('\n');
1991 PRINTCHAR ('#');
1992 PRINTCHAR ('^');
1993 if (SUB_CHAR_TABLE_P (obj))
1994 PRINTCHAR ('^');
1995 size &= PSEUDOVECTOR_SIZE_MASK;
1997 if (size & PSEUDOVECTOR_FLAG)
1998 goto badtype;
2000 PRINTCHAR ('[');
2002 int i, idx = SUB_CHAR_TABLE_P (obj) ? SUB_CHAR_TABLE_OFFSET : 0;
2003 register Lisp_Object tem;
2004 ptrdiff_t real_size = size;
2006 /* For a sub char-table, print heading non-Lisp data first. */
2007 if (SUB_CHAR_TABLE_P (obj))
2009 i = sprintf (buf, "%d %d", XSUB_CHAR_TABLE (obj)->depth,
2010 XSUB_CHAR_TABLE (obj)->min_char);
2011 strout (buf, i, i, printcharfun);
2014 /* Don't print more elements than the specified maximum. */
2015 if (NATNUMP (Vprint_length)
2016 && XFASTINT (Vprint_length) < size)
2017 size = XFASTINT (Vprint_length);
2019 for (i = idx; i < size; i++)
2021 if (i) PRINTCHAR (' ');
2022 tem = AREF (obj, i);
2023 print_object (tem, printcharfun, escapeflag);
2025 if (size < real_size)
2026 strout (" ...", 4, 4, printcharfun);
2028 PRINTCHAR (']');
2030 break;
2032 case Lisp_Misc:
2033 switch (XMISCTYPE (obj))
2035 case Lisp_Misc_Marker:
2036 strout ("#<marker ", -1, -1, printcharfun);
2037 /* Do you think this is necessary? */
2038 if (XMARKER (obj)->insertion_type != 0)
2039 strout ("(moves after insertion) ", -1, -1, printcharfun);
2040 if (! XMARKER (obj)->buffer)
2041 strout ("in no buffer", -1, -1, printcharfun);
2042 else
2044 int len = sprintf (buf, "at %"pD"d", marker_position (obj));
2045 strout (buf, len, len, printcharfun);
2046 strout (" in ", -1, -1, printcharfun);
2047 print_string (BVAR (XMARKER (obj)->buffer, name), printcharfun);
2049 PRINTCHAR ('>');
2050 break;
2052 case Lisp_Misc_Overlay:
2053 strout ("#<overlay ", -1, -1, printcharfun);
2054 if (! XMARKER (OVERLAY_START (obj))->buffer)
2055 strout ("in no buffer", -1, -1, printcharfun);
2056 else
2058 int len = sprintf (buf, "from %"pD"d to %"pD"d in ",
2059 marker_position (OVERLAY_START (obj)),
2060 marker_position (OVERLAY_END (obj)));
2061 strout (buf, len, len, printcharfun);
2062 print_string (BVAR (XMARKER (OVERLAY_START (obj))->buffer, name),
2063 printcharfun);
2065 PRINTCHAR ('>');
2066 break;
2068 /* Remaining cases shouldn't happen in normal usage, but let's
2069 print them anyway for the benefit of the debugger. */
2071 case Lisp_Misc_Free:
2072 strout ("#<misc free cell>", -1, -1, printcharfun);
2073 break;
2075 case Lisp_Misc_Save_Value:
2077 int i;
2078 struct Lisp_Save_Value *v = XSAVE_VALUE (obj);
2080 strout ("#<save-value ", -1, -1, printcharfun);
2082 if (v->save_type == SAVE_TYPE_MEMORY)
2084 ptrdiff_t amount = v->data[1].integer;
2086 #if GC_MARK_STACK
2088 /* valid_lisp_object_p is reliable, so try to print up
2089 to 8 saved objects. This code is rarely used, so
2090 it's OK that valid_lisp_object_p is slow. */
2092 int limit = min (amount, 8);
2093 Lisp_Object *area = v->data[0].pointer;
2095 i = sprintf (buf, "with %"pD"d objects", amount);
2096 strout (buf, i, i, printcharfun);
2098 for (i = 0; i < limit; i++)
2100 Lisp_Object maybe = area[i];
2101 int valid = valid_lisp_object_p (maybe);
2103 if (0 < valid)
2105 PRINTCHAR (' ');
2106 print_object (maybe, printcharfun, escapeflag);
2108 else
2109 strout (valid ? " <some>" : " <invalid>",
2110 -1, -1, printcharfun);
2112 if (i == limit && i < amount)
2113 strout (" ...", 4, 4, printcharfun);
2115 #else /* not GC_MARK_STACK */
2117 /* There is no reliable way to determine whether the objects
2118 are initialized, so do not try to print them. */
2120 i = sprintf (buf, "with %"pD"d objects", amount);
2121 strout (buf, i, i, printcharfun);
2123 #endif /* GC_MARK_STACK */
2125 else
2127 /* Print each slot according to its type. */
2128 int index;
2129 for (index = 0; index < SAVE_VALUE_SLOTS; index++)
2131 if (index)
2132 PRINTCHAR (' ');
2134 switch (save_type (v, index))
2136 case SAVE_UNUSED:
2137 i = sprintf (buf, "<unused>");
2138 break;
2140 case SAVE_POINTER:
2141 i = sprintf (buf, "<pointer %p>",
2142 v->data[index].pointer);
2143 break;
2145 case SAVE_FUNCPOINTER:
2146 i = sprintf (buf, "<funcpointer %p>",
2147 ((void *) (intptr_t)
2148 v->data[index].funcpointer));
2149 break;
2151 case SAVE_INTEGER:
2152 i = sprintf (buf, "<integer %"pD"d>",
2153 v->data[index].integer);
2154 break;
2156 case SAVE_OBJECT:
2157 print_object (v->data[index].object, printcharfun,
2158 escapeflag);
2159 continue;
2161 default:
2162 emacs_abort ();
2165 strout (buf, i, i, printcharfun);
2168 PRINTCHAR ('>');
2170 break;
2172 default:
2173 goto badtype;
2175 break;
2177 default:
2178 badtype:
2180 int len;
2181 /* We're in trouble if this happens!
2182 Probably should just emacs_abort (). */
2183 strout ("#<EMACS BUG: INVALID DATATYPE ", -1, -1, printcharfun);
2184 if (MISCP (obj))
2185 len = sprintf (buf, "(MISC 0x%04x)", (int) XMISCTYPE (obj));
2186 else if (VECTORLIKEP (obj))
2187 len = sprintf (buf, "(PVEC 0x%08"pD"x)", ASIZE (obj));
2188 else
2189 len = sprintf (buf, "(0x%02x)", (int) XTYPE (obj));
2190 strout (buf, len, len, printcharfun);
2191 strout (" Save your buffers immediately and please report this bug>",
2192 -1, -1, printcharfun);
2196 print_depth--;
2200 /* Print a description of INTERVAL using PRINTCHARFUN.
2201 This is part of printing a string that has text properties. */
2203 static void
2204 print_interval (INTERVAL interval, Lisp_Object printcharfun)
2206 if (NILP (interval->plist))
2207 return;
2208 PRINTCHAR (' ');
2209 print_object (make_number (interval->position), printcharfun, 1);
2210 PRINTCHAR (' ');
2211 print_object (make_number (interval->position + LENGTH (interval)),
2212 printcharfun, 1);
2213 PRINTCHAR (' ');
2214 print_object (interval->plist, printcharfun, 1);
2217 /* Initialize debug_print stuff early to have it working from the very
2218 beginning. */
2220 void
2221 init_print_once (void)
2223 DEFSYM (Qexternal_debugging_output, "external-debugging-output");
2224 defsubr (&Sexternal_debugging_output);
2227 void
2228 syms_of_print (void)
2230 DEFSYM (Qtemp_buffer_setup_hook, "temp-buffer-setup-hook");
2232 DEFVAR_LISP ("standard-output", Vstandard_output,
2233 doc: /* Output stream `print' uses by default for outputting a character.
2234 This may be any function of one argument.
2235 It may also be a buffer (output is inserted before point)
2236 or a marker (output is inserted and the marker is advanced)
2237 or the symbol t (output appears in the echo area). */);
2238 Vstandard_output = Qt;
2239 DEFSYM (Qstandard_output, "standard-output");
2241 DEFVAR_LISP ("float-output-format", Vfloat_output_format,
2242 doc: /* The format descriptor string used to print floats.
2243 This is a %-spec like those accepted by `printf' in C,
2244 but with some restrictions. It must start with the two characters `%.'.
2245 After that comes an integer precision specification,
2246 and then a letter which controls the format.
2247 The letters allowed are `e', `f' and `g'.
2248 Use `e' for exponential notation \"DIG.DIGITSeEXPT\"
2249 Use `f' for decimal point notation \"DIGITS.DIGITS\".
2250 Use `g' to choose the shorter of those two formats for the number at hand.
2251 The precision in any of these cases is the number of digits following
2252 the decimal point. With `f', a precision of 0 means to omit the
2253 decimal point. 0 is not allowed with `e' or `g'.
2255 A value of nil means to use the shortest notation
2256 that represents the number without losing information. */);
2257 Vfloat_output_format = Qnil;
2258 DEFSYM (Qfloat_output_format, "float-output-format");
2260 DEFVAR_LISP ("print-length", Vprint_length,
2261 doc: /* Maximum length of list to print before abbreviating.
2262 A value of nil means no limit. See also `eval-expression-print-length'. */);
2263 Vprint_length = Qnil;
2265 DEFVAR_LISP ("print-level", Vprint_level,
2266 doc: /* Maximum depth of list nesting to print before abbreviating.
2267 A value of nil means no limit. See also `eval-expression-print-level'. */);
2268 Vprint_level = Qnil;
2270 DEFVAR_BOOL ("print-escape-newlines", print_escape_newlines,
2271 doc: /* Non-nil means print newlines in strings as `\\n'.
2272 Also print formfeeds as `\\f'. */);
2273 print_escape_newlines = 0;
2275 DEFVAR_BOOL ("print-escape-nonascii", print_escape_nonascii,
2276 doc: /* Non-nil means print unibyte non-ASCII chars in strings as \\OOO.
2277 \(OOO is the octal representation of the character code.)
2278 Only single-byte characters are affected, and only in `prin1'.
2279 When the output goes in a multibyte buffer, this feature is
2280 enabled regardless of the value of the variable. */);
2281 print_escape_nonascii = 0;
2283 DEFVAR_BOOL ("print-escape-multibyte", print_escape_multibyte,
2284 doc: /* Non-nil means print multibyte characters in strings as \\xXXXX.
2285 \(XXXX is the hex representation of the character code.)
2286 This affects only `prin1'. */);
2287 print_escape_multibyte = 0;
2289 DEFVAR_BOOL ("print-quoted", print_quoted,
2290 doc: /* Non-nil means print quoted forms with reader syntax.
2291 I.e., (quote foo) prints as 'foo, (function foo) as #'foo. */);
2292 print_quoted = 0;
2294 DEFVAR_LISP ("print-gensym", Vprint_gensym,
2295 doc: /* Non-nil means print uninterned symbols so they will read as uninterned.
2296 I.e., the value of (make-symbol \"foobar\") prints as #:foobar.
2297 When the uninterned symbol appears within a recursive data structure,
2298 and the symbol appears more than once, in addition use the #N# and #N=
2299 constructs as needed, so that multiple references to the same symbol are
2300 shared once again when the text is read back. */);
2301 Vprint_gensym = Qnil;
2303 DEFVAR_LISP ("print-circle", Vprint_circle,
2304 doc: /* Non-nil means print recursive structures using #N= and #N# syntax.
2305 If nil, printing proceeds recursively and may lead to
2306 `max-lisp-eval-depth' being exceeded or an error may occur:
2307 \"Apparently circular structure being printed.\" Also see
2308 `print-length' and `print-level'.
2309 If non-nil, shared substructures anywhere in the structure are printed
2310 with `#N=' before the first occurrence (in the order of the print
2311 representation) and `#N#' in place of each subsequent occurrence,
2312 where N is a positive decimal integer. */);
2313 Vprint_circle = Qnil;
2315 DEFVAR_LISP ("print-continuous-numbering", Vprint_continuous_numbering,
2316 doc: /* Non-nil means number continuously across print calls.
2317 This affects the numbers printed for #N= labels and #M# references.
2318 See also `print-circle', `print-gensym', and `print-number-table'.
2319 This variable should not be set with `setq'; bind it with a `let' instead. */);
2320 Vprint_continuous_numbering = Qnil;
2322 DEFVAR_LISP ("print-number-table", Vprint_number_table,
2323 doc: /* A vector used internally to produce `#N=' labels and `#N#' references.
2324 The Lisp printer uses this vector to detect Lisp objects referenced more
2325 than once.
2327 When you bind `print-continuous-numbering' to t, you should probably
2328 also bind `print-number-table' to nil. This ensures that the value of
2329 `print-number-table' can be garbage-collected once the printing is
2330 done. If all elements of `print-number-table' are nil, it means that
2331 the printing done so far has not found any shared structure or objects
2332 that need to be recorded in the table. */);
2333 Vprint_number_table = Qnil;
2335 DEFVAR_LISP ("print-charset-text-property", Vprint_charset_text_property,
2336 doc: /* A flag to control printing of `charset' text property on printing a string.
2337 The value must be nil, t, or `default'.
2339 If the value is nil, don't print the text property `charset'.
2341 If the value is t, always print the text property `charset'.
2343 If the value is `default', print the text property `charset' only when
2344 the value is different from what is guessed in the current charset
2345 priorities. */);
2346 Vprint_charset_text_property = Qdefault;
2348 /* prin1_to_string_buffer initialized in init_buffer_once in buffer.c */
2349 staticpro (&Vprin1_to_string_buffer);
2351 defsubr (&Sprin1);
2352 defsubr (&Sprin1_to_string);
2353 defsubr (&Serror_message_string);
2354 defsubr (&Sprinc);
2355 defsubr (&Sprint);
2356 defsubr (&Sterpri);
2357 defsubr (&Swrite_char);
2358 #ifdef WITH_REDIRECT_DEBUGGING_OUTPUT
2359 defsubr (&Sredirect_debugging_output);
2360 #endif
2362 DEFSYM (Qprint_escape_newlines, "print-escape-newlines");
2363 DEFSYM (Qprint_escape_multibyte, "print-escape-multibyte");
2364 DEFSYM (Qprint_escape_nonascii, "print-escape-nonascii");
2366 print_prune_charset_plist = Qnil;
2367 staticpro (&print_prune_charset_plist);