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