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