Commit | Line | Data |
---|---|---|
38010d50 | 1 | /* Lisp object printing and output streams. |
4746118a | 2 | Copyright (C) 1985, 1986, 1988, 1992 Free Software Foundation, Inc. |
38010d50 JB |
3 | |
4 | This file is part of GNU Emacs. | |
5 | ||
6 | GNU Emacs is free software; you can redistribute it and/or modify | |
7 | it under the terms of the GNU General Public License as published by | |
4746118a | 8 | the Free Software Foundation; either version 2, or (at your option) |
38010d50 JB |
9 | any later version. |
10 | ||
11 | GNU Emacs is distributed in the hope that it will be useful, | |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | GNU General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU General Public License | |
17 | along with GNU Emacs; see the file COPYING. If not, write to | |
18 | the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */ | |
19 | ||
20 | ||
21 | #include "config.h" | |
22 | #include <stdio.h> | |
23 | #undef NULL | |
24 | #include "lisp.h" | |
25 | ||
26 | #ifndef standalone | |
27 | #include "buffer.h" | |
0137dbf7 | 28 | #include "frame.h" |
38010d50 JB |
29 | #include "window.h" |
30 | #include "process.h" | |
31 | #include "dispextern.h" | |
32 | #include "termchar.h" | |
33 | #endif /* not standalone */ | |
34 | ||
35 | Lisp_Object Vstandard_output, Qstandard_output; | |
36 | ||
37 | #ifdef LISP_FLOAT_TYPE | |
38 | Lisp_Object Vfloat_output_format, Qfloat_output_format; | |
39 | #endif /* LISP_FLOAT_TYPE */ | |
40 | ||
41 | /* Avoid actual stack overflow in print. */ | |
42 | int print_depth; | |
43 | ||
ec838c39 RS |
44 | /* Detect most circularities to print finite output. */ |
45 | #define PRINT_CIRCLE 200 | |
46 | Lisp_Object being_printed[PRINT_CIRCLE]; | |
47 | ||
38010d50 JB |
48 | /* Maximum length of list to print in full; noninteger means |
49 | effectively infinity */ | |
50 | ||
51 | Lisp_Object Vprint_length; | |
52 | ||
53 | /* Maximum depth of list to print in full; noninteger means | |
54 | effectively infinity. */ | |
55 | ||
56 | Lisp_Object Vprint_level; | |
57 | ||
58 | /* Nonzero means print newlines in strings as \n. */ | |
59 | ||
60 | int print_escape_newlines; | |
61 | ||
62 | Lisp_Object Qprint_escape_newlines; | |
63 | ||
64 | /* Nonzero means print newline before next minibuffer message. | |
65 | Defined in xdisp.c */ | |
66 | ||
67 | extern int noninteractive_need_newline; | |
68 | #ifdef MAX_PRINT_CHARS | |
69 | static int print_chars; | |
70 | static int max_print; | |
71 | #endif /* MAX_PRINT_CHARS */ | |
72 | \f | |
73 | #if 0 | |
74 | /* Convert between chars and GLYPHs */ | |
75 | ||
76 | int | |
77 | glyphlen (glyphs) | |
78 | register GLYPH *glyphs; | |
79 | { | |
80 | register int i = 0; | |
81 | ||
82 | while (glyphs[i]) | |
83 | i++; | |
84 | return i; | |
85 | } | |
86 | ||
87 | void | |
88 | str_to_glyph_cpy (str, glyphs) | |
89 | char *str; | |
90 | GLYPH *glyphs; | |
91 | { | |
92 | register GLYPH *gp = glyphs; | |
93 | register char *cp = str; | |
94 | ||
95 | while (*cp) | |
96 | *gp++ = *cp++; | |
97 | } | |
98 | ||
99 | void | |
100 | str_to_glyph_ncpy (str, glyphs, n) | |
101 | char *str; | |
102 | GLYPH *glyphs; | |
103 | register int n; | |
104 | { | |
105 | register GLYPH *gp = glyphs; | |
106 | register char *cp = str; | |
107 | ||
108 | while (n-- > 0) | |
109 | *gp++ = *cp++; | |
110 | } | |
111 | ||
112 | void | |
113 | glyph_to_str_cpy (glyphs, str) | |
114 | GLYPH *glyphs; | |
115 | char *str; | |
116 | { | |
117 | register GLYPH *gp = glyphs; | |
118 | register char *cp = str; | |
119 | ||
120 | while (*gp) | |
121 | *str++ = *gp++ & 0377; | |
122 | } | |
123 | #endif | |
124 | \f | |
125 | /* Low level output routines for charaters and strings */ | |
126 | ||
127 | /* Lisp functions to do output using a stream | |
128 | must have the stream in a variable called printcharfun | |
129 | and must start with PRINTPREPARE and end with PRINTFINISH. | |
130 | Use PRINTCHAR to output one character, | |
131 | or call strout to output a block of characters. | |
132 | Also, each one must have the declarations | |
133 | struct buffer *old = current_buffer; | |
134 | int old_point = -1, start_point; | |
135 | Lisp_Object original; | |
136 | */ | |
137 | ||
138 | #define PRINTPREPARE \ | |
139 | original = printcharfun; \ | |
10eebdbb | 140 | if (NILP (printcharfun)) printcharfun = Qt; \ |
38010d50 JB |
141 | if (XTYPE (printcharfun) == Lisp_Buffer) \ |
142 | { if (XBUFFER (printcharfun) != current_buffer) Fset_buffer (printcharfun); \ | |
143 | printcharfun = Qnil;}\ | |
144 | if (XTYPE (printcharfun) == Lisp_Marker) \ | |
145 | { if (XMARKER (original)->buffer != current_buffer) \ | |
146 | set_buffer_internal (XMARKER (original)->buffer); \ | |
147 | old_point = point; \ | |
148 | SET_PT (marker_position (printcharfun)); \ | |
149 | start_point = point; \ | |
150 | printcharfun = Qnil;} | |
151 | ||
152 | #define PRINTFINISH \ | |
153 | if (XTYPE (original) == Lisp_Marker) \ | |
154 | Fset_marker (original, make_number (point), Qnil); \ | |
155 | if (old_point >= 0) \ | |
156 | SET_PT ((old_point >= start_point ? point - start_point : 0) + old_point); \ | |
157 | if (old != current_buffer) \ | |
158 | set_buffer_internal (old) | |
159 | ||
160 | #define PRINTCHAR(ch) printchar (ch, printcharfun) | |
161 | ||
0137dbf7 | 162 | /* Index of first unused element of FRAME_MESSAGE_BUF(selected_frame). */ |
38010d50 JB |
163 | static int printbufidx; |
164 | ||
165 | static void | |
166 | printchar (ch, fun) | |
167 | unsigned char ch; | |
168 | Lisp_Object fun; | |
169 | { | |
170 | Lisp_Object ch1; | |
171 | ||
172 | #ifdef MAX_PRINT_CHARS | |
173 | if (max_print) | |
174 | print_chars++; | |
175 | #endif /* MAX_PRINT_CHARS */ | |
176 | #ifndef standalone | |
177 | if (EQ (fun, Qnil)) | |
178 | { | |
179 | QUIT; | |
180 | insert (&ch, 1); | |
181 | return; | |
182 | } | |
183 | ||
184 | if (EQ (fun, Qt)) | |
185 | { | |
186 | if (noninteractive) | |
187 | { | |
188 | putchar (ch); | |
189 | noninteractive_need_newline = 1; | |
190 | return; | |
191 | } | |
192 | ||
0137dbf7 | 193 | if (echo_area_glyphs != FRAME_MESSAGE_BUF (selected_frame) |
38010d50 JB |
194 | || !message_buf_print) |
195 | { | |
0137dbf7 | 196 | echo_area_glyphs = FRAME_MESSAGE_BUF (selected_frame); |
38010d50 JB |
197 | printbufidx = 0; |
198 | message_buf_print = 1; | |
199 | } | |
200 | ||
0137dbf7 JB |
201 | if (printbufidx < FRAME_WIDTH (selected_frame) - 1) |
202 | FRAME_MESSAGE_BUF (selected_frame)[printbufidx++] = ch; | |
203 | FRAME_MESSAGE_BUF (selected_frame)[printbufidx] = 0; | |
38010d50 JB |
204 | |
205 | return; | |
206 | } | |
207 | #endif /* not standalone */ | |
208 | ||
209 | XFASTINT (ch1) = ch; | |
210 | call1 (fun, ch1); | |
211 | } | |
212 | ||
213 | static void | |
214 | strout (ptr, size, printcharfun) | |
215 | char *ptr; | |
216 | int size; | |
217 | Lisp_Object printcharfun; | |
218 | { | |
219 | int i = 0; | |
220 | ||
221 | if (EQ (printcharfun, Qnil)) | |
222 | { | |
223 | insert (ptr, size >= 0 ? size : strlen (ptr)); | |
224 | #ifdef MAX_PRINT_CHARS | |
225 | if (max_print) | |
226 | print_chars += size >= 0 ? size : strlen(ptr); | |
227 | #endif /* MAX_PRINT_CHARS */ | |
228 | return; | |
229 | } | |
230 | if (EQ (printcharfun, Qt)) | |
231 | { | |
232 | i = size >= 0 ? size : strlen (ptr); | |
233 | #ifdef MAX_PRINT_CHARS | |
234 | if (max_print) | |
235 | print_chars += i; | |
236 | #endif /* MAX_PRINT_CHARS */ | |
237 | ||
238 | if (noninteractive) | |
239 | { | |
240 | fwrite (ptr, 1, i, stdout); | |
241 | noninteractive_need_newline = 1; | |
242 | return; | |
243 | } | |
244 | ||
0137dbf7 | 245 | if (echo_area_glyphs != FRAME_MESSAGE_BUF (selected_frame) |
38010d50 JB |
246 | || !message_buf_print) |
247 | { | |
0137dbf7 | 248 | echo_area_glyphs = FRAME_MESSAGE_BUF (selected_frame); |
38010d50 JB |
249 | printbufidx = 0; |
250 | message_buf_print = 1; | |
251 | } | |
252 | ||
0137dbf7 JB |
253 | if (i > FRAME_WIDTH (selected_frame) - printbufidx - 1) |
254 | i = FRAME_WIDTH (selected_frame) - printbufidx - 1; | |
255 | bcopy (ptr, &FRAME_MESSAGE_BUF (selected_frame) [printbufidx], i); | |
38010d50 | 256 | printbufidx += i; |
0137dbf7 | 257 | FRAME_MESSAGE_BUF (selected_frame) [printbufidx] = 0; |
38010d50 JB |
258 | |
259 | return; | |
260 | } | |
261 | ||
262 | if (size >= 0) | |
263 | while (i < size) | |
264 | PRINTCHAR (ptr[i++]); | |
265 | else | |
266 | while (ptr[i]) | |
267 | PRINTCHAR (ptr[i++]); | |
268 | } | |
269 | ||
270 | /* Print the contents of a string STRING using PRINTCHARFUN. | |
271 | It isn't safe to use strout, because printing one char can relocate. */ | |
272 | ||
273 | print_string (string, printcharfun) | |
274 | Lisp_Object string; | |
275 | Lisp_Object printcharfun; | |
276 | { | |
277 | if (EQ (printcharfun, Qnil) || EQ (printcharfun, Qt)) | |
0137dbf7 | 278 | /* In predictable cases, strout is safe: output to buffer or frame. */ |
38010d50 JB |
279 | strout (XSTRING (string)->data, XSTRING (string)->size, printcharfun); |
280 | else | |
281 | { | |
282 | /* Otherwise, fetch the string address for each character. */ | |
283 | int i; | |
284 | int size = XSTRING (string)->size; | |
285 | struct gcpro gcpro1; | |
286 | GCPRO1 (string); | |
287 | for (i = 0; i < size; i++) | |
288 | PRINTCHAR (XSTRING (string)->data[i]); | |
289 | UNGCPRO; | |
290 | } | |
291 | } | |
292 | \f | |
293 | DEFUN ("write-char", Fwrite_char, Swrite_char, 1, 2, 0, | |
294 | "Output character CHAR to stream STREAM.\n\ | |
295 | STREAM defaults to the value of `standard-output' (which see).") | |
296 | (ch, printcharfun) | |
297 | Lisp_Object ch, printcharfun; | |
298 | { | |
299 | struct buffer *old = current_buffer; | |
300 | int old_point = -1; | |
301 | int start_point; | |
302 | Lisp_Object original; | |
303 | ||
10eebdbb | 304 | if (NILP (printcharfun)) |
38010d50 JB |
305 | printcharfun = Vstandard_output; |
306 | CHECK_NUMBER (ch, 0); | |
307 | PRINTPREPARE; | |
308 | PRINTCHAR (XINT (ch)); | |
309 | PRINTFINISH; | |
310 | return ch; | |
311 | } | |
312 | ||
313 | /* Used from outside of print.c to print a block of SIZE chars at DATA | |
314 | on the default output stream. | |
315 | Do not use this on the contents of a Lisp string. */ | |
316 | ||
317 | write_string (data, size) | |
318 | char *data; | |
319 | int size; | |
320 | { | |
321 | struct buffer *old = current_buffer; | |
322 | Lisp_Object printcharfun; | |
323 | int old_point = -1; | |
324 | int start_point; | |
325 | Lisp_Object original; | |
326 | ||
327 | printcharfun = Vstandard_output; | |
328 | ||
329 | PRINTPREPARE; | |
330 | strout (data, size, printcharfun); | |
331 | PRINTFINISH; | |
332 | } | |
333 | ||
334 | /* Used from outside of print.c to print a block of SIZE chars at DATA | |
335 | on a specified stream PRINTCHARFUN. | |
336 | Do not use this on the contents of a Lisp string. */ | |
337 | ||
338 | write_string_1 (data, size, printcharfun) | |
339 | char *data; | |
340 | int size; | |
341 | Lisp_Object printcharfun; | |
342 | { | |
343 | struct buffer *old = current_buffer; | |
344 | int old_point = -1; | |
345 | int start_point; | |
346 | Lisp_Object original; | |
347 | ||
348 | PRINTPREPARE; | |
349 | strout (data, size, printcharfun); | |
350 | PRINTFINISH; | |
351 | } | |
352 | ||
353 | ||
354 | #ifndef standalone | |
355 | ||
356 | void | |
357 | temp_output_buffer_setup (bufname) | |
358 | char *bufname; | |
359 | { | |
360 | register struct buffer *old = current_buffer; | |
361 | register Lisp_Object buf; | |
362 | ||
363 | Fset_buffer (Fget_buffer_create (build_string (bufname))); | |
364 | ||
365 | current_buffer->read_only = Qnil; | |
366 | Ferase_buffer (); | |
367 | ||
368 | XSET (buf, Lisp_Buffer, current_buffer); | |
369 | specbind (Qstandard_output, buf); | |
370 | ||
371 | set_buffer_internal (old); | |
372 | } | |
373 | ||
374 | Lisp_Object | |
375 | internal_with_output_to_temp_buffer (bufname, function, args) | |
376 | char *bufname; | |
377 | Lisp_Object (*function) (); | |
378 | Lisp_Object args; | |
379 | { | |
380 | int count = specpdl_ptr - specpdl; | |
381 | Lisp_Object buf, val; | |
382 | ||
383 | record_unwind_protect (Fset_buffer, Fcurrent_buffer ()); | |
384 | temp_output_buffer_setup (bufname); | |
385 | buf = Vstandard_output; | |
386 | ||
387 | val = (*function) (args); | |
388 | ||
389 | temp_output_buffer_show (buf); | |
390 | ||
391 | return unbind_to (count, val); | |
392 | } | |
393 | ||
394 | DEFUN ("with-output-to-temp-buffer", Fwith_output_to_temp_buffer, Swith_output_to_temp_buffer, | |
395 | 1, UNEVALLED, 0, | |
396 | "Bind `standard-output' to buffer BUFNAME, eval BODY, then show that buffer.\n\ | |
397 | The buffer is cleared out initially, and marked as unmodified when done.\n\ | |
398 | All output done by BODY is inserted in that buffer by default.\n\ | |
399 | The buffer is displayed in another window, but not selected.\n\ | |
400 | The value of the last form in BODY is returned.\n\ | |
401 | If BODY does not finish normally, the buffer BUFNAME is not displayed.\n\n\ | |
402 | If variable `temp-buffer-show-hook' is non-nil, call it at the end\n\ | |
403 | to get the buffer displayed. It gets one argument, the buffer to display.") | |
404 | (args) | |
405 | Lisp_Object args; | |
406 | { | |
407 | struct gcpro gcpro1; | |
408 | Lisp_Object name; | |
409 | int count = specpdl_ptr - specpdl; | |
410 | Lisp_Object buf, val; | |
411 | ||
412 | GCPRO1(args); | |
413 | name = Feval (Fcar (args)); | |
414 | UNGCPRO; | |
415 | ||
416 | CHECK_STRING (name, 0); | |
417 | temp_output_buffer_setup (XSTRING (name)->data); | |
418 | buf = Vstandard_output; | |
419 | ||
420 | val = Fprogn (Fcdr (args)); | |
421 | ||
422 | temp_output_buffer_show (buf); | |
423 | ||
424 | return unbind_to (count, val); | |
425 | } | |
426 | #endif /* not standalone */ | |
427 | \f | |
428 | static void print (); | |
429 | ||
430 | DEFUN ("terpri", Fterpri, Sterpri, 0, 1, 0, | |
431 | "Output a newline to STREAM.\n\ | |
432 | If STREAM is omitted or nil, the value of `standard-output' is used.") | |
433 | (printcharfun) | |
434 | Lisp_Object printcharfun; | |
435 | { | |
436 | struct buffer *old = current_buffer; | |
437 | int old_point = -1; | |
438 | int start_point; | |
439 | Lisp_Object original; | |
440 | ||
10eebdbb | 441 | if (NILP (printcharfun)) |
38010d50 JB |
442 | printcharfun = Vstandard_output; |
443 | PRINTPREPARE; | |
444 | PRINTCHAR ('\n'); | |
445 | PRINTFINISH; | |
446 | return Qt; | |
447 | } | |
448 | ||
449 | DEFUN ("prin1", Fprin1, Sprin1, 1, 2, 0, | |
450 | "Output the printed representation of OBJECT, any Lisp object.\n\ | |
451 | Quoting characters are printed when needed to make output that `read'\n\ | |
452 | can handle, whenever this is possible.\n\ | |
453 | Output stream is STREAM, or value of `standard-output' (which see).") | |
454 | (obj, printcharfun) | |
455 | Lisp_Object obj, printcharfun; | |
456 | { | |
457 | struct buffer *old = current_buffer; | |
458 | int old_point = -1; | |
459 | int start_point; | |
460 | Lisp_Object original; | |
461 | ||
462 | #ifdef MAX_PRINT_CHARS | |
463 | max_print = 0; | |
464 | #endif /* MAX_PRINT_CHARS */ | |
10eebdbb | 465 | if (NILP (printcharfun)) |
38010d50 JB |
466 | printcharfun = Vstandard_output; |
467 | PRINTPREPARE; | |
468 | print_depth = 0; | |
469 | print (obj, printcharfun, 1); | |
470 | PRINTFINISH; | |
471 | return obj; | |
472 | } | |
473 | ||
474 | /* a buffer which is used to hold output being built by prin1-to-string */ | |
475 | Lisp_Object Vprin1_to_string_buffer; | |
476 | ||
477 | DEFUN ("prin1-to-string", Fprin1_to_string, Sprin1_to_string, 1, 2, 0, | |
478 | "Return a string containing the printed representation of OBJECT,\n\ | |
479 | any Lisp object. Quoting characters are used when needed to make output\n\ | |
480 | that `read' can handle, whenever this is possible, unless the optional\n\ | |
481 | second argument NOESCAPE is non-nil.") | |
482 | (obj, noescape) | |
483 | Lisp_Object obj, noescape; | |
484 | { | |
485 | struct buffer *old = current_buffer; | |
486 | int old_point = -1; | |
487 | int start_point; | |
488 | Lisp_Object original, printcharfun; | |
489 | struct gcpro gcpro1; | |
490 | ||
491 | printcharfun = Vprin1_to_string_buffer; | |
492 | PRINTPREPARE; | |
493 | print_depth = 0; | |
10eebdbb | 494 | print (obj, printcharfun, NILP (noescape)); |
38010d50 JB |
495 | /* Make Vprin1_to_string_buffer be the default buffer after PRINTFINSH */ |
496 | PRINTFINISH; | |
497 | set_buffer_internal (XBUFFER (Vprin1_to_string_buffer)); | |
498 | obj = Fbuffer_string (); | |
499 | ||
500 | GCPRO1 (obj); | |
501 | Ferase_buffer (); | |
502 | set_buffer_internal (old); | |
503 | UNGCPRO; | |
504 | ||
505 | return obj; | |
506 | } | |
507 | ||
508 | DEFUN ("princ", Fprinc, Sprinc, 1, 2, 0, | |
509 | "Output the printed representation of OBJECT, any Lisp object.\n\ | |
510 | No quoting characters are used; no delimiters are printed around\n\ | |
511 | the contents of strings.\n\ | |
512 | Output stream is STREAM, or value of standard-output (which see).") | |
513 | (obj, printcharfun) | |
514 | Lisp_Object obj, printcharfun; | |
515 | { | |
516 | struct buffer *old = current_buffer; | |
517 | int old_point = -1; | |
518 | int start_point; | |
519 | Lisp_Object original; | |
520 | ||
10eebdbb | 521 | if (NILP (printcharfun)) |
38010d50 JB |
522 | printcharfun = Vstandard_output; |
523 | PRINTPREPARE; | |
524 | print_depth = 0; | |
525 | print (obj, printcharfun, 0); | |
526 | PRINTFINISH; | |
527 | return obj; | |
528 | } | |
529 | ||
530 | DEFUN ("print", Fprint, Sprint, 1, 2, 0, | |
531 | "Output the printed representation of OBJECT, with newlines around it.\n\ | |
532 | Quoting characters are printed when needed to make output that `read'\n\ | |
533 | can handle, whenever this is possible.\n\ | |
534 | Output stream is STREAM, or value of `standard-output' (which see).") | |
535 | (obj, printcharfun) | |
536 | Lisp_Object obj, printcharfun; | |
537 | { | |
538 | struct buffer *old = current_buffer; | |
539 | int old_point = -1; | |
540 | int start_point; | |
541 | Lisp_Object original; | |
542 | struct gcpro gcpro1; | |
543 | ||
544 | #ifdef MAX_PRINT_CHARS | |
545 | print_chars = 0; | |
546 | max_print = MAX_PRINT_CHARS; | |
547 | #endif /* MAX_PRINT_CHARS */ | |
10eebdbb | 548 | if (NILP (printcharfun)) |
38010d50 JB |
549 | printcharfun = Vstandard_output; |
550 | GCPRO1 (obj); | |
551 | PRINTPREPARE; | |
552 | print_depth = 0; | |
553 | PRINTCHAR ('\n'); | |
554 | print (obj, printcharfun, 1); | |
555 | PRINTCHAR ('\n'); | |
556 | PRINTFINISH; | |
557 | #ifdef MAX_PRINT_CHARS | |
558 | max_print = 0; | |
559 | print_chars = 0; | |
560 | #endif /* MAX_PRINT_CHARS */ | |
561 | UNGCPRO; | |
562 | return obj; | |
563 | } | |
564 | ||
565 | /* The subroutine object for external-debugging-output is kept here | |
566 | for the convenience of the debugger. */ | |
567 | Lisp_Object Qexternal_debugging_output; | |
568 | ||
4746118a JB |
569 | DEFUN ("external-debugging-output", Fexternal_debugging_output, Sexternal_debugging_output, 1, 1, 0, |
570 | "Write CHARACTER to stderr.\n\ | |
38010d50 JB |
571 | You can call print while debugging emacs, and pass it this function\n\ |
572 | to make it write to the debugging output.\n") | |
4746118a JB |
573 | (character) |
574 | Lisp_Object character; | |
38010d50 JB |
575 | { |
576 | CHECK_NUMBER (character, 0); | |
577 | putc (XINT (character), stderr); | |
578 | ||
579 | return character; | |
580 | } | |
581 | \f | |
582 | #ifdef LISP_FLOAT_TYPE | |
583 | ||
38010d50 | 584 | /* |
edb2a707 | 585 | * The buffer should be at least as large as the max string size of the |
38010d50 JB |
586 | * largest float, printed in the biggest notation. This is undoubtably |
587 | * 20d float_output_format, with the negative of the C-constant "HUGE" | |
588 | * from <math.h>. | |
589 | * | |
590 | * On the vax the worst case is -1e38 in 20d format which takes 61 bytes. | |
591 | * | |
592 | * I assume that IEEE-754 format numbers can take 329 bytes for the worst | |
593 | * case of -1e307 in 20d float_output_format. What is one to do (short of | |
594 | * re-writing _doprnt to be more sane)? | |
595 | * -wsr | |
596 | */ | |
edb2a707 RS |
597 | |
598 | void | |
599 | float_to_string (buf, data) | |
600 | char *buf; | |
38010d50 JB |
601 | double data; |
602 | { | |
603 | register unsigned char *cp, c; | |
604 | register int width; | |
605 | ||
10eebdbb | 606 | if (NILP (Vfloat_output_format) |
38010d50 JB |
607 | || XTYPE (Vfloat_output_format) != Lisp_String) |
608 | lose: | |
609 | sprintf (buf, "%.20g", data); | |
610 | else /* oink oink */ | |
611 | { | |
612 | /* Check that the spec we have is fully valid. | |
613 | This means not only valid for printf, | |
614 | but meant for floats, and reasonable. */ | |
615 | cp = XSTRING (Vfloat_output_format)->data; | |
616 | ||
617 | if (cp[0] != '%') | |
618 | goto lose; | |
619 | if (cp[1] != '.') | |
620 | goto lose; | |
621 | ||
622 | cp += 2; | |
623 | for (width = 0; | |
624 | ((c = *cp) >= '0' && c <= '9'); | |
625 | cp++) | |
626 | { | |
627 | width *= 10; | |
628 | width += c - '0'; | |
629 | } | |
630 | ||
631 | if (*cp != 'e' && *cp != 'f' && *cp != 'g') | |
632 | goto lose; | |
633 | ||
634 | if (width < (*cp != 'e') || width > DBL_DIG) | |
635 | goto lose; | |
636 | ||
637 | if (cp[1] != 0) | |
638 | goto lose; | |
639 | ||
640 | sprintf (buf, XSTRING (Vfloat_output_format)->data, data); | |
641 | } | |
edb2a707 RS |
642 | |
643 | /* Make sure there is a decimal point or an exponent, | |
644 | so that the value is readable as a float. */ | |
645 | for (cp = buf; *cp; cp++) | |
646 | if (*cp < '0' || *cp > '9') | |
647 | break; | |
648 | ||
649 | if (*cp == 0) | |
650 | { | |
651 | *cp++ = '.'; | |
652 | *cp++ = '0'; | |
653 | *cp++ = 0; | |
654 | } | |
38010d50 JB |
655 | } |
656 | #endif /* LISP_FLOAT_TYPE */ | |
657 | \f | |
658 | static void | |
659 | print (obj, printcharfun, escapeflag) | |
660 | #ifndef RTPC_REGISTER_BUG | |
661 | register Lisp_Object obj; | |
662 | #else | |
663 | Lisp_Object obj; | |
664 | #endif | |
665 | register Lisp_Object printcharfun; | |
666 | int escapeflag; | |
667 | { | |
668 | char buf[30]; | |
669 | ||
670 | QUIT; | |
671 | ||
ec838c39 RS |
672 | #if 1 /* I'm not sure this is really worth doing. */ |
673 | /* Detect circularities and truncate them. | |
674 | No need to offer any alternative--this is better than an error. */ | |
675 | if (XTYPE (obj) == Lisp_Cons || XTYPE (obj) == Lisp_Vector | |
676 | || XTYPE (obj) == Lisp_Compiled) | |
677 | { | |
678 | int i; | |
679 | for (i = 0; i < print_depth; i++) | |
680 | if (EQ (obj, being_printed[i])) | |
681 | { | |
682 | sprintf (buf, "#%d", i); | |
683 | strout (buf, -1, printcharfun); | |
684 | return; | |
685 | } | |
686 | } | |
687 | #endif | |
688 | ||
689 | being_printed[print_depth] = obj; | |
38010d50 JB |
690 | print_depth++; |
691 | ||
ec838c39 | 692 | if (print_depth > PRINT_CIRCLE) |
38010d50 JB |
693 | error ("Apparently circular structure being printed"); |
694 | #ifdef MAX_PRINT_CHARS | |
695 | if (max_print && print_chars > max_print) | |
696 | { | |
697 | PRINTCHAR ('\n'); | |
698 | print_chars = 0; | |
699 | } | |
700 | #endif /* MAX_PRINT_CHARS */ | |
701 | ||
702 | #ifdef SWITCH_ENUM_BUG | |
703 | switch ((int) XTYPE (obj)) | |
704 | #else | |
705 | switch (XTYPE (obj)) | |
706 | #endif | |
707 | { | |
708 | default: | |
709 | /* We're in trouble if this happens! | |
710 | Probably should just abort () */ | |
711 | strout ("#<EMACS BUG: INVALID DATATYPE ", -1, printcharfun); | |
712 | sprintf (buf, "(#o%3o)", (int) XTYPE (obj)); | |
713 | strout (buf, -1, printcharfun); | |
714 | strout (" Save your buffers immediately and please report this bug>", | |
715 | -1, printcharfun); | |
716 | break; | |
717 | ||
718 | #ifdef LISP_FLOAT_TYPE | |
719 | case Lisp_Float: | |
720 | { | |
721 | char pigbuf[350]; /* see comments in float_to_string */ | |
722 | ||
723 | float_to_string (pigbuf, XFLOAT(obj)->data); | |
724 | strout (pigbuf, -1, printcharfun); | |
725 | } | |
726 | break; | |
727 | #endif /* LISP_FLOAT_TYPE */ | |
728 | ||
729 | case Lisp_Int: | |
730 | sprintf (buf, "%d", XINT (obj)); | |
731 | strout (buf, -1, printcharfun); | |
732 | break; | |
733 | ||
734 | case Lisp_String: | |
735 | if (!escapeflag) | |
736 | print_string (obj, printcharfun); | |
737 | else | |
738 | { | |
739 | register int i; | |
740 | register unsigned char c; | |
741 | Lisp_Object obj1; | |
742 | struct gcpro gcpro1; | |
743 | ||
744 | /* You can't gcpro register variables, so copy obj to a | |
745 | non-register variable so we can gcpro it without | |
746 | making it non-register. */ | |
747 | obj1 = obj; | |
748 | GCPRO1 (obj1); | |
749 | ||
750 | PRINTCHAR ('\"'); | |
751 | for (i = 0; i < XSTRING (obj)->size; i++) | |
752 | { | |
753 | QUIT; | |
754 | c = XSTRING (obj)->data[i]; | |
755 | if (c == '\n' && print_escape_newlines) | |
756 | { | |
757 | PRINTCHAR ('\\'); | |
758 | PRINTCHAR ('n'); | |
759 | } | |
760 | else | |
761 | { | |
762 | if (c == '\"' || c == '\\') | |
763 | PRINTCHAR ('\\'); | |
764 | PRINTCHAR (c); | |
765 | } | |
766 | } | |
767 | PRINTCHAR ('\"'); | |
768 | UNGCPRO; | |
769 | } | |
770 | break; | |
771 | ||
772 | case Lisp_Symbol: | |
773 | { | |
774 | register int confusing; | |
775 | register unsigned char *p = XSYMBOL (obj)->name->data; | |
776 | register unsigned char *end = p + XSYMBOL (obj)->name->size; | |
777 | register unsigned char c; | |
778 | ||
779 | if (p != end && (*p == '-' || *p == '+')) p++; | |
780 | if (p == end) | |
781 | confusing = 0; | |
782 | else | |
783 | { | |
784 | while (p != end && *p >= '0' && *p <= '9') | |
785 | p++; | |
786 | confusing = (end == p); | |
787 | } | |
788 | ||
789 | p = XSYMBOL (obj)->name->data; | |
790 | while (p != end) | |
791 | { | |
792 | QUIT; | |
793 | c = *p++; | |
794 | if (escapeflag) | |
795 | { | |
796 | if (c == '\"' || c == '\\' || c == '\'' || c == ';' || c == '#' || | |
797 | c == '(' || c == ')' || c == ',' || c =='.' || c == '`' || | |
798 | c == '[' || c == ']' || c == '?' || c <= 040 || confusing) | |
799 | PRINTCHAR ('\\'), confusing = 0; | |
800 | } | |
801 | PRINTCHAR (c); | |
802 | } | |
803 | } | |
804 | break; | |
805 | ||
806 | case Lisp_Cons: | |
807 | /* If deeper than spec'd depth, print placeholder. */ | |
808 | if (XTYPE (Vprint_level) == Lisp_Int | |
809 | && print_depth > XINT (Vprint_level)) | |
810 | { | |
811 | strout ("...", -1, printcharfun); | |
812 | break; | |
813 | } | |
814 | ||
815 | PRINTCHAR ('('); | |
816 | { | |
817 | register int i = 0; | |
818 | register int max = 0; | |
819 | ||
820 | if (XTYPE (Vprint_length) == Lisp_Int) | |
821 | max = XINT (Vprint_length); | |
ec838c39 RS |
822 | /* Could recognize circularities in cdrs here, |
823 | but that would make printing of long lists quadratic. | |
824 | It's not worth doing. */ | |
38010d50 JB |
825 | while (CONSP (obj)) |
826 | { | |
827 | if (i++) | |
828 | PRINTCHAR (' '); | |
829 | if (max && i > max) | |
830 | { | |
831 | strout ("...", 3, printcharfun); | |
832 | break; | |
833 | } | |
834 | print (Fcar (obj), printcharfun, escapeflag); | |
835 | obj = Fcdr (obj); | |
836 | } | |
837 | } | |
10eebdbb | 838 | if (!NILP (obj) && !CONSP (obj)) |
38010d50 JB |
839 | { |
840 | strout (" . ", 3, printcharfun); | |
841 | print (obj, printcharfun, escapeflag); | |
842 | } | |
843 | PRINTCHAR (')'); | |
844 | break; | |
845 | ||
846 | case Lisp_Compiled: | |
200f684e | 847 | strout ("#", -1, printcharfun); |
38010d50 JB |
848 | case Lisp_Vector: |
849 | PRINTCHAR ('['); | |
850 | { | |
851 | register int i; | |
852 | register Lisp_Object tem; | |
853 | for (i = 0; i < XVECTOR (obj)->size; i++) | |
854 | { | |
855 | if (i) PRINTCHAR (' '); | |
856 | tem = XVECTOR (obj)->contents[i]; | |
857 | print (tem, printcharfun, escapeflag); | |
858 | } | |
859 | } | |
860 | PRINTCHAR (']'); | |
38010d50 JB |
861 | break; |
862 | ||
863 | #ifndef standalone | |
864 | case Lisp_Buffer: | |
10eebdbb | 865 | if (NILP (XBUFFER (obj)->name)) |
38010d50 JB |
866 | strout ("#<killed buffer>", -1, printcharfun); |
867 | else if (escapeflag) | |
868 | { | |
869 | strout ("#<buffer ", -1, printcharfun); | |
870 | print_string (XBUFFER (obj)->name, printcharfun); | |
871 | PRINTCHAR ('>'); | |
872 | } | |
873 | else | |
874 | print_string (XBUFFER (obj)->name, printcharfun); | |
875 | break; | |
876 | ||
877 | case Lisp_Process: | |
878 | if (escapeflag) | |
879 | { | |
880 | strout ("#<process ", -1, printcharfun); | |
881 | print_string (XPROCESS (obj)->name, printcharfun); | |
882 | PRINTCHAR ('>'); | |
883 | } | |
884 | else | |
885 | print_string (XPROCESS (obj)->name, printcharfun); | |
886 | break; | |
887 | ||
888 | case Lisp_Window: | |
889 | strout ("#<window ", -1, printcharfun); | |
890 | sprintf (buf, "%d", XFASTINT (XWINDOW (obj)->sequence_number)); | |
891 | strout (buf, -1, printcharfun); | |
10eebdbb | 892 | if (!NILP (XWINDOW (obj)->buffer)) |
38010d50 JB |
893 | { |
894 | strout (" on ", -1, printcharfun); | |
895 | print_string (XBUFFER (XWINDOW (obj)->buffer)->name, printcharfun); | |
896 | } | |
897 | PRINTCHAR ('>'); | |
898 | break; | |
899 | ||
900 | case Lisp_Window_Configuration: | |
901 | strout ("#<window-configuration>", -1, printcharfun); | |
902 | break; | |
903 | ||
0137dbf7 JB |
904 | #ifdef MULTI_FRAME |
905 | case Lisp_Frame: | |
906 | strout ((FRAME_LIVE_P (XFRAME (obj)) | |
907 | ? "#<frame " : "#<dead frame "), | |
42dcdd5f | 908 | -1, printcharfun); |
0137dbf7 | 909 | print_string (XFRAME (obj)->name, printcharfun); |
8caf6c73 | 910 | sprintf (buf, " 0x%x", (unsigned int) (XFRAME (obj))); |
38010d50 JB |
911 | strout (buf, -1, printcharfun); |
912 | strout (">", -1, printcharfun); | |
913 | break; | |
0137dbf7 | 914 | #endif /* MULTI_FRAME */ |
38010d50 JB |
915 | |
916 | case Lisp_Marker: | |
917 | strout ("#<marker ", -1, printcharfun); | |
918 | if (!(XMARKER (obj)->buffer)) | |
919 | strout ("in no buffer", -1, printcharfun); | |
920 | else | |
921 | { | |
922 | sprintf (buf, "at %d", marker_position (obj)); | |
923 | strout (buf, -1, printcharfun); | |
924 | strout (" in ", -1, printcharfun); | |
925 | print_string (XMARKER (obj)->buffer->name, printcharfun); | |
926 | } | |
927 | PRINTCHAR ('>'); | |
928 | break; | |
929 | #endif /* standalone */ | |
930 | ||
931 | case Lisp_Subr: | |
932 | strout ("#<subr ", -1, printcharfun); | |
933 | strout (XSUBR (obj)->symbol_name, -1, printcharfun); | |
934 | PRINTCHAR ('>'); | |
935 | break; | |
936 | } | |
937 | ||
938 | print_depth--; | |
939 | } | |
940 | \f | |
941 | void | |
942 | syms_of_print () | |
943 | { | |
944 | staticpro (&Qprint_escape_newlines); | |
945 | Qprint_escape_newlines = intern ("print-escape-newlines"); | |
946 | ||
947 | DEFVAR_LISP ("standard-output", &Vstandard_output, | |
948 | "Output stream `print' uses by default for outputting a character.\n\ | |
949 | This may be any function of one argument.\n\ | |
950 | It may also be a buffer (output is inserted before point)\n\ | |
951 | or a marker (output is inserted and the marker is advanced)\n\ | |
952 | or the symbol t (output appears in the minibuffer line)."); | |
953 | Vstandard_output = Qt; | |
954 | Qstandard_output = intern ("standard-output"); | |
955 | staticpro (&Qstandard_output); | |
956 | ||
957 | #ifdef LISP_FLOAT_TYPE | |
958 | DEFVAR_LISP ("float-output-format", &Vfloat_output_format, | |
06ef7355 | 959 | "The format descriptor string used to print floats.\n\ |
38010d50 JB |
960 | This is a %-spec like those accepted by `printf' in C,\n\ |
961 | but with some restrictions. It must start with the two characters `%.'.\n\ | |
962 | After that comes an integer precision specification,\n\ | |
963 | and then a letter which controls the format.\n\ | |
964 | The letters allowed are `e', `f' and `g'.\n\ | |
965 | Use `e' for exponential notation \"DIG.DIGITSeEXPT\"\n\ | |
966 | Use `f' for decimal point notation \"DIGITS.DIGITS\".\n\ | |
967 | Use `g' to choose the shorter of those two formats for the number at hand.\n\ | |
968 | The precision in any of these cases is the number of digits following\n\ | |
969 | the decimal point. With `f', a precision of 0 means to omit the\n\ | |
970 | decimal point. 0 is not allowed with `f' or `g'.\n\n\ | |
971 | A value of nil means to use `%.20g'."); | |
972 | Vfloat_output_format = Qnil; | |
973 | Qfloat_output_format = intern ("float-output-format"); | |
974 | staticpro (&Qfloat_output_format); | |
975 | #endif /* LISP_FLOAT_TYPE */ | |
976 | ||
977 | DEFVAR_LISP ("print-length", &Vprint_length, | |
aa734e17 | 978 | "Maximum length of list to print before abbreviating.\n\ |
38010d50 JB |
979 | A value of nil means no limit."); |
980 | Vprint_length = Qnil; | |
981 | ||
982 | DEFVAR_LISP ("print-level", &Vprint_level, | |
aa734e17 | 983 | "Maximum depth of list nesting to print before abbreviating.\n\ |
38010d50 JB |
984 | A value of nil means no limit."); |
985 | Vprint_level = Qnil; | |
986 | ||
987 | DEFVAR_BOOL ("print-escape-newlines", &print_escape_newlines, | |
988 | "Non-nil means print newlines in strings as backslash-n."); | |
989 | print_escape_newlines = 0; | |
990 | ||
991 | /* prin1_to_string_buffer initialized in init_buffer_once in buffer.c */ | |
992 | staticpro (&Vprin1_to_string_buffer); | |
993 | ||
994 | defsubr (&Sprin1); | |
995 | defsubr (&Sprin1_to_string); | |
996 | defsubr (&Sprinc); | |
997 | defsubr (&Sprint); | |
998 | defsubr (&Sterpri); | |
999 | defsubr (&Swrite_char); | |
1000 | defsubr (&Sexternal_debugging_output); | |
1001 | ||
1002 | Qexternal_debugging_output = intern ("external-debugging-output"); | |
1003 | staticpro (&Qexternal_debugging_output); | |
1004 | ||
1005 | #ifndef standalone | |
1006 | defsubr (&Swith_output_to_temp_buffer); | |
1007 | #endif /* not standalone */ | |
1008 | } |