(Ftry_completion, Fall_completions, Fcompleting_read): Harmonize arguments
[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
18the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20
18160b98 21#include <config.h>
38010d50 22#include <stdio.h>
38010d50
JB
23#include "lisp.h"
24
25#ifndef standalone
26#include "buffer.h"
0137dbf7 27#include "frame.h"
38010d50
JB
28#include "window.h"
29#include "process.h"
30#include "dispextern.h"
31#include "termchar.h"
077d751f 32#include "keyboard.h"
38010d50
JB
33#endif /* not standalone */
34
7651e1f5
RS
35#ifdef USE_TEXT_PROPERTIES
36#include "intervals.h"
37#endif
38
38010d50
JB
39Lisp_Object Vstandard_output, Qstandard_output;
40
41#ifdef LISP_FLOAT_TYPE
42Lisp_Object Vfloat_output_format, Qfloat_output_format;
43#endif /* LISP_FLOAT_TYPE */
44
45/* Avoid actual stack overflow in print. */
46int print_depth;
47
ec838c39
RS
48/* Detect most circularities to print finite output. */
49#define PRINT_CIRCLE 200
50Lisp_Object being_printed[PRINT_CIRCLE];
51
38010d50
JB
52/* Maximum length of list to print in full; noninteger means
53 effectively infinity */
54
55Lisp_Object Vprint_length;
56
57/* Maximum depth of list to print in full; noninteger means
58 effectively infinity. */
59
60Lisp_Object Vprint_level;
61
62/* Nonzero means print newlines in strings as \n. */
63
64int print_escape_newlines;
65
66Lisp_Object Qprint_escape_newlines;
67
5259c737 68/* Nonzero means print newline to stdout before next minibuffer message.
38010d50
JB
69 Defined in xdisp.c */
70
71extern int noninteractive_need_newline;
5259c737 72
38010d50
JB
73#ifdef MAX_PRINT_CHARS
74static int print_chars;
75static int max_print;
76#endif /* MAX_PRINT_CHARS */
7651e1f5
RS
77
78void print_interval ();
38010d50
JB
79\f
80#if 0
81/* Convert between chars and GLYPHs */
82
83int
84glyphlen (glyphs)
85 register GLYPH *glyphs;
86{
87 register int i = 0;
88
89 while (glyphs[i])
90 i++;
91 return i;
92}
93
94void
95str_to_glyph_cpy (str, glyphs)
96 char *str;
97 GLYPH *glyphs;
98{
99 register GLYPH *gp = glyphs;
100 register char *cp = str;
101
102 while (*cp)
103 *gp++ = *cp++;
104}
105
106void
107str_to_glyph_ncpy (str, glyphs, n)
108 char *str;
109 GLYPH *glyphs;
110 register int n;
111{
112 register GLYPH *gp = glyphs;
113 register char *cp = str;
114
115 while (n-- > 0)
116 *gp++ = *cp++;
117}
118
119void
120glyph_to_str_cpy (glyphs, str)
121 GLYPH *glyphs;
122 char *str;
123{
124 register GLYPH *gp = glyphs;
125 register char *cp = str;
126
127 while (*gp)
128 *str++ = *gp++ & 0377;
129}
130#endif
131\f
eb8c3be9 132/* Low level output routines for characters and strings */
38010d50
JB
133
134/* Lisp functions to do output using a stream
135 must have the stream in a variable called printcharfun
136 and must start with PRINTPREPARE and end with PRINTFINISH.
137 Use PRINTCHAR to output one character,
138 or call strout to output a block of characters.
139 Also, each one must have the declarations
140 struct buffer *old = current_buffer;
141 int old_point = -1, start_point;
142 Lisp_Object original;
143*/
144
cdaa87fd
RS
145#define PRINTPREPARE \
146 original = printcharfun; \
147 if (NILP (printcharfun)) printcharfun = Qt; \
d4ae1f7e 148 if (BUFFERP (printcharfun)) \
cdaa87fd
RS
149 { if (XBUFFER (printcharfun) != current_buffer) \
150 Fset_buffer (printcharfun); \
151 printcharfun = Qnil;} \
d4ae1f7e 152 if (MARKERP (printcharfun)) \
cdaa87fd
RS
153 { if (!(XMARKER (original)->buffer)) \
154 error ("Marker does not point anywhere"); \
155 if (XMARKER (original)->buffer != current_buffer) \
156 set_buffer_internal (XMARKER (original)->buffer); \
157 old_point = point; \
158 SET_PT (marker_position (printcharfun)); \
159 start_point = point; \
38010d50
JB
160 printcharfun = Qnil;}
161
cdaa87fd 162#define PRINTFINISH \
d4ae1f7e 163 if (MARKERP (original)) \
cdaa87fd
RS
164 Fset_marker (original, make_number (point), Qnil); \
165 if (old_point >= 0) \
166 SET_PT (old_point + (old_point >= start_point \
167 ? point - start_point : 0)); \
168 if (old != current_buffer) \
38010d50
JB
169 set_buffer_internal (old)
170
171#define PRINTCHAR(ch) printchar (ch, printcharfun)
172
b8b1b8fd 173/* Index of first unused element of FRAME_MESSAGE_BUF(mini_frame). */
38010d50
JB
174static int printbufidx;
175
176static void
177printchar (ch, fun)
178 unsigned char ch;
179 Lisp_Object fun;
180{
181 Lisp_Object ch1;
182
183#ifdef MAX_PRINT_CHARS
184 if (max_print)
185 print_chars++;
186#endif /* MAX_PRINT_CHARS */
187#ifndef standalone
188 if (EQ (fun, Qnil))
189 {
190 QUIT;
191 insert (&ch, 1);
192 return;
193 }
194
195 if (EQ (fun, Qt))
196 {
c217b048 197 FRAME_PTR mini_frame
b8b1b8fd
RS
198 = XFRAME (WINDOW_FRAME (XWINDOW (minibuf_window)));
199
38010d50
JB
200 if (noninteractive)
201 {
202 putchar (ch);
203 noninteractive_need_newline = 1;
204 return;
205 }
206
b8b1b8fd 207 if (echo_area_glyphs != FRAME_MESSAGE_BUF (mini_frame)
38010d50
JB
208 || !message_buf_print)
209 {
aee72e4f 210 message_log_maybe_newline ();
b8b1b8fd 211 echo_area_glyphs = FRAME_MESSAGE_BUF (mini_frame);
38010d50 212 printbufidx = 0;
708d172a 213 echo_area_glyphs_length = 0;
38010d50
JB
214 message_buf_print = 1;
215 }
216
5259c737 217 message_dolog (&ch, 1, 0);
b8b1b8fd
RS
218 if (printbufidx < FRAME_WIDTH (mini_frame) - 1)
219 FRAME_MESSAGE_BUF (mini_frame)[printbufidx++] = ch;
220 FRAME_MESSAGE_BUF (mini_frame)[printbufidx] = 0;
708d172a 221 echo_area_glyphs_length = printbufidx;
38010d50
JB
222
223 return;
224 }
225#endif /* not standalone */
226
b5d25c37 227 XSETFASTINT (ch1, ch);
38010d50
JB
228 call1 (fun, ch1);
229}
230
231static void
232strout (ptr, size, printcharfun)
233 char *ptr;
234 int size;
235 Lisp_Object printcharfun;
236{
237 int i = 0;
238
239 if (EQ (printcharfun, Qnil))
240 {
241 insert (ptr, size >= 0 ? size : strlen (ptr));
242#ifdef MAX_PRINT_CHARS
243 if (max_print)
244 print_chars += size >= 0 ? size : strlen(ptr);
245#endif /* MAX_PRINT_CHARS */
246 return;
247 }
248 if (EQ (printcharfun, Qt))
249 {
c217b048 250 FRAME_PTR mini_frame
b8b1b8fd
RS
251 = XFRAME (WINDOW_FRAME (XWINDOW (minibuf_window)));
252
38010d50
JB
253 i = size >= 0 ? size : strlen (ptr);
254#ifdef MAX_PRINT_CHARS
255 if (max_print)
256 print_chars += i;
257#endif /* MAX_PRINT_CHARS */
258
259 if (noninteractive)
260 {
261 fwrite (ptr, 1, i, stdout);
262 noninteractive_need_newline = 1;
263 return;
264 }
265
b8b1b8fd 266 if (echo_area_glyphs != FRAME_MESSAGE_BUF (mini_frame)
38010d50
JB
267 || !message_buf_print)
268 {
aee72e4f 269 message_log_maybe_newline ();
b8b1b8fd 270 echo_area_glyphs = FRAME_MESSAGE_BUF (mini_frame);
38010d50 271 printbufidx = 0;
708d172a 272 echo_area_glyphs_length = 0;
38010d50
JB
273 message_buf_print = 1;
274 }
275
5259c737 276 message_dolog (ptr, i, 0);
b8b1b8fd
RS
277 if (i > FRAME_WIDTH (mini_frame) - printbufidx - 1)
278 i = FRAME_WIDTH (mini_frame) - printbufidx - 1;
279 bcopy (ptr, &FRAME_MESSAGE_BUF (mini_frame) [printbufidx], i);
38010d50 280 printbufidx += i;
708d172a 281 echo_area_glyphs_length = printbufidx;
b8b1b8fd 282 FRAME_MESSAGE_BUF (mini_frame) [printbufidx] = 0;
38010d50
JB
283
284 return;
285 }
286
287 if (size >= 0)
288 while (i < size)
289 PRINTCHAR (ptr[i++]);
290 else
291 while (ptr[i])
292 PRINTCHAR (ptr[i++]);
293}
294
295/* Print the contents of a string STRING using PRINTCHARFUN.
ed2c35ef
RS
296 It isn't safe to use strout in many cases,
297 because printing one char can relocate. */
38010d50
JB
298
299print_string (string, printcharfun)
300 Lisp_Object string;
301 Lisp_Object printcharfun;
302{
5e887863
RS
303 if (EQ (printcharfun, Qt))
304 /* strout is safe for output to a frame (echo area). */
38010d50 305 strout (XSTRING (string)->data, XSTRING (string)->size, printcharfun);
5e887863
RS
306 else if (EQ (printcharfun, Qnil))
307 {
308#ifdef MAX_PRINT_CHARS
309 if (max_print)
310 print_chars += XSTRING (string)->size;
311#endif /* MAX_PRINT_CHARS */
312 insert_from_string (string, 0, XSTRING (string)->size, 1);
313 }
38010d50
JB
314 else
315 {
316 /* Otherwise, fetch the string address for each character. */
317 int i;
318 int size = XSTRING (string)->size;
319 struct gcpro gcpro1;
320 GCPRO1 (string);
321 for (i = 0; i < size; i++)
322 PRINTCHAR (XSTRING (string)->data[i]);
323 UNGCPRO;
324 }
325}
326\f
327DEFUN ("write-char", Fwrite_char, Swrite_char, 1, 2, 0,
57c9eb68
KH
328 "Output character CHAR to stream PRINTCHARFUN.\n\
329PRINTCHARFUN defaults to the value of `standard-output' (which see).")
38010d50
JB
330 (ch, printcharfun)
331 Lisp_Object ch, printcharfun;
332{
333 struct buffer *old = current_buffer;
334 int old_point = -1;
335 int start_point;
336 Lisp_Object original;
337
10eebdbb 338 if (NILP (printcharfun))
38010d50
JB
339 printcharfun = Vstandard_output;
340 CHECK_NUMBER (ch, 0);
341 PRINTPREPARE;
342 PRINTCHAR (XINT (ch));
343 PRINTFINISH;
344 return ch;
345}
346
347/* Used from outside of print.c to print a block of SIZE chars at DATA
348 on the default output stream.
349 Do not use this on the contents of a Lisp string. */
350
351write_string (data, size)
352 char *data;
353 int size;
354{
355 struct buffer *old = current_buffer;
356 Lisp_Object printcharfun;
357 int old_point = -1;
358 int start_point;
359 Lisp_Object original;
360
361 printcharfun = Vstandard_output;
362
363 PRINTPREPARE;
364 strout (data, size, printcharfun);
365 PRINTFINISH;
366}
367
368/* Used from outside of print.c to print a block of SIZE chars at DATA
369 on a specified stream PRINTCHARFUN.
370 Do not use this on the contents of a Lisp string. */
371
372write_string_1 (data, size, printcharfun)
373 char *data;
374 int size;
375 Lisp_Object printcharfun;
376{
377 struct buffer *old = current_buffer;
378 int old_point = -1;
379 int start_point;
380 Lisp_Object original;
381
382 PRINTPREPARE;
383 strout (data, size, printcharfun);
384 PRINTFINISH;
385}
386
387
388#ifndef standalone
389
390void
391temp_output_buffer_setup (bufname)
392 char *bufname;
393{
394 register struct buffer *old = current_buffer;
395 register Lisp_Object buf;
396
397 Fset_buffer (Fget_buffer_create (build_string (bufname)));
398
2a1c968a 399 current_buffer->directory = old->directory;
38010d50
JB
400 current_buffer->read_only = Qnil;
401 Ferase_buffer ();
402
633307b5 403 XSETBUFFER (buf, current_buffer);
38010d50
JB
404 specbind (Qstandard_output, buf);
405
406 set_buffer_internal (old);
407}
408
409Lisp_Object
410internal_with_output_to_temp_buffer (bufname, function, args)
411 char *bufname;
412 Lisp_Object (*function) ();
413 Lisp_Object args;
414{
415 int count = specpdl_ptr - specpdl;
416 Lisp_Object buf, val;
0ab39c81 417 struct gcpro gcpro1;
38010d50 418
0ab39c81 419 GCPRO1 (args);
38010d50
JB
420 record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
421 temp_output_buffer_setup (bufname);
422 buf = Vstandard_output;
0ab39c81 423 UNGCPRO;
38010d50
JB
424
425 val = (*function) (args);
426
0ab39c81 427 GCPRO1 (val);
38010d50 428 temp_output_buffer_show (buf);
0ab39c81 429 UNGCPRO;
38010d50
JB
430
431 return unbind_to (count, val);
432}
433
434DEFUN ("with-output-to-temp-buffer", Fwith_output_to_temp_buffer, Swith_output_to_temp_buffer,
435 1, UNEVALLED, 0,
436 "Bind `standard-output' to buffer BUFNAME, eval BODY, then show that buffer.\n\
437The buffer is cleared out initially, and marked as unmodified when done.\n\
438All output done by BODY is inserted in that buffer by default.\n\
439The buffer is displayed in another window, but not selected.\n\
440The value of the last form in BODY is returned.\n\
441If BODY does not finish normally, the buffer BUFNAME is not displayed.\n\n\
483288d7 442If variable `temp-buffer-show-function' is non-nil, call it at the end\n\
38010d50
JB
443to get the buffer displayed. It gets one argument, the buffer to display.")
444 (args)
445 Lisp_Object args;
446{
447 struct gcpro gcpro1;
448 Lisp_Object name;
449 int count = specpdl_ptr - specpdl;
450 Lisp_Object buf, val;
451
452 GCPRO1(args);
453 name = Feval (Fcar (args));
454 UNGCPRO;
455
456 CHECK_STRING (name, 0);
457 temp_output_buffer_setup (XSTRING (name)->data);
458 buf = Vstandard_output;
459
460 val = Fprogn (Fcdr (args));
461
462 temp_output_buffer_show (buf);
463
464 return unbind_to (count, val);
465}
466#endif /* not standalone */
467\f
468static void print ();
469
470DEFUN ("terpri", Fterpri, Sterpri, 0, 1, 0,
57c9eb68
KH
471 "Output a newline to stream PRINTCHARFUN.\n\
472If PRINTCHARFUN is omitted or nil, the value of `standard-output' is used.")
38010d50
JB
473 (printcharfun)
474 Lisp_Object printcharfun;
475{
476 struct buffer *old = current_buffer;
477 int old_point = -1;
478 int start_point;
479 Lisp_Object original;
480
10eebdbb 481 if (NILP (printcharfun))
38010d50
JB
482 printcharfun = Vstandard_output;
483 PRINTPREPARE;
484 PRINTCHAR ('\n');
485 PRINTFINISH;
486 return Qt;
487}
488
489DEFUN ("prin1", Fprin1, Sprin1, 1, 2, 0,
490 "Output the printed representation of OBJECT, any Lisp object.\n\
491Quoting characters are printed when needed to make output that `read'\n\
492can handle, whenever this is possible.\n\
57c9eb68 493Output stream is PRINTCHARFUN, or value of `standard-output' (which see).")
38010d50
JB
494 (obj, printcharfun)
495 Lisp_Object obj, printcharfun;
496{
497 struct buffer *old = current_buffer;
498 int old_point = -1;
499 int start_point;
500 Lisp_Object original;
501
502#ifdef MAX_PRINT_CHARS
503 max_print = 0;
504#endif /* MAX_PRINT_CHARS */
10eebdbb 505 if (NILP (printcharfun))
38010d50
JB
506 printcharfun = Vstandard_output;
507 PRINTPREPARE;
508 print_depth = 0;
509 print (obj, printcharfun, 1);
510 PRINTFINISH;
511 return obj;
512}
513
514/* a buffer which is used to hold output being built by prin1-to-string */
515Lisp_Object Vprin1_to_string_buffer;
516
517DEFUN ("prin1-to-string", Fprin1_to_string, Sprin1_to_string, 1, 2, 0,
518 "Return a string containing the printed representation of OBJECT,\n\
519any Lisp object. Quoting characters are used when needed to make output\n\
520that `read' can handle, whenever this is possible, unless the optional\n\
521second argument NOESCAPE is non-nil.")
522 (obj, noescape)
523 Lisp_Object obj, noescape;
524{
525 struct buffer *old = current_buffer;
526 int old_point = -1;
527 int start_point;
528 Lisp_Object original, printcharfun;
529 struct gcpro gcpro1;
530
531 printcharfun = Vprin1_to_string_buffer;
532 PRINTPREPARE;
533 print_depth = 0;
10eebdbb 534 print (obj, printcharfun, NILP (noescape));
38010d50
JB
535 /* Make Vprin1_to_string_buffer be the default buffer after PRINTFINSH */
536 PRINTFINISH;
537 set_buffer_internal (XBUFFER (Vprin1_to_string_buffer));
538 obj = Fbuffer_string ();
539
540 GCPRO1 (obj);
541 Ferase_buffer ();
542 set_buffer_internal (old);
543 UNGCPRO;
544
545 return obj;
546}
547
548DEFUN ("princ", Fprinc, Sprinc, 1, 2, 0,
549 "Output the printed representation of OBJECT, any Lisp object.\n\
550No quoting characters are used; no delimiters are printed around\n\
551the contents of strings.\n\
57c9eb68 552Output stream is PRINTCHARFUN, or value of standard-output (which see).")
38010d50
JB
553 (obj, printcharfun)
554 Lisp_Object obj, printcharfun;
555{
556 struct buffer *old = current_buffer;
557 int old_point = -1;
558 int start_point;
559 Lisp_Object original;
560
10eebdbb 561 if (NILP (printcharfun))
38010d50
JB
562 printcharfun = Vstandard_output;
563 PRINTPREPARE;
564 print_depth = 0;
565 print (obj, printcharfun, 0);
566 PRINTFINISH;
567 return obj;
568}
569
570DEFUN ("print", Fprint, Sprint, 1, 2, 0,
571 "Output the printed representation of OBJECT, with newlines around it.\n\
572Quoting characters are printed when needed to make output that `read'\n\
573can handle, whenever this is possible.\n\
57c9eb68 574Output stream is PRINTCHARFUN, or value of `standard-output' (which see).")
38010d50
JB
575 (obj, printcharfun)
576 Lisp_Object obj, printcharfun;
577{
578 struct buffer *old = current_buffer;
579 int old_point = -1;
580 int start_point;
581 Lisp_Object original;
582 struct gcpro gcpro1;
583
584#ifdef MAX_PRINT_CHARS
585 print_chars = 0;
586 max_print = MAX_PRINT_CHARS;
587#endif /* MAX_PRINT_CHARS */
10eebdbb 588 if (NILP (printcharfun))
38010d50
JB
589 printcharfun = Vstandard_output;
590 GCPRO1 (obj);
591 PRINTPREPARE;
592 print_depth = 0;
593 PRINTCHAR ('\n');
594 print (obj, printcharfun, 1);
595 PRINTCHAR ('\n');
596 PRINTFINISH;
597#ifdef MAX_PRINT_CHARS
598 max_print = 0;
599 print_chars = 0;
600#endif /* MAX_PRINT_CHARS */
601 UNGCPRO;
602 return obj;
603}
604
605/* The subroutine object for external-debugging-output is kept here
606 for the convenience of the debugger. */
607Lisp_Object Qexternal_debugging_output;
608
4746118a
JB
609DEFUN ("external-debugging-output", Fexternal_debugging_output, Sexternal_debugging_output, 1, 1, 0,
610 "Write CHARACTER to stderr.\n\
38010d50
JB
611You can call print while debugging emacs, and pass it this function\n\
612to make it write to the debugging output.\n")
4746118a
JB
613 (character)
614 Lisp_Object character;
38010d50
JB
615{
616 CHECK_NUMBER (character, 0);
617 putc (XINT (character), stderr);
618
619 return character;
620}
cf1bb91b
RS
621
622/* This is the interface for debugging printing. */
623
624void
625debug_print (arg)
626 Lisp_Object arg;
627{
628 Fprin1 (arg, Qexternal_debugging_output);
3684eb78 629 fprintf (stderr, "\r\n");
cf1bb91b 630}
38010d50 631\f
113620cc
KH
632DEFUN ("error-message-string", Ferror_message_string, Serror_message_string,
633 1, 1, 0,
634 "Convert an error value (ERROR-SYMBOL . DATA) to an error message.")
635 (obj)
636 Lisp_Object obj;
637{
638 struct buffer *old = current_buffer;
639 Lisp_Object original, printcharfun, value;
640 struct gcpro gcpro1;
641
642 print_error_message (obj, Vprin1_to_string_buffer, NULL);
643
644 set_buffer_internal (XBUFFER (Vprin1_to_string_buffer));
645 value = Fbuffer_string ();
646
647 GCPRO1 (value);
648 Ferase_buffer ();
649 set_buffer_internal (old);
650 UNGCPRO;
651
652 return value;
653}
654
655/* Print an error message for the error DATA
656 onto Lisp output stream STREAM (suitable for the print functions). */
657
658print_error_message (data, stream)
659 Lisp_Object data, stream;
660{
661 Lisp_Object errname, errmsg, file_error, tail;
662 struct gcpro gcpro1;
663 int i;
664
665 errname = Fcar (data);
666
667 if (EQ (errname, Qerror))
668 {
669 data = Fcdr (data);
670 if (!CONSP (data)) data = Qnil;
671 errmsg = Fcar (data);
672 file_error = Qnil;
673 }
674 else
675 {
676 errmsg = Fget (errname, Qerror_message);
677 file_error = Fmemq (Qfile_error,
678 Fget (errname, Qerror_conditions));
679 }
680
681 /* Print an error message including the data items. */
682
683 tail = Fcdr_safe (data);
684 GCPRO1 (tail);
685
686 /* For file-error, make error message by concatenating
687 all the data items. They are all strings. */
688 if (!NILP (file_error) && !NILP (tail))
689 errmsg = XCONS (tail)->car, tail = XCONS (tail)->cdr;
690
691 if (STRINGP (errmsg))
692 Fprinc (errmsg, stream);
693 else
694 write_string_1 ("peculiar error", -1, stream);
695
696 for (i = 0; CONSP (tail); tail = Fcdr (tail), i++)
697 {
698 write_string_1 (i ? ", " : ": ", 2, stream);
699 if (!NILP (file_error))
700 Fprinc (Fcar (tail), stream);
701 else
702 Fprin1 (Fcar (tail), stream);
703 }
704 UNGCPRO;
705}
706\f
38010d50
JB
707#ifdef LISP_FLOAT_TYPE
708
38010d50 709/*
edb2a707 710 * The buffer should be at least as large as the max string size of the
8e6208c5 711 * largest float, printed in the biggest notation. This is undoubtedly
38010d50
JB
712 * 20d float_output_format, with the negative of the C-constant "HUGE"
713 * from <math.h>.
714 *
715 * On the vax the worst case is -1e38 in 20d format which takes 61 bytes.
716 *
717 * I assume that IEEE-754 format numbers can take 329 bytes for the worst
718 * case of -1e307 in 20d float_output_format. What is one to do (short of
719 * re-writing _doprnt to be more sane)?
720 * -wsr
721 */
edb2a707
RS
722
723void
724float_to_string (buf, data)
8b24d146 725 unsigned char *buf;
38010d50
JB
726 double data;
727{
c7b14277 728 unsigned char *cp;
322890c4 729 int width;
38010d50 730
10eebdbb 731 if (NILP (Vfloat_output_format)
d4ae1f7e 732 || !STRINGP (Vfloat_output_format))
38010d50 733 lose:
322890c4
RS
734 {
735 sprintf (buf, "%.17g", data);
736 width = -1;
737 }
38010d50
JB
738 else /* oink oink */
739 {
740 /* Check that the spec we have is fully valid.
741 This means not only valid for printf,
742 but meant for floats, and reasonable. */
743 cp = XSTRING (Vfloat_output_format)->data;
744
745 if (cp[0] != '%')
746 goto lose;
747 if (cp[1] != '.')
748 goto lose;
749
750 cp += 2;
c7b14277
JB
751
752 /* Check the width specification. */
322890c4 753 width = -1;
c7b14277 754 if ('0' <= *cp && *cp <= '9')
381cd4bb
KH
755 {
756 width = 0;
757 do
758 width = (width * 10) + (*cp++ - '0');
759 while (*cp >= '0' && *cp <= '9');
760
761 /* A precision of zero is valid only for %f. */
762 if (width > DBL_DIG
763 || (width == 0 && *cp != 'f'))
764 goto lose;
765 }
38010d50
JB
766
767 if (*cp != 'e' && *cp != 'f' && *cp != 'g')
768 goto lose;
769
38010d50
JB
770 if (cp[1] != 0)
771 goto lose;
772
773 sprintf (buf, XSTRING (Vfloat_output_format)->data, data);
774 }
edb2a707 775
c7b14277
JB
776 /* Make sure there is a decimal point with digit after, or an
777 exponent, so that the value is readable as a float. But don't do
322890c4
RS
778 this with "%.0f"; it's valid for that not to produce a decimal
779 point. Note that width can be 0 only for %.0f. */
780 if (width != 0)
0601fd3d 781 {
c7b14277
JB
782 for (cp = buf; *cp; cp++)
783 if ((*cp < '0' || *cp > '9') && *cp != '-')
784 break;
0601fd3d 785
c7b14277
JB
786 if (*cp == '.' && cp[1] == 0)
787 {
788 cp[1] = '0';
789 cp[2] = 0;
790 }
791
792 if (*cp == 0)
793 {
794 *cp++ = '.';
795 *cp++ = '0';
796 *cp++ = 0;
797 }
edb2a707 798 }
38010d50
JB
799}
800#endif /* LISP_FLOAT_TYPE */
801\f
802static void
803print (obj, printcharfun, escapeflag)
38010d50 804 Lisp_Object obj;
38010d50
JB
805 register Lisp_Object printcharfun;
806 int escapeflag;
807{
808 char buf[30];
809
810 QUIT;
811
ec838c39
RS
812#if 1 /* I'm not sure this is really worth doing. */
813 /* Detect circularities and truncate them.
814 No need to offer any alternative--this is better than an error. */
d4ae1f7e 815 if (CONSP (obj) || VECTORP (obj) || COMPILEDP (obj))
ec838c39
RS
816 {
817 int i;
818 for (i = 0; i < print_depth; i++)
819 if (EQ (obj, being_printed[i]))
820 {
821 sprintf (buf, "#%d", i);
822 strout (buf, -1, printcharfun);
823 return;
824 }
825 }
826#endif
827
828 being_printed[print_depth] = obj;
38010d50
JB
829 print_depth++;
830
ec838c39 831 if (print_depth > PRINT_CIRCLE)
38010d50
JB
832 error ("Apparently circular structure being printed");
833#ifdef MAX_PRINT_CHARS
834 if (max_print && print_chars > max_print)
835 {
836 PRINTCHAR ('\n');
837 print_chars = 0;
838 }
839#endif /* MAX_PRINT_CHARS */
840
ca0569ad 841 switch (XGCTYPE (obj))
38010d50 842 {
ca0569ad 843 case Lisp_Int:
b8180922
RS
844 if (sizeof (int) == sizeof (EMACS_INT))
845 sprintf (buf, "%d", XINT (obj));
846 else if (sizeof (long) == sizeof (EMACS_INT))
847 sprintf (buf, "%ld", XINT (obj));
848 else
849 abort ();
38010d50 850 strout (buf, -1, printcharfun);
ca0569ad
RS
851 break;
852
e0f93814 853#ifdef LISP_FLOAT_TYPE
ca0569ad
RS
854 case Lisp_Float:
855 {
856 char pigbuf[350]; /* see comments in float_to_string */
38010d50 857
ca0569ad
RS
858 float_to_string (pigbuf, XFLOAT(obj)->data);
859 strout (pigbuf, -1, printcharfun);
860 }
861 break;
e0f93814 862#endif
ca0569ad
RS
863
864 case Lisp_String:
38010d50
JB
865 if (!escapeflag)
866 print_string (obj, printcharfun);
867 else
868 {
869 register int i;
870 register unsigned char c;
38010d50
JB
871 struct gcpro gcpro1;
872
7651e1f5
RS
873 GCPRO1 (obj);
874
875#ifdef USE_TEXT_PROPERTIES
876 if (!NULL_INTERVAL_P (XSTRING (obj)->intervals))
877 {
878 PRINTCHAR ('#');
879 PRINTCHAR ('(');
880 }
881#endif
38010d50
JB
882
883 PRINTCHAR ('\"');
884 for (i = 0; i < XSTRING (obj)->size; i++)
885 {
886 QUIT;
887 c = XSTRING (obj)->data[i];
888 if (c == '\n' && print_escape_newlines)
889 {
890 PRINTCHAR ('\\');
891 PRINTCHAR ('n');
892 }
c6f7982f
RM
893 else if (c == '\f' && print_escape_newlines)
894 {
895 PRINTCHAR ('\\');
896 PRINTCHAR ('f');
897 }
38010d50
JB
898 else
899 {
900 if (c == '\"' || c == '\\')
901 PRINTCHAR ('\\');
902 PRINTCHAR (c);
903 }
904 }
905 PRINTCHAR ('\"');
7651e1f5
RS
906
907#ifdef USE_TEXT_PROPERTIES
908 if (!NULL_INTERVAL_P (XSTRING (obj)->intervals))
909 {
7651e1f5
RS
910 traverse_intervals (XSTRING (obj)->intervals,
911 0, 0, print_interval, printcharfun);
912 PRINTCHAR (')');
913 }
914#endif
915
38010d50
JB
916 UNGCPRO;
917 }
ca0569ad 918 break;
38010d50 919
ca0569ad
RS
920 case Lisp_Symbol:
921 {
922 register int confusing;
923 register unsigned char *p = XSYMBOL (obj)->name->data;
924 register unsigned char *end = p + XSYMBOL (obj)->name->size;
925 register unsigned char c;
926
927 if (p != end && (*p == '-' || *p == '+')) p++;
928 if (p == end)
929 confusing = 0;
930 else
931 {
932 while (p != end && *p >= '0' && *p <= '9')
933 p++;
934 confusing = (end == p);
935 }
936
937 p = XSYMBOL (obj)->name->data;
938 while (p != end)
939 {
940 QUIT;
941 c = *p++;
942 if (escapeflag)
943 {
944 if (c == '\"' || c == '\\' || c == '\'' || c == ';' || c == '#' ||
945 c == '(' || c == ')' || c == ',' || c =='.' || c == '`' ||
946 c == '[' || c == ']' || c == '?' || c <= 040 || confusing)
947 PRINTCHAR ('\\'), confusing = 0;
948 }
949 PRINTCHAR (c);
950 }
951 }
952 break;
953
954 case Lisp_Cons:
38010d50 955 /* If deeper than spec'd depth, print placeholder. */
d4ae1f7e 956 if (INTEGERP (Vprint_level)
38010d50 957 && print_depth > XINT (Vprint_level))
e0f93814
KH
958 strout ("...", -1, printcharfun);
959 else
38010d50 960 {
e0f93814 961 PRINTCHAR ('(');
38010d50 962 {
e0f93814
KH
963 register int i = 0;
964 register int max = 0;
965
966 if (INTEGERP (Vprint_length))
967 max = XINT (Vprint_length);
968 /* Could recognize circularities in cdrs here,
969 but that would make printing of long lists quadratic.
970 It's not worth doing. */
971 while (CONSP (obj))
38010d50 972 {
e0f93814
KH
973 if (i++)
974 PRINTCHAR (' ');
975 if (max && i > max)
976 {
977 strout ("...", 3, printcharfun);
978 break;
979 }
980 print (Fcar (obj), printcharfun, escapeflag);
981 obj = Fcdr (obj);
38010d50 982 }
38010d50 983 }
e0f93814
KH
984 if (!NILP (obj) && !CONSP (obj))
985 {
986 strout (" . ", 3, printcharfun);
987 print (obj, printcharfun, escapeflag);
988 }
989 PRINTCHAR (')');
38010d50 990 }
ca0569ad
RS
991 break;
992
993 case Lisp_Vectorlike:
994 if (PROCESSP (obj))
995 {
996 if (escapeflag)
997 {
998 strout ("#<process ", -1, printcharfun);
999 print_string (XPROCESS (obj)->name, printcharfun);
1000 PRINTCHAR ('>');
1001 }
1002 else
1003 print_string (XPROCESS (obj)->name, printcharfun);
1004 }
ed2c35ef
RS
1005 else if (BOOL_VECTOR_P (obj))
1006 {
1007 register int i;
1008 register unsigned char c;
1009 struct gcpro gcpro1;
ed2c35ef 1010 int size_in_chars
68be917d 1011 = (XBOOL_VECTOR (obj)->size + BITS_PER_CHAR) / BITS_PER_CHAR;
ed2c35ef
RS
1012
1013 GCPRO1 (obj);
1014
1015 PRINTCHAR ('#');
1016 PRINTCHAR ('&');
1017 sprintf (buf, "%d", XBOOL_VECTOR (obj)->size);
1018 strout (buf, -1, printcharfun);
1019 PRINTCHAR ('\"');
1020 for (i = 0; i < size_in_chars; i++)
1021 {
1022 QUIT;
1023 c = XBOOL_VECTOR (obj)->data[i];
1024 if (c == '\n' && print_escape_newlines)
1025 {
1026 PRINTCHAR ('\\');
1027 PRINTCHAR ('n');
1028 }
1029 else if (c == '\f' && print_escape_newlines)
1030 {
1031 PRINTCHAR ('\\');
1032 PRINTCHAR ('f');
1033 }
1034 else
1035 {
1036 if (c == '\"' || c == '\\')
1037 PRINTCHAR ('\\');
1038 PRINTCHAR (c);
1039 }
1040 }
1041 PRINTCHAR ('\"');
1042
1043 UNGCPRO;
1044 }
ca0569ad
RS
1045 else if (SUBRP (obj))
1046 {
1047 strout ("#<subr ", -1, printcharfun);
1048 strout (XSUBR (obj)->symbol_name, -1, printcharfun);
1049 PRINTCHAR ('>');
1050 }
1051#ifndef standalone
1052 else if (WINDOWP (obj))
1053 {
1054 strout ("#<window ", -1, printcharfun);
1055 sprintf (buf, "%d", XFASTINT (XWINDOW (obj)->sequence_number));
1056 strout (buf, -1, printcharfun);
1057 if (!NILP (XWINDOW (obj)->buffer))
1058 {
1059 strout (" on ", -1, printcharfun);
1060 print_string (XBUFFER (XWINDOW (obj)->buffer)->name, printcharfun);
1061 }
1062 PRINTCHAR ('>');
1063 }
908b0ae5
RS
1064 else if (BUFFERP (obj))
1065 {
1066 if (NILP (XBUFFER (obj)->name))
1067 strout ("#<killed buffer>", -1, printcharfun);
1068 else if (escapeflag)
1069 {
1070 strout ("#<buffer ", -1, printcharfun);
1071 print_string (XBUFFER (obj)->name, printcharfun);
1072 PRINTCHAR ('>');
1073 }
1074 else
1075 print_string (XBUFFER (obj)->name, printcharfun);
1076 }
ca0569ad
RS
1077 else if (WINDOW_CONFIGURATIONP (obj))
1078 {
1079 strout ("#<window-configuration>", -1, printcharfun);
1080 }
1081#ifdef MULTI_FRAME
1082 else if (FRAMEP (obj))
1083 {
1084 strout ((FRAME_LIVE_P (XFRAME (obj))
1085 ? "#<frame " : "#<dead frame "),
1086 -1, printcharfun);
1087 print_string (XFRAME (obj)->name, printcharfun);
1088 sprintf (buf, " 0x%lx", (unsigned long) (XFRAME (obj)));
1089 strout (buf, -1, printcharfun);
1090 PRINTCHAR ('>');
1091 }
1092#endif
1093#endif /* not standalone */
1094 else
1095 {
1096 int size = XVECTOR (obj)->size;
1097 if (COMPILEDP (obj))
1098 {
1099 PRINTCHAR ('#');
1100 size &= PSEUDOVECTOR_SIZE_MASK;
1101 }
ed2c35ef
RS
1102 if (CHAR_TABLE_P (obj))
1103 {
1104 /* We print a char-table as if it were a vector,
1105 lumping the parent and default slots in with the
1106 character slots. But we add #^ as a prefix. */
1107 PRINTCHAR ('#');
1108 PRINTCHAR ('^');
1109 size &= PSEUDOVECTOR_SIZE_MASK;
1110 }
00d76abc
KH
1111 if (size & PSEUDOVECTOR_FLAG)
1112 goto badtype;
ca0569ad
RS
1113
1114 PRINTCHAR ('[');
38010d50 1115 {
ca0569ad
RS
1116 register int i;
1117 register Lisp_Object tem;
1118 for (i = 0; i < size; i++)
1119 {
1120 if (i) PRINTCHAR (' ');
1121 tem = XVECTOR (obj)->contents[i];
1122 print (tem, printcharfun, escapeflag);
1123 }
38010d50 1124 }
ca0569ad
RS
1125 PRINTCHAR (']');
1126 }
1127 break;
1128
38010d50 1129#ifndef standalone
ca0569ad 1130 case Lisp_Misc:
5db20f08 1131 switch (XMISCTYPE (obj))
38010d50 1132 {
00d76abc 1133 case Lisp_Misc_Marker:
ca0569ad
RS
1134 strout ("#<marker ", -1, printcharfun);
1135 if (!(XMARKER (obj)->buffer))
1136 strout ("in no buffer", -1, printcharfun);
1137 else
1138 {
1139 sprintf (buf, "at %d", marker_position (obj));
1140 strout (buf, -1, printcharfun);
1141 strout (" in ", -1, printcharfun);
1142 print_string (XMARKER (obj)->buffer->name, printcharfun);
1143 }
38010d50 1144 PRINTCHAR ('>');
908b0ae5 1145 break;
00d76abc
KH
1146
1147 case Lisp_Misc_Overlay:
ca0569ad
RS
1148 strout ("#<overlay ", -1, printcharfun);
1149 if (!(XMARKER (OVERLAY_START (obj))->buffer))
1150 strout ("in no buffer", -1, printcharfun);
1151 else
1152 {
1153 sprintf (buf, "from %d to %d in ",
1154 marker_position (OVERLAY_START (obj)),
1155 marker_position (OVERLAY_END (obj)));
1156 strout (buf, -1, printcharfun);
1157 print_string (XMARKER (OVERLAY_START (obj))->buffer->name,
1158 printcharfun);
1159 }
1160 PRINTCHAR ('>');
908b0ae5 1161 break;
00d76abc
KH
1162
1163 /* Remaining cases shouldn't happen in normal usage, but let's print
1164 them anyway for the benefit of the debugger. */
1165 case Lisp_Misc_Free:
1166 strout ("#<misc free cell>", -1, printcharfun);
1167 break;
1168
1169 case Lisp_Misc_Intfwd:
1170 sprintf (buf, "#<intfwd to %d>", *XINTFWD (obj)->intvar);
1171 strout (buf, -1, printcharfun);
1172 break;
1173
1174 case Lisp_Misc_Boolfwd:
1175 sprintf (buf, "#<boolfwd to %s>",
1176 (*XBOOLFWD (obj)->boolvar ? "t" : "nil"));
1177 strout (buf, -1, printcharfun);
1178 break;
1179
1180 case Lisp_Misc_Objfwd:
1181 strout (buf, "#<objfwd to ", -1, printcharfun);
1182 print (*XOBJFWD (obj)->objvar, printcharfun, escapeflag);
1183 PRINTCHAR ('>');
1184 break;
1185
1186 case Lisp_Misc_Buffer_Objfwd:
1187 strout (buf, "#<buffer_objfwd to ", -1, printcharfun);
3ac613c1
KH
1188 print (*(Lisp_Object *)((char *)current_buffer
1189 + XBUFFER_OBJFWD (obj)->offset),
1190 printcharfun, escapeflag);
1191 PRINTCHAR ('>');
1192 break;
1193
fb917148
KH
1194 case Lisp_Misc_Kboard_Objfwd:
1195 strout (buf, "#<kboard_objfwd to ", -1, printcharfun);
1196 print (*(Lisp_Object *)((char *) current_kboard
1197 + XKBOARD_OBJFWD (obj)->offset),
7ae137a9 1198 printcharfun, escapeflag);
00d76abc
KH
1199 PRINTCHAR ('>');
1200 break;
1201
1202 case Lisp_Misc_Buffer_Local_Value:
1203 strout ("#<buffer_local_value ", -1, printcharfun);
1204 goto do_buffer_local;
1205 case Lisp_Misc_Some_Buffer_Local_Value:
1206 strout ("#<some_buffer_local_value ", -1, printcharfun);
1207 do_buffer_local:
1208 strout ("[realvalue] ", -1, printcharfun);
1209 print (XBUFFER_LOCAL_VALUE (obj)->car, printcharfun, escapeflag);
1210 strout ("[buffer] ", -1, printcharfun);
1211 print (XCONS (XBUFFER_LOCAL_VALUE (obj)->cdr)->car,
1212 printcharfun, escapeflag);
1213 strout ("[alist-elt] ", -1, printcharfun);
1214 print (XCONS (XCONS (XBUFFER_LOCAL_VALUE (obj)->cdr)->cdr)->car,
1215 printcharfun, escapeflag);
1216 strout ("[default-value] ", -1, printcharfun);
1217 print (XCONS (XCONS (XBUFFER_LOCAL_VALUE (obj)->cdr)->cdr)->cdr,
1218 printcharfun, escapeflag);
1219 PRINTCHAR ('>');
1220 break;
1221
1222 default:
1223 goto badtype;
e0f93814 1224 }
00d76abc 1225 break;
38010d50 1226#endif /* standalone */
ca0569ad
RS
1227
1228 default:
00d76abc 1229 badtype:
ca0569ad
RS
1230 {
1231 /* We're in trouble if this happens!
1232 Probably should just abort () */
1233 strout ("#<EMACS BUG: INVALID DATATYPE ", -1, printcharfun);
00d76abc 1234 if (MISCP (obj))
5db20f08 1235 sprintf (buf, "(MISC 0x%04x)", (int) XMISCTYPE (obj));
00d76abc
KH
1236 else if (VECTORLIKEP (obj))
1237 sprintf (buf, "(PVEC 0x%08x)", (int) XVECTOR (obj)->size);
1238 else
1239 sprintf (buf, "(0x%02x)", (int) XTYPE (obj));
ca0569ad
RS
1240 strout (buf, -1, printcharfun);
1241 strout (" Save your buffers immediately and please report this bug>",
1242 -1, printcharfun);
1243 }
38010d50
JB
1244 }
1245
1246 print_depth--;
1247}
1248\f
7651e1f5
RS
1249#ifdef USE_TEXT_PROPERTIES
1250
1251/* Print a description of INTERVAL using PRINTCHARFUN.
1252 This is part of printing a string that has text properties. */
1253
1254void
1255print_interval (interval, printcharfun)
1256 INTERVAL interval;
1257 Lisp_Object printcharfun;
1258{
30503c0b 1259 PRINTCHAR (' ');
7651e1f5
RS
1260 print (make_number (interval->position), printcharfun, 1);
1261 PRINTCHAR (' ');
1262 print (make_number (interval->position + LENGTH (interval)),
1263 printcharfun, 1);
1264 PRINTCHAR (' ');
1265 print (interval->plist, printcharfun, 1);
7651e1f5
RS
1266}
1267
1268#endif /* USE_TEXT_PROPERTIES */
1269\f
38010d50
JB
1270void
1271syms_of_print ()
1272{
1273 staticpro (&Qprint_escape_newlines);
1274 Qprint_escape_newlines = intern ("print-escape-newlines");
1275
1276 DEFVAR_LISP ("standard-output", &Vstandard_output,
1277 "Output stream `print' uses by default for outputting a character.\n\
1278This may be any function of one argument.\n\
1279It may also be a buffer (output is inserted before point)\n\
1280or a marker (output is inserted and the marker is advanced)\n\
113620cc 1281or the symbol t (output appears in the echo area).");
38010d50
JB
1282 Vstandard_output = Qt;
1283 Qstandard_output = intern ("standard-output");
1284 staticpro (&Qstandard_output);
1285
1286#ifdef LISP_FLOAT_TYPE
1287 DEFVAR_LISP ("float-output-format", &Vfloat_output_format,
06ef7355 1288 "The format descriptor string used to print floats.\n\
38010d50
JB
1289This is a %-spec like those accepted by `printf' in C,\n\
1290but with some restrictions. It must start with the two characters `%.'.\n\
1291After that comes an integer precision specification,\n\
1292and then a letter which controls the format.\n\
1293The letters allowed are `e', `f' and `g'.\n\
1294Use `e' for exponential notation \"DIG.DIGITSeEXPT\"\n\
1295Use `f' for decimal point notation \"DIGITS.DIGITS\".\n\
1296Use `g' to choose the shorter of those two formats for the number at hand.\n\
1297The precision in any of these cases is the number of digits following\n\
1298the decimal point. With `f', a precision of 0 means to omit the\n\
c7b14277 1299decimal point. 0 is not allowed with `e' or `g'.\n\n\
322890c4 1300A value of nil means to use `%.17g'.");
38010d50
JB
1301 Vfloat_output_format = Qnil;
1302 Qfloat_output_format = intern ("float-output-format");
1303 staticpro (&Qfloat_output_format);
1304#endif /* LISP_FLOAT_TYPE */
1305
1306 DEFVAR_LISP ("print-length", &Vprint_length,
aa734e17 1307 "Maximum length of list to print before abbreviating.\n\
38010d50
JB
1308A value of nil means no limit.");
1309 Vprint_length = Qnil;
1310
1311 DEFVAR_LISP ("print-level", &Vprint_level,
aa734e17 1312 "Maximum depth of list nesting to print before abbreviating.\n\
38010d50
JB
1313A value of nil means no limit.");
1314 Vprint_level = Qnil;
1315
1316 DEFVAR_BOOL ("print-escape-newlines", &print_escape_newlines,
a8920a17 1317 "Non-nil means print newlines in strings as backslash-n.\n\
c6f7982f 1318Also print formfeeds as backslash-f.");
38010d50
JB
1319 print_escape_newlines = 0;
1320
1321 /* prin1_to_string_buffer initialized in init_buffer_once in buffer.c */
1322 staticpro (&Vprin1_to_string_buffer);
1323
1324 defsubr (&Sprin1);
1325 defsubr (&Sprin1_to_string);
113620cc 1326 defsubr (&Serror_message_string);
38010d50
JB
1327 defsubr (&Sprinc);
1328 defsubr (&Sprint);
1329 defsubr (&Sterpri);
1330 defsubr (&Swrite_char);
1331 defsubr (&Sexternal_debugging_output);
1332
1333 Qexternal_debugging_output = intern ("external-debugging-output");
1334 staticpro (&Qexternal_debugging_output);
1335
1336#ifndef standalone
1337 defsubr (&Swith_output_to_temp_buffer);
1338#endif /* not standalone */
1339}