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