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