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