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