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