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