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