Update copyright year.
[bpt/emacs.git] / src / print.c
CommitLineData
38010d50 1/* Lisp object printing and output streams.
31c8f881 2 Copyright (C) 1985, 86, 88, 93, 94, 95, 97, 98 Free Software Foundation, Inc.
38010d50
JB
3
4This file is part of GNU Emacs.
5
6GNU Emacs is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
4746118a 8the Free Software Foundation; either version 2, or (at your option)
38010d50
JB
9any later version.
10
11GNU Emacs is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU Emacs; see the file COPYING. If not, write to
3b7ad313
EN
18the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19Boston, MA 02111-1307, USA. */
38010d50
JB
20
21
18160b98 22#include <config.h>
38010d50 23#include <stdio.h>
38010d50
JB
24#include "lisp.h"
25
26#ifndef standalone
27#include "buffer.h"
087e3c46 28#include "charset.h"
0137dbf7 29#include "frame.h"
38010d50
JB
30#include "window.h"
31#include "process.h"
32#include "dispextern.h"
33#include "termchar.h"
077d751f 34#include "keyboard.h"
38010d50
JB
35#endif /* not standalone */
36
7651e1f5
RS
37#ifdef USE_TEXT_PROPERTIES
38#include "intervals.h"
39#endif
40
38010d50
JB
41Lisp_Object Vstandard_output, Qstandard_output;
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
130Lisp_Object Qprint_escape_newlines;
131
2f100b5c
EN
132/* Nonzero means print (quote foo) forms as 'foo, etc. */
133
134int print_quoted;
135
e0f69431
RS
136/* Non-nil means print #: before uninterned symbols.
137 Neither t nor nil means so that and don't clear Vprint_gensym_alist
138 on entry to and exit from print functions. */
081e0581 139
e0f69431 140Lisp_Object Vprint_gensym;
081e0581
EN
141
142/* Association list of certain objects that are `eq' in the form being
143 printed and which should be `eq' when read back in, using the #n=object
144 and #n# reader forms. Each element has the form (object . n). */
145
e0f69431 146Lisp_Object Vprint_gensym_alist;
2f100b5c 147
5259c737 148/* Nonzero means print newline to stdout before next minibuffer message.
38010d50
JB
149 Defined in xdisp.c */
150
151extern int noninteractive_need_newline;
5259c737 152
aec2b95b
RS
153extern int minibuffer_auto_raise;
154
38010d50
JB
155#ifdef MAX_PRINT_CHARS
156static int print_chars;
157static int max_print;
158#endif /* MAX_PRINT_CHARS */
7651e1f5
RS
159
160void print_interval ();
38010d50
JB
161\f
162#if 0
163/* Convert between chars and GLYPHs */
164
165int
166glyphlen (glyphs)
167 register GLYPH *glyphs;
168{
169 register int i = 0;
170
171 while (glyphs[i])
172 i++;
173 return i;
174}
175
176void
177str_to_glyph_cpy (str, glyphs)
178 char *str;
179 GLYPH *glyphs;
180{
181 register GLYPH *gp = glyphs;
182 register char *cp = str;
183
184 while (*cp)
185 *gp++ = *cp++;
186}
187
188void
189str_to_glyph_ncpy (str, glyphs, n)
190 char *str;
191 GLYPH *glyphs;
192 register int n;
193{
194 register GLYPH *gp = glyphs;
195 register char *cp = str;
196
197 while (n-- > 0)
198 *gp++ = *cp++;
199}
200
201void
202glyph_to_str_cpy (glyphs, str)
203 GLYPH *glyphs;
204 char *str;
205{
206 register GLYPH *gp = glyphs;
207 register char *cp = str;
208
209 while (*gp)
210 *str++ = *gp++ & 0377;
211}
212#endif
213\f
eb8c3be9 214/* Low level output routines for characters and strings */
38010d50
JB
215
216/* Lisp functions to do output using a stream
081e0581
EN
217 must have the stream in a variable called printcharfun
218 and must start with PRINTPREPARE, end with PRINTFINISH,
219 and use PRINTDECLARE to declare common variables.
220 Use PRINTCHAR to output one character,
221 or call strout to output a block of characters.
38010d50
JB
222*/
223
081e0581
EN
224#define PRINTDECLARE \
225 struct buffer *old = current_buffer; \
226 int old_point = -1, start_point; \
6ddd6eee 227 int old_point_byte, start_point_byte; \
08e8d297
RS
228 int specpdl_count = specpdl_ptr - specpdl; \
229 int free_print_buffer = 0; \
081e0581
EN
230 Lisp_Object original
231
cdaa87fd
RS
232#define PRINTPREPARE \
233 original = printcharfun; \
234 if (NILP (printcharfun)) printcharfun = Qt; \
d4ae1f7e 235 if (BUFFERP (printcharfun)) \
08e8d297
RS
236 { \
237 if (XBUFFER (printcharfun) != current_buffer) \
cdaa87fd 238 Fset_buffer (printcharfun); \
08e8d297
RS
239 printcharfun = Qnil; \
240 } \
d4ae1f7e 241 if (MARKERP (printcharfun)) \
08e8d297
RS
242 { \
243 if (!(XMARKER (original)->buffer)) \
cdaa87fd
RS
244 error ("Marker does not point anywhere"); \
245 if (XMARKER (original)->buffer != current_buffer) \
246 set_buffer_internal (XMARKER (original)->buffer); \
6ec8bbd2 247 old_point = PT; \
6ddd6eee
RS
248 old_point_byte = PT_BYTE; \
249 SET_PT_BOTH (marker_position (printcharfun), \
250 marker_byte_position (printcharfun)); \
6ec8bbd2 251 start_point = PT; \
6ddd6eee 252 start_point_byte = PT_BYTE; \
08e8d297
RS
253 printcharfun = Qnil; \
254 } \
6fec5601
RS
255 if (NILP (printcharfun)) \
256 { \
dc2a0b79 257 Lisp_Object string; \
08e8d297 258 if (print_buffer != 0) \
dc2a0b79
RS
259 { \
260 string = make_multibyte_string (print_buffer, \
261 print_buffer_pos, \
262 print_buffer_pos_byte); \
263 record_unwind_protect (print_unwind, string); \
264 } \
08e8d297
RS
265 else \
266 { \
267 print_buffer_size = 1000; \
268 print_buffer = (char *) xmalloc (print_buffer_size); \
b3da2c73 269 free_print_buffer = 1; \
08e8d297 270 } \
6fec5601 271 print_buffer_pos = 0; \
dc2a0b79 272 print_buffer_pos_byte = 0; \
6fec5601 273 } \
e0f69431
RS
274 if (!CONSP (Vprint_gensym)) \
275 Vprint_gensym_alist = Qnil
38010d50 276
cdaa87fd 277#define PRINTFINISH \
6fec5601 278 if (NILP (printcharfun)) \
dc2a0b79
RS
279 insert_1_both (print_buffer, print_buffer_pos, \
280 print_buffer_pos_byte, 0, 1, 0); \
08e8d297 281 if (free_print_buffer) \
09eddb56 282 { \
99351a0d 283 xfree (print_buffer); \
09eddb56
RS
284 print_buffer = 0; \
285 } \
08e8d297 286 unbind_to (specpdl_count, Qnil); \
d4ae1f7e 287 if (MARKERP (original)) \
6ddd6eee 288 set_marker_both (original, Qnil, PT, PT_BYTE); \
cdaa87fd 289 if (old_point >= 0) \
6ddd6eee
RS
290 SET_PT_BOTH (old_point + (old_point >= start_point \
291 ? PT - start_point : 0), \
292 old_point_byte + (old_point_byte >= start_point_byte \
293 ? PT_BYTE - start_point_byte : 0)); \
cdaa87fd 294 if (old != current_buffer) \
081e0581 295 set_buffer_internal (old); \
e0f69431
RS
296 if (!CONSP (Vprint_gensym)) \
297 Vprint_gensym_alist = Qnil
38010d50
JB
298
299#define PRINTCHAR(ch) printchar (ch, printcharfun)
300
09eddb56
RS
301/* Nonzero if there is no room to print any more characters
302 so print might as well return right away. */
303
304#define PRINTFULLP() \
305 (EQ (printcharfun, Qt) && !noninteractive \
306 && printbufidx >= FRAME_WIDTH (XFRAME (WINDOW_FRAME (XWINDOW (minibuf_window)))))
307
08e8d297
RS
308/* This is used to restore the saved contents of print_buffer
309 when there is a recursive call to print. */
310static Lisp_Object
311print_unwind (saved_text)
312 Lisp_Object saved_text;
313{
314 bcopy (XSTRING (saved_text)->data, print_buffer, XSTRING (saved_text)->size);
315}
316
09eddb56 317/* Index of first unused element of FRAME_MESSAGE_BUF (mini_frame). */
38010d50
JB
318static int printbufidx;
319
320static void
321printchar (ch, fun)
087e3c46 322 unsigned int ch;
38010d50
JB
323 Lisp_Object fun;
324{
325 Lisp_Object ch1;
326
327#ifdef MAX_PRINT_CHARS
328 if (max_print)
329 print_chars++;
330#endif /* MAX_PRINT_CHARS */
331#ifndef standalone
332 if (EQ (fun, Qnil))
333 {
087e3c46 334 int len;
dc22f25e 335 unsigned char work[4], *str;
087e3c46 336
38010d50 337 QUIT;
087e3c46 338 len = CHAR_STRING (ch, work, str);
dc2a0b79 339 if (print_buffer_pos_byte + len >= print_buffer_size)
6fec5601
RS
340 print_buffer = (char *) xrealloc (print_buffer,
341 print_buffer_size *= 2);
dc2a0b79
RS
342 bcopy (str, print_buffer + print_buffer_pos_byte, len);
343 print_buffer_pos += 1;
344 print_buffer_pos_byte += len;
38010d50
JB
345 return;
346 }
347
348 if (EQ (fun, Qt))
349 {
c217b048 350 FRAME_PTR mini_frame
b8b1b8fd 351 = XFRAME (WINDOW_FRAME (XWINDOW (minibuf_window)));
087e3c46
KH
352 unsigned char work[4], *str;
353 int len = CHAR_STRING (ch, work, str);
b8b1b8fd 354
09eddb56
RS
355 QUIT;
356
38010d50
JB
357 if (noninteractive)
358 {
087e3c46
KH
359 while (len--)
360 putchar (*str), str++;
38010d50
JB
361 noninteractive_need_newline = 1;
362 return;
363 }
364
b8b1b8fd 365 if (echo_area_glyphs != FRAME_MESSAGE_BUF (mini_frame)
38010d50
JB
366 || !message_buf_print)
367 {
aee72e4f 368 message_log_maybe_newline ();
b8b1b8fd 369 echo_area_glyphs = FRAME_MESSAGE_BUF (mini_frame);
38010d50 370 printbufidx = 0;
708d172a 371 echo_area_glyphs_length = 0;
38010d50 372 message_buf_print = 1;
aec2b95b
RS
373
374 if (minibuffer_auto_raise)
375 {
376 Lisp_Object mini_window;
377
378 /* Get the frame containing the minibuffer
379 that the selected frame is using. */
380 mini_window = FRAME_MINIBUF_WINDOW (selected_frame);
381
382 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
383 }
38010d50
JB
384 }
385
dc2a0b79 386 message_dolog (str, len, 0, len > 1);
087e3c46
KH
387 if (printbufidx < FRAME_MESSAGE_BUF_SIZE (mini_frame) - len)
388 bcopy (str, &FRAME_MESSAGE_BUF (mini_frame)[printbufidx], len),
389 printbufidx += len;
b8b1b8fd 390 FRAME_MESSAGE_BUF (mini_frame)[printbufidx] = 0;
708d172a 391 echo_area_glyphs_length = printbufidx;
38010d50
JB
392
393 return;
394 }
395#endif /* not standalone */
396
b5d25c37 397 XSETFASTINT (ch1, ch);
38010d50
JB
398 call1 (fun, ch1);
399}
400
401static void
dc2a0b79 402strout (ptr, size, size_byte, printcharfun, multibyte)
38010d50 403 char *ptr;
dc2a0b79 404 int size, size_byte;
38010d50 405 Lisp_Object printcharfun;
dc2a0b79 406 int multibyte;
38010d50
JB
407{
408 int i = 0;
409
087e3c46 410 if (size < 0)
dc2a0b79 411 size_byte = size = strlen (ptr);
087e3c46 412
38010d50
JB
413 if (EQ (printcharfun, Qnil))
414 {
dc2a0b79 415 if (print_buffer_pos_byte + size_byte > print_buffer_size)
6fec5601 416 {
dc2a0b79 417 print_buffer_size = print_buffer_size * 2 + size_byte;
6fec5601
RS
418 print_buffer = (char *) xrealloc (print_buffer,
419 print_buffer_size);
420 }
dc2a0b79 421 bcopy (ptr, print_buffer + print_buffer_pos_byte, size_byte);
6fec5601 422 print_buffer_pos += size;
dc2a0b79 423 print_buffer_pos_byte += size_byte;
6fec5601 424
38010d50
JB
425#ifdef MAX_PRINT_CHARS
426 if (max_print)
6fec5601 427 print_chars += size;
38010d50
JB
428#endif /* MAX_PRINT_CHARS */
429 return;
430 }
431 if (EQ (printcharfun, Qt))
432 {
c217b048 433 FRAME_PTR mini_frame
b8b1b8fd
RS
434 = XFRAME (WINDOW_FRAME (XWINDOW (minibuf_window)));
435
09eddb56
RS
436 QUIT;
437
38010d50
JB
438#ifdef MAX_PRINT_CHARS
439 if (max_print)
087e3c46 440 print_chars += size;
38010d50
JB
441#endif /* MAX_PRINT_CHARS */
442
443 if (noninteractive)
444 {
dc2a0b79 445 fwrite (ptr, 1, size_byte, stdout);
38010d50
JB
446 noninteractive_need_newline = 1;
447 return;
448 }
449
b8b1b8fd 450 if (echo_area_glyphs != FRAME_MESSAGE_BUF (mini_frame)
38010d50
JB
451 || !message_buf_print)
452 {
aee72e4f 453 message_log_maybe_newline ();
b8b1b8fd 454 echo_area_glyphs = FRAME_MESSAGE_BUF (mini_frame);
38010d50 455 printbufidx = 0;
708d172a 456 echo_area_glyphs_length = 0;
38010d50 457 message_buf_print = 1;
aec2b95b
RS
458
459 if (minibuffer_auto_raise)
460 {
461 Lisp_Object mini_window;
462
463 /* Get the frame containing the minibuffer
464 that the selected frame is using. */
465 mini_window = FRAME_MINIBUF_WINDOW (selected_frame);
466
467 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
468 }
38010d50
JB
469 }
470
dc2a0b79
RS
471 message_dolog (ptr, size_byte, 0, multibyte);
472 if (size_byte > FRAME_MESSAGE_BUF_SIZE (mini_frame) - printbufidx - 1)
087e3c46 473 {
dc2a0b79 474 size_byte = FRAME_MESSAGE_BUF_SIZE (mini_frame) - printbufidx - 1;
087e3c46 475 /* Rewind incomplete multi-byte form. */
dc2a0b79 476 while (size_byte && (unsigned char) ptr[size] >= 0xA0) size--;
087e3c46 477 }
dc2a0b79
RS
478 bcopy (ptr, &FRAME_MESSAGE_BUF (mini_frame) [printbufidx], size_byte);
479 printbufidx += size_byte;
708d172a 480 echo_area_glyphs_length = printbufidx;
b8b1b8fd 481 FRAME_MESSAGE_BUF (mini_frame) [printbufidx] = 0;
38010d50
JB
482
483 return;
484 }
485
087e3c46 486 i = 0;
dc2a0b79
RS
487 if (size == size_byte)
488 while (i < size_byte)
489 {
490 int ch = ptr[i++];
087e3c46 491
dc2a0b79
RS
492 PRINTCHAR (ch);
493 }
494 else
495 while (i < size_byte)
496 {
497 /* Here, we must convert each multi-byte form to the
498 corresponding character code before handing it to PRINTCHAR. */
499 int len;
500 int ch = STRING_CHAR_AND_LENGTH (ptr + i, size_byte - i, len);
501
502 PRINTCHAR (ch);
503 i += len;
504 }
38010d50
JB
505}
506
507/* Print the contents of a string STRING using PRINTCHARFUN.
ed2c35ef
RS
508 It isn't safe to use strout in many cases,
509 because printing one char can relocate. */
38010d50 510
dc2a0b79 511static void
38010d50
JB
512print_string (string, printcharfun)
513 Lisp_Object string;
514 Lisp_Object printcharfun;
515{
6fec5601
RS
516 if (EQ (printcharfun, Qt) || NILP (printcharfun))
517 /* strout is safe for output to a frame (echo area) or to print_buffer. */
dc2a0b79
RS
518 strout (XSTRING (string)->data,
519 XSTRING (string)->size,
520 XSTRING (string)->size_byte,
521 printcharfun, STRING_MULTIBYTE (string));
38010d50
JB
522 else
523 {
dc2a0b79
RS
524 /* Otherwise, string may be relocated by printing one char.
525 So re-fetch the string address for each character. */
38010d50
JB
526 int i;
527 int size = XSTRING (string)->size;
dc2a0b79 528 int size_byte = XSTRING (string)->size_byte;
38010d50
JB
529 struct gcpro gcpro1;
530 GCPRO1 (string);
dc2a0b79
RS
531 if (size == size_byte)
532 for (i = 0; i < size; i++)
533 PRINTCHAR (XSTRING (string)->data[i]);
534 else
535 for (i = 0; i < size_byte; i++)
536 {
537 /* Here, we must convert each multi-byte form to the
538 corresponding character code before handing it to PRINTCHAR. */
539 int len;
540 int ch = STRING_CHAR_AND_LENGTH (XSTRING (string)->data + i,
541 size_byte - i, len);
542
543 PRINTCHAR (ch);
544 i += len;
545 }
38010d50
JB
546 UNGCPRO;
547 }
548}
549\f
550DEFUN ("write-char", Fwrite_char, Swrite_char, 1, 2, 0,
3738a371 551 "Output character CHARACTER to stream PRINTCHARFUN.\n\
57c9eb68 552PRINTCHARFUN defaults to the value of `standard-output' (which see).")
3738a371
EN
553 (character, printcharfun)
554 Lisp_Object character, printcharfun;
38010d50 555{
081e0581 556 PRINTDECLARE;
38010d50 557
10eebdbb 558 if (NILP (printcharfun))
38010d50 559 printcharfun = Vstandard_output;
3738a371 560 CHECK_NUMBER (character, 0);
38010d50 561 PRINTPREPARE;
3738a371 562 PRINTCHAR (XINT (character));
38010d50 563 PRINTFINISH;
3738a371 564 return character;
38010d50
JB
565}
566
dc2a0b79
RS
567/* Used from outside of print.c to print a block of SIZE
568 single-byte chars at DATA on the default output stream.
38010d50
JB
569 Do not use this on the contents of a Lisp string. */
570
dc22f25e 571void
38010d50
JB
572write_string (data, size)
573 char *data;
574 int size;
575{
081e0581 576 PRINTDECLARE;
38010d50 577 Lisp_Object printcharfun;
38010d50
JB
578
579 printcharfun = Vstandard_output;
580
581 PRINTPREPARE;
dc2a0b79 582 strout (data, size, size, printcharfun, 0);
38010d50
JB
583 PRINTFINISH;
584}
585
dc2a0b79
RS
586/* Used from outside of print.c to print a block of SIZE
587 single-byte chars at DATA on a specified stream PRINTCHARFUN.
38010d50
JB
588 Do not use this on the contents of a Lisp string. */
589
dc22f25e 590void
38010d50
JB
591write_string_1 (data, size, printcharfun)
592 char *data;
593 int size;
594 Lisp_Object printcharfun;
595{
081e0581 596 PRINTDECLARE;
38010d50
JB
597
598 PRINTPREPARE;
dc2a0b79 599 strout (data, size, size, printcharfun, 0);
38010d50
JB
600 PRINTFINISH;
601}
602
603
604#ifndef standalone
605
606void
607temp_output_buffer_setup (bufname)
608 char *bufname;
609{
610 register struct buffer *old = current_buffer;
611 register Lisp_Object buf;
612
613 Fset_buffer (Fget_buffer_create (build_string (bufname)));
614
2a1c968a 615 current_buffer->directory = old->directory;
38010d50
JB
616 current_buffer->read_only = Qnil;
617 Ferase_buffer ();
618
633307b5 619 XSETBUFFER (buf, current_buffer);
38010d50
JB
620 specbind (Qstandard_output, buf);
621
622 set_buffer_internal (old);
623}
624
625Lisp_Object
626internal_with_output_to_temp_buffer (bufname, function, args)
627 char *bufname;
3d03cf9f 628 Lisp_Object (*function) ();
38010d50
JB
629 Lisp_Object args;
630{
631 int count = specpdl_ptr - specpdl;
632 Lisp_Object buf, val;
0ab39c81 633 struct gcpro gcpro1;
38010d50 634
0ab39c81 635 GCPRO1 (args);
38010d50
JB
636 record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
637 temp_output_buffer_setup (bufname);
638 buf = Vstandard_output;
0ab39c81 639 UNGCPRO;
38010d50
JB
640
641 val = (*function) (args);
642
0ab39c81 643 GCPRO1 (val);
38010d50 644 temp_output_buffer_show (buf);
0ab39c81 645 UNGCPRO;
38010d50
JB
646
647 return unbind_to (count, val);
648}
649
650DEFUN ("with-output-to-temp-buffer", Fwith_output_to_temp_buffer, Swith_output_to_temp_buffer,
651 1, UNEVALLED, 0,
652 "Bind `standard-output' to buffer BUFNAME, eval BODY, then show that buffer.\n\
653The buffer is cleared out initially, and marked as unmodified when done.\n\
654All output done by BODY is inserted in that buffer by default.\n\
655The buffer is displayed in another window, but not selected.\n\
656The value of the last form in BODY is returned.\n\
657If BODY does not finish normally, the buffer BUFNAME is not displayed.\n\n\
483288d7 658If variable `temp-buffer-show-function' is non-nil, call it at the end\n\
38010d50
JB
659to get the buffer displayed. It gets one argument, the buffer to display.")
660 (args)
661 Lisp_Object args;
662{
663 struct gcpro gcpro1;
664 Lisp_Object name;
665 int count = specpdl_ptr - specpdl;
666 Lisp_Object buf, val;
667
668 GCPRO1(args);
669 name = Feval (Fcar (args));
670 UNGCPRO;
671
672 CHECK_STRING (name, 0);
673 temp_output_buffer_setup (XSTRING (name)->data);
674 buf = Vstandard_output;
675
676 val = Fprogn (Fcdr (args));
677
678 temp_output_buffer_show (buf);
679
680 return unbind_to (count, val);
681}
682#endif /* not standalone */
683\f
684static void print ();
685
686DEFUN ("terpri", Fterpri, Sterpri, 0, 1, 0,
57c9eb68
KH
687 "Output a newline to stream PRINTCHARFUN.\n\
688If PRINTCHARFUN is omitted or nil, the value of `standard-output' is used.")
38010d50
JB
689 (printcharfun)
690 Lisp_Object printcharfun;
691{
081e0581 692 PRINTDECLARE;
38010d50 693
10eebdbb 694 if (NILP (printcharfun))
38010d50
JB
695 printcharfun = Vstandard_output;
696 PRINTPREPARE;
697 PRINTCHAR ('\n');
698 PRINTFINISH;
699 return Qt;
700}
701
702DEFUN ("prin1", Fprin1, Sprin1, 1, 2, 0,
703 "Output the printed representation of OBJECT, any Lisp object.\n\
704Quoting characters are printed when needed to make output that `read'\n\
705can handle, whenever this is possible.\n\
57c9eb68 706Output stream is PRINTCHARFUN, or value of `standard-output' (which see).")
3738a371
EN
707 (object, printcharfun)
708 Lisp_Object object, printcharfun;
38010d50 709{
081e0581 710 PRINTDECLARE;
38010d50
JB
711
712#ifdef MAX_PRINT_CHARS
713 max_print = 0;
714#endif /* MAX_PRINT_CHARS */
10eebdbb 715 if (NILP (printcharfun))
38010d50
JB
716 printcharfun = Vstandard_output;
717 PRINTPREPARE;
718 print_depth = 0;
3738a371 719 print (object, printcharfun, 1);
38010d50 720 PRINTFINISH;
3738a371 721 return object;
38010d50
JB
722}
723
724/* a buffer which is used to hold output being built by prin1-to-string */
725Lisp_Object Vprin1_to_string_buffer;
726
727DEFUN ("prin1-to-string", Fprin1_to_string, Sprin1_to_string, 1, 2, 0,
728 "Return a string containing the printed representation of OBJECT,\n\
729any Lisp object. Quoting characters are used when needed to make output\n\
730that `read' can handle, whenever this is possible, unless the optional\n\
731second argument NOESCAPE is non-nil.")
3738a371
EN
732 (object, noescape)
733 Lisp_Object object, noescape;
38010d50 734{
081e0581
EN
735 PRINTDECLARE;
736 Lisp_Object printcharfun;
2a42e8f6
KH
737 struct gcpro gcpro1, gcpro2;
738 Lisp_Object tem;
739
740 /* Save and restore this--we are altering a buffer
741 but we don't want to deactivate the mark just for that.
742 No need for specbind, since errors deactivate the mark. */
743 tem = Vdeactivate_mark;
744 GCPRO2 (object, tem);
38010d50
JB
745
746 printcharfun = Vprin1_to_string_buffer;
747 PRINTPREPARE;
748 print_depth = 0;
3738a371 749 print (object, printcharfun, NILP (noescape));
38010d50
JB
750 /* Make Vprin1_to_string_buffer be the default buffer after PRINTFINSH */
751 PRINTFINISH;
752 set_buffer_internal (XBUFFER (Vprin1_to_string_buffer));
3738a371 753 object = Fbuffer_string ();
38010d50 754
38010d50
JB
755 Ferase_buffer ();
756 set_buffer_internal (old);
2a42e8f6
KH
757
758 Vdeactivate_mark = tem;
38010d50
JB
759 UNGCPRO;
760
3738a371 761 return object;
38010d50
JB
762}
763
764DEFUN ("princ", Fprinc, Sprinc, 1, 2, 0,
765 "Output the printed representation of OBJECT, any Lisp object.\n\
766No quoting characters are used; no delimiters are printed around\n\
767the contents of strings.\n\
57c9eb68 768Output stream is PRINTCHARFUN, or value of standard-output (which see).")
3738a371
EN
769 (object, printcharfun)
770 Lisp_Object object, printcharfun;
38010d50 771{
081e0581 772 PRINTDECLARE;
38010d50 773
10eebdbb 774 if (NILP (printcharfun))
38010d50
JB
775 printcharfun = Vstandard_output;
776 PRINTPREPARE;
777 print_depth = 0;
3738a371 778 print (object, printcharfun, 0);
38010d50 779 PRINTFINISH;
3738a371 780 return object;
38010d50
JB
781}
782
783DEFUN ("print", Fprint, Sprint, 1, 2, 0,
784 "Output the printed representation of OBJECT, with newlines around it.\n\
785Quoting characters are printed when needed to make output that `read'\n\
786can handle, whenever this is possible.\n\
57c9eb68 787Output stream is PRINTCHARFUN, or value of `standard-output' (which see).")
3738a371
EN
788 (object, printcharfun)
789 Lisp_Object object, printcharfun;
38010d50 790{
081e0581 791 PRINTDECLARE;
38010d50
JB
792 struct gcpro gcpro1;
793
794#ifdef MAX_PRINT_CHARS
795 print_chars = 0;
796 max_print = MAX_PRINT_CHARS;
797#endif /* MAX_PRINT_CHARS */
10eebdbb 798 if (NILP (printcharfun))
38010d50 799 printcharfun = Vstandard_output;
3738a371 800 GCPRO1 (object);
38010d50
JB
801 PRINTPREPARE;
802 print_depth = 0;
803 PRINTCHAR ('\n');
3738a371 804 print (object, printcharfun, 1);
38010d50
JB
805 PRINTCHAR ('\n');
806 PRINTFINISH;
807#ifdef MAX_PRINT_CHARS
808 max_print = 0;
809 print_chars = 0;
810#endif /* MAX_PRINT_CHARS */
811 UNGCPRO;
3738a371 812 return object;
38010d50
JB
813}
814
815/* The subroutine object for external-debugging-output is kept here
816 for the convenience of the debugger. */
817Lisp_Object Qexternal_debugging_output;
818
4746118a
JB
819DEFUN ("external-debugging-output", Fexternal_debugging_output, Sexternal_debugging_output, 1, 1, 0,
820 "Write CHARACTER to stderr.\n\
38010d50
JB
821You can call print while debugging emacs, and pass it this function\n\
822to make it write to the debugging output.\n")
4746118a
JB
823 (character)
824 Lisp_Object character;
38010d50
JB
825{
826 CHECK_NUMBER (character, 0);
827 putc (XINT (character), stderr);
cd22039d
RS
828
829#ifdef WINDOWSNT
830 /* Send the output to a debugger (nothing happens if there isn't one). */
831 {
832 char buf[2] = {(char) XINT (character), '\0'};
833 OutputDebugString (buf);
834 }
835#endif
836
38010d50
JB
837 return character;
838}
cf1bb91b
RS
839
840/* This is the interface for debugging printing. */
841
842void
843debug_print (arg)
844 Lisp_Object arg;
845{
846 Fprin1 (arg, Qexternal_debugging_output);
3684eb78 847 fprintf (stderr, "\r\n");
cf1bb91b 848}
38010d50 849\f
113620cc
KH
850DEFUN ("error-message-string", Ferror_message_string, Serror_message_string,
851 1, 1, 0,
852 "Convert an error value (ERROR-SYMBOL . DATA) to an error message.")
853 (obj)
854 Lisp_Object obj;
855{
856 struct buffer *old = current_buffer;
857 Lisp_Object original, printcharfun, value;
858 struct gcpro gcpro1;
859
0872e11f
RS
860 /* If OBJ is (error STRING), just return STRING.
861 That is not only faster, it also avoids the need to allocate
862 space here when the error is due to memory full. */
863 if (CONSP (obj) && EQ (XCONS (obj)->car, Qerror)
864 && CONSP (XCONS (obj)->cdr)
865 && STRINGP (XCONS (XCONS (obj)->cdr)->car)
866 && NILP (XCONS (XCONS (obj)->cdr)->cdr))
867 return XCONS (XCONS (obj)->cdr)->car;
868
dc22f25e 869 print_error_message (obj, Vprin1_to_string_buffer);
113620cc
KH
870
871 set_buffer_internal (XBUFFER (Vprin1_to_string_buffer));
872 value = Fbuffer_string ();
873
874 GCPRO1 (value);
875 Ferase_buffer ();
876 set_buffer_internal (old);
877 UNGCPRO;
878
879 return value;
880}
881
882/* Print an error message for the error DATA
883 onto Lisp output stream STREAM (suitable for the print functions). */
884
dc22f25e 885void
113620cc
KH
886print_error_message (data, stream)
887 Lisp_Object data, stream;
888{
889 Lisp_Object errname, errmsg, file_error, tail;
890 struct gcpro gcpro1;
891 int i;
892
893 errname = Fcar (data);
894
895 if (EQ (errname, Qerror))
896 {
897 data = Fcdr (data);
898 if (!CONSP (data)) data = Qnil;
899 errmsg = Fcar (data);
900 file_error = Qnil;
901 }
902 else
903 {
904 errmsg = Fget (errname, Qerror_message);
905 file_error = Fmemq (Qfile_error,
906 Fget (errname, Qerror_conditions));
907 }
908
909 /* Print an error message including the data items. */
910
911 tail = Fcdr_safe (data);
912 GCPRO1 (tail);
913
914 /* For file-error, make error message by concatenating
915 all the data items. They are all strings. */
916 if (!NILP (file_error) && !NILP (tail))
917 errmsg = XCONS (tail)->car, tail = XCONS (tail)->cdr;
918
919 if (STRINGP (errmsg))
920 Fprinc (errmsg, stream);
921 else
922 write_string_1 ("peculiar error", -1, stream);
923
924 for (i = 0; CONSP (tail); tail = Fcdr (tail), i++)
925 {
926 write_string_1 (i ? ", " : ": ", 2, stream);
927 if (!NILP (file_error))
928 Fprinc (Fcar (tail), stream);
929 else
930 Fprin1 (Fcar (tail), stream);
931 }
932 UNGCPRO;
933}
934\f
38010d50
JB
935#ifdef LISP_FLOAT_TYPE
936
38010d50 937/*
edb2a707 938 * The buffer should be at least as large as the max string size of the
8e6208c5 939 * largest float, printed in the biggest notation. This is undoubtedly
38010d50
JB
940 * 20d float_output_format, with the negative of the C-constant "HUGE"
941 * from <math.h>.
942 *
943 * On the vax the worst case is -1e38 in 20d format which takes 61 bytes.
944 *
945 * I assume that IEEE-754 format numbers can take 329 bytes for the worst
946 * case of -1e307 in 20d float_output_format. What is one to do (short of
947 * re-writing _doprnt to be more sane)?
948 * -wsr
949 */
edb2a707
RS
950
951void
952float_to_string (buf, data)
8b24d146 953 unsigned char *buf;
38010d50
JB
954 double data;
955{
c7b14277 956 unsigned char *cp;
322890c4 957 int width;
38010d50 958
10eebdbb 959 if (NILP (Vfloat_output_format)
d4ae1f7e 960 || !STRINGP (Vfloat_output_format))
38010d50 961 lose:
322890c4 962 {
f356c3fb
PE
963 /* Generate the fewest number of digits that represent the
964 floating point value without losing information.
965 The following method is simple but a bit slow.
966 For ideas about speeding things up, please see:
967
968 Guy L Steele Jr & Jon L White, How to print floating-point numbers
969 accurately. SIGPLAN notices 25, 6 (June 1990), 112-126.
970
971 Robert G Burger & R Kent Dybvig, Printing floating point numbers
972 quickly and accurately, SIGPLAN notices 31, 5 (May 1996), 108-116. */
973
974 width = fabs (data) < DBL_MIN ? 1 : DBL_DIG;
975 do
976 sprintf (buf, "%.*g", width, data);
977 while (width++ < DOUBLE_DIGITS_BOUND && atof (buf) != data);
322890c4 978 }
38010d50
JB
979 else /* oink oink */
980 {
981 /* Check that the spec we have is fully valid.
982 This means not only valid for printf,
983 but meant for floats, and reasonable. */
984 cp = XSTRING (Vfloat_output_format)->data;
985
986 if (cp[0] != '%')
987 goto lose;
988 if (cp[1] != '.')
989 goto lose;
990
991 cp += 2;
c7b14277
JB
992
993 /* Check the width specification. */
322890c4 994 width = -1;
c7b14277 995 if ('0' <= *cp && *cp <= '9')
381cd4bb
KH
996 {
997 width = 0;
998 do
999 width = (width * 10) + (*cp++ - '0');
1000 while (*cp >= '0' && *cp <= '9');
1001
1002 /* A precision of zero is valid only for %f. */
1003 if (width > DBL_DIG
1004 || (width == 0 && *cp != 'f'))
1005 goto lose;
1006 }
38010d50
JB
1007
1008 if (*cp != 'e' && *cp != 'f' && *cp != 'g')
1009 goto lose;
1010
38010d50
JB
1011 if (cp[1] != 0)
1012 goto lose;
1013
1014 sprintf (buf, XSTRING (Vfloat_output_format)->data, data);
1015 }
edb2a707 1016
c7b14277
JB
1017 /* Make sure there is a decimal point with digit after, or an
1018 exponent, so that the value is readable as a float. But don't do
322890c4
RS
1019 this with "%.0f"; it's valid for that not to produce a decimal
1020 point. Note that width can be 0 only for %.0f. */
1021 if (width != 0)
0601fd3d 1022 {
c7b14277
JB
1023 for (cp = buf; *cp; cp++)
1024 if ((*cp < '0' || *cp > '9') && *cp != '-')
1025 break;
0601fd3d 1026
c7b14277
JB
1027 if (*cp == '.' && cp[1] == 0)
1028 {
1029 cp[1] = '0';
1030 cp[2] = 0;
1031 }
1032
1033 if (*cp == 0)
1034 {
1035 *cp++ = '.';
1036 *cp++ = '0';
1037 *cp++ = 0;
1038 }
edb2a707 1039 }
38010d50
JB
1040}
1041#endif /* LISP_FLOAT_TYPE */
1042\f
1043static void
1044print (obj, printcharfun, escapeflag)
38010d50 1045 Lisp_Object obj;
38010d50
JB
1046 register Lisp_Object printcharfun;
1047 int escapeflag;
1048{
1049 char buf[30];
1050
1051 QUIT;
1052
ec838c39
RS
1053#if 1 /* I'm not sure this is really worth doing. */
1054 /* Detect circularities and truncate them.
1055 No need to offer any alternative--this is better than an error. */
d4ae1f7e 1056 if (CONSP (obj) || VECTORP (obj) || COMPILEDP (obj))
ec838c39
RS
1057 {
1058 int i;
1059 for (i = 0; i < print_depth; i++)
1060 if (EQ (obj, being_printed[i]))
1061 {
1062 sprintf (buf, "#%d", i);
dc2a0b79 1063 strout (buf, -1, -1, printcharfun, 0);
ec838c39
RS
1064 return;
1065 }
1066 }
1067#endif
1068
1069 being_printed[print_depth] = obj;
38010d50
JB
1070 print_depth++;
1071
ec838c39 1072 if (print_depth > PRINT_CIRCLE)
38010d50
JB
1073 error ("Apparently circular structure being printed");
1074#ifdef MAX_PRINT_CHARS
1075 if (max_print && print_chars > max_print)
1076 {
1077 PRINTCHAR ('\n');
1078 print_chars = 0;
1079 }
1080#endif /* MAX_PRINT_CHARS */
1081
ca0569ad 1082 switch (XGCTYPE (obj))
38010d50 1083 {
ca0569ad 1084 case Lisp_Int:
b8180922
RS
1085 if (sizeof (int) == sizeof (EMACS_INT))
1086 sprintf (buf, "%d", XINT (obj));
1087 else if (sizeof (long) == sizeof (EMACS_INT))
1088 sprintf (buf, "%ld", XINT (obj));
1089 else
1090 abort ();
dc2a0b79 1091 strout (buf, -1, -1, printcharfun, 0);
ca0569ad
RS
1092 break;
1093
e0f93814 1094#ifdef LISP_FLOAT_TYPE
ca0569ad
RS
1095 case Lisp_Float:
1096 {
1097 char pigbuf[350]; /* see comments in float_to_string */
38010d50 1098
ca0569ad 1099 float_to_string (pigbuf, XFLOAT(obj)->data);
dc2a0b79 1100 strout (pigbuf, -1, -1, printcharfun, 0);
ca0569ad
RS
1101 }
1102 break;
e0f93814 1103#endif
ca0569ad
RS
1104
1105 case Lisp_String:
38010d50
JB
1106 if (!escapeflag)
1107 print_string (obj, printcharfun);
1108 else
1109 {
dc2a0b79 1110 register int i, i_byte;
38010d50 1111 register unsigned char c;
38010d50 1112 struct gcpro gcpro1;
dc2a0b79 1113 int size_byte;
38010d50 1114
7651e1f5
RS
1115 GCPRO1 (obj);
1116
1117#ifdef USE_TEXT_PROPERTIES
1118 if (!NULL_INTERVAL_P (XSTRING (obj)->intervals))
1119 {
1120 PRINTCHAR ('#');
1121 PRINTCHAR ('(');
1122 }
1123#endif
38010d50
JB
1124
1125 PRINTCHAR ('\"');
dc2a0b79
RS
1126 size_byte = XSTRING (obj)->size_byte;
1127
1128 for (i = 0, i_byte = 0; i_byte < size_byte;)
38010d50 1129 {
6ddd6eee
RS
1130 /* Here, we must convert each multi-byte form to the
1131 corresponding character code before handing it to PRINTCHAR. */
1132 int len;
dc2a0b79
RS
1133 int c;
1134
1135 if (STRING_MULTIBYTE (obj))
1136 FETCH_STRING_CHAR_ADVANCE (c, obj, i, i_byte);
1137 else
1138 c = XSTRING (obj)->data[i_byte++];
1139
38010d50 1140 QUIT;
6ddd6eee 1141
38010d50
JB
1142 if (c == '\n' && print_escape_newlines)
1143 {
1144 PRINTCHAR ('\\');
1145 PRINTCHAR ('n');
1146 }
c6f7982f
RM
1147 else if (c == '\f' && print_escape_newlines)
1148 {
1149 PRINTCHAR ('\\');
1150 PRINTCHAR ('f');
1151 }
974a6ff5
KH
1152 else if ((! SINGLE_BYTE_CHAR_P (c)
1153 && NILP (current_buffer->enable_multibyte_characters)))
dc2a0b79
RS
1154 {
1155 /* When multibyte is disabled,
1156 print multibyte string chars using hex escapes. */
1157 unsigned char outbuf[50];
1158 sprintf (outbuf, "\\x%x", c);
1159 strout (outbuf, -1, -1, printcharfun, 0);
1160 }
974a6ff5
KH
1161 else if (SINGLE_BYTE_CHAR_P (c)
1162 && ! ASCII_BYTE_P (c)
1163 && ! NILP (current_buffer->enable_multibyte_characters))
1164 {
1165 /* When multibyte is enabled,
1166 print single-byte non-ASCII string chars
1167 using octal escapes. */
1168 unsigned char outbuf[5];
1169 sprintf (outbuf, "\\%03o", c);
1170 strout (outbuf, -1, -1, printcharfun, 0);
1171 }
38010d50
JB
1172 else
1173 {
1174 if (c == '\"' || c == '\\')
1175 PRINTCHAR ('\\');
1176 PRINTCHAR (c);
1177 }
1178 }
1179 PRINTCHAR ('\"');
7651e1f5
RS
1180
1181#ifdef USE_TEXT_PROPERTIES
1182 if (!NULL_INTERVAL_P (XSTRING (obj)->intervals))
1183 {
7651e1f5
RS
1184 traverse_intervals (XSTRING (obj)->intervals,
1185 0, 0, print_interval, printcharfun);
1186 PRINTCHAR (')');
1187 }
1188#endif
1189
38010d50
JB
1190 UNGCPRO;
1191 }
ca0569ad 1192 break;
38010d50 1193
ca0569ad
RS
1194 case Lisp_Symbol:
1195 {
1196 register int confusing;
1197 register unsigned char *p = XSYMBOL (obj)->name->data;
dc2a0b79 1198 register unsigned char *end = p + XSYMBOL (obj)->name->size_byte;
ca0569ad 1199 register unsigned char c;
dc2a0b79
RS
1200 int i, i_byte, size_byte;
1201 Lisp_Object name;
1202
1203 XSETSTRING (name, XSYMBOL (obj)->name);
ca0569ad
RS
1204
1205 if (p != end && (*p == '-' || *p == '+')) p++;
1206 if (p == end)
1207 confusing = 0;
d27497e3
RS
1208 /* If symbol name begins with a digit, and ends with a digit,
1209 and contains nothing but digits and `e', it could be treated
1210 as a number. So set CONFUSING.
1211
1212 Symbols that contain periods could also be taken as numbers,
1213 but periods are always escaped, so we don't have to worry
1214 about them here. */
1215 else if (*p >= '0' && *p <= '9'
1216 && end[-1] >= '0' && end[-1] <= '9')
ca0569ad 1217 {
e837058b
RS
1218 while (p != end && ((*p >= '0' && *p <= '9')
1219 /* Needed for \2e10. */
1220 || *p == 'e'))
ca0569ad
RS
1221 p++;
1222 confusing = (end == p);
1223 }
d27497e3
RS
1224 else
1225 confusing = 0;
ca0569ad 1226
081e0581
EN
1227 /* If we print an uninterned symbol as part of a complex object and
1228 the flag print-gensym is non-nil, prefix it with #n= to read the
1229 object back with the #n# reader syntax later if needed. */
e0f69431 1230 if (! NILP (Vprint_gensym) && NILP (XSYMBOL (obj)->obarray))
081e0581
EN
1231 {
1232 if (print_depth > 1)
1233 {
1234 Lisp_Object tem;
e0f69431 1235 tem = Fassq (obj, Vprint_gensym_alist);
081e0581
EN
1236 if (CONSP (tem))
1237 {
1238 PRINTCHAR ('#');
1239 print (XCDR (tem), printcharfun, escapeflag);
1240 PRINTCHAR ('#');
1241 break;
1242 }
1243 else
1244 {
e0f69431
RS
1245 if (CONSP (Vprint_gensym_alist))
1246 XSETFASTINT (tem, XFASTINT (XCDR (XCAR (Vprint_gensym_alist))) + 1);
081e0581
EN
1247 else
1248 XSETFASTINT (tem, 1);
e0f69431 1249 Vprint_gensym_alist = Fcons (Fcons (obj, tem), Vprint_gensym_alist);
081e0581
EN
1250
1251 PRINTCHAR ('#');
1252 print (tem, printcharfun, escapeflag);
1253 PRINTCHAR ('=');
1254 }
1255 }
1256 PRINTCHAR ('#');
1257 PRINTCHAR (':');
1258 }
1259
dc2a0b79
RS
1260 size_byte = XSTRING (name)->size_byte;
1261
1262 for (i = 0, i_byte = 0; i_byte < size_byte;)
ca0569ad 1263 {
6ddd6eee
RS
1264 /* Here, we must convert each multi-byte form to the
1265 corresponding character code before handing it to PRINTCHAR. */
dc2a0b79
RS
1266
1267 if (STRING_MULTIBYTE (name))
1268 FETCH_STRING_CHAR_ADVANCE (c, name, i, i_byte);
1269 else
1270 c = XSTRING (name)->data[i_byte++];
1271
ca0569ad 1272 QUIT;
09eddb56 1273
ca0569ad
RS
1274 if (escapeflag)
1275 {
09eddb56
RS
1276 if (c == '\"' || c == '\\' || c == '\''
1277 || c == ';' || c == '#' || c == '(' || c == ')'
1278 || c == ',' || c =='.' || c == '`'
1279 || c == '[' || c == ']' || c == '?' || c <= 040
1280 || confusing)
ca0569ad
RS
1281 PRINTCHAR ('\\'), confusing = 0;
1282 }
1283 PRINTCHAR (c);
1284 }
1285 }
1286 break;
1287
1288 case Lisp_Cons:
38010d50 1289 /* If deeper than spec'd depth, print placeholder. */
d4ae1f7e 1290 if (INTEGERP (Vprint_level)
38010d50 1291 && print_depth > XINT (Vprint_level))
dc2a0b79 1292 strout ("...", -1, -1, printcharfun, 0);
2f100b5c
EN
1293 else if (print_quoted && CONSP (XCDR (obj)) && NILP (XCDR (XCDR (obj)))
1294 && (EQ (XCAR (obj), Qquote)))
1295 {
1296 PRINTCHAR ('\'');
1297 print (XCAR (XCDR (obj)), printcharfun, escapeflag);
1298 }
1299 else if (print_quoted && CONSP (XCDR (obj)) && NILP (XCDR (XCDR (obj)))
1300 && (EQ (XCAR (obj), Qfunction)))
1301 {
1302 PRINTCHAR ('#');
1303 PRINTCHAR ('\'');
1304 print (XCAR (XCDR (obj)), printcharfun, escapeflag);
1305 }
1306 else if (print_quoted && CONSP (XCDR (obj)) && NILP (XCDR (XCDR (obj)))
1307 && ((EQ (XCAR (obj), Qbackquote)
1308 || EQ (XCAR (obj), Qcomma)
1309 || EQ (XCAR (obj), Qcomma_at)
1310 || EQ (XCAR (obj), Qcomma_dot))))
1311 {
1312 print (XCAR (obj), printcharfun, 0);
1313 print (XCAR (XCDR (obj)), printcharfun, escapeflag);
1314 }
e0f93814 1315 else
38010d50 1316 {
e0f93814 1317 PRINTCHAR ('(');
38010d50 1318 {
e0f93814
KH
1319 register int i = 0;
1320 register int max = 0;
1321
1322 if (INTEGERP (Vprint_length))
1323 max = XINT (Vprint_length);
1324 /* Could recognize circularities in cdrs here,
1325 but that would make printing of long lists quadratic.
1326 It's not worth doing. */
1327 while (CONSP (obj))
38010d50 1328 {
e0f93814
KH
1329 if (i++)
1330 PRINTCHAR (' ');
1331 if (max && i > max)
1332 {
dc2a0b79 1333 strout ("...", 3, 3, printcharfun, 0);
e0f93814
KH
1334 break;
1335 }
2f100b5c
EN
1336 print (XCAR (obj), printcharfun, escapeflag);
1337 obj = XCDR (obj);
38010d50 1338 }
38010d50 1339 }
2f100b5c 1340 if (!NILP (obj))
e0f93814 1341 {
dc2a0b79 1342 strout (" . ", 3, 3, printcharfun, 0);
e0f93814
KH
1343 print (obj, printcharfun, escapeflag);
1344 }
1345 PRINTCHAR (')');
38010d50 1346 }
ca0569ad
RS
1347 break;
1348
1349 case Lisp_Vectorlike:
1350 if (PROCESSP (obj))
1351 {
1352 if (escapeflag)
1353 {
dc2a0b79 1354 strout ("#<process ", -1, -1, printcharfun, 0);
ca0569ad
RS
1355 print_string (XPROCESS (obj)->name, printcharfun);
1356 PRINTCHAR ('>');
1357 }
1358 else
1359 print_string (XPROCESS (obj)->name, printcharfun);
1360 }
ed2c35ef
RS
1361 else if (BOOL_VECTOR_P (obj))
1362 {
1363 register int i;
1364 register unsigned char c;
1365 struct gcpro gcpro1;
ed2c35ef 1366 int size_in_chars
1bad7c59 1367 = (XBOOL_VECTOR (obj)->size + BITS_PER_CHAR - 1) / BITS_PER_CHAR;
ed2c35ef
RS
1368
1369 GCPRO1 (obj);
1370
1371 PRINTCHAR ('#');
1372 PRINTCHAR ('&');
1373 sprintf (buf, "%d", XBOOL_VECTOR (obj)->size);
dc2a0b79 1374 strout (buf, -1, -1, printcharfun, 0);
ed2c35ef 1375 PRINTCHAR ('\"');
a40384bc
RS
1376
1377 /* Don't print more characters than the specified maximum. */
1378 if (INTEGERP (Vprint_length)
1379 && XINT (Vprint_length) < size_in_chars)
1380 size_in_chars = XINT (Vprint_length);
1381
ed2c35ef
RS
1382 for (i = 0; i < size_in_chars; i++)
1383 {
1384 QUIT;
1385 c = XBOOL_VECTOR (obj)->data[i];
1386 if (c == '\n' && print_escape_newlines)
1387 {
1388 PRINTCHAR ('\\');
1389 PRINTCHAR ('n');
1390 }
1391 else if (c == '\f' && print_escape_newlines)
1392 {
1393 PRINTCHAR ('\\');
1394 PRINTCHAR ('f');
1395 }
1396 else
1397 {
1398 if (c == '\"' || c == '\\')
1399 PRINTCHAR ('\\');
1400 PRINTCHAR (c);
1401 }
1402 }
1403 PRINTCHAR ('\"');
1404
1405 UNGCPRO;
1406 }
ca0569ad
RS
1407 else if (SUBRP (obj))
1408 {
dc2a0b79
RS
1409 strout ("#<subr ", -1, -1, printcharfun, 0);
1410 strout (XSUBR (obj)->symbol_name, -1, -1, printcharfun, 0);
ca0569ad
RS
1411 PRINTCHAR ('>');
1412 }
1413#ifndef standalone
1414 else if (WINDOWP (obj))
1415 {
dc2a0b79 1416 strout ("#<window ", -1, -1, printcharfun, 0);
ca0569ad 1417 sprintf (buf, "%d", XFASTINT (XWINDOW (obj)->sequence_number));
dc2a0b79 1418 strout (buf, -1, -1, printcharfun, 0);
ca0569ad
RS
1419 if (!NILP (XWINDOW (obj)->buffer))
1420 {
dc2a0b79 1421 strout (" on ", -1, -1, printcharfun, 0);
ca0569ad
RS
1422 print_string (XBUFFER (XWINDOW (obj)->buffer)->name, printcharfun);
1423 }
1424 PRINTCHAR ('>');
1425 }
908b0ae5
RS
1426 else if (BUFFERP (obj))
1427 {
1428 if (NILP (XBUFFER (obj)->name))
dc2a0b79 1429 strout ("#<killed buffer>", -1, -1, printcharfun, 0);
908b0ae5
RS
1430 else if (escapeflag)
1431 {
dc2a0b79 1432 strout ("#<buffer ", -1, -1, printcharfun, 0);
908b0ae5
RS
1433 print_string (XBUFFER (obj)->name, printcharfun);
1434 PRINTCHAR ('>');
1435 }
1436 else
1437 print_string (XBUFFER (obj)->name, printcharfun);
1438 }
ca0569ad
RS
1439 else if (WINDOW_CONFIGURATIONP (obj))
1440 {
dc2a0b79 1441 strout ("#<window-configuration>", -1, -1, printcharfun, 0);
ca0569ad 1442 }
ca0569ad
RS
1443 else if (FRAMEP (obj))
1444 {
1445 strout ((FRAME_LIVE_P (XFRAME (obj))
1446 ? "#<frame " : "#<dead frame "),
dc2a0b79 1447 -1, -1, printcharfun, 0);
ca0569ad 1448 print_string (XFRAME (obj)->name, printcharfun);
dc2a0b79
RS
1449 sprintf (buf, " 0x%lx\\ ", (unsigned long) (XFRAME (obj)));
1450 strout (buf, -1, -1, printcharfun, 0);
ca0569ad
RS
1451 PRINTCHAR ('>');
1452 }
ca0569ad
RS
1453#endif /* not standalone */
1454 else
1455 {
1456 int size = XVECTOR (obj)->size;
1457 if (COMPILEDP (obj))
1458 {
1459 PRINTCHAR ('#');
1460 size &= PSEUDOVECTOR_SIZE_MASK;
1461 }
ed2c35ef
RS
1462 if (CHAR_TABLE_P (obj))
1463 {
1464 /* We print a char-table as if it were a vector,
1465 lumping the parent and default slots in with the
1466 character slots. But we add #^ as a prefix. */
1467 PRINTCHAR ('#');
1468 PRINTCHAR ('^');
3701b5de
KH
1469 if (SUB_CHAR_TABLE_P (obj))
1470 PRINTCHAR ('^');
ed2c35ef
RS
1471 size &= PSEUDOVECTOR_SIZE_MASK;
1472 }
00d76abc
KH
1473 if (size & PSEUDOVECTOR_FLAG)
1474 goto badtype;
ca0569ad
RS
1475
1476 PRINTCHAR ('[');
38010d50 1477 {
ca0569ad
RS
1478 register int i;
1479 register Lisp_Object tem;
a40384bc
RS
1480
1481 /* Don't print more elements than the specified maximum. */
1482 if (INTEGERP (Vprint_length)
1483 && XINT (Vprint_length) < size)
1484 size = XINT (Vprint_length);
1485
ca0569ad
RS
1486 for (i = 0; i < size; i++)
1487 {
1488 if (i) PRINTCHAR (' ');
1489 tem = XVECTOR (obj)->contents[i];
1490 print (tem, printcharfun, escapeflag);
1491 }
38010d50 1492 }
ca0569ad
RS
1493 PRINTCHAR (']');
1494 }
1495 break;
1496
38010d50 1497#ifndef standalone
ca0569ad 1498 case Lisp_Misc:
5db20f08 1499 switch (XMISCTYPE (obj))
38010d50 1500 {
00d76abc 1501 case Lisp_Misc_Marker:
dc2a0b79 1502 strout ("#<marker ", -1, -1, printcharfun, 0);
087e3c46
KH
1503 /* Do you think this is necessary? */
1504 if (XMARKER (obj)->insertion_type != 0)
dc2a0b79 1505 strout ("(before-insertion) ", -1, -1, printcharfun, 0);
ca0569ad 1506 if (!(XMARKER (obj)->buffer))
dc2a0b79 1507 strout ("in no buffer", -1, -1, printcharfun, 0);
ca0569ad
RS
1508 else
1509 {
1510 sprintf (buf, "at %d", marker_position (obj));
dc2a0b79
RS
1511 strout (buf, -1, -1, printcharfun, 0);
1512 strout (" in ", -1, -1, printcharfun, 0);
ca0569ad
RS
1513 print_string (XMARKER (obj)->buffer->name, printcharfun);
1514 }
38010d50 1515 PRINTCHAR ('>');
908b0ae5 1516 break;
00d76abc
KH
1517
1518 case Lisp_Misc_Overlay:
dc2a0b79 1519 strout ("#<overlay ", -1, -1, printcharfun, 0);
ca0569ad 1520 if (!(XMARKER (OVERLAY_START (obj))->buffer))
dc2a0b79 1521 strout ("in no buffer", -1, -1, printcharfun, 0);
ca0569ad
RS
1522 else
1523 {
1524 sprintf (buf, "from %d to %d in ",
1525 marker_position (OVERLAY_START (obj)),
1526 marker_position (OVERLAY_END (obj)));
dc2a0b79 1527 strout (buf, -1, -1, printcharfun, 0);
ca0569ad
RS
1528 print_string (XMARKER (OVERLAY_START (obj))->buffer->name,
1529 printcharfun);
1530 }
1531 PRINTCHAR ('>');
908b0ae5 1532 break;
00d76abc
KH
1533
1534 /* Remaining cases shouldn't happen in normal usage, but let's print
1535 them anyway for the benefit of the debugger. */
1536 case Lisp_Misc_Free:
dc2a0b79 1537 strout ("#<misc free cell>", -1, -1, printcharfun, 0);
00d76abc
KH
1538 break;
1539
1540 case Lisp_Misc_Intfwd:
1541 sprintf (buf, "#<intfwd to %d>", *XINTFWD (obj)->intvar);
dc2a0b79 1542 strout (buf, -1, -1, printcharfun, 0);
00d76abc
KH
1543 break;
1544
1545 case Lisp_Misc_Boolfwd:
1546 sprintf (buf, "#<boolfwd to %s>",
1547 (*XBOOLFWD (obj)->boolvar ? "t" : "nil"));
dc2a0b79 1548 strout (buf, -1, -1, printcharfun, 0);
00d76abc
KH
1549 break;
1550
1551 case Lisp_Misc_Objfwd:
dc2a0b79 1552 strout ("#<objfwd to ", -1, -1, printcharfun, 0);
00d76abc
KH
1553 print (*XOBJFWD (obj)->objvar, printcharfun, escapeflag);
1554 PRINTCHAR ('>');
1555 break;
1556
1557 case Lisp_Misc_Buffer_Objfwd:
dc2a0b79 1558 strout ("#<buffer_objfwd to ", -1, -1, printcharfun, 0);
3ac613c1
KH
1559 print (*(Lisp_Object *)((char *)current_buffer
1560 + XBUFFER_OBJFWD (obj)->offset),
1561 printcharfun, escapeflag);
1562 PRINTCHAR ('>');
1563 break;
1564
fb917148 1565 case Lisp_Misc_Kboard_Objfwd:
dc2a0b79 1566 strout ("#<kboard_objfwd to ", -1, -1, printcharfun, 0);
fb917148
KH
1567 print (*(Lisp_Object *)((char *) current_kboard
1568 + XKBOARD_OBJFWD (obj)->offset),
7ae137a9 1569 printcharfun, escapeflag);
00d76abc
KH
1570 PRINTCHAR ('>');
1571 break;
1572
1573 case Lisp_Misc_Buffer_Local_Value:
dc2a0b79 1574 strout ("#<buffer_local_value ", -1, -1, printcharfun, 0);
00d76abc
KH
1575 goto do_buffer_local;
1576 case Lisp_Misc_Some_Buffer_Local_Value:
dc2a0b79 1577 strout ("#<some_buffer_local_value ", -1, -1, printcharfun, 0);
00d76abc 1578 do_buffer_local:
dc2a0b79 1579 strout ("[realvalue] ", -1, -1, printcharfun, 0);
00d76abc 1580 print (XBUFFER_LOCAL_VALUE (obj)->car, printcharfun, escapeflag);
dc2a0b79 1581 strout ("[buffer] ", -1, -1, printcharfun, 0);
00d76abc
KH
1582 print (XCONS (XBUFFER_LOCAL_VALUE (obj)->cdr)->car,
1583 printcharfun, escapeflag);
dc2a0b79 1584 strout ("[alist-elt] ", -1, -1, printcharfun, 0);
00d76abc
KH
1585 print (XCONS (XCONS (XBUFFER_LOCAL_VALUE (obj)->cdr)->cdr)->car,
1586 printcharfun, escapeflag);
dc2a0b79 1587 strout ("[default-value] ", -1, -1, printcharfun, 0);
00d76abc
KH
1588 print (XCONS (XCONS (XBUFFER_LOCAL_VALUE (obj)->cdr)->cdr)->cdr,
1589 printcharfun, escapeflag);
1590 PRINTCHAR ('>');
1591 break;
1592
1593 default:
1594 goto badtype;
e0f93814 1595 }
00d76abc 1596 break;
38010d50 1597#endif /* standalone */
ca0569ad
RS
1598
1599 default:
00d76abc 1600 badtype:
ca0569ad
RS
1601 {
1602 /* We're in trouble if this happens!
1603 Probably should just abort () */
dc2a0b79 1604 strout ("#<EMACS BUG: INVALID DATATYPE ", -1, -1, printcharfun, 0);
00d76abc 1605 if (MISCP (obj))
5db20f08 1606 sprintf (buf, "(MISC 0x%04x)", (int) XMISCTYPE (obj));
00d76abc
KH
1607 else if (VECTORLIKEP (obj))
1608 sprintf (buf, "(PVEC 0x%08x)", (int) XVECTOR (obj)->size);
1609 else
1610 sprintf (buf, "(0x%02x)", (int) XTYPE (obj));
dc2a0b79 1611 strout (buf, -1, -1, printcharfun, 0);
ca0569ad 1612 strout (" Save your buffers immediately and please report this bug>",
dc2a0b79 1613 -1, -1, printcharfun, 0);
ca0569ad 1614 }
38010d50
JB
1615 }
1616
1617 print_depth--;
1618}
1619\f
7651e1f5
RS
1620#ifdef USE_TEXT_PROPERTIES
1621
1622/* Print a description of INTERVAL using PRINTCHARFUN.
1623 This is part of printing a string that has text properties. */
1624
1625void
1626print_interval (interval, printcharfun)
1627 INTERVAL interval;
1628 Lisp_Object printcharfun;
1629{
30503c0b 1630 PRINTCHAR (' ');
7651e1f5
RS
1631 print (make_number (interval->position), printcharfun, 1);
1632 PRINTCHAR (' ');
1633 print (make_number (interval->position + LENGTH (interval)),
1634 printcharfun, 1);
1635 PRINTCHAR (' ');
1636 print (interval->plist, printcharfun, 1);
7651e1f5
RS
1637}
1638
1639#endif /* USE_TEXT_PROPERTIES */
1640\f
38010d50
JB
1641void
1642syms_of_print ()
1643{
38010d50
JB
1644 DEFVAR_LISP ("standard-output", &Vstandard_output,
1645 "Output stream `print' uses by default for outputting a character.\n\
1646This may be any function of one argument.\n\
1647It may also be a buffer (output is inserted before point)\n\
1648or a marker (output is inserted and the marker is advanced)\n\
113620cc 1649or the symbol t (output appears in the echo area).");
38010d50
JB
1650 Vstandard_output = Qt;
1651 Qstandard_output = intern ("standard-output");
1652 staticpro (&Qstandard_output);
1653
1654#ifdef LISP_FLOAT_TYPE
1655 DEFVAR_LISP ("float-output-format", &Vfloat_output_format,
06ef7355 1656 "The format descriptor string used to print floats.\n\
38010d50
JB
1657This is a %-spec like those accepted by `printf' in C,\n\
1658but with some restrictions. It must start with the two characters `%.'.\n\
1659After that comes an integer precision specification,\n\
1660and then a letter which controls the format.\n\
1661The letters allowed are `e', `f' and `g'.\n\
1662Use `e' for exponential notation \"DIG.DIGITSeEXPT\"\n\
1663Use `f' for decimal point notation \"DIGITS.DIGITS\".\n\
1664Use `g' to choose the shorter of those two formats for the number at hand.\n\
1665The precision in any of these cases is the number of digits following\n\
1666the decimal point. With `f', a precision of 0 means to omit the\n\
c7b14277 1667decimal point. 0 is not allowed with `e' or `g'.\n\n\
f356c3fb
PE
1668A value of nil means to use the shortest notation\n\
1669that represents the number without losing information.");
38010d50
JB
1670 Vfloat_output_format = Qnil;
1671 Qfloat_output_format = intern ("float-output-format");
1672 staticpro (&Qfloat_output_format);
1673#endif /* LISP_FLOAT_TYPE */
1674
1675 DEFVAR_LISP ("print-length", &Vprint_length,
aa734e17 1676 "Maximum length of list to print before abbreviating.\n\
38010d50
JB
1677A value of nil means no limit.");
1678 Vprint_length = Qnil;
1679
1680 DEFVAR_LISP ("print-level", &Vprint_level,
aa734e17 1681 "Maximum depth of list nesting to print before abbreviating.\n\
38010d50
JB
1682A value of nil means no limit.");
1683 Vprint_level = Qnil;
1684
1685 DEFVAR_BOOL ("print-escape-newlines", &print_escape_newlines,
a8920a17 1686 "Non-nil means print newlines in strings as backslash-n.\n\
c6f7982f 1687Also print formfeeds as backslash-f.");
38010d50
JB
1688 print_escape_newlines = 0;
1689
2f100b5c
EN
1690 DEFVAR_BOOL ("print-quoted", &print_quoted,
1691 "Non-nil means print quoted forms with reader syntax.\n\
1692I.e., (quote foo) prints as 'foo, (function foo) as #'foo, and, backquoted\n\
1693forms print in the new syntax.");
1694 print_quoted = 0;
1695
e0f69431 1696 DEFVAR_LISP ("print-gensym", &Vprint_gensym,
081e0581 1697 "Non-nil means print uninterned symbols so they will read as uninterned.\n\
265375e7 1698I.e., the value of (make-symbol \"foobar\") prints as #:foobar.\n\
e0f69431
RS
1699When the uninterned symbol appears within a larger data structure,\n\
1700in addition use the #...# and #...= constructs as needed,\n\
1701so that multiple references to the same symbol are shared once again\n\
1702when the text is read back.\n\
1703\n\
1704If the value of `print-gensym' is a cons cell, then in addition refrain from\n\
1705clearing `print-gensym-alist' on entry to and exit from printing functions,\n\
1706so that the use of #...# and #...= can carry over for several separately\n\
1707printed objects.");
1708 Vprint_gensym = Qnil;
1709
1710 DEFVAR_LISP ("print-gensym-alist", &Vprint_gensym_alist,
1711 "Association list of elements (GENSYM . N) to guide use of #N# and #N=.\n\
1712In each element, GENSYM is an uninterned symbol that has been associated\n\
1713with #N= for the specified value of N.");
1714 Vprint_gensym_alist = Qnil;
081e0581 1715
38010d50
JB
1716 /* prin1_to_string_buffer initialized in init_buffer_once in buffer.c */
1717 staticpro (&Vprin1_to_string_buffer);
1718
1719 defsubr (&Sprin1);
1720 defsubr (&Sprin1_to_string);
113620cc 1721 defsubr (&Serror_message_string);
38010d50
JB
1722 defsubr (&Sprinc);
1723 defsubr (&Sprint);
1724 defsubr (&Sterpri);
1725 defsubr (&Swrite_char);
1726 defsubr (&Sexternal_debugging_output);
1727
1728 Qexternal_debugging_output = intern ("external-debugging-output");
1729 staticpro (&Qexternal_debugging_output);
1730
2f100b5c
EN
1731 Qprint_escape_newlines = intern ("print-escape-newlines");
1732 staticpro (&Qprint_escape_newlines);
1733
38010d50
JB
1734#ifndef standalone
1735 defsubr (&Swith_output_to_temp_buffer);
1736#endif /* not standalone */
1737}