(code_convert_string): Add record_unwind_protect to
[bpt/emacs.git] / src / print.c
... / ...
CommitLineData
1/* Lisp object printing and output streams.
2 Copyright (C) 1985, 86, 88, 93, 94, 95, 97, 1998
3 Free Software Foundation, Inc.
4
5This file is part of GNU Emacs.
6
7GNU Emacs is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2, or (at your option)
10any later version.
11
12GNU Emacs is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with GNU Emacs; see the file COPYING. If not, write to
19the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20Boston, MA 02111-1307, USA. */
21
22
23#include <config.h>
24#include <stdio.h>
25#include "lisp.h"
26#include "buffer.h"
27#include "charset.h"
28#include "frame.h"
29#include "window.h"
30#include "process.h"
31#include "dispextern.h"
32#include "termchar.h"
33#include "keyboard.h"
34
35#ifdef USE_TEXT_PROPERTIES
36#include "intervals.h"
37#endif
38
39Lisp_Object Vstandard_output, Qstandard_output;
40
41Lisp_Object Qtemp_buffer_setup_hook;
42
43/* These are used to print like we read. */
44extern Lisp_Object Qbackquote, Qcomma, Qcomma_at, Qcomma_dot, Qfunction;
45
46#ifdef LISP_FLOAT_TYPE
47Lisp_Object Vfloat_output_format, Qfloat_output_format;
48
49/* Work around a problem that happens because math.h on hpux 7
50 defines two static variables--which, in Emacs, are not really static,
51 because `static' is defined as nothing. The problem is that they are
52 defined both here and in lread.c.
53 These macros prevent the name conflict. */
54#if defined (HPUX) && !defined (HPUX8)
55#define _MAXLDBL print_maxldbl
56#define _NMAXLDBL print_nmaxldbl
57#endif
58
59#include <math.h>
60
61#if STDC_HEADERS
62#include <float.h>
63#include <stdlib.h>
64#endif
65
66/* Default to values appropriate for IEEE floating point. */
67#ifndef FLT_RADIX
68#define FLT_RADIX 2
69#endif
70#ifndef DBL_MANT_DIG
71#define DBL_MANT_DIG 53
72#endif
73#ifndef DBL_DIG
74#define DBL_DIG 15
75#endif
76#ifndef DBL_MIN
77#define DBL_MIN 2.2250738585072014e-308
78#endif
79
80#ifdef DBL_MIN_REPLACEMENT
81#undef DBL_MIN
82#define DBL_MIN DBL_MIN_REPLACEMENT
83#endif
84
85/* Define DOUBLE_DIGITS_BOUND, an upper bound on the number of decimal digits
86 needed to express a float without losing information.
87 The general-case formula is valid for the usual case, IEEE floating point,
88 but many compilers can't optimize the formula to an integer constant,
89 so make a special case for it. */
90#if FLT_RADIX == 2 && DBL_MANT_DIG == 53
91#define DOUBLE_DIGITS_BOUND 17 /* IEEE floating point */
92#else
93#define DOUBLE_DIGITS_BOUND ((int) ceil (log10 (pow (FLT_RADIX, DBL_MANT_DIG))))
94#endif
95
96#endif /* LISP_FLOAT_TYPE */
97
98/* Avoid actual stack overflow in print. */
99int print_depth;
100
101/* Detect most circularities to print finite output. */
102#define PRINT_CIRCLE 200
103Lisp_Object being_printed[PRINT_CIRCLE];
104
105/* When printing into a buffer, first we put the text in this
106 block, then insert it all at once. */
107char *print_buffer;
108
109/* Size allocated in print_buffer. */
110int print_buffer_size;
111/* Chars stored in print_buffer. */
112int print_buffer_pos;
113/* Bytes stored in print_buffer. */
114int print_buffer_pos_byte;
115
116/* Maximum length of list to print in full; noninteger means
117 effectively infinity */
118
119Lisp_Object Vprint_length;
120
121/* Maximum depth of list to print in full; noninteger means
122 effectively infinity. */
123
124Lisp_Object Vprint_level;
125
126/* Nonzero means print newlines in strings as \n. */
127
128int print_escape_newlines;
129
130/* Nonzero means to print single-byte non-ascii characters in strings as
131 octal escapes. */
132
133int print_escape_nonascii;
134
135/* Nonzero means to print multibyte characters in strings as hex escapes. */
136
137int print_escape_multibyte;
138
139Lisp_Object Qprint_escape_newlines;
140Lisp_Object Qprint_escape_multibyte, Qprint_escape_nonascii;
141
142/* Nonzero means print (quote foo) forms as 'foo, etc. */
143
144int print_quoted;
145
146/* Non-nil means print #: before uninterned symbols. */
147
148Lisp_Object Vprint_gensym;
149
150/* Non-nil means print recursive structures using #n= and #n# syntax. */
151
152Lisp_Object Vprint_circle;
153
154/* Non-nil means keep continuous number for #n= and #n# syntax
155 between several print functions. */
156
157Lisp_Object Vprint_continuous_numbering;
158
159/* Vprint_number_table is a vector like [OBJ1 STAT1 OBJ2 STAT2 ...],
160 where OBJn are objects going to be printed, and STATn are their status,
161 which may be different meanings during process. See the comments of
162 the functions print and print_preprocess for details.
163 print_number_index keeps the last position the next object should be added,
164 twice of which is the actual vector position in Vprint_number_table. */
165int print_number_index;
166Lisp_Object Vprint_number_table;
167
168/* PRINT_NUMBER_OBJECT returns the I'th object in Vprint_number_table TABLE.
169 PRINT_NUMBER_STATUS returns the status of the I'th object in TABLE.
170 See the comment of the variable Vprint_number_table. */
171#define PRINT_NUMBER_OBJECT(table,i) XVECTOR ((table))->contents[(i) * 2]
172#define PRINT_NUMBER_STATUS(table,i) XVECTOR ((table))->contents[(i) * 2 + 1]
173
174/* Nonzero means print newline to stdout before next minibuffer message.
175 Defined in xdisp.c */
176
177extern int noninteractive_need_newline;
178
179extern int minibuffer_auto_raise;
180
181#ifdef MAX_PRINT_CHARS
182static int print_chars;
183static int max_print;
184#endif /* MAX_PRINT_CHARS */
185
186void print_interval ();
187
188\f
189/* Low level output routines for characters and strings */
190
191/* Lisp functions to do output using a stream
192 must have the stream in a variable called printcharfun
193 and must start with PRINTPREPARE, end with PRINTFINISH,
194 and use PRINTDECLARE to declare common variables.
195 Use PRINTCHAR to output one character,
196 or call strout to output a block of characters. */
197
198#define PRINTDECLARE \
199 struct buffer *old = current_buffer; \
200 int old_point = -1, start_point; \
201 int old_point_byte, start_point_byte; \
202 int specpdl_count = specpdl_ptr - specpdl; \
203 int free_print_buffer = 0; \
204 int multibyte = !NILP (current_buffer->enable_multibyte_characters); \
205 Lisp_Object original
206
207#define PRINTPREPARE \
208 original = printcharfun; \
209 if (NILP (printcharfun)) printcharfun = Qt; \
210 if (BUFFERP (printcharfun)) \
211 { \
212 if (XBUFFER (printcharfun) != current_buffer) \
213 Fset_buffer (printcharfun); \
214 printcharfun = Qnil; \
215 } \
216 if (MARKERP (printcharfun)) \
217 { \
218 if (!(XMARKER (original)->buffer)) \
219 error ("Marker does not point anywhere"); \
220 if (XMARKER (original)->buffer != current_buffer) \
221 set_buffer_internal (XMARKER (original)->buffer); \
222 old_point = PT; \
223 old_point_byte = PT_BYTE; \
224 SET_PT_BOTH (marker_position (printcharfun), \
225 marker_byte_position (printcharfun)); \
226 start_point = PT; \
227 start_point_byte = PT_BYTE; \
228 printcharfun = Qnil; \
229 } \
230 if (NILP (printcharfun)) \
231 { \
232 Lisp_Object string; \
233 if (NILP (current_buffer->enable_multibyte_characters) \
234 && ! print_escape_multibyte) \
235 specbind (Qprint_escape_multibyte, Qt); \
236 if (! NILP (current_buffer->enable_multibyte_characters) \
237 && ! print_escape_nonascii) \
238 specbind (Qprint_escape_nonascii, Qt); \
239 if (print_buffer != 0) \
240 { \
241 string = make_string_from_bytes (print_buffer, \
242 print_buffer_pos, \
243 print_buffer_pos_byte); \
244 record_unwind_protect (print_unwind, string); \
245 } \
246 else \
247 { \
248 print_buffer_size = 1000; \
249 print_buffer = (char *) xmalloc (print_buffer_size); \
250 free_print_buffer = 1; \
251 } \
252 print_buffer_pos = 0; \
253 print_buffer_pos_byte = 0; \
254 } \
255 if (EQ (printcharfun, Qt)) \
256 setup_echo_area_for_printing (multibyte);
257
258#define PRINTFINISH \
259 if (NILP (printcharfun)) \
260 { \
261 if (print_buffer_pos != print_buffer_pos_byte \
262 && NILP (current_buffer->enable_multibyte_characters)) \
263 { \
264 unsigned char *temp \
265 = (unsigned char *) alloca (print_buffer_pos + 1); \
266 copy_text (print_buffer, temp, print_buffer_pos_byte, \
267 1, 0); \
268 insert_1_both (temp, print_buffer_pos, \
269 print_buffer_pos, 0, 1, 0); \
270 } \
271 else \
272 insert_1_both (print_buffer, print_buffer_pos, \
273 print_buffer_pos_byte, 0, 1, 0); \
274 } \
275 if (free_print_buffer) \
276 { \
277 xfree (print_buffer); \
278 print_buffer = 0; \
279 } \
280 unbind_to (specpdl_count, Qnil); \
281 if (MARKERP (original)) \
282 set_marker_both (original, Qnil, PT, PT_BYTE); \
283 if (old_point >= 0) \
284 SET_PT_BOTH (old_point + (old_point >= start_point \
285 ? PT - start_point : 0), \
286 old_point_byte + (old_point_byte >= start_point_byte \
287 ? PT_BYTE - start_point_byte : 0)); \
288 if (old != current_buffer) \
289 set_buffer_internal (old);
290
291#define PRINTCHAR(ch) printchar (ch, printcharfun)
292
293/* This is used to restore the saved contents of print_buffer
294 when there is a recursive call to print. */
295
296static Lisp_Object
297print_unwind (saved_text)
298 Lisp_Object saved_text;
299{
300 bcopy (XSTRING (saved_text)->data, print_buffer, XSTRING (saved_text)->size);
301}
302
303
304/* Print character CH using method FUN. FUN nil means print to
305 print_buffer. FUN t means print to echo area or stdout if
306 non-interactive. If FUN is neither nil nor t, call FUN with CH as
307 argument. */
308
309static void
310printchar (ch, fun)
311 unsigned int ch;
312 Lisp_Object fun;
313{
314#ifdef MAX_PRINT_CHARS
315 if (max_print)
316 print_chars++;
317#endif /* MAX_PRINT_CHARS */
318
319 if (!NILP (fun) && !EQ (fun, Qt))
320 call1 (fun, make_number (ch));
321 else
322 {
323 unsigned char work[4], *str;
324 int len = CHAR_STRING (ch, work, str);
325
326 QUIT;
327
328 if (NILP (fun))
329 {
330 if (print_buffer_pos_byte + len >= print_buffer_size)
331 print_buffer = (char *) xrealloc (print_buffer,
332 print_buffer_size *= 2);
333 bcopy (str, print_buffer + print_buffer_pos_byte, len);
334 print_buffer_pos += 1;
335 print_buffer_pos_byte += len;
336 }
337 else if (noninteractive)
338 {
339 fwrite (str, 1, len, stdout);
340 noninteractive_need_newline = 1;
341 }
342 else
343 {
344 int multibyte_p
345 = !NILP (current_buffer->enable_multibyte_characters);
346
347 if (!message_buf_print)
348 setup_echo_area_for_printing (multibyte_p);
349
350 insert_char (ch);
351 message_dolog (str, len, 0, multibyte_p);
352 }
353 }
354}
355
356
357/* Output SIZE characters, SIZE_BYTE bytes from string PTR using
358 method PRINTCHARFUN. If SIZE < 0, use the string length of PTR for
359 both SIZE and SIZE_BYTE. PRINTCHARFUN nil means output to
360 print_buffer. PRINTCHARFUN t means output to the echo area or to
361 stdout if non-interactive. If neither nil nor t, call Lisp
362 function PRINTCHARFUN for each character printed. MULTIBYTE
363 non-zero means PTR contains multibyte characters. */
364
365static void
366strout (ptr, size, size_byte, printcharfun, multibyte)
367 char *ptr;
368 int size, size_byte;
369 Lisp_Object printcharfun;
370 int multibyte;
371{
372 if (size < 0)
373 size_byte = size = strlen (ptr);
374
375 if (NILP (printcharfun))
376 {
377 if (print_buffer_pos_byte + size_byte > print_buffer_size)
378 {
379 print_buffer_size = print_buffer_size * 2 + size_byte;
380 print_buffer = (char *) xrealloc (print_buffer,
381 print_buffer_size);
382 }
383 bcopy (ptr, print_buffer + print_buffer_pos_byte, size_byte);
384 print_buffer_pos += size;
385 print_buffer_pos_byte += size_byte;
386
387#ifdef MAX_PRINT_CHARS
388 if (max_print)
389 print_chars += size;
390#endif /* MAX_PRINT_CHARS */
391 }
392 else if (noninteractive)
393 {
394 fwrite (ptr, 1, size_byte, stdout);
395 noninteractive_need_newline = 1;
396 }
397 else if (EQ (printcharfun, Qt))
398 {
399 /* Output to echo area. We're trying to avoid a little overhead
400 here, that's the reason we don't call printchar to do the
401 job. */
402 int i;
403 int multibyte_p
404 = !NILP (current_buffer->enable_multibyte_characters);
405
406 if (!message_buf_print)
407 setup_echo_area_for_printing (multibyte_p);
408
409 message_dolog (ptr, size_byte, 0, multibyte_p);
410
411 if (size == size_byte)
412 {
413 for (i = 0; i < size; ++i)
414 insert_char (*ptr++);
415 }
416 else
417 {
418 int len;
419 for (i = 0; i < size_byte; i += len)
420 {
421 int ch = STRING_CHAR_AND_LENGTH (ptr + i, size_byte - i, len);
422 insert_char (ch);
423 }
424 }
425
426#ifdef MAX_PRINT_CHARS
427 if (max_print)
428 print_chars += size;
429#endif /* MAX_PRINT_CHARS */
430 }
431 else
432 {
433 /* PRINTCHARFUN is a Lisp function. */
434 int i = 0;
435
436 if (size == size_byte)
437 {
438 while (i < size_byte)
439 {
440 int ch = ptr[i++];
441 PRINTCHAR (ch);
442 }
443 }
444 else
445 {
446 while (i < size_byte)
447 {
448 /* Here, we must convert each multi-byte form to the
449 corresponding character code before handing it to
450 PRINTCHAR. */
451 int len;
452 int ch = STRING_CHAR_AND_LENGTH (ptr + i, size_byte - i, len);
453 PRINTCHAR (ch);
454 i += len;
455 }
456 }
457 }
458}
459
460/* Print the contents of a string STRING using PRINTCHARFUN.
461 It isn't safe to use strout in many cases,
462 because printing one char can relocate. */
463
464static void
465print_string (string, printcharfun)
466 Lisp_Object string;
467 Lisp_Object printcharfun;
468{
469 if (EQ (printcharfun, Qt) || NILP (printcharfun))
470 {
471 int chars;
472
473 if (STRING_MULTIBYTE (string))
474 chars = XSTRING (string)->size;
475 else if (EQ (printcharfun, Qt)
476 ? ! NILP (buffer_defaults.enable_multibyte_characters)
477 : ! NILP (current_buffer->enable_multibyte_characters))
478 chars = multibyte_chars_in_text (XSTRING (string)->data,
479 STRING_BYTES (XSTRING (string)));
480 else
481 chars = STRING_BYTES (XSTRING (string));
482
483 /* strout is safe for output to a frame (echo area) or to print_buffer. */
484 strout (XSTRING (string)->data,
485 chars, STRING_BYTES (XSTRING (string)),
486 printcharfun, STRING_MULTIBYTE (string));
487 }
488 else
489 {
490 /* Otherwise, string may be relocated by printing one char.
491 So re-fetch the string address for each character. */
492 int i;
493 int size = XSTRING (string)->size;
494 int size_byte = STRING_BYTES (XSTRING (string));
495 struct gcpro gcpro1;
496 GCPRO1 (string);
497 if (size == size_byte)
498 for (i = 0; i < size; i++)
499 PRINTCHAR (XSTRING (string)->data[i]);
500 else
501 for (i = 0; i < size_byte; i++)
502 {
503 /* Here, we must convert each multi-byte form to the
504 corresponding character code before handing it to PRINTCHAR. */
505 int len;
506 int ch = STRING_CHAR_AND_LENGTH (XSTRING (string)->data + i,
507 size_byte - i, len);
508 if (!CHAR_VALID_P (ch, 0))
509 {
510 ch = XSTRING (string)->data[i];
511 len = 1;
512 }
513 PRINTCHAR (ch);
514 i += len;
515 }
516 UNGCPRO;
517 }
518}
519\f
520DEFUN ("write-char", Fwrite_char, Swrite_char, 1, 2, 0,
521 "Output character CHARACTER to stream PRINTCHARFUN.\n\
522PRINTCHARFUN defaults to the value of `standard-output' (which see).")
523 (character, printcharfun)
524 Lisp_Object character, printcharfun;
525{
526 PRINTDECLARE;
527
528 if (NILP (printcharfun))
529 printcharfun = Vstandard_output;
530 CHECK_NUMBER (character, 0);
531 PRINTPREPARE;
532 PRINTCHAR (XINT (character));
533 PRINTFINISH;
534 return character;
535}
536
537/* Used from outside of print.c to print a block of SIZE
538 single-byte chars at DATA on the default output stream.
539 Do not use this on the contents of a Lisp string. */
540
541void
542write_string (data, size)
543 char *data;
544 int size;
545{
546 PRINTDECLARE;
547 Lisp_Object printcharfun;
548
549 printcharfun = Vstandard_output;
550
551 PRINTPREPARE;
552 strout (data, size, size, printcharfun, 0);
553 PRINTFINISH;
554}
555
556/* Used from outside of print.c to print a block of SIZE
557 single-byte chars at DATA on a specified stream PRINTCHARFUN.
558 Do not use this on the contents of a Lisp string. */
559
560void
561write_string_1 (data, size, printcharfun)
562 char *data;
563 int size;
564 Lisp_Object printcharfun;
565{
566 PRINTDECLARE;
567
568 PRINTPREPARE;
569 strout (data, size, size, printcharfun, 0);
570 PRINTFINISH;
571}
572
573
574void
575temp_output_buffer_setup (bufname)
576 char *bufname;
577{
578 int count = specpdl_ptr - specpdl;
579 register struct buffer *old = current_buffer;
580 register Lisp_Object buf;
581
582 record_unwind_protect (set_buffer_if_live, Fcurrent_buffer ());
583
584 Fset_buffer (Fget_buffer_create (build_string (bufname)));
585
586 current_buffer->directory = old->directory;
587 current_buffer->read_only = Qnil;
588 current_buffer->filename = Qnil;
589 current_buffer->undo_list = Qt;
590 current_buffer->overlays_before = Qnil;
591 current_buffer->overlays_after = Qnil;
592 current_buffer->enable_multibyte_characters
593 = buffer_defaults.enable_multibyte_characters;
594 Ferase_buffer ();
595 XSETBUFFER (buf, current_buffer);
596
597 call1 (Vrun_hooks, Qtemp_buffer_setup_hook);
598
599 unbind_to (count, Qnil);
600
601 specbind (Qstandard_output, buf);
602}
603
604Lisp_Object
605internal_with_output_to_temp_buffer (bufname, function, args)
606 char *bufname;
607 Lisp_Object (*function) P_ ((Lisp_Object));
608 Lisp_Object args;
609{
610 int count = specpdl_ptr - specpdl;
611 Lisp_Object buf, val;
612 struct gcpro gcpro1;
613
614 GCPRO1 (args);
615 record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
616 temp_output_buffer_setup (bufname);
617 buf = Vstandard_output;
618 UNGCPRO;
619
620 val = (*function) (args);
621
622 GCPRO1 (val);
623 temp_output_buffer_show (buf);
624 UNGCPRO;
625
626 return unbind_to (count, val);
627}
628
629DEFUN ("with-output-to-temp-buffer", Fwith_output_to_temp_buffer, Swith_output_to_temp_buffer,
630 1, UNEVALLED, 0,
631 "Bind `standard-output' to buffer BUFNAME, eval BODY, then show that buffer.\n\
632The buffer is cleared out initially, and marked as unmodified when done.\n\
633All output done by BODY is inserted in that buffer by default.\n\
634The buffer is displayed in another window, but not selected.\n\
635The value of the last form in BODY is returned.\n\
636If BODY does not finish normally, the buffer BUFNAME is not displayed.\n\
637\n\
638The hook `temp-buffer-setup-hook' is run before BODY,\n\
639with the buffer BUFNAME temporarily current.\n\
640The hook `temp-buffer-show-hook' is run after the buffer is displayed,\n\
641with the buffer temporarily current, and the window that was used\n\
642to display it temporarily selected.\n\
643\n\
644If variable `temp-buffer-show-function' is non-nil, call it at the end\n\
645to get the buffer displayed instead of just displaying the non-selected\n\
646buffer and calling the hook. It gets one argument, the buffer to display.")
647 (args)
648 Lisp_Object args;
649{
650 struct gcpro gcpro1;
651 Lisp_Object name;
652 int count = specpdl_ptr - specpdl;
653 Lisp_Object buf, val;
654
655 GCPRO1(args);
656 name = Feval (Fcar (args));
657 UNGCPRO;
658
659 CHECK_STRING (name, 0);
660 temp_output_buffer_setup (XSTRING (name)->data);
661 buf = Vstandard_output;
662
663 val = Fprogn (Fcdr (args));
664
665 temp_output_buffer_show (buf);
666
667 return unbind_to (count, val);
668}
669
670\f
671static void print ();
672static void print_preprocess ();
673#ifdef USE_TEXT_PROPERTIES
674static void print_preprocess_string ();
675#endif /* USE_TEXT_PROPERTIES */
676static void print_object ();
677
678DEFUN ("terpri", Fterpri, Sterpri, 0, 1, 0,
679 "Output a newline to stream PRINTCHARFUN.\n\
680If PRINTCHARFUN is omitted or nil, the value of `standard-output' is used.")
681 (printcharfun)
682 Lisp_Object printcharfun;
683{
684 PRINTDECLARE;
685
686 if (NILP (printcharfun))
687 printcharfun = Vstandard_output;
688 PRINTPREPARE;
689 PRINTCHAR ('\n');
690 PRINTFINISH;
691 return Qt;
692}
693
694DEFUN ("prin1", Fprin1, Sprin1, 1, 2, 0,
695 "Output the printed representation of OBJECT, any Lisp object.\n\
696Quoting characters are printed when needed to make output that `read'\n\
697can handle, whenever this is possible.\n\
698Output stream is PRINTCHARFUN, or value of `standard-output' (which see).")
699 (object, printcharfun)
700 Lisp_Object object, printcharfun;
701{
702 PRINTDECLARE;
703
704#ifdef MAX_PRINT_CHARS
705 max_print = 0;
706#endif /* MAX_PRINT_CHARS */
707 if (NILP (printcharfun))
708 printcharfun = Vstandard_output;
709 PRINTPREPARE;
710 print (object, printcharfun, 1);
711 PRINTFINISH;
712 return object;
713}
714
715/* a buffer which is used to hold output being built by prin1-to-string */
716Lisp_Object Vprin1_to_string_buffer;
717
718DEFUN ("prin1-to-string", Fprin1_to_string, Sprin1_to_string, 1, 2, 0,
719 "Return a string containing the printed representation of OBJECT,\n\
720any Lisp object. Quoting characters are used when needed to make output\n\
721that `read' can handle, whenever this is possible, unless the optional\n\
722second argument NOESCAPE is non-nil.")
723 (object, noescape)
724 Lisp_Object object, noescape;
725{
726 PRINTDECLARE;
727 Lisp_Object printcharfun;
728 struct gcpro gcpro1, gcpro2;
729 Lisp_Object tem;
730
731 /* Save and restore this--we are altering a buffer
732 but we don't want to deactivate the mark just for that.
733 No need for specbind, since errors deactivate the mark. */
734 tem = Vdeactivate_mark;
735 GCPRO2 (object, tem);
736
737 printcharfun = Vprin1_to_string_buffer;
738 PRINTPREPARE;
739 print (object, printcharfun, NILP (noescape));
740 /* Make Vprin1_to_string_buffer be the default buffer after PRINTFINSH */
741 PRINTFINISH;
742 set_buffer_internal (XBUFFER (Vprin1_to_string_buffer));
743 object = Fbuffer_string ();
744
745 Ferase_buffer ();
746 set_buffer_internal (old);
747
748 Vdeactivate_mark = tem;
749 UNGCPRO;
750
751 return object;
752}
753
754DEFUN ("princ", Fprinc, Sprinc, 1, 2, 0,
755 "Output the printed representation of OBJECT, any Lisp object.\n\
756No quoting characters are used; no delimiters are printed around\n\
757the contents of strings.\n\
758Output stream is PRINTCHARFUN, or value of standard-output (which see).")
759 (object, printcharfun)
760 Lisp_Object object, printcharfun;
761{
762 PRINTDECLARE;
763
764 if (NILP (printcharfun))
765 printcharfun = Vstandard_output;
766 PRINTPREPARE;
767 print (object, printcharfun, 0);
768 PRINTFINISH;
769 return object;
770}
771
772DEFUN ("print", Fprint, Sprint, 1, 2, 0,
773 "Output the printed representation of OBJECT, with newlines around it.\n\
774Quoting characters are printed when needed to make output that `read'\n\
775can handle, whenever this is possible.\n\
776Output stream is PRINTCHARFUN, or value of `standard-output' (which see).")
777 (object, printcharfun)
778 Lisp_Object object, printcharfun;
779{
780 PRINTDECLARE;
781 struct gcpro gcpro1;
782
783#ifdef MAX_PRINT_CHARS
784 print_chars = 0;
785 max_print = MAX_PRINT_CHARS;
786#endif /* MAX_PRINT_CHARS */
787 if (NILP (printcharfun))
788 printcharfun = Vstandard_output;
789 GCPRO1 (object);
790 PRINTPREPARE;
791 PRINTCHAR ('\n');
792 print (object, printcharfun, 1);
793 PRINTCHAR ('\n');
794 PRINTFINISH;
795#ifdef MAX_PRINT_CHARS
796 max_print = 0;
797 print_chars = 0;
798#endif /* MAX_PRINT_CHARS */
799 UNGCPRO;
800 return object;
801}
802
803/* The subroutine object for external-debugging-output is kept here
804 for the convenience of the debugger. */
805Lisp_Object Qexternal_debugging_output;
806
807DEFUN ("external-debugging-output", Fexternal_debugging_output, Sexternal_debugging_output, 1, 1, 0,
808 "Write CHARACTER to stderr.\n\
809You can call print while debugging emacs, and pass it this function\n\
810to make it write to the debugging output.\n")
811 (character)
812 Lisp_Object character;
813{
814 CHECK_NUMBER (character, 0);
815 putc (XINT (character), stderr);
816
817#ifdef WINDOWSNT
818 /* Send the output to a debugger (nothing happens if there isn't one). */
819 {
820 char buf[2] = {(char) XINT (character), '\0'};
821 OutputDebugString (buf);
822 }
823#endif
824
825 return character;
826}
827
828/* This is the interface for debugging printing. */
829
830void
831debug_print (arg)
832 Lisp_Object arg;
833{
834 Fprin1 (arg, Qexternal_debugging_output);
835 fprintf (stderr, "\r\n");
836}
837\f
838DEFUN ("error-message-string", Ferror_message_string, Serror_message_string,
839 1, 1, 0,
840 "Convert an error value (ERROR-SYMBOL . DATA) to an error message.")
841 (obj)
842 Lisp_Object obj;
843{
844 struct buffer *old = current_buffer;
845 Lisp_Object value;
846 struct gcpro gcpro1;
847
848 /* If OBJ is (error STRING), just return STRING.
849 That is not only faster, it also avoids the need to allocate
850 space here when the error is due to memory full. */
851 if (CONSP (obj) && EQ (XCAR (obj), Qerror)
852 && CONSP (XCDR (obj))
853 && STRINGP (XCAR (XCDR (obj)))
854 && NILP (XCDR (XCDR (obj))))
855 return XCAR (XCDR (obj));
856
857 print_error_message (obj, Vprin1_to_string_buffer);
858
859 set_buffer_internal (XBUFFER (Vprin1_to_string_buffer));
860 value = Fbuffer_string ();
861
862 GCPRO1 (value);
863 Ferase_buffer ();
864 set_buffer_internal (old);
865 UNGCPRO;
866
867 return value;
868}
869
870/* Print an error message for the error DATA
871 onto Lisp output stream STREAM (suitable for the print functions). */
872
873void
874print_error_message (data, stream)
875 Lisp_Object data, stream;
876{
877 Lisp_Object errname, errmsg, file_error, tail;
878 struct gcpro gcpro1;
879 int i;
880
881 errname = Fcar (data);
882
883 if (EQ (errname, Qerror))
884 {
885 data = Fcdr (data);
886 if (!CONSP (data)) data = Qnil;
887 errmsg = Fcar (data);
888 file_error = Qnil;
889 }
890 else
891 {
892 errmsg = Fget (errname, Qerror_message);
893 file_error = Fmemq (Qfile_error,
894 Fget (errname, Qerror_conditions));
895 }
896
897 /* Print an error message including the data items. */
898
899 tail = Fcdr_safe (data);
900 GCPRO1 (tail);
901
902 /* For file-error, make error message by concatenating
903 all the data items. They are all strings. */
904 if (!NILP (file_error) && CONSP (tail))
905 errmsg = XCAR (tail), tail = XCDR (tail);
906
907 if (STRINGP (errmsg))
908 Fprinc (errmsg, stream);
909 else
910 write_string_1 ("peculiar error", -1, stream);
911
912 for (i = 0; CONSP (tail); tail = Fcdr (tail), i++)
913 {
914 write_string_1 (i ? ", " : ": ", 2, stream);
915 if (!NILP (file_error))
916 Fprinc (Fcar (tail), stream);
917 else
918 Fprin1 (Fcar (tail), stream);
919 }
920 UNGCPRO;
921}
922\f
923#ifdef LISP_FLOAT_TYPE
924
925/*
926 * The buffer should be at least as large as the max string size of the
927 * largest float, printed in the biggest notation. This is undoubtedly
928 * 20d float_output_format, with the negative of the C-constant "HUGE"
929 * from <math.h>.
930 *
931 * On the vax the worst case is -1e38 in 20d format which takes 61 bytes.
932 *
933 * I assume that IEEE-754 format numbers can take 329 bytes for the worst
934 * case of -1e307 in 20d float_output_format. What is one to do (short of
935 * re-writing _doprnt to be more sane)?
936 * -wsr
937 */
938
939void
940float_to_string (buf, data)
941 unsigned char *buf;
942 double data;
943{
944 unsigned char *cp;
945 int width;
946
947 /* Check for plus infinity in a way that won't lose
948 if there is no plus infinity. */
949 if (data == data / 2 && data > 1.0)
950 {
951 strcpy (buf, "1.0e+INF");
952 return;
953 }
954 /* Likewise for minus infinity. */
955 if (data == data / 2 && data < -1.0)
956 {
957 strcpy (buf, "-1.0e+INF");
958 return;
959 }
960 /* Check for NaN in a way that won't fail if there are no NaNs. */
961 if (! (data * 0.0 >= 0.0))
962 {
963 strcpy (buf, "0.0e+NaN");
964 return;
965 }
966
967 if (NILP (Vfloat_output_format)
968 || !STRINGP (Vfloat_output_format))
969 lose:
970 {
971 /* Generate the fewest number of digits that represent the
972 floating point value without losing information.
973 The following method is simple but a bit slow.
974 For ideas about speeding things up, please see:
975
976 Guy L Steele Jr & Jon L White, How to print floating-point numbers
977 accurately. SIGPLAN notices 25, 6 (June 1990), 112-126.
978
979 Robert G Burger & R Kent Dybvig, Printing floating point numbers
980 quickly and accurately, SIGPLAN notices 31, 5 (May 1996), 108-116. */
981
982 width = fabs (data) < DBL_MIN ? 1 : DBL_DIG;
983 do
984 sprintf (buf, "%.*g", width, data);
985 while (width++ < DOUBLE_DIGITS_BOUND && atof (buf) != data);
986 }
987 else /* oink oink */
988 {
989 /* Check that the spec we have is fully valid.
990 This means not only valid for printf,
991 but meant for floats, and reasonable. */
992 cp = XSTRING (Vfloat_output_format)->data;
993
994 if (cp[0] != '%')
995 goto lose;
996 if (cp[1] != '.')
997 goto lose;
998
999 cp += 2;
1000
1001 /* Check the width specification. */
1002 width = -1;
1003 if ('0' <= *cp && *cp <= '9')
1004 {
1005 width = 0;
1006 do
1007 width = (width * 10) + (*cp++ - '0');
1008 while (*cp >= '0' && *cp <= '9');
1009
1010 /* A precision of zero is valid only for %f. */
1011 if (width > DBL_DIG
1012 || (width == 0 && *cp != 'f'))
1013 goto lose;
1014 }
1015
1016 if (*cp != 'e' && *cp != 'f' && *cp != 'g')
1017 goto lose;
1018
1019 if (cp[1] != 0)
1020 goto lose;
1021
1022 sprintf (buf, XSTRING (Vfloat_output_format)->data, data);
1023 }
1024
1025 /* Make sure there is a decimal point with digit after, or an
1026 exponent, so that the value is readable as a float. But don't do
1027 this with "%.0f"; it's valid for that not to produce a decimal
1028 point. Note that width can be 0 only for %.0f. */
1029 if (width != 0)
1030 {
1031 for (cp = buf; *cp; cp++)
1032 if ((*cp < '0' || *cp > '9') && *cp != '-')
1033 break;
1034
1035 if (*cp == '.' && cp[1] == 0)
1036 {
1037 cp[1] = '0';
1038 cp[2] = 0;
1039 }
1040
1041 if (*cp == 0)
1042 {
1043 *cp++ = '.';
1044 *cp++ = '0';
1045 *cp++ = 0;
1046 }
1047 }
1048}
1049#endif /* LISP_FLOAT_TYPE */
1050\f
1051static void
1052print (obj, printcharfun, escapeflag)
1053 Lisp_Object obj;
1054 register Lisp_Object printcharfun;
1055 int escapeflag;
1056{
1057 print_depth = 0;
1058
1059 /* Reset print_number_index and Vprint_number_table only when
1060 the variable Vprint_continuous_numbering is nil. Otherwise,
1061 the values of these variables will be kept between several
1062 print functions. */
1063 if (NILP (Vprint_continuous_numbering))
1064 {
1065 print_number_index = 0;
1066 Vprint_number_table = Qnil;
1067 }
1068
1069 /* Construct Vprint_number_table for print-gensym and print-circle. */
1070 if (!NILP (Vprint_gensym) || !NILP (Vprint_circle))
1071 {
1072 int i, start, index;
1073 /* Construct Vprint_number_table. */
1074 start = index = print_number_index;
1075 print_preprocess (obj);
1076 /* Remove unnecessary objects, which appear only once in OBJ;
1077 that is, whose status is Qnil. */
1078 for (i = start; i < print_number_index; i++)
1079 if (!NILP (PRINT_NUMBER_STATUS (Vprint_number_table, i)))
1080 {
1081 PRINT_NUMBER_OBJECT (Vprint_number_table, index)
1082 = PRINT_NUMBER_OBJECT (Vprint_number_table, i);
1083 /* Reset the status field for the next print step. Now this
1084 field means whether the object has already been printed. */
1085 PRINT_NUMBER_STATUS (Vprint_number_table, index) = Qnil;
1086 index++;
1087 }
1088 print_number_index = index;
1089 }
1090
1091 print_object (obj, printcharfun, escapeflag);
1092}
1093
1094/* Construct Vprint_number_table according to the structure of OBJ.
1095 OBJ itself and all its elements will be added to Vprint_number_table
1096 recursively if it is a list, vector, compiled function, char-table,
1097 string (its text properties will be traced), or a symbol that has
1098 no obarray (this is for the print-gensym feature).
1099 The status fields of Vprint_number_table mean whether each object appears
1100 more than once in OBJ: Qnil at the first time, and Qt after that . */
1101static void
1102print_preprocess (obj)
1103 Lisp_Object obj;
1104{
1105 int i, size;
1106
1107 loop:
1108 if (STRINGP (obj) || CONSP (obj) || VECTORP (obj)
1109 || COMPILEDP (obj) || CHAR_TABLE_P (obj)
1110 || (! NILP (Vprint_gensym)
1111 && SYMBOLP (obj) && NILP (XSYMBOL (obj)->obarray)))
1112 {
1113 for (i = 0; i < print_number_index; i++)
1114 if (PRINT_NUMBER_OBJECT (Vprint_number_table, i) == obj)
1115 {
1116 /* OBJ appears more than once. Let's remember that. */
1117 PRINT_NUMBER_STATUS (Vprint_number_table, i) = Qt;
1118 return;
1119 }
1120
1121 /* OBJ is not yet recorded. Let's add to the table. */
1122 if (print_number_index == 0)
1123 {
1124 /* Initialize the table. */
1125 Vprint_number_table = Fmake_vector (make_number (40), Qnil);
1126 }
1127 else if (XVECTOR (Vprint_number_table)->size == print_number_index * 2)
1128 {
1129 /* Reallocate the table. */
1130 int i = print_number_index * 4;
1131 Lisp_Object old_table = Vprint_number_table;
1132 Vprint_number_table = Fmake_vector (make_number (i), Qnil);
1133 for (i = 0; i < print_number_index; i++)
1134 {
1135 PRINT_NUMBER_OBJECT (Vprint_number_table, i)
1136 = PRINT_NUMBER_OBJECT (old_table, i);
1137 PRINT_NUMBER_STATUS (Vprint_number_table, i)
1138 = PRINT_NUMBER_STATUS (old_table, i);
1139 }
1140 }
1141 PRINT_NUMBER_OBJECT (Vprint_number_table, print_number_index) = obj;
1142 /* If Vprint_continuous_numbering is non-nil and OBJ is a gensym,
1143 always print the gensym with a number. This is a special for
1144 the lisp function byte-compile-output-docform. */
1145 if (! NILP (Vprint_continuous_numbering) && SYMBOLP (obj)
1146 && NILP (XSYMBOL (obj)->obarray))
1147 PRINT_NUMBER_STATUS (Vprint_number_table, print_number_index) = Qt;
1148 print_number_index++;
1149
1150 switch (XGCTYPE (obj))
1151 {
1152 case Lisp_String:
1153#ifdef USE_TEXT_PROPERTIES
1154 /* A string may have text properties, which can be circular. */
1155 traverse_intervals (XSTRING (obj)->intervals, 0, 0,
1156 print_preprocess_string, Qnil);
1157#endif /* USE_TEXT_PROPERTIES */
1158 break;
1159
1160 case Lisp_Cons:
1161 print_preprocess (XCAR (obj));
1162 obj = XCDR (obj);
1163 goto loop;
1164
1165 case Lisp_Vectorlike:
1166 size = XVECTOR (obj)->size & PSEUDOVECTOR_SIZE_MASK;
1167 for (i = 0; i < size; i++)
1168 print_preprocess (XVECTOR (obj)->contents[i]);
1169 }
1170 }
1171}
1172
1173#ifdef USE_TEXT_PROPERTIES
1174static void
1175print_preprocess_string (interval, arg)
1176 INTERVAL interval;
1177 Lisp_Object arg;
1178{
1179 print_preprocess (interval->plist);
1180}
1181#endif /* USE_TEXT_PROPERTIES */
1182
1183static void
1184print_object (obj, printcharfun, escapeflag)
1185 Lisp_Object obj;
1186 register Lisp_Object printcharfun;
1187 int escapeflag;
1188{
1189 char buf[30];
1190
1191 QUIT;
1192
1193 /* Detect circularities and truncate them. */
1194 if (STRINGP (obj) || CONSP (obj) || VECTORP (obj)
1195 || COMPILEDP (obj) || CHAR_TABLE_P (obj)
1196 || (! NILP (Vprint_gensym)
1197 && SYMBOLP (obj) && NILP (XSYMBOL (obj)->obarray)))
1198 {
1199 if (NILP (Vprint_circle) && NILP (Vprint_gensym))
1200 {
1201 /* Simple but incomplete way. */
1202 int i;
1203 for (i = 0; i < print_depth; i++)
1204 if (EQ (obj, being_printed[i]))
1205 {
1206 sprintf (buf, "#%d", i);
1207 strout (buf, -1, -1, printcharfun, 0);
1208 return;
1209 }
1210 being_printed[print_depth] = obj;
1211 }
1212 else
1213 {
1214 /* With the print-circle feature. */
1215 int i;
1216 for (i = 0; i < print_number_index; i++)
1217 if (PRINT_NUMBER_OBJECT (Vprint_number_table, i) == obj)
1218 {
1219 if (NILP (PRINT_NUMBER_STATUS (Vprint_number_table, i)))
1220 {
1221 /* Add a prefix #n= if OBJ has not yet been printed;
1222 that is, its status field is nil. */
1223 sprintf (buf, "#%d=", i + 1);
1224 strout (buf, -1, -1, printcharfun, 0);
1225 /* OBJ is going to be printed. Set the status to t. */
1226 PRINT_NUMBER_STATUS (Vprint_number_table, i) = Qt;
1227 break;
1228 }
1229 else
1230 {
1231 /* Just print #n# if OBJ has already been printed. */
1232 sprintf (buf, "#%d#", i + 1);
1233 strout (buf, -1, -1, printcharfun, 0);
1234 return;
1235 }
1236 }
1237 }
1238 }
1239
1240 print_depth++;
1241
1242 if (print_depth > PRINT_CIRCLE)
1243 error ("Apparently circular structure being printed");
1244#ifdef MAX_PRINT_CHARS
1245 if (max_print && print_chars > max_print)
1246 {
1247 PRINTCHAR ('\n');
1248 print_chars = 0;
1249 }
1250#endif /* MAX_PRINT_CHARS */
1251
1252 switch (XGCTYPE (obj))
1253 {
1254 case Lisp_Int:
1255 if (sizeof (int) == sizeof (EMACS_INT))
1256 sprintf (buf, "%d", XINT (obj));
1257 else if (sizeof (long) == sizeof (EMACS_INT))
1258 sprintf (buf, "%ld", (long) XINT (obj));
1259 else
1260 abort ();
1261 strout (buf, -1, -1, printcharfun, 0);
1262 break;
1263
1264#ifdef LISP_FLOAT_TYPE
1265 case Lisp_Float:
1266 {
1267 char pigbuf[350]; /* see comments in float_to_string */
1268
1269 float_to_string (pigbuf, XFLOAT_DATA (obj));
1270 strout (pigbuf, -1, -1, printcharfun, 0);
1271 }
1272 break;
1273#endif
1274
1275 case Lisp_String:
1276 if (!escapeflag)
1277 print_string (obj, printcharfun);
1278 else
1279 {
1280 register int i, i_byte;
1281 struct gcpro gcpro1;
1282 unsigned char *str;
1283 int size_byte;
1284 /* 1 means we must ensure that the next character we output
1285 cannot be taken as part of a hex character escape. */
1286 int need_nonhex = 0;
1287
1288 GCPRO1 (obj);
1289
1290#ifdef USE_TEXT_PROPERTIES
1291 if (!NULL_INTERVAL_P (XSTRING (obj)->intervals))
1292 {
1293 PRINTCHAR ('#');
1294 PRINTCHAR ('(');
1295 }
1296#endif
1297
1298 PRINTCHAR ('\"');
1299 str = XSTRING (obj)->data;
1300 size_byte = STRING_BYTES (XSTRING (obj));
1301
1302 for (i = 0, i_byte = 0; i_byte < size_byte;)
1303 {
1304 /* Here, we must convert each multi-byte form to the
1305 corresponding character code before handing it to PRINTCHAR. */
1306 int len;
1307 int c;
1308
1309 if (STRING_MULTIBYTE (obj))
1310 {
1311 c = STRING_CHAR_AND_LENGTH (str + i_byte,
1312 size_byte - i_byte, len);
1313 if (CHAR_VALID_P (c, 0))
1314 i_byte += len;
1315 else
1316 c = str[i_byte++];
1317 }
1318 else
1319 c = str[i_byte++];
1320
1321 QUIT;
1322
1323 if (c == '\n' && print_escape_newlines)
1324 {
1325 PRINTCHAR ('\\');
1326 PRINTCHAR ('n');
1327 }
1328 else if (c == '\f' && print_escape_newlines)
1329 {
1330 PRINTCHAR ('\\');
1331 PRINTCHAR ('f');
1332 }
1333 else if (! SINGLE_BYTE_CHAR_P (c) && print_escape_multibyte)
1334 {
1335 /* When multibyte is disabled,
1336 print multibyte string chars using hex escapes. */
1337 unsigned char outbuf[50];
1338 sprintf (outbuf, "\\x%x", c);
1339 strout (outbuf, -1, -1, printcharfun, 0);
1340 need_nonhex = 1;
1341 }
1342 else if (SINGLE_BYTE_CHAR_P (c) && ! ASCII_BYTE_P (c)
1343 && print_escape_nonascii)
1344 {
1345 /* When printing in a multibyte buffer
1346 or when explicitly requested,
1347 print single-byte non-ASCII string chars
1348 using octal escapes. */
1349 unsigned char outbuf[5];
1350 sprintf (outbuf, "\\%03o", c);
1351 strout (outbuf, -1, -1, printcharfun, 0);
1352 }
1353 else
1354 {
1355 /* If we just had a hex escape, and this character
1356 could be taken as part of it,
1357 output `\ ' to prevent that. */
1358 if (need_nonhex)
1359 {
1360 need_nonhex = 0;
1361 if ((c >= 'a' && c <= 'f')
1362 || (c >= 'A' && c <= 'F')
1363 || (c >= '0' && c <= '9'))
1364 strout ("\\ ", -1, -1, printcharfun, 0);
1365 }
1366
1367 if (c == '\"' || c == '\\')
1368 PRINTCHAR ('\\');
1369 PRINTCHAR (c);
1370 }
1371 }
1372 PRINTCHAR ('\"');
1373
1374#ifdef USE_TEXT_PROPERTIES
1375 if (!NULL_INTERVAL_P (XSTRING (obj)->intervals))
1376 {
1377 traverse_intervals (XSTRING (obj)->intervals,
1378 0, 0, print_interval, printcharfun);
1379 PRINTCHAR (')');
1380 }
1381#endif
1382
1383 UNGCPRO;
1384 }
1385 break;
1386
1387 case Lisp_Symbol:
1388 {
1389 register int confusing;
1390 register unsigned char *p = XSYMBOL (obj)->name->data;
1391 register unsigned char *end = p + STRING_BYTES (XSYMBOL (obj)->name);
1392 register int c;
1393 int i, i_byte, size_byte;
1394 Lisp_Object name;
1395
1396 XSETSTRING (name, XSYMBOL (obj)->name);
1397
1398 if (p != end && (*p == '-' || *p == '+')) p++;
1399 if (p == end)
1400 confusing = 0;
1401 /* If symbol name begins with a digit, and ends with a digit,
1402 and contains nothing but digits and `e', it could be treated
1403 as a number. So set CONFUSING.
1404
1405 Symbols that contain periods could also be taken as numbers,
1406 but periods are always escaped, so we don't have to worry
1407 about them here. */
1408 else if (*p >= '0' && *p <= '9'
1409 && end[-1] >= '0' && end[-1] <= '9')
1410 {
1411 while (p != end && ((*p >= '0' && *p <= '9')
1412 /* Needed for \2e10. */
1413 || *p == 'e'))
1414 p++;
1415 confusing = (end == p);
1416 }
1417 else
1418 confusing = 0;
1419
1420 if (! NILP (Vprint_gensym) && NILP (XSYMBOL (obj)->obarray))
1421 {
1422 PRINTCHAR ('#');
1423 PRINTCHAR (':');
1424 }
1425
1426 size_byte = STRING_BYTES (XSTRING (name));
1427
1428 for (i = 0, i_byte = 0; i_byte < size_byte;)
1429 {
1430 /* Here, we must convert each multi-byte form to the
1431 corresponding character code before handing it to PRINTCHAR. */
1432
1433 if (STRING_MULTIBYTE (name))
1434 FETCH_STRING_CHAR_ADVANCE (c, name, i, i_byte);
1435 else
1436 c = XSTRING (name)->data[i_byte++];
1437
1438 QUIT;
1439
1440 if (escapeflag)
1441 {
1442 if (c == '\"' || c == '\\' || c == '\''
1443 || c == ';' || c == '#' || c == '(' || c == ')'
1444 || c == ',' || c =='.' || c == '`'
1445 || c == '[' || c == ']' || c == '?' || c <= 040
1446 || confusing)
1447 PRINTCHAR ('\\'), confusing = 0;
1448 }
1449 PRINTCHAR (c);
1450 }
1451 }
1452 break;
1453
1454 case Lisp_Cons:
1455 /* If deeper than spec'd depth, print placeholder. */
1456 if (INTEGERP (Vprint_level)
1457 && print_depth > XINT (Vprint_level))
1458 strout ("...", -1, -1, printcharfun, 0);
1459 else if (print_quoted && CONSP (XCDR (obj)) && NILP (XCDR (XCDR (obj)))
1460 && (EQ (XCAR (obj), Qquote)))
1461 {
1462 PRINTCHAR ('\'');
1463 print_object (XCAR (XCDR (obj)), printcharfun, escapeflag);
1464 }
1465 else if (print_quoted && CONSP (XCDR (obj)) && NILP (XCDR (XCDR (obj)))
1466 && (EQ (XCAR (obj), Qfunction)))
1467 {
1468 PRINTCHAR ('#');
1469 PRINTCHAR ('\'');
1470 print_object (XCAR (XCDR (obj)), printcharfun, escapeflag);
1471 }
1472 else if (print_quoted && CONSP (XCDR (obj)) && NILP (XCDR (XCDR (obj)))
1473 && ((EQ (XCAR (obj), Qbackquote)
1474 || EQ (XCAR (obj), Qcomma)
1475 || EQ (XCAR (obj), Qcomma_at)
1476 || EQ (XCAR (obj), Qcomma_dot))))
1477 {
1478 print_object (XCAR (obj), printcharfun, 0);
1479 print_object (XCAR (XCDR (obj)), printcharfun, escapeflag);
1480 }
1481 else
1482 {
1483 PRINTCHAR ('(');
1484 {
1485 register int i = 0;
1486 register int print_length = 0;
1487 Lisp_Object halftail = obj;
1488
1489 if (INTEGERP (Vprint_length))
1490 print_length = XINT (Vprint_length);
1491 while (CONSP (obj))
1492 {
1493 /* Detect circular list. */
1494 if (NILP (Vprint_circle))
1495 {
1496 /* Simple but imcomplete way. */
1497 if (i != 0 && EQ (obj, halftail))
1498 {
1499 sprintf (buf, " . #%d", i / 2);
1500 strout (buf, -1, -1, printcharfun, 0);
1501 goto end_of_list;
1502 }
1503 }
1504 else
1505 {
1506 /* With the print-circle feature. */
1507 if (i != 0)
1508 {
1509 int i;
1510 for (i = 0; i < print_number_index; i++)
1511 if (PRINT_NUMBER_OBJECT (Vprint_number_table, i) == obj)
1512 {
1513 if (NILP (PRINT_NUMBER_STATUS (Vprint_number_table, i)))
1514 {
1515 strout (" . ", 3, 3, printcharfun, 0);
1516 print_object (obj, printcharfun, escapeflag);
1517 }
1518 else
1519 {
1520 sprintf (buf, " . #%d#", i + 1);
1521 strout (buf, -1, -1, printcharfun, 0);
1522 }
1523 goto end_of_list;
1524 }
1525 }
1526 }
1527 if (i++)
1528 PRINTCHAR (' ');
1529 if (print_length && i > print_length)
1530 {
1531 strout ("...", 3, 3, printcharfun, 0);
1532 goto end_of_list;
1533 }
1534 print_object (XCAR (obj), printcharfun, escapeflag);
1535 obj = XCDR (obj);
1536 if (!(i & 1))
1537 halftail = XCDR (halftail);
1538 }
1539 }
1540 if (!NILP (obj))
1541 {
1542 strout (" . ", 3, 3, printcharfun, 0);
1543 print_object (obj, printcharfun, escapeflag);
1544 }
1545 end_of_list:
1546 PRINTCHAR (')');
1547 }
1548 break;
1549
1550 case Lisp_Vectorlike:
1551 if (PROCESSP (obj))
1552 {
1553 if (escapeflag)
1554 {
1555 strout ("#<process ", -1, -1, printcharfun, 0);
1556 print_string (XPROCESS (obj)->name, printcharfun);
1557 PRINTCHAR ('>');
1558 }
1559 else
1560 print_string (XPROCESS (obj)->name, printcharfun);
1561 }
1562 else if (BOOL_VECTOR_P (obj))
1563 {
1564 register int i;
1565 register unsigned char c;
1566 struct gcpro gcpro1;
1567 int size_in_chars
1568 = (XBOOL_VECTOR (obj)->size + BITS_PER_CHAR - 1) / BITS_PER_CHAR;
1569
1570 GCPRO1 (obj);
1571
1572 PRINTCHAR ('#');
1573 PRINTCHAR ('&');
1574 sprintf (buf, "%d", XBOOL_VECTOR (obj)->size);
1575 strout (buf, -1, -1, printcharfun, 0);
1576 PRINTCHAR ('\"');
1577
1578 /* Don't print more characters than the specified maximum. */
1579 if (INTEGERP (Vprint_length)
1580 && XINT (Vprint_length) < size_in_chars)
1581 size_in_chars = XINT (Vprint_length);
1582
1583 for (i = 0; i < size_in_chars; i++)
1584 {
1585 QUIT;
1586 c = XBOOL_VECTOR (obj)->data[i];
1587 if (c == '\n' && print_escape_newlines)
1588 {
1589 PRINTCHAR ('\\');
1590 PRINTCHAR ('n');
1591 }
1592 else if (c == '\f' && print_escape_newlines)
1593 {
1594 PRINTCHAR ('\\');
1595 PRINTCHAR ('f');
1596 }
1597 else
1598 {
1599 if (c == '\"' || c == '\\')
1600 PRINTCHAR ('\\');
1601 PRINTCHAR (c);
1602 }
1603 }
1604 PRINTCHAR ('\"');
1605
1606 UNGCPRO;
1607 }
1608 else if (SUBRP (obj))
1609 {
1610 strout ("#<subr ", -1, -1, printcharfun, 0);
1611 strout (XSUBR (obj)->symbol_name, -1, -1, printcharfun, 0);
1612 PRINTCHAR ('>');
1613 }
1614 else if (WINDOWP (obj))
1615 {
1616 strout ("#<window ", -1, -1, printcharfun, 0);
1617 sprintf (buf, "%d", XFASTINT (XWINDOW (obj)->sequence_number));
1618 strout (buf, -1, -1, printcharfun, 0);
1619 if (!NILP (XWINDOW (obj)->buffer))
1620 {
1621 strout (" on ", -1, -1, printcharfun, 0);
1622 print_string (XBUFFER (XWINDOW (obj)->buffer)->name, printcharfun);
1623 }
1624 PRINTCHAR ('>');
1625 }
1626 else if (HASH_TABLE_P (obj))
1627 {
1628 struct Lisp_Hash_Table *h = XHASH_TABLE (obj);
1629 strout ("#<hash-table", -1, -1, printcharfun, 0);
1630 if (SYMBOLP (h->test))
1631 {
1632 PRINTCHAR (' ');
1633 PRINTCHAR ('\'');
1634 strout (XSYMBOL (h->test)->name->data, -1, -1, printcharfun, 0);
1635 PRINTCHAR (' ');
1636 strout (XSYMBOL (h->weak)->name->data, -1, -1, printcharfun, 0);
1637 PRINTCHAR (' ');
1638 sprintf (buf, "%d/%d", XFASTINT (h->count),
1639 XVECTOR (h->next)->size);
1640 strout (buf, -1, -1, printcharfun, 0);
1641 }
1642 sprintf (buf, " 0x%lx", (unsigned long) h);
1643 strout (buf, -1, -1, printcharfun, 0);
1644 PRINTCHAR ('>');
1645 }
1646 else if (BUFFERP (obj))
1647 {
1648 if (NILP (XBUFFER (obj)->name))
1649 strout ("#<killed buffer>", -1, -1, printcharfun, 0);
1650 else if (escapeflag)
1651 {
1652 strout ("#<buffer ", -1, -1, printcharfun, 0);
1653 print_string (XBUFFER (obj)->name, printcharfun);
1654 PRINTCHAR ('>');
1655 }
1656 else
1657 print_string (XBUFFER (obj)->name, printcharfun);
1658 }
1659 else if (WINDOW_CONFIGURATIONP (obj))
1660 {
1661 strout ("#<window-configuration>", -1, -1, printcharfun, 0);
1662 }
1663 else if (FRAMEP (obj))
1664 {
1665 strout ((FRAME_LIVE_P (XFRAME (obj))
1666 ? "#<frame " : "#<dead frame "),
1667 -1, -1, printcharfun, 0);
1668 print_string (XFRAME (obj)->name, printcharfun);
1669 sprintf (buf, " 0x%lx\\ ", (unsigned long) (XFRAME (obj)));
1670 strout (buf, -1, -1, printcharfun, 0);
1671 PRINTCHAR ('>');
1672 }
1673 else
1674 {
1675 int size = XVECTOR (obj)->size;
1676 if (COMPILEDP (obj))
1677 {
1678 PRINTCHAR ('#');
1679 size &= PSEUDOVECTOR_SIZE_MASK;
1680 }
1681 if (CHAR_TABLE_P (obj))
1682 {
1683 /* We print a char-table as if it were a vector,
1684 lumping the parent and default slots in with the
1685 character slots. But we add #^ as a prefix. */
1686 PRINTCHAR ('#');
1687 PRINTCHAR ('^');
1688 if (SUB_CHAR_TABLE_P (obj))
1689 PRINTCHAR ('^');
1690 size &= PSEUDOVECTOR_SIZE_MASK;
1691 }
1692 if (size & PSEUDOVECTOR_FLAG)
1693 goto badtype;
1694
1695 PRINTCHAR ('[');
1696 {
1697 register int i;
1698 register Lisp_Object tem;
1699
1700 /* Don't print more elements than the specified maximum. */
1701 if (INTEGERP (Vprint_length)
1702 && XINT (Vprint_length) < size)
1703 size = XINT (Vprint_length);
1704
1705 for (i = 0; i < size; i++)
1706 {
1707 if (i) PRINTCHAR (' ');
1708 tem = XVECTOR (obj)->contents[i];
1709 print_object (tem, printcharfun, escapeflag);
1710 }
1711 }
1712 PRINTCHAR (']');
1713 }
1714 break;
1715
1716 case Lisp_Misc:
1717 switch (XMISCTYPE (obj))
1718 {
1719 case Lisp_Misc_Marker:
1720 strout ("#<marker ", -1, -1, printcharfun, 0);
1721 /* Do you think this is necessary? */
1722 if (XMARKER (obj)->insertion_type != 0)
1723 strout ("(before-insertion) ", -1, -1, printcharfun, 0);
1724 if (!(XMARKER (obj)->buffer))
1725 strout ("in no buffer", -1, -1, printcharfun, 0);
1726 else
1727 {
1728 sprintf (buf, "at %d", marker_position (obj));
1729 strout (buf, -1, -1, printcharfun, 0);
1730 strout (" in ", -1, -1, printcharfun, 0);
1731 print_string (XMARKER (obj)->buffer->name, printcharfun);
1732 }
1733 PRINTCHAR ('>');
1734 break;
1735
1736 case Lisp_Misc_Overlay:
1737 strout ("#<overlay ", -1, -1, printcharfun, 0);
1738 if (!(XMARKER (OVERLAY_START (obj))->buffer))
1739 strout ("in no buffer", -1, -1, printcharfun, 0);
1740 else
1741 {
1742 sprintf (buf, "from %d to %d in ",
1743 marker_position (OVERLAY_START (obj)),
1744 marker_position (OVERLAY_END (obj)));
1745 strout (buf, -1, -1, printcharfun, 0);
1746 print_string (XMARKER (OVERLAY_START (obj))->buffer->name,
1747 printcharfun);
1748 }
1749 PRINTCHAR ('>');
1750 break;
1751
1752 /* Remaining cases shouldn't happen in normal usage, but let's print
1753 them anyway for the benefit of the debugger. */
1754 case Lisp_Misc_Free:
1755 strout ("#<misc free cell>", -1, -1, printcharfun, 0);
1756 break;
1757
1758 case Lisp_Misc_Intfwd:
1759 sprintf (buf, "#<intfwd to %d>", *XINTFWD (obj)->intvar);
1760 strout (buf, -1, -1, printcharfun, 0);
1761 break;
1762
1763 case Lisp_Misc_Boolfwd:
1764 sprintf (buf, "#<boolfwd to %s>",
1765 (*XBOOLFWD (obj)->boolvar ? "t" : "nil"));
1766 strout (buf, -1, -1, printcharfun, 0);
1767 break;
1768
1769 case Lisp_Misc_Objfwd:
1770 strout ("#<objfwd to ", -1, -1, printcharfun, 0);
1771 print_object (*XOBJFWD (obj)->objvar, printcharfun, escapeflag);
1772 PRINTCHAR ('>');
1773 break;
1774
1775 case Lisp_Misc_Buffer_Objfwd:
1776 strout ("#<buffer_objfwd to ", -1, -1, printcharfun, 0);
1777 print_object (*(Lisp_Object *)((char *)current_buffer
1778 + XBUFFER_OBJFWD (obj)->offset),
1779 printcharfun, escapeflag);
1780 PRINTCHAR ('>');
1781 break;
1782
1783 case Lisp_Misc_Kboard_Objfwd:
1784 strout ("#<kboard_objfwd to ", -1, -1, printcharfun, 0);
1785 print_object (*(Lisp_Object *)((char *) current_kboard
1786 + XKBOARD_OBJFWD (obj)->offset),
1787 printcharfun, escapeflag);
1788 PRINTCHAR ('>');
1789 break;
1790
1791 case Lisp_Misc_Buffer_Local_Value:
1792 strout ("#<buffer_local_value ", -1, -1, printcharfun, 0);
1793 goto do_buffer_local;
1794 case Lisp_Misc_Some_Buffer_Local_Value:
1795 strout ("#<some_buffer_local_value ", -1, -1, printcharfun, 0);
1796 do_buffer_local:
1797 strout ("[realvalue] ", -1, -1, printcharfun, 0);
1798 print_object (XBUFFER_LOCAL_VALUE (obj)->realvalue,
1799 printcharfun, escapeflag);
1800 if (XBUFFER_LOCAL_VALUE (obj)->found_for_buffer)
1801 strout ("[local in buffer] ", -1, -1, printcharfun, 0);
1802 else
1803 strout ("[buffer] ", -1, -1, printcharfun, 0);
1804 print_object (XBUFFER_LOCAL_VALUE (obj)->buffer,
1805 printcharfun, escapeflag);
1806 if (XBUFFER_LOCAL_VALUE (obj)->check_frame)
1807 {
1808 if (XBUFFER_LOCAL_VALUE (obj)->found_for_frame)
1809 strout ("[local in frame] ", -1, -1, printcharfun, 0);
1810 else
1811 strout ("[frame] ", -1, -1, printcharfun, 0);
1812 print_object (XBUFFER_LOCAL_VALUE (obj)->frame,
1813 printcharfun, escapeflag);
1814 }
1815 strout ("[alist-elt] ", -1, -1, printcharfun, 0);
1816 print_object (XCAR (XBUFFER_LOCAL_VALUE (obj)->cdr),
1817 printcharfun, escapeflag);
1818 strout ("[default-value] ", -1, -1, printcharfun, 0);
1819 print_object (XCDR (XBUFFER_LOCAL_VALUE (obj)->cdr),
1820 printcharfun, escapeflag);
1821 PRINTCHAR ('>');
1822 break;
1823
1824 default:
1825 goto badtype;
1826 }
1827 break;
1828
1829 default:
1830 badtype:
1831 {
1832 /* We're in trouble if this happens!
1833 Probably should just abort () */
1834 strout ("#<EMACS BUG: INVALID DATATYPE ", -1, -1, printcharfun, 0);
1835 if (MISCP (obj))
1836 sprintf (buf, "(MISC 0x%04x)", (int) XMISCTYPE (obj));
1837 else if (VECTORLIKEP (obj))
1838 sprintf (buf, "(PVEC 0x%08x)", (int) XVECTOR (obj)->size);
1839 else
1840 sprintf (buf, "(0x%02x)", (int) XTYPE (obj));
1841 strout (buf, -1, -1, printcharfun, 0);
1842 strout (" Save your buffers immediately and please report this bug>",
1843 -1, -1, printcharfun, 0);
1844 }
1845 }
1846
1847 print_depth--;
1848}
1849\f
1850#ifdef USE_TEXT_PROPERTIES
1851
1852/* Print a description of INTERVAL using PRINTCHARFUN.
1853 This is part of printing a string that has text properties. */
1854
1855void
1856print_interval (interval, printcharfun)
1857 INTERVAL interval;
1858 Lisp_Object printcharfun;
1859{
1860 PRINTCHAR (' ');
1861 print_object (make_number (interval->position), printcharfun, 1);
1862 PRINTCHAR (' ');
1863 print_object (make_number (interval->position + LENGTH (interval)),
1864 printcharfun, 1);
1865 PRINTCHAR (' ');
1866 print_object (interval->plist, printcharfun, 1);
1867}
1868
1869#endif /* USE_TEXT_PROPERTIES */
1870\f
1871void
1872syms_of_print ()
1873{
1874 Qtemp_buffer_setup_hook = intern ("temp-buffer-setup-hook");
1875 staticpro (&Qtemp_buffer_setup_hook);
1876
1877 DEFVAR_LISP ("standard-output", &Vstandard_output,
1878 "Output stream `print' uses by default for outputting a character.\n\
1879This may be any function of one argument.\n\
1880It may also be a buffer (output is inserted before point)\n\
1881or a marker (output is inserted and the marker is advanced)\n\
1882or the symbol t (output appears in the echo area).");
1883 Vstandard_output = Qt;
1884 Qstandard_output = intern ("standard-output");
1885 staticpro (&Qstandard_output);
1886
1887#ifdef LISP_FLOAT_TYPE
1888 DEFVAR_LISP ("float-output-format", &Vfloat_output_format,
1889 "The format descriptor string used to print floats.\n\
1890This is a %-spec like those accepted by `printf' in C,\n\
1891but with some restrictions. It must start with the two characters `%.'.\n\
1892After that comes an integer precision specification,\n\
1893and then a letter which controls the format.\n\
1894The letters allowed are `e', `f' and `g'.\n\
1895Use `e' for exponential notation \"DIG.DIGITSeEXPT\"\n\
1896Use `f' for decimal point notation \"DIGITS.DIGITS\".\n\
1897Use `g' to choose the shorter of those two formats for the number at hand.\n\
1898The precision in any of these cases is the number of digits following\n\
1899the decimal point. With `f', a precision of 0 means to omit the\n\
1900decimal point. 0 is not allowed with `e' or `g'.\n\n\
1901A value of nil means to use the shortest notation\n\
1902that represents the number without losing information.");
1903 Vfloat_output_format = Qnil;
1904 Qfloat_output_format = intern ("float-output-format");
1905 staticpro (&Qfloat_output_format);
1906#endif /* LISP_FLOAT_TYPE */
1907
1908 DEFVAR_LISP ("print-length", &Vprint_length,
1909 "Maximum length of list to print before abbreviating.\n\
1910A value of nil means no limit.");
1911 Vprint_length = Qnil;
1912
1913 DEFVAR_LISP ("print-level", &Vprint_level,
1914 "Maximum depth of list nesting to print before abbreviating.\n\
1915A value of nil means no limit.");
1916 Vprint_level = Qnil;
1917
1918 DEFVAR_BOOL ("print-escape-newlines", &print_escape_newlines,
1919 "Non-nil means print newlines in strings as backslash-n.\n\
1920Also print formfeeds as backslash-f.");
1921 print_escape_newlines = 0;
1922
1923 DEFVAR_BOOL ("print-escape-nonascii", &print_escape_nonascii,
1924 "Non-nil means print unibyte non-ASCII chars in strings as \\OOO.\n\
1925\(OOO is the octal representation of the character code.)\n\
1926Only single-byte characters are affected, and only in `prin1'.");
1927 print_escape_nonascii = 0;
1928
1929 DEFVAR_BOOL ("print-escape-multibyte", &print_escape_multibyte,
1930 "Non-nil means print multibyte characters in strings as \\xXXXX.\n\
1931\(XXX is the hex representation of the character code.)\n\
1932This affects only `prin1'.");
1933 print_escape_multibyte = 0;
1934
1935 DEFVAR_BOOL ("print-quoted", &print_quoted,
1936 "Non-nil means print quoted forms with reader syntax.\n\
1937I.e., (quote foo) prints as 'foo, (function foo) as #'foo, and, backquoted\n\
1938forms print in the new syntax.");
1939 print_quoted = 0;
1940
1941 DEFVAR_LISP ("print-gensym", &Vprint_gensym,
1942 "Non-nil means print uninterned symbols so they will read as uninterned.\n\
1943I.e., the value of (make-symbol \"foobar\") prints as #:foobar.\n\
1944When the uninterned symbol appears within a recursive data structure\n\
1945and the symbol appears more than once, in addition use the #N# and #N=\n\
1946constructs as needed, so that multiple references to the same symbol are\n\
1947shared once again when the text is read back.");
1948 Vprint_gensym = Qnil;
1949
1950 DEFVAR_LISP ("print-circle", &Vprint_circle,
1951 "*Non-nil means print recursive structures using #N= and #N# syntax.\n\
1952If nil, printing proceeds recursively and may lead to\n\
1953`max-lisp-eval-depth' being exceeded or an error may occur:\n\
1954\"Apparently circular structure being printed.\" Also see\n\
1955`print-length' and `print-level'.\n\
1956If non-nil, shared substructures anywhere in the structure are printed\n\
1957with `#N=' before the first occurrence (in the order of the print\n\
1958representation) and `#N#' in place of each subsequent occurrence,\n\
1959where N is a positive decimal integer.");
1960 Vprint_circle = Qnil;
1961
1962 DEFVAR_LISP ("print-continuous-numbering", &Vprint_continuous_numbering,
1963 "*Non-nil means keep numbering between several print functions.\n\
1964See `print-gensym' nad `print-circle'. See also `print-number-table'.");
1965 Vprint_continuous_numbering = Qnil;
1966
1967 DEFVAR_LISP ("print-number-table", &Vprint_number_table,
1968 "A vector keeping the information of the current printed object.\n\
1969This variable shouldn't be modified in Lisp level, but should be binded\n\
1970with nil using let at the same position with `print-continuous-numbering',\n\
1971so that the value of this variable can be freed after printing.");
1972 Vprint_number_table = Qnil;
1973
1974 /* prin1_to_string_buffer initialized in init_buffer_once in buffer.c */
1975 staticpro (&Vprin1_to_string_buffer);
1976
1977 defsubr (&Sprin1);
1978 defsubr (&Sprin1_to_string);
1979 defsubr (&Serror_message_string);
1980 defsubr (&Sprinc);
1981 defsubr (&Sprint);
1982 defsubr (&Sterpri);
1983 defsubr (&Swrite_char);
1984 defsubr (&Sexternal_debugging_output);
1985
1986 Qexternal_debugging_output = intern ("external-debugging-output");
1987 staticpro (&Qexternal_debugging_output);
1988
1989 Qprint_escape_newlines = intern ("print-escape-newlines");
1990 staticpro (&Qprint_escape_newlines);
1991
1992 Qprint_escape_multibyte = intern ("print-escape-multibyte");
1993 staticpro (&Qprint_escape_multibyte);
1994
1995 Qprint_escape_nonascii = intern ("print-escape-nonascii");
1996 staticpro (&Qprint_escape_nonascii);
1997
1998 defsubr (&Swith_output_to_temp_buffer);
1999}