Commit | Line | Data |
---|---|---|
38010d50 | 1 | /* Lisp object printing and output streams. |
95df8112 | 2 | |
acaf905b | 3 | Copyright (C) 1985-1986, 1988, 1993-1995, 1997-2012 |
95df8112 | 4 | Free Software Foundation, Inc. |
38010d50 JB |
5 | |
6 | This file is part of GNU Emacs. | |
7 | ||
9ec0b715 | 8 | GNU Emacs is free software: you can redistribute it and/or modify |
38010d50 | 9 | it under the terms of the GNU General Public License as published by |
9ec0b715 GM |
10 | the Free Software Foundation, either version 3 of the License, or |
11 | (at your option) any later version. | |
38010d50 JB |
12 | |
13 | GNU Emacs is distributed in the hope that it will be useful, | |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | GNU General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU General Public License | |
9ec0b715 | 19 | along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */ |
38010d50 JB |
20 | |
21 | ||
18160b98 | 22 | #include <config.h> |
38010d50 | 23 | #include <stdio.h> |
d7306fe6 | 24 | #include <setjmp.h> |
38010d50 | 25 | #include "lisp.h" |
8b4b4467 | 26 | #include "character.h" |
e5560ff7 | 27 | #include "buffer.h" |
71ea13cb | 28 | #include "charset.h" |
2538fae4 | 29 | #include "keyboard.h" |
0137dbf7 | 30 | #include "frame.h" |
38010d50 JB |
31 | #include "window.h" |
32 | #include "process.h" | |
33 | #include "dispextern.h" | |
34 | #include "termchar.h" | |
7651e1f5 | 35 | #include "intervals.h" |
f9467be3 | 36 | #include "blockinput.h" |
f76c8b1e | 37 | #include "termhooks.h" /* For struct terminal. */ |
2a7b7982 | 38 | #include "font.h" |
7651e1f5 | 39 | |
29208e82 | 40 | Lisp_Object Qstandard_output; |
38010d50 | 41 | |
955cbe7b | 42 | static Lisp_Object Qtemp_buffer_setup_hook; |
d9c21094 | 43 | |
2f100b5c | 44 | /* These are used to print like we read. */ |
2f100b5c | 45 | |
955cbe7b | 46 | static Lisp_Object Qfloat_output_format; |
f356c3fb | 47 | |
f356c3fb | 48 | #include <math.h> |
f356c3fb | 49 | #include <float.h> |
fa2c4f56 | 50 | #include <ftoastr.h> |
f356c3fb PE |
51 | |
52 | /* Default to values appropriate for IEEE floating point. */ | |
f356c3fb PE |
53 | #ifndef DBL_DIG |
54 | #define DBL_DIG 15 | |
55 | #endif | |
f356c3fb | 56 | |
38010d50 | 57 | /* Avoid actual stack overflow in print. */ |
a8b7caa3 | 58 | static ptrdiff_t print_depth; |
38010d50 | 59 | |
a22dec27 | 60 | /* Level of nesting inside outputting backquote in new style. */ |
a8b7caa3 | 61 | static ptrdiff_t new_backquote_output; |
0330bb60 | 62 | |
ec838c39 RS |
63 | /* Detect most circularities to print finite output. */ |
64 | #define PRINT_CIRCLE 200 | |
ad64fc97 | 65 | static Lisp_Object being_printed[PRINT_CIRCLE]; |
ec838c39 | 66 | |
6fec5601 RS |
67 | /* When printing into a buffer, first we put the text in this |
68 | block, then insert it all at once. */ | |
ad64fc97 | 69 | static char *print_buffer; |
6fec5601 RS |
70 | |
71 | /* Size allocated in print_buffer. */ | |
d311d28c | 72 | static ptrdiff_t print_buffer_size; |
dc2a0b79 | 73 | /* Chars stored in print_buffer. */ |
d311d28c | 74 | static ptrdiff_t print_buffer_pos; |
dc2a0b79 | 75 | /* Bytes stored in print_buffer. */ |
d311d28c | 76 | static ptrdiff_t print_buffer_pos_byte; |
6fec5601 | 77 | |
835d0be6 | 78 | Lisp_Object Qprint_escape_newlines; |
955cbe7b | 79 | static Lisp_Object Qprint_escape_multibyte, Qprint_escape_nonascii; |
835d0be6 | 80 | |
a65b85b5 SM |
81 | /* Vprint_number_table is a table, that keeps objects that are going to |
82 | be printed, to allow use of #n= and #n# to express sharing. | |
83 | For any given object, the table can give the following values: | |
84 | t the object will be printed only once. | |
85 | -N the object will be printed several times and will take number N. | |
86 | N the object has been printed so we can refer to it as #N#. | |
87 | print_number_index holds the largest N already used. | |
88 | N has to be striclty larger than 0 since we need to distinguish -N. */ | |
a8b7caa3 | 89 | static ptrdiff_t print_number_index; |
ad64fc97 | 90 | static void print_interval (INTERVAL interval, Lisp_Object printcharfun); |
38010d50 | 91 | |
945b0111 | 92 | /* GDB resets this to zero on W32 to disable OutputDebugString calls. */ |
42c8bc9b | 93 | int print_output_debug_flag EXTERNALLY_VISIBLE = 1; |
945b0111 | 94 | |
38010d50 | 95 | \f |
4ae29f89 | 96 | /* Low level output routines for characters and strings. */ |
38010d50 JB |
97 | |
98 | /* Lisp functions to do output using a stream | |
081e0581 EN |
99 | must have the stream in a variable called printcharfun |
100 | and must start with PRINTPREPARE, end with PRINTFINISH, | |
101 | and use PRINTDECLARE to declare common variables. | |
102 | Use PRINTCHAR to output one character, | |
4ae29f89 | 103 | or call strout to output a block of characters. */ |
0788646c GM |
104 | |
105 | #define PRINTDECLARE \ | |
106 | struct buffer *old = current_buffer; \ | |
d311d28c PE |
107 | ptrdiff_t old_point = -1, start_point = -1; \ |
108 | ptrdiff_t old_point_byte = -1, start_point_byte = -1; \ | |
109 | ptrdiff_t specpdl_count = SPECPDL_INDEX (); \ | |
0788646c | 110 | int free_print_buffer = 0; \ |
4b4deea2 | 111 | int multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters)); \ |
081e0581 EN |
112 | Lisp_Object original |
113 | ||
0788646c GM |
114 | #define PRINTPREPARE \ |
115 | original = printcharfun; \ | |
116 | if (NILP (printcharfun)) printcharfun = Qt; \ | |
117 | if (BUFFERP (printcharfun)) \ | |
118 | { \ | |
119 | if (XBUFFER (printcharfun) != current_buffer) \ | |
120 | Fset_buffer (printcharfun); \ | |
121 | printcharfun = Qnil; \ | |
122 | } \ | |
123 | if (MARKERP (printcharfun)) \ | |
124 | { \ | |
d311d28c | 125 | ptrdiff_t marker_pos; \ |
cd3587c1 | 126 | if (! XMARKER (printcharfun)->buffer) \ |
0788646c | 127 | error ("Marker does not point anywhere"); \ |
1cf21850 LK |
128 | if (XMARKER (printcharfun)->buffer != current_buffer) \ |
129 | set_buffer_internal (XMARKER (printcharfun)->buffer); \ | |
130 | marker_pos = marker_position (printcharfun); \ | |
131 | if (marker_pos < BEGV || marker_pos > ZV) \ | |
132 | error ("Marker is outside the accessible part of the buffer"); \ | |
0788646c GM |
133 | old_point = PT; \ |
134 | old_point_byte = PT_BYTE; \ | |
1cf21850 | 135 | SET_PT_BOTH (marker_pos, \ |
0788646c GM |
136 | marker_byte_position (printcharfun)); \ |
137 | start_point = PT; \ | |
138 | start_point_byte = PT_BYTE; \ | |
139 | printcharfun = Qnil; \ | |
140 | } \ | |
141 | if (NILP (printcharfun)) \ | |
142 | { \ | |
143 | Lisp_Object string; \ | |
4b4deea2 | 144 | if (NILP (BVAR (current_buffer, enable_multibyte_characters)) \ |
0788646c GM |
145 | && ! print_escape_multibyte) \ |
146 | specbind (Qprint_escape_multibyte, Qt); \ | |
4b4deea2 | 147 | if (! NILP (BVAR (current_buffer, enable_multibyte_characters)) \ |
d39f07c2 RS |
148 | && ! print_escape_nonascii) \ |
149 | specbind (Qprint_escape_nonascii, Qt); \ | |
0788646c GM |
150 | if (print_buffer != 0) \ |
151 | { \ | |
152 | string = make_string_from_bytes (print_buffer, \ | |
153 | print_buffer_pos, \ | |
154 | print_buffer_pos_byte); \ | |
155 | record_unwind_protect (print_unwind, string); \ | |
156 | } \ | |
157 | else \ | |
158 | { \ | |
d311d28c | 159 | int new_size = 1000; \ |
23f86fce | 160 | print_buffer = xmalloc (new_size); \ |
ff5844ad | 161 | print_buffer_size = new_size; \ |
0788646c GM |
162 | free_print_buffer = 1; \ |
163 | } \ | |
164 | print_buffer_pos = 0; \ | |
165 | print_buffer_pos_byte = 0; \ | |
166 | } \ | |
5494b50f | 167 | if (EQ (printcharfun, Qt) && ! noninteractive) \ |
0f25ecc6 | 168 | setup_echo_area_for_printing (multibyte); |
38010d50 | 169 | |
8a2ab0c6 RS |
170 | #define PRINTFINISH \ |
171 | if (NILP (printcharfun)) \ | |
172 | { \ | |
173 | if (print_buffer_pos != print_buffer_pos_byte \ | |
4b4deea2 | 174 | && NILP (BVAR (current_buffer, enable_multibyte_characters))) \ |
8a2ab0c6 | 175 | { \ |
38182d90 | 176 | unsigned char *temp = alloca (print_buffer_pos + 1); \ |
09125ef8 PE |
177 | copy_text ((unsigned char *) print_buffer, temp, \ |
178 | print_buffer_pos_byte, 1, 0); \ | |
b68864e5 | 179 | insert_1_both ((char *) temp, print_buffer_pos, \ |
8a2ab0c6 RS |
180 | print_buffer_pos, 0, 1, 0); \ |
181 | } \ | |
182 | else \ | |
183 | insert_1_both (print_buffer, print_buffer_pos, \ | |
184 | print_buffer_pos_byte, 0, 1, 0); \ | |
570fab6c | 185 | signal_after_change (PT - print_buffer_pos, 0, print_buffer_pos);\ |
8a2ab0c6 RS |
186 | } \ |
187 | if (free_print_buffer) \ | |
188 | { \ | |
189 | xfree (print_buffer); \ | |
190 | print_buffer = 0; \ | |
191 | } \ | |
192 | unbind_to (specpdl_count, Qnil); \ | |
193 | if (MARKERP (original)) \ | |
194 | set_marker_both (original, Qnil, PT, PT_BYTE); \ | |
195 | if (old_point >= 0) \ | |
196 | SET_PT_BOTH (old_point + (old_point >= start_point \ | |
197 | ? PT - start_point : 0), \ | |
6ddd6eee | 198 | old_point_byte + (old_point_byte >= start_point_byte \ |
cd3587c1 | 199 | ? PT_BYTE - start_point_byte : 0)); \ |
a3d794a1 | 200 | set_buffer_internal (old); |
38010d50 JB |
201 | |
202 | #define PRINTCHAR(ch) printchar (ch, printcharfun) | |
203 | ||
08e8d297 RS |
204 | /* This is used to restore the saved contents of print_buffer |
205 | when there is a recursive call to print. */ | |
0788646c | 206 | |
08e8d297 | 207 | static Lisp_Object |
971de7fb | 208 | print_unwind (Lisp_Object saved_text) |
08e8d297 | 209 | { |
72af86bd | 210 | memcpy (print_buffer, SDATA (saved_text), SCHARS (saved_text)); |
ee5263af | 211 | return Qnil; |
08e8d297 RS |
212 | } |
213 | ||
0788646c GM |
214 | |
215 | /* Print character CH using method FUN. FUN nil means print to | |
216 | print_buffer. FUN t means print to echo area or stdout if | |
217 | non-interactive. If FUN is neither nil nor t, call FUN with CH as | |
218 | argument. */ | |
38010d50 JB |
219 | |
220 | static void | |
971de7fb | 221 | printchar (unsigned int ch, Lisp_Object fun) |
38010d50 | 222 | { |
0788646c GM |
223 | if (!NILP (fun) && !EQ (fun, Qt)) |
224 | call1 (fun, make_number (ch)); | |
225 | else | |
38010d50 | 226 | { |
19a86a03 KH |
227 | unsigned char str[MAX_MULTIBYTE_LENGTH]; |
228 | int len = CHAR_STRING (ch, str); | |
229 | ||
09eddb56 | 230 | QUIT; |
177c0ea7 | 231 | |
0788646c | 232 | if (NILP (fun)) |
9a4d01d8 | 233 | { |
d311d28c PE |
234 | ptrdiff_t incr = len - (print_buffer_size - print_buffer_pos_byte); |
235 | if (0 < incr) | |
236 | print_buffer = | |
237 | xpalloc (print_buffer, &print_buffer_size, incr, -1, 1); | |
72af86bd | 238 | memcpy (print_buffer + print_buffer_pos_byte, str, len); |
0788646c GM |
239 | print_buffer_pos += 1; |
240 | print_buffer_pos_byte += len; | |
9a4d01d8 | 241 | } |
0788646c | 242 | else if (noninteractive) |
1134b854 | 243 | { |
0788646c GM |
244 | fwrite (str, 1, len, stdout); |
245 | noninteractive_need_newline = 1; | |
1134b854 | 246 | } |
0788646c | 247 | else |
d366d2e4 | 248 | { |
0788646c | 249 | int multibyte_p |
4b4deea2 | 250 | = !NILP (BVAR (current_buffer, enable_multibyte_characters)); |
177c0ea7 | 251 | |
eb7b678b | 252 | setup_echo_area_for_printing (multibyte_p); |
0788646c | 253 | insert_char (ch); |
09125ef8 | 254 | message_dolog ((char *) str, len, 0, multibyte_p); |
d366d2e4 | 255 | } |
38010d50 | 256 | } |
38010d50 JB |
257 | } |
258 | ||
0788646c GM |
259 | |
260 | /* Output SIZE characters, SIZE_BYTE bytes from string PTR using | |
261 | method PRINTCHARFUN. If SIZE < 0, use the string length of PTR for | |
262 | both SIZE and SIZE_BYTE. PRINTCHARFUN nil means output to | |
263 | print_buffer. PRINTCHARFUN t means output to the echo area or to | |
264 | stdout if non-interactive. If neither nil nor t, call Lisp | |
265 | function PRINTCHARFUN for each character printed. MULTIBYTE | |
efb15f96 RS |
266 | non-zero means PTR contains multibyte characters. |
267 | ||
268 | In the case where PRINTCHARFUN is nil, it is safe for PTR to point | |
269 | to data in a Lisp string. Otherwise that is not safe. */ | |
0788646c | 270 | |
38010d50 | 271 | static void |
d311d28c | 272 | strout (const char *ptr, ptrdiff_t size, ptrdiff_t size_byte, |
1db5b1ad | 273 | Lisp_Object printcharfun) |
38010d50 | 274 | { |
087e3c46 | 275 | if (size < 0) |
dc2a0b79 | 276 | size_byte = size = strlen (ptr); |
087e3c46 | 277 | |
0788646c | 278 | if (NILP (printcharfun)) |
38010d50 | 279 | { |
d311d28c PE |
280 | ptrdiff_t incr = size_byte - (print_buffer_size - print_buffer_pos_byte); |
281 | if (0 < incr) | |
282 | print_buffer = xpalloc (print_buffer, &print_buffer_size, incr, -1, 1); | |
72af86bd | 283 | memcpy (print_buffer + print_buffer_pos_byte, ptr, size_byte); |
6fec5601 | 284 | print_buffer_pos += size; |
dc2a0b79 | 285 | print_buffer_pos_byte += size_byte; |
38010d50 | 286 | } |
7b0cb8a0 | 287 | else if (noninteractive && EQ (printcharfun, Qt)) |
38010d50 | 288 | { |
0788646c GM |
289 | fwrite (ptr, 1, size_byte, stdout); |
290 | noninteractive_need_newline = 1; | |
291 | } | |
292 | else if (EQ (printcharfun, Qt)) | |
293 | { | |
294 | /* Output to echo area. We're trying to avoid a little overhead | |
295 | here, that's the reason we don't call printchar to do the | |
296 | job. */ | |
297 | int i; | |
298 | int multibyte_p | |
4b4deea2 | 299 | = !NILP (BVAR (current_buffer, enable_multibyte_characters)); |
177c0ea7 | 300 | |
eb7b678b | 301 | setup_echo_area_for_printing (multibyte_p); |
0788646c | 302 | message_dolog (ptr, size_byte, 0, multibyte_p); |
177c0ea7 | 303 | |
0788646c | 304 | if (size == size_byte) |
38010d50 | 305 | { |
0788646c | 306 | for (i = 0; i < size; ++i) |
bcdbfd36 | 307 | insert_char ((unsigned char) *ptr++); |
38010d50 | 308 | } |
0788646c | 309 | else |
38010d50 | 310 | { |
0788646c GM |
311 | int len; |
312 | for (i = 0; i < size_byte; i += len) | |
aec2b95b | 313 | { |
09125ef8 PE |
314 | int ch = STRING_CHAR_AND_LENGTH ((const unsigned char *) ptr + i, |
315 | len); | |
0788646c | 316 | insert_char (ch); |
aec2b95b | 317 | } |
38010d50 | 318 | } |
0788646c GM |
319 | } |
320 | else | |
321 | { | |
322 | /* PRINTCHARFUN is a Lisp function. */ | |
d311d28c | 323 | ptrdiff_t i = 0; |
38010d50 | 324 | |
0788646c | 325 | if (size == size_byte) |
4ad8bb20 | 326 | { |
0788646c | 327 | while (i < size_byte) |
4ad8bb20 | 328 | { |
0788646c GM |
329 | int ch = ptr[i++]; |
330 | PRINTCHAR (ch); | |
4ad8bb20 | 331 | } |
4ad8bb20 | 332 | } |
0788646c | 333 | else |
087e3c46 | 334 | { |
0788646c GM |
335 | while (i < size_byte) |
336 | { | |
337 | /* Here, we must convert each multi-byte form to the | |
338 | corresponding character code before handing it to | |
339 | PRINTCHAR. */ | |
340 | int len; | |
09125ef8 PE |
341 | int ch = STRING_CHAR_AND_LENGTH ((const unsigned char *) ptr + i, |
342 | len); | |
0788646c GM |
343 | PRINTCHAR (ch); |
344 | i += len; | |
345 | } | |
087e3c46 | 346 | } |
38010d50 | 347 | } |
38010d50 JB |
348 | } |
349 | ||
350 | /* Print the contents of a string STRING using PRINTCHARFUN. | |
ed2c35ef RS |
351 | It isn't safe to use strout in many cases, |
352 | because printing one char can relocate. */ | |
38010d50 | 353 | |
dc2a0b79 | 354 | static void |
971de7fb | 355 | print_string (Lisp_Object string, Lisp_Object printcharfun) |
38010d50 | 356 | { |
6fec5601 | 357 | if (EQ (printcharfun, Qt) || NILP (printcharfun)) |
375fcc09 | 358 | { |
d311d28c | 359 | ptrdiff_t chars; |
375fcc09 | 360 | |
8b4b4467 KH |
361 | if (print_escape_nonascii) |
362 | string = string_escape_byte8 (string); | |
363 | ||
375fcc09 | 364 | if (STRING_MULTIBYTE (string)) |
d5db4077 | 365 | chars = SCHARS (string); |
8b4b4467 KH |
366 | else if (! print_escape_nonascii |
367 | && (EQ (printcharfun, Qt) | |
4b4deea2 TT |
368 | ? ! NILP (BVAR (&buffer_defaults, enable_multibyte_characters)) |
369 | : ! NILP (BVAR (current_buffer, enable_multibyte_characters)))) | |
a76ef35d KH |
370 | { |
371 | /* If unibyte string STRING contains 8-bit codes, we must | |
372 | convert STRING to a multibyte string containing the same | |
373 | character codes. */ | |
374 | Lisp_Object newstr; | |
d311d28c | 375 | ptrdiff_t bytes; |
a76ef35d | 376 | |
d5db4077 | 377 | chars = SBYTES (string); |
de883a70 | 378 | bytes = count_size_as_multibyte (SDATA (string), chars); |
a76ef35d KH |
379 | if (chars < bytes) |
380 | { | |
381 | newstr = make_uninit_multibyte_string (chars, bytes); | |
72af86bd | 382 | memcpy (SDATA (newstr), SDATA (string), chars); |
d5db4077 | 383 | str_to_multibyte (SDATA (newstr), bytes, chars); |
a76ef35d KH |
384 | string = newstr; |
385 | } | |
386 | } | |
375fcc09 | 387 | else |
d5db4077 | 388 | chars = SBYTES (string); |
375fcc09 | 389 | |
efb15f96 RS |
390 | if (EQ (printcharfun, Qt)) |
391 | { | |
392 | /* Output to echo area. */ | |
d311d28c | 393 | ptrdiff_t nbytes = SBYTES (string); |
efb15f96 RS |
394 | |
395 | /* Copy the string contents so that relocation of STRING by | |
396 | GC does not cause trouble. */ | |
397 | USE_SAFE_ALLOCA; | |
98c6f1e3 | 398 | char *buffer = SAFE_ALLOCA (nbytes); |
72af86bd | 399 | memcpy (buffer, SDATA (string), nbytes); |
efb15f96 | 400 | |
98c6f1e3 | 401 | strout (buffer, chars, nbytes, printcharfun); |
efb15f96 RS |
402 | |
403 | SAFE_FREE (); | |
404 | } | |
405 | else | |
406 | /* No need to copy, since output to print_buffer can't GC. */ | |
1db5b1ad | 407 | strout (SSDATA (string), chars, SBYTES (string), printcharfun); |
375fcc09 | 408 | } |
38010d50 JB |
409 | else |
410 | { | |
dc2a0b79 RS |
411 | /* Otherwise, string may be relocated by printing one char. |
412 | So re-fetch the string address for each character. */ | |
d311d28c PE |
413 | ptrdiff_t i; |
414 | ptrdiff_t size = SCHARS (string); | |
415 | ptrdiff_t size_byte = SBYTES (string); | |
38010d50 JB |
416 | struct gcpro gcpro1; |
417 | GCPRO1 (string); | |
dc2a0b79 RS |
418 | if (size == size_byte) |
419 | for (i = 0; i < size; i++) | |
d5db4077 | 420 | PRINTCHAR (SREF (string, i)); |
dc2a0b79 | 421 | else |
6b61353c | 422 | for (i = 0; i < size_byte; ) |
dc2a0b79 RS |
423 | { |
424 | /* Here, we must convert each multi-byte form to the | |
425 | corresponding character code before handing it to PRINTCHAR. */ | |
426 | int len; | |
62a6e103 | 427 | int ch = STRING_CHAR_AND_LENGTH (SDATA (string) + i, len); |
dc2a0b79 RS |
428 | PRINTCHAR (ch); |
429 | i += len; | |
430 | } | |
38010d50 JB |
431 | UNGCPRO; |
432 | } | |
433 | } | |
434 | \f | |
435 | DEFUN ("write-char", Fwrite_char, Swrite_char, 1, 2, 0, | |
8c1a1077 PJ |
436 | doc: /* Output character CHARACTER to stream PRINTCHARFUN. |
437 | PRINTCHARFUN defaults to the value of `standard-output' (which see). */) | |
5842a27b | 438 | (Lisp_Object character, Lisp_Object printcharfun) |
38010d50 | 439 | { |
081e0581 | 440 | PRINTDECLARE; |
38010d50 | 441 | |
10eebdbb | 442 | if (NILP (printcharfun)) |
38010d50 | 443 | printcharfun = Vstandard_output; |
b7826503 | 444 | CHECK_NUMBER (character); |
38010d50 | 445 | PRINTPREPARE; |
3738a371 | 446 | PRINTCHAR (XINT (character)); |
38010d50 | 447 | PRINTFINISH; |
3738a371 | 448 | return character; |
38010d50 JB |
449 | } |
450 | ||
dc2a0b79 RS |
451 | /* Used from outside of print.c to print a block of SIZE |
452 | single-byte chars at DATA on the default output stream. | |
38010d50 JB |
453 | Do not use this on the contents of a Lisp string. */ |
454 | ||
dc22f25e | 455 | void |
a8fe7202 | 456 | write_string (const char *data, int size) |
38010d50 | 457 | { |
081e0581 | 458 | PRINTDECLARE; |
38010d50 | 459 | Lisp_Object printcharfun; |
38010d50 JB |
460 | |
461 | printcharfun = Vstandard_output; | |
462 | ||
463 | PRINTPREPARE; | |
1db5b1ad | 464 | strout (data, size, size, printcharfun); |
38010d50 JB |
465 | PRINTFINISH; |
466 | } | |
467 | ||
2f7c71a1 AS |
468 | /* Used to print a block of SIZE single-byte chars at DATA on a |
469 | specified stream PRINTCHARFUN. | |
38010d50 JB |
470 | Do not use this on the contents of a Lisp string. */ |
471 | ||
2f7c71a1 | 472 | static void |
a8fe7202 | 473 | write_string_1 (const char *data, int size, Lisp_Object printcharfun) |
38010d50 | 474 | { |
081e0581 | 475 | PRINTDECLARE; |
38010d50 JB |
476 | |
477 | PRINTPREPARE; | |
1db5b1ad | 478 | strout (data, size, size, printcharfun); |
38010d50 JB |
479 | PRINTFINISH; |
480 | } | |
481 | ||
482 | ||
38010d50 | 483 | void |
971de7fb | 484 | temp_output_buffer_setup (const char *bufname) |
38010d50 | 485 | { |
d311d28c | 486 | ptrdiff_t count = SPECPDL_INDEX (); |
38010d50 JB |
487 | register struct buffer *old = current_buffer; |
488 | register Lisp_Object buf; | |
489 | ||
66322887 | 490 | record_unwind_current_buffer (); |
d9c21094 | 491 | |
38010d50 JB |
492 | Fset_buffer (Fget_buffer_create (build_string (bufname))); |
493 | ||
9756ea0f | 494 | Fkill_all_local_variables (); |
fd531951 | 495 | delete_all_overlays (current_buffer); |
39eb03f1 PE |
496 | bset_directory (current_buffer, BVAR (old, directory)); |
497 | bset_read_only (current_buffer, Qnil); | |
498 | bset_filename (current_buffer, Qnil); | |
499 | bset_undo_list (current_buffer, Qt); | |
fd318b54 DA |
500 | eassert (current_buffer->overlays_before == NULL); |
501 | eassert (current_buffer->overlays_after == NULL); | |
39eb03f1 PE |
502 | bset_enable_multibyte_characters |
503 | (current_buffer, BVAR (&buffer_defaults, enable_multibyte_characters)); | |
d28eab19 KH |
504 | specbind (Qinhibit_read_only, Qt); |
505 | specbind (Qinhibit_modification_hooks, Qt); | |
38010d50 | 506 | Ferase_buffer (); |
633307b5 | 507 | XSETBUFFER (buf, current_buffer); |
38010d50 | 508 | |
98040cf1 | 509 | Frun_hooks (1, &Qtemp_buffer_setup_hook); |
d9c21094 RS |
510 | |
511 | unbind_to (count, Qnil); | |
512 | ||
513 | specbind (Qstandard_output, buf); | |
38010d50 | 514 | } |
38010d50 | 515 | \f |
971de7fb DN |
516 | static void print (Lisp_Object obj, register Lisp_Object printcharfun, int escapeflag); |
517 | static void print_preprocess (Lisp_Object obj); | |
518 | static void print_preprocess_string (INTERVAL interval, Lisp_Object arg); | |
519 | static void print_object (Lisp_Object obj, register Lisp_Object printcharfun, int escapeflag); | |
38010d50 | 520 | |
a7ca3326 | 521 | DEFUN ("terpri", Fterpri, Sterpri, 0, 1, 0, |
8c1a1077 PJ |
522 | doc: /* Output a newline to stream PRINTCHARFUN. |
523 | If PRINTCHARFUN is omitted or nil, the value of `standard-output' is used. */) | |
5842a27b | 524 | (Lisp_Object printcharfun) |
38010d50 | 525 | { |
081e0581 | 526 | PRINTDECLARE; |
38010d50 | 527 | |
10eebdbb | 528 | if (NILP (printcharfun)) |
38010d50 JB |
529 | printcharfun = Vstandard_output; |
530 | PRINTPREPARE; | |
531 | PRINTCHAR ('\n'); | |
532 | PRINTFINISH; | |
533 | return Qt; | |
534 | } | |
535 | ||
a7ca3326 | 536 | DEFUN ("prin1", Fprin1, Sprin1, 1, 2, 0, |
8c1a1077 PJ |
537 | doc: /* Output the printed representation of OBJECT, any Lisp object. |
538 | Quoting characters are printed when needed to make output that `read' | |
9474c847 | 539 | can handle, whenever this is possible. For complex objects, the behavior |
7fab9223 | 540 | is controlled by `print-level' and `print-length', which see. |
8c1a1077 PJ |
541 | |
542 | OBJECT is any of the Lisp data types: a number, a string, a symbol, | |
543 | a list, a buffer, a window, a frame, etc. | |
544 | ||
545 | A printed representation of an object is text which describes that object. | |
546 | ||
547 | Optional argument PRINTCHARFUN is the output stream, which can be one | |
548 | of these: | |
549 | ||
550 | - a buffer, in which case output is inserted into that buffer at point; | |
551 | - a marker, in which case output is inserted at marker's position; | |
552 | - a function, in which case that function is called once for each | |
553 | character of OBJECT's printed representation; | |
554 | - a symbol, in which case that symbol's function definition is called; or | |
555 | - t, in which case the output is displayed in the echo area. | |
556 | ||
557 | If PRINTCHARFUN is omitted, the value of `standard-output' (which see) | |
558 | is used instead. */) | |
5842a27b | 559 | (Lisp_Object object, Lisp_Object printcharfun) |
38010d50 | 560 | { |
081e0581 | 561 | PRINTDECLARE; |
38010d50 | 562 | |
10eebdbb | 563 | if (NILP (printcharfun)) |
38010d50 JB |
564 | printcharfun = Vstandard_output; |
565 | PRINTPREPARE; | |
3738a371 | 566 | print (object, printcharfun, 1); |
38010d50 | 567 | PRINTFINISH; |
3738a371 | 568 | return object; |
38010d50 JB |
569 | } |
570 | ||
571 | /* a buffer which is used to hold output being built by prin1-to-string */ | |
572 | Lisp_Object Vprin1_to_string_buffer; | |
573 | ||
a7ca3326 | 574 | DEFUN ("prin1-to-string", Fprin1_to_string, Sprin1_to_string, 1, 2, 0, |
fd4d156b RS |
575 | doc: /* Return a string containing the printed representation of OBJECT. |
576 | OBJECT can be any Lisp object. This function outputs quoting characters | |
cf393f9b | 577 | when necessary to make output that `read' can handle, whenever possible, |
a2612656 EZ |
578 | unless the optional second argument NOESCAPE is non-nil. For complex objects, |
579 | the behavior is controlled by `print-level' and `print-length', which see. | |
8c1a1077 PJ |
580 | |
581 | OBJECT is any of the Lisp data types: a number, a string, a symbol, | |
582 | a list, a buffer, a window, a frame, etc. | |
583 | ||
584 | A printed representation of an object is text which describes that object. */) | |
5842a27b | 585 | (Lisp_Object object, Lisp_Object noescape) |
38010d50 | 586 | { |
081e0581 | 587 | Lisp_Object printcharfun; |
fce31d69 | 588 | bool prev_abort_on_gc; |
ca2de342 RS |
589 | /* struct gcpro gcpro1, gcpro2; */ |
590 | Lisp_Object save_deactivate_mark; | |
d311d28c | 591 | ptrdiff_t count = SPECPDL_INDEX (); |
6b61353c | 592 | struct buffer *previous; |
ca2de342 RS |
593 | |
594 | specbind (Qinhibit_modification_hooks, Qt); | |
2a42e8f6 | 595 | |
6b61353c KH |
596 | { |
597 | PRINTDECLARE; | |
598 | ||
599 | /* Save and restore this--we are altering a buffer | |
600 | but we don't want to deactivate the mark just for that. | |
601 | No need for specbind, since errors deactivate the mark. */ | |
602 | save_deactivate_mark = Vdeactivate_mark; | |
603 | /* GCPRO2 (object, save_deactivate_mark); */ | |
fce31d69 PE |
604 | prev_abort_on_gc = abort_on_gc; |
605 | abort_on_gc = 1; | |
6b61353c KH |
606 | |
607 | printcharfun = Vprin1_to_string_buffer; | |
608 | PRINTPREPARE; | |
609 | print (object, printcharfun, NILP (noescape)); | |
6196cffe | 610 | /* Make Vprin1_to_string_buffer be the default buffer after PRINTFINISH */ |
6b61353c KH |
611 | PRINTFINISH; |
612 | } | |
38010d50 | 613 | |
6b61353c | 614 | previous = current_buffer; |
38010d50 | 615 | set_buffer_internal (XBUFFER (Vprin1_to_string_buffer)); |
3738a371 | 616 | object = Fbuffer_string (); |
b51ee131 SM |
617 | if (SBYTES (object) == SCHARS (object)) |
618 | STRING_SET_UNIBYTE (object); | |
38010d50 | 619 | |
28b8f740 | 620 | /* Note that this won't make prepare_to_modify_buffer call |
6b61353c KH |
621 | ask-user-about-supersession-threat because this buffer |
622 | does not visit a file. */ | |
38010d50 | 623 | Ferase_buffer (); |
6b61353c | 624 | set_buffer_internal (previous); |
2a42e8f6 | 625 | |
ca2de342 RS |
626 | Vdeactivate_mark = save_deactivate_mark; |
627 | /* UNGCPRO; */ | |
38010d50 | 628 | |
fce31d69 | 629 | abort_on_gc = prev_abort_on_gc; |
ca2de342 | 630 | return unbind_to (count, object); |
38010d50 JB |
631 | } |
632 | ||
ad64fc97 | 633 | DEFUN ("princ", Fprinc, Sprinc, 1, 2, 0, |
8c1a1077 PJ |
634 | doc: /* Output the printed representation of OBJECT, any Lisp object. |
635 | No quoting characters are used; no delimiters are printed around | |
636 | the contents of strings. | |
637 | ||
638 | OBJECT is any of the Lisp data types: a number, a string, a symbol, | |
639 | a list, a buffer, a window, a frame, etc. | |
640 | ||
641 | A printed representation of an object is text which describes that object. | |
642 | ||
643 | Optional argument PRINTCHARFUN is the output stream, which can be one | |
644 | of these: | |
645 | ||
646 | - a buffer, in which case output is inserted into that buffer at point; | |
647 | - a marker, in which case output is inserted at marker's position; | |
648 | - a function, in which case that function is called once for each | |
649 | character of OBJECT's printed representation; | |
650 | - a symbol, in which case that symbol's function definition is called; or | |
651 | - t, in which case the output is displayed in the echo area. | |
652 | ||
653 | If PRINTCHARFUN is omitted, the value of `standard-output' (which see) | |
654 | is used instead. */) | |
5842a27b | 655 | (Lisp_Object object, Lisp_Object printcharfun) |
38010d50 | 656 | { |
081e0581 | 657 | PRINTDECLARE; |
38010d50 | 658 | |
10eebdbb | 659 | if (NILP (printcharfun)) |
38010d50 JB |
660 | printcharfun = Vstandard_output; |
661 | PRINTPREPARE; | |
3738a371 | 662 | print (object, printcharfun, 0); |
38010d50 | 663 | PRINTFINISH; |
3738a371 | 664 | return object; |
38010d50 JB |
665 | } |
666 | ||
a7ca3326 | 667 | DEFUN ("print", Fprint, Sprint, 1, 2, 0, |
8c1a1077 PJ |
668 | doc: /* Output the printed representation of OBJECT, with newlines around it. |
669 | Quoting characters are printed when needed to make output that `read' | |
9474c847 | 670 | can handle, whenever this is possible. For complex objects, the behavior |
7fab9223 | 671 | is controlled by `print-level' and `print-length', which see. |
8c1a1077 PJ |
672 | |
673 | OBJECT is any of the Lisp data types: a number, a string, a symbol, | |
674 | a list, a buffer, a window, a frame, etc. | |
675 | ||
676 | A printed representation of an object is text which describes that object. | |
677 | ||
678 | Optional argument PRINTCHARFUN is the output stream, which can be one | |
679 | of these: | |
680 | ||
681 | - a buffer, in which case output is inserted into that buffer at point; | |
682 | - a marker, in which case output is inserted at marker's position; | |
683 | - a function, in which case that function is called once for each | |
684 | character of OBJECT's printed representation; | |
685 | - a symbol, in which case that symbol's function definition is called; or | |
686 | - t, in which case the output is displayed in the echo area. | |
687 | ||
688 | If PRINTCHARFUN is omitted, the value of `standard-output' (which see) | |
689 | is used instead. */) | |
5842a27b | 690 | (Lisp_Object object, Lisp_Object printcharfun) |
38010d50 | 691 | { |
081e0581 | 692 | PRINTDECLARE; |
38010d50 JB |
693 | struct gcpro gcpro1; |
694 | ||
10eebdbb | 695 | if (NILP (printcharfun)) |
38010d50 | 696 | printcharfun = Vstandard_output; |
3738a371 | 697 | GCPRO1 (object); |
38010d50 | 698 | PRINTPREPARE; |
38010d50 | 699 | PRINTCHAR ('\n'); |
3738a371 | 700 | print (object, printcharfun, 1); |
38010d50 JB |
701 | PRINTCHAR ('\n'); |
702 | PRINTFINISH; | |
38010d50 | 703 | UNGCPRO; |
3738a371 | 704 | return object; |
38010d50 JB |
705 | } |
706 | ||
707 | /* The subroutine object for external-debugging-output is kept here | |
708 | for the convenience of the debugger. */ | |
709 | Lisp_Object Qexternal_debugging_output; | |
710 | ||
4746118a | 711 | DEFUN ("external-debugging-output", Fexternal_debugging_output, Sexternal_debugging_output, 1, 1, 0, |
8c1a1077 PJ |
712 | doc: /* Write CHARACTER to stderr. |
713 | You can call print while debugging emacs, and pass it this function | |
714 | to make it write to the debugging output. */) | |
5842a27b | 715 | (Lisp_Object character) |
38010d50 | 716 | { |
0fd11aa5 PE |
717 | CHECK_NUMBER (character); |
718 | putc (XINT (character) & 0xFF, stderr); | |
cd22039d RS |
719 | |
720 | #ifdef WINDOWSNT | |
721 | /* Send the output to a debugger (nothing happens if there isn't one). */ | |
945b0111 EZ |
722 | if (print_output_debug_flag) |
723 | { | |
724 | char buf[2] = {(char) XINT (character), '\0'}; | |
725 | OutputDebugString (buf); | |
726 | } | |
cd22039d RS |
727 | #endif |
728 | ||
38010d50 JB |
729 | return character; |
730 | } | |
cf1bb91b | 731 | |
c33f8948 RS |
732 | /* This function is never called. Its purpose is to prevent |
733 | print_output_debug_flag from being optimized away. */ | |
734 | ||
c38bf546 | 735 | extern void debug_output_compilation_hack (int) EXTERNALLY_VISIBLE; |
dae581bf | 736 | void |
971de7fb | 737 | debug_output_compilation_hack (int x) |
c33f8948 RS |
738 | { |
739 | print_output_debug_flag = x; | |
740 | } | |
6b61353c | 741 | |
cd3587c1 | 742 | #if defined (GNU_LINUX) |
6b61353c KH |
743 | |
744 | /* This functionality is not vitally important in general, so we rely on | |
745 | non-portable ability to use stderr as lvalue. */ | |
746 | ||
747 | #define WITH_REDIRECT_DEBUGGING_OUTPUT 1 | |
748 | ||
ad64fc97 | 749 | static FILE *initial_stderr_stream = NULL; |
6b61353c KH |
750 | |
751 | DEFUN ("redirect-debugging-output", Fredirect_debugging_output, Sredirect_debugging_output, | |
752 | 1, 2, | |
753 | "FDebug output file: \nP", | |
754 | doc: /* Redirect debugging output (stderr stream) to file FILE. | |
755 | If FILE is nil, reset target to the initial stderr stream. | |
756 | Optional arg APPEND non-nil (interactively, with prefix arg) means | |
28b8f740 | 757 | append to existing target file. */) |
5842a27b | 758 | (Lisp_Object file, Lisp_Object append) |
6b61353c KH |
759 | { |
760 | if (initial_stderr_stream != NULL) | |
f9467be3 YM |
761 | { |
762 | BLOCK_INPUT; | |
763 | fclose (stderr); | |
764 | UNBLOCK_INPUT; | |
765 | } | |
6b61353c KH |
766 | stderr = initial_stderr_stream; |
767 | initial_stderr_stream = NULL; | |
768 | ||
769 | if (STRINGP (file)) | |
770 | { | |
771 | file = Fexpand_file_name (file, Qnil); | |
772 | initial_stderr_stream = stderr; | |
d4d7173a | 773 | stderr = fopen (SSDATA (file), NILP (append) ? "w" : "a"); |
6b61353c KH |
774 | if (stderr == NULL) |
775 | { | |
776 | stderr = initial_stderr_stream; | |
777 | initial_stderr_stream = NULL; | |
778 | report_file_error ("Cannot open debugging output stream", | |
779 | Fcons (file, Qnil)); | |
780 | } | |
781 | } | |
782 | return Qnil; | |
783 | } | |
784 | #endif /* GNU_LINUX */ | |
785 | ||
786 | ||
cf1bb91b RS |
787 | /* This is the interface for debugging printing. */ |
788 | ||
789 | void | |
971de7fb | 790 | debug_print (Lisp_Object arg) |
cf1bb91b RS |
791 | { |
792 | Fprin1 (arg, Qexternal_debugging_output); | |
3684eb78 | 793 | fprintf (stderr, "\r\n"); |
cf1bb91b | 794 | } |
bd90dcd0 | 795 | |
e3b27b31 | 796 | void safe_debug_print (Lisp_Object) EXTERNALLY_VISIBLE; |
bd90dcd0 | 797 | void |
971de7fb | 798 | safe_debug_print (Lisp_Object arg) |
bd90dcd0 KS |
799 | { |
800 | int valid = valid_lisp_object_p (arg); | |
801 | ||
802 | if (valid > 0) | |
803 | debug_print (arg); | |
804 | else | |
c2982e87 | 805 | fprintf (stderr, "#<%s_LISP_OBJECT 0x%08"pI"x>\r\n", |
bd90dcd0 | 806 | !valid ? "INVALID" : "SOME", |
c2982e87 | 807 | XHASH (arg)); |
bd90dcd0 KS |
808 | } |
809 | ||
38010d50 | 810 | \f |
a7ca3326 | 811 | DEFUN ("error-message-string", Ferror_message_string, Serror_message_string, |
113620cc | 812 | 1, 1, 0, |
6b61353c KH |
813 | doc: /* Convert an error value (ERROR-SYMBOL . DATA) to an error message. |
814 | See Info anchor `(elisp)Definition of signal' for some details on how this | |
815 | error message is constructed. */) | |
5842a27b | 816 | (Lisp_Object obj) |
113620cc KH |
817 | { |
818 | struct buffer *old = current_buffer; | |
63fbf4ff | 819 | Lisp_Object value; |
113620cc KH |
820 | struct gcpro gcpro1; |
821 | ||
0872e11f RS |
822 | /* If OBJ is (error STRING), just return STRING. |
823 | That is not only faster, it also avoids the need to allocate | |
824 | space here when the error is due to memory full. */ | |
94b342ce KR |
825 | if (CONSP (obj) && EQ (XCAR (obj), Qerror) |
826 | && CONSP (XCDR (obj)) | |
827 | && STRINGP (XCAR (XCDR (obj))) | |
828 | && NILP (XCDR (XCDR (obj)))) | |
829 | return XCAR (XCDR (obj)); | |
0872e11f | 830 | |
240e806c | 831 | print_error_message (obj, Vprin1_to_string_buffer, 0, Qnil); |
113620cc KH |
832 | |
833 | set_buffer_internal (XBUFFER (Vprin1_to_string_buffer)); | |
834 | value = Fbuffer_string (); | |
835 | ||
836 | GCPRO1 (value); | |
837 | Ferase_buffer (); | |
838 | set_buffer_internal (old); | |
839 | UNGCPRO; | |
840 | ||
841 | return value; | |
842 | } | |
843 | ||
c02279de | 844 | /* Print an error message for the error DATA onto Lisp output stream |
7ab56fea RS |
845 | STREAM (suitable for the print functions). |
846 | CONTEXT is a C string describing the context of the error. | |
847 | CALLER is the Lisp function inside which the error was signaled. */ | |
113620cc | 848 | |
dc22f25e | 849 | void |
a8fe7202 AS |
850 | print_error_message (Lisp_Object data, Lisp_Object stream, const char *context, |
851 | Lisp_Object caller) | |
113620cc KH |
852 | { |
853 | Lisp_Object errname, errmsg, file_error, tail; | |
854 | struct gcpro gcpro1; | |
113620cc | 855 | |
240e806c RS |
856 | if (context != 0) |
857 | write_string_1 (context, -1, stream); | |
858 | ||
859 | /* If we know from where the error was signaled, show it in | |
860 | *Messages*. */ | |
861 | if (!NILP (caller) && SYMBOLP (caller)) | |
862 | { | |
11fb15d5 | 863 | Lisp_Object cname = SYMBOL_NAME (caller); |
98c6f1e3 | 864 | ptrdiff_t cnamelen = SBYTES (cname); |
d311d28c | 865 | USE_SAFE_ALLOCA; |
98c6f1e3 PE |
866 | char *name = SAFE_ALLOCA (cnamelen); |
867 | memcpy (name, SDATA (cname), cnamelen); | |
868 | message_dolog (name, cnamelen, 0, 0); | |
240e806c | 869 | message_dolog (": ", 2, 0, 0); |
d311d28c | 870 | SAFE_FREE (); |
240e806c RS |
871 | } |
872 | ||
113620cc KH |
873 | errname = Fcar (data); |
874 | ||
875 | if (EQ (errname, Qerror)) | |
876 | { | |
877 | data = Fcdr (data); | |
c02279de GM |
878 | if (!CONSP (data)) |
879 | data = Qnil; | |
113620cc KH |
880 | errmsg = Fcar (data); |
881 | file_error = Qnil; | |
882 | } | |
883 | else | |
884 | { | |
71873e2b | 885 | Lisp_Object error_conditions = Fget (errname, Qerror_conditions); |
113620cc | 886 | errmsg = Fget (errname, Qerror_message); |
c02279de | 887 | file_error = Fmemq (Qfile_error, error_conditions); |
113620cc KH |
888 | } |
889 | ||
890 | /* Print an error message including the data items. */ | |
891 | ||
892 | tail = Fcdr_safe (data); | |
893 | GCPRO1 (tail); | |
894 | ||
895 | /* For file-error, make error message by concatenating | |
896 | all the data items. They are all strings. */ | |
8c29413d | 897 | if (!NILP (file_error) && CONSP (tail)) |
94b342ce | 898 | errmsg = XCAR (tail), tail = XCDR (tail); |
113620cc | 899 | |
71873e2b SM |
900 | { |
901 | const char *sep = ": "; | |
902 | ||
903 | if (!STRINGP (errmsg)) | |
904 | write_string_1 ("peculiar error", -1, stream); | |
905 | else if (SCHARS (errmsg)) | |
906 | Fprinc (errmsg, stream); | |
907 | else | |
908 | sep = NULL; | |
909 | ||
910 | for (; CONSP (tail); tail = XCDR (tail), sep = ", ") | |
911 | { | |
912 | Lisp_Object obj; | |
99027bdd | 913 | |
71873e2b SM |
914 | if (sep) |
915 | write_string_1 (sep, 2, stream); | |
916 | obj = XCAR (tail); | |
917 | if (!NILP (file_error) | |
918 | || EQ (errname, Qend_of_file) || EQ (errname, Quser_error)) | |
919 | Fprinc (obj, stream); | |
920 | else | |
921 | Fprin1 (obj, stream); | |
922 | } | |
923 | } | |
177c0ea7 | 924 | |
113620cc KH |
925 | UNGCPRO; |
926 | } | |
38010d50 | 927 | |
c02279de GM |
928 | |
929 | \f | |
38010d50 | 930 | /* |
edb2a707 | 931 | * The buffer should be at least as large as the max string size of the |
8e6208c5 | 932 | * largest float, printed in the biggest notation. This is undoubtedly |
38010d50 JB |
933 | * 20d float_output_format, with the negative of the C-constant "HUGE" |
934 | * from <math.h>. | |
177c0ea7 | 935 | * |
38010d50 | 936 | * On the vax the worst case is -1e38 in 20d format which takes 61 bytes. |
177c0ea7 | 937 | * |
38010d50 JB |
938 | * I assume that IEEE-754 format numbers can take 329 bytes for the worst |
939 | * case of -1e307 in 20d float_output_format. What is one to do (short of | |
940 | * re-writing _doprnt to be more sane)? | |
941 | * -wsr | |
6e8e6bf2 | 942 | * Given the above, the buffer must be least FLOAT_TO_STRING_BUFSIZE bytes. |
38010d50 | 943 | */ |
edb2a707 | 944 | |
99027bdd | 945 | int |
57ace6d0 | 946 | float_to_string (char *buf, double data) |
38010d50 | 947 | { |
57ace6d0 | 948 | char *cp; |
322890c4 | 949 | int width; |
99027bdd | 950 | int len; |
177c0ea7 | 951 | |
7f45de2d RS |
952 | /* Check for plus infinity in a way that won't lose |
953 | if there is no plus infinity. */ | |
954 | if (data == data / 2 && data > 1.0) | |
955 | { | |
99027bdd PE |
956 | static char const infinity_string[] = "1.0e+INF"; |
957 | strcpy (buf, infinity_string); | |
958 | return sizeof infinity_string - 1; | |
7f45de2d RS |
959 | } |
960 | /* Likewise for minus infinity. */ | |
961 | if (data == data / 2 && data < -1.0) | |
962 | { | |
99027bdd PE |
963 | static char const minus_infinity_string[] = "-1.0e+INF"; |
964 | strcpy (buf, minus_infinity_string); | |
965 | return sizeof minus_infinity_string - 1; | |
7f45de2d RS |
966 | } |
967 | /* Check for NaN in a way that won't fail if there are no NaNs. */ | |
968 | if (! (data * 0.0 >= 0.0)) | |
969 | { | |
68c45bf0 PE |
970 | /* Prepend "-" if the NaN's sign bit is negative. |
971 | The sign bit of a double is the bit that is 1 in -0.0. */ | |
99027bdd | 972 | static char const NaN_string[] = "0.0e+NaN"; |
68c45bf0 PE |
973 | int i; |
974 | union { double d; char c[sizeof (double)]; } u_data, u_minus_zero; | |
99027bdd | 975 | int negative = 0; |
68c45bf0 PE |
976 | u_data.d = data; |
977 | u_minus_zero.d = - 0.0; | |
978 | for (i = 0; i < sizeof (double); i++) | |
979 | if (u_data.c[i] & u_minus_zero.c[i]) | |
980 | { | |
99027bdd PE |
981 | *buf = '-'; |
982 | negative = 1; | |
68c45bf0 PE |
983 | break; |
984 | } | |
177c0ea7 | 985 | |
99027bdd PE |
986 | strcpy (buf + negative, NaN_string); |
987 | return negative + sizeof NaN_string - 1; | |
7f45de2d RS |
988 | } |
989 | ||
10eebdbb | 990 | if (NILP (Vfloat_output_format) |
d4ae1f7e | 991 | || !STRINGP (Vfloat_output_format)) |
38010d50 | 992 | lose: |
322890c4 | 993 | { |
f356c3fb | 994 | /* Generate the fewest number of digits that represent the |
6e8e6bf2 | 995 | floating point value without losing information. */ |
99027bdd | 996 | len = dtoastr (buf, FLOAT_TO_STRING_BUFSIZE - 2, 0, 0, data); |
75b43359 MWD |
997 | /* The decimal point must be printed, or the byte compiler can |
998 | get confused (Bug#8033). */ | |
999 | width = 1; | |
322890c4 | 1000 | } |
38010d50 JB |
1001 | else /* oink oink */ |
1002 | { | |
1003 | /* Check that the spec we have is fully valid. | |
1004 | This means not only valid for printf, | |
1005 | but meant for floats, and reasonable. */ | |
57ace6d0 | 1006 | cp = SSDATA (Vfloat_output_format); |
38010d50 JB |
1007 | |
1008 | if (cp[0] != '%') | |
1009 | goto lose; | |
1010 | if (cp[1] != '.') | |
1011 | goto lose; | |
1012 | ||
1013 | cp += 2; | |
c7b14277 JB |
1014 | |
1015 | /* Check the width specification. */ | |
322890c4 | 1016 | width = -1; |
c7b14277 | 1017 | if ('0' <= *cp && *cp <= '9') |
381cd4bb KH |
1018 | { |
1019 | width = 0; | |
1020 | do | |
aca216ff PE |
1021 | { |
1022 | width = (width * 10) + (*cp++ - '0'); | |
1023 | if (DBL_DIG < width) | |
1024 | goto lose; | |
1025 | } | |
381cd4bb KH |
1026 | while (*cp >= '0' && *cp <= '9'); |
1027 | ||
1028 | /* A precision of zero is valid only for %f. */ | |
aca216ff | 1029 | if (width == 0 && *cp != 'f') |
381cd4bb KH |
1030 | goto lose; |
1031 | } | |
38010d50 JB |
1032 | |
1033 | if (*cp != 'e' && *cp != 'f' && *cp != 'g') | |
1034 | goto lose; | |
1035 | ||
38010d50 JB |
1036 | if (cp[1] != 0) |
1037 | goto lose; | |
1038 | ||
99027bdd | 1039 | len = sprintf (buf, SSDATA (Vfloat_output_format), data); |
38010d50 | 1040 | } |
edb2a707 | 1041 | |
c7b14277 JB |
1042 | /* Make sure there is a decimal point with digit after, or an |
1043 | exponent, so that the value is readable as a float. But don't do | |
322890c4 RS |
1044 | this with "%.0f"; it's valid for that not to produce a decimal |
1045 | point. Note that width can be 0 only for %.0f. */ | |
1046 | if (width != 0) | |
0601fd3d | 1047 | { |
c7b14277 JB |
1048 | for (cp = buf; *cp; cp++) |
1049 | if ((*cp < '0' || *cp > '9') && *cp != '-') | |
1050 | break; | |
0601fd3d | 1051 | |
c7b14277 JB |
1052 | if (*cp == '.' && cp[1] == 0) |
1053 | { | |
1054 | cp[1] = '0'; | |
1055 | cp[2] = 0; | |
99027bdd | 1056 | len++; |
c7b14277 | 1057 | } |
75b43359 | 1058 | else if (*cp == 0) |
c7b14277 JB |
1059 | { |
1060 | *cp++ = '.'; | |
1061 | *cp++ = '0'; | |
1062 | *cp++ = 0; | |
99027bdd | 1063 | len += 2; |
c7b14277 | 1064 | } |
edb2a707 | 1065 | } |
99027bdd PE |
1066 | |
1067 | return len; | |
38010d50 | 1068 | } |
cc94f3b2 | 1069 | |
38010d50 JB |
1070 | \f |
1071 | static void | |
971de7fb | 1072 | print (Lisp_Object obj, register Lisp_Object printcharfun, int escapeflag) |
38010d50 | 1073 | { |
a22dec27 | 1074 | new_backquote_output = 0; |
38010d50 | 1075 | |
0f25ecc6 RS |
1076 | /* Reset print_number_index and Vprint_number_table only when |
1077 | the variable Vprint_continuous_numbering is nil. Otherwise, | |
1078 | the values of these variables will be kept between several | |
1079 | print functions. */ | |
e026eb89 KS |
1080 | if (NILP (Vprint_continuous_numbering) |
1081 | || NILP (Vprint_number_table)) | |
0f25ecc6 RS |
1082 | { |
1083 | print_number_index = 0; | |
1084 | Vprint_number_table = Qnil; | |
1085 | } | |
38010d50 | 1086 | |
0f25ecc6 RS |
1087 | /* Construct Vprint_number_table for print-gensym and print-circle. */ |
1088 | if (!NILP (Vprint_gensym) || !NILP (Vprint_circle)) | |
ec838c39 | 1089 | { |
e6d4cddd RS |
1090 | /* Construct Vprint_number_table. |
1091 | This increments print_number_index for the objects added. */ | |
9a6a4c40 | 1092 | print_depth = 0; |
0f25ecc6 | 1093 | print_preprocess (obj); |
e6d4cddd | 1094 | |
a65b85b5 SM |
1095 | if (HASH_TABLE_P (Vprint_number_table)) |
1096 | { /* Remove unnecessary objects, which appear only once in OBJ; | |
a179e3f7 | 1097 | that is, whose status is Qt. */ |
a65b85b5 | 1098 | struct Lisp_Hash_Table *h = XHASH_TABLE (Vprint_number_table); |
d311d28c | 1099 | ptrdiff_t i; |
e6d4cddd | 1100 | |
a65b85b5 SM |
1101 | for (i = 0; i < HASH_TABLE_SIZE (h); ++i) |
1102 | if (!NILP (HASH_HASH (h, i)) | |
1103 | && EQ (HASH_VALUE (h, i), Qt)) | |
1104 | Fremhash (HASH_KEY (h, i), Vprint_number_table); | |
1105 | } | |
0f25ecc6 RS |
1106 | } |
1107 | ||
9a6a4c40 | 1108 | print_depth = 0; |
0f25ecc6 RS |
1109 | print_object (obj, printcharfun, escapeflag); |
1110 | } | |
1111 | ||
fb103ca9 SM |
1112 | #define PRINT_CIRCLE_CANDIDATE_P(obj) \ |
1113 | (STRINGP (obj) || CONSP (obj) \ | |
1114 | || (VECTORLIKEP (obj) \ | |
1115 | && (VECTORP (obj) || COMPILEDP (obj) \ | |
1116 | || CHAR_TABLE_P (obj) || SUB_CHAR_TABLE_P (obj) \ | |
1117 | || HASH_TABLE_P (obj) || FONTP (obj))) \ | |
1118 | || (! NILP (Vprint_gensym) \ | |
1119 | && SYMBOLP (obj) \ | |
1120 | && !SYMBOL_INTERNED_P (obj))) | |
1121 | ||
0f25ecc6 RS |
1122 | /* Construct Vprint_number_table according to the structure of OBJ. |
1123 | OBJ itself and all its elements will be added to Vprint_number_table | |
1124 | recursively if it is a list, vector, compiled function, char-table, | |
1125 | string (its text properties will be traced), or a symbol that has | |
1126 | no obarray (this is for the print-gensym feature). | |
1127 | The status fields of Vprint_number_table mean whether each object appears | |
1128 | more than once in OBJ: Qnil at the first time, and Qt after that . */ | |
1129 | static void | |
971de7fb | 1130 | print_preprocess (Lisp_Object obj) |
0f25ecc6 | 1131 | { |
6b61353c | 1132 | int i; |
d311d28c | 1133 | ptrdiff_t size; |
c1132671 RS |
1134 | int loop_count = 0; |
1135 | Lisp_Object halftail; | |
1136 | ||
1137 | /* Avoid infinite recursion for circular nested structure | |
1138 | in the case where Vprint_circle is nil. */ | |
1139 | if (NILP (Vprint_circle)) | |
1140 | { | |
4ae29f89 SM |
1141 | /* Give up if we go so deep that print_object will get an error. */ |
1142 | /* See similar code in print_object. */ | |
1143 | if (print_depth >= PRINT_CIRCLE) | |
1144 | error ("Apparently circular structure being printed"); | |
1145 | ||
c1132671 RS |
1146 | for (i = 0; i < print_depth; i++) |
1147 | if (EQ (obj, being_printed[i])) | |
1148 | return; | |
1149 | being_printed[print_depth] = obj; | |
1150 | } | |
1151 | ||
c1132671 RS |
1152 | print_depth++; |
1153 | halftail = obj; | |
0f25ecc6 RS |
1154 | |
1155 | loop: | |
fb103ca9 | 1156 | if (PRINT_CIRCLE_CANDIDATE_P (obj)) |
0f25ecc6 | 1157 | { |
a65b85b5 SM |
1158 | if (!HASH_TABLE_P (Vprint_number_table)) |
1159 | { | |
1160 | Lisp_Object args[2]; | |
1161 | args[0] = QCtest; | |
1162 | args[1] = Qeq; | |
1163 | Vprint_number_table = Fmake_hash_table (2, args); | |
1164 | } | |
1165 | ||
aca2020b KH |
1166 | /* In case print-circle is nil and print-gensym is t, |
1167 | add OBJ to Vprint_number_table only when OBJ is a symbol. */ | |
1168 | if (! NILP (Vprint_circle) || SYMBOLP (obj)) | |
0f25ecc6 | 1169 | { |
a65b85b5 SM |
1170 | Lisp_Object num = Fgethash (obj, Vprint_number_table, Qnil); |
1171 | if (!NILP (num) | |
1172 | /* If Vprint_continuous_numbering is non-nil and OBJ is a gensym, | |
1173 | always print the gensym with a number. This is a special for | |
1174 | the lisp function byte-compile-output-docform. */ | |
1175 | || (!NILP (Vprint_continuous_numbering) | |
1176 | && SYMBOLP (obj) | |
1177 | && !SYMBOL_INTERNED_P (obj))) | |
1178 | { /* OBJ appears more than once. Let's remember that. */ | |
17870c01 | 1179 | if (!INTEGERP (num)) |
aca2020b | 1180 | { |
a65b85b5 SM |
1181 | print_number_index++; |
1182 | /* Negative number indicates it hasn't been printed yet. */ | |
1183 | Fputhash (obj, make_number (- print_number_index), | |
1184 | Vprint_number_table); | |
aca2020b | 1185 | } |
a65b85b5 SM |
1186 | print_depth--; |
1187 | return; | |
0f25ecc6 | 1188 | } |
a65b85b5 SM |
1189 | else |
1190 | /* OBJ is not yet recorded. Let's add to the table. */ | |
1191 | Fputhash (obj, Qt, Vprint_number_table); | |
0f25ecc6 | 1192 | } |
0f25ecc6 | 1193 | |
8e50cc2d | 1194 | switch (XTYPE (obj)) |
0f25ecc6 RS |
1195 | { |
1196 | case Lisp_String: | |
0f25ecc6 | 1197 | /* A string may have text properties, which can be circular. */ |
0c94c8d6 | 1198 | traverse_intervals_noorder (string_intervals (obj), |
8bbfc258 | 1199 | print_preprocess_string, Qnil); |
0f25ecc6 RS |
1200 | break; |
1201 | ||
1202 | case Lisp_Cons: | |
c1132671 RS |
1203 | /* Use HALFTAIL and LOOP_COUNT to detect circular lists, |
1204 | just as in print_object. */ | |
1205 | if (loop_count && EQ (obj, halftail)) | |
1206 | break; | |
0f25ecc6 RS |
1207 | print_preprocess (XCAR (obj)); |
1208 | obj = XCDR (obj); | |
c1132671 RS |
1209 | loop_count++; |
1210 | if (!(loop_count & 1)) | |
1211 | halftail = XCDR (halftail); | |
0f25ecc6 RS |
1212 | goto loop; |
1213 | ||
1214 | case Lisp_Vectorlike: | |
77b37c05 | 1215 | size = ASIZE (obj); |
6b61353c KH |
1216 | if (size & PSEUDOVECTOR_FLAG) |
1217 | size &= PSEUDOVECTOR_SIZE_MASK; | |
0f25ecc6 | 1218 | for (i = 0; i < size; i++) |
28be1ada | 1219 | print_preprocess (AREF (obj, i)); |
3e0de07f SM |
1220 | if (HASH_TABLE_P (obj)) |
1221 | { /* For hash tables, the key_and_value slot is past | |
a65b85b5 SM |
1222 | `size' because it needs to be marked specially in case |
1223 | the table is weak. */ | |
3e0de07f SM |
1224 | struct Lisp_Hash_Table *h = XHASH_TABLE (obj); |
1225 | print_preprocess (h->key_and_value); | |
1226 | } | |
ee5263af GM |
1227 | break; |
1228 | ||
1229 | default: | |
1230 | break; | |
0f25ecc6 RS |
1231 | } |
1232 | } | |
c1132671 | 1233 | print_depth--; |
0f25ecc6 RS |
1234 | } |
1235 | ||
0f25ecc6 | 1236 | static void |
971de7fb | 1237 | print_preprocess_string (INTERVAL interval, Lisp_Object arg) |
0f25ecc6 RS |
1238 | { |
1239 | print_preprocess (interval->plist); | |
1240 | } | |
0f25ecc6 | 1241 | |
971de7fb | 1242 | static void print_check_string_charset_prop (INTERVAL interval, Lisp_Object string); |
71ea13cb KH |
1243 | |
1244 | #define PRINT_STRING_NON_CHARSET_FOUND 1 | |
1245 | #define PRINT_STRING_UNSAFE_CHARSET_FOUND 2 | |
1246 | ||
4ae29f89 | 1247 | /* Bitwise or of the above macros. */ |
71ea13cb KH |
1248 | static int print_check_string_result; |
1249 | ||
1250 | static void | |
971de7fb | 1251 | print_check_string_charset_prop (INTERVAL interval, Lisp_Object string) |
71ea13cb KH |
1252 | { |
1253 | Lisp_Object val; | |
1254 | ||
1255 | if (NILP (interval->plist) | |
1256 | || (print_check_string_result == (PRINT_STRING_NON_CHARSET_FOUND | |
1257 | | PRINT_STRING_UNSAFE_CHARSET_FOUND))) | |
1258 | return; | |
1259 | for (val = interval->plist; CONSP (val) && ! EQ (XCAR (val), Qcharset); | |
1260 | val = XCDR (XCDR (val))); | |
1261 | if (! CONSP (val)) | |
1262 | { | |
1263 | print_check_string_result |= PRINT_STRING_NON_CHARSET_FOUND; | |
1264 | return; | |
1265 | } | |
1266 | if (! (print_check_string_result & PRINT_STRING_NON_CHARSET_FOUND)) | |
1267 | { | |
1268 | if (! EQ (val, interval->plist) | |
1269 | || CONSP (XCDR (XCDR (val)))) | |
1270 | print_check_string_result |= PRINT_STRING_NON_CHARSET_FOUND; | |
1271 | } | |
1272 | if (NILP (Vprint_charset_text_property) | |
1273 | || ! (print_check_string_result & PRINT_STRING_UNSAFE_CHARSET_FOUND)) | |
1274 | { | |
1275 | int i, c; | |
d311d28c PE |
1276 | ptrdiff_t charpos = interval->position; |
1277 | ptrdiff_t bytepos = string_char_to_byte (string, charpos); | |
71ea13cb KH |
1278 | Lisp_Object charset; |
1279 | ||
1280 | charset = XCAR (XCDR (val)); | |
1281 | for (i = 0; i < LENGTH (interval); i++) | |
1282 | { | |
1283 | FETCH_STRING_CHAR_ADVANCE (c, string, charpos, bytepos); | |
c80bcdbc KH |
1284 | if (! ASCII_CHAR_P (c) |
1285 | && ! EQ (CHARSET_NAME (CHAR_CHARSET (c)), charset)) | |
71ea13cb KH |
1286 | { |
1287 | print_check_string_result |= PRINT_STRING_UNSAFE_CHARSET_FOUND; | |
1288 | break; | |
1289 | } | |
1290 | } | |
1291 | } | |
1292 | } | |
1293 | ||
1294 | /* The value is (charset . nil). */ | |
1295 | static Lisp_Object print_prune_charset_plist; | |
1296 | ||
1297 | static Lisp_Object | |
971de7fb | 1298 | print_prune_string_charset (Lisp_Object string) |
71ea13cb KH |
1299 | { |
1300 | print_check_string_result = 0; | |
0c94c8d6 | 1301 | traverse_intervals (string_intervals (string), 0, |
71ea13cb KH |
1302 | print_check_string_charset_prop, string); |
1303 | if (! (print_check_string_result & PRINT_STRING_UNSAFE_CHARSET_FOUND)) | |
1304 | { | |
1305 | string = Fcopy_sequence (string); | |
1306 | if (print_check_string_result & PRINT_STRING_NON_CHARSET_FOUND) | |
1307 | { | |
1308 | if (NILP (print_prune_charset_plist)) | |
1309 | print_prune_charset_plist = Fcons (Qcharset, Qnil); | |
eabe04c0 KH |
1310 | Fremove_text_properties (make_number (0), |
1311 | make_number (SCHARS (string)), | |
71ea13cb KH |
1312 | print_prune_charset_plist, string); |
1313 | } | |
1314 | else | |
eabe04c0 KH |
1315 | Fset_text_properties (make_number (0), make_number (SCHARS (string)), |
1316 | Qnil, string); | |
71ea13cb KH |
1317 | } |
1318 | return string; | |
1319 | } | |
1320 | ||
0f25ecc6 | 1321 | static void |
971de7fb | 1322 | print_object (Lisp_Object obj, register Lisp_Object printcharfun, int escapeflag) |
0f25ecc6 | 1323 | { |
aca216ff PE |
1324 | char buf[max (sizeof "from..to..in " + 2 * INT_STRLEN_BOUND (EMACS_INT), |
1325 | max (sizeof " . #" + INT_STRLEN_BOUND (printmax_t), | |
1326 | 40))]; | |
0f25ecc6 RS |
1327 | |
1328 | QUIT; | |
1329 | ||
1330 | /* Detect circularities and truncate them. */ | |
4ae29f89 | 1331 | if (NILP (Vprint_circle)) |
0f25ecc6 | 1332 | { |
4ae29f89 SM |
1333 | /* Simple but incomplete way. */ |
1334 | int i; | |
1335 | ||
1336 | /* See similar code in print_preprocess. */ | |
1337 | if (print_depth >= PRINT_CIRCLE) | |
1338 | error ("Apparently circular structure being printed"); | |
1339 | ||
1340 | for (i = 0; i < print_depth; i++) | |
1341 | if (EQ (obj, being_printed[i])) | |
1342 | { | |
99027bdd PE |
1343 | int len = sprintf (buf, "#%d", i); |
1344 | strout (buf, len, len, printcharfun); | |
4ae29f89 SM |
1345 | return; |
1346 | } | |
1347 | being_printed[print_depth] = obj; | |
1348 | } | |
1349 | else if (PRINT_CIRCLE_CANDIDATE_P (obj)) | |
1350 | { | |
1351 | /* With the print-circle feature. */ | |
1352 | Lisp_Object num = Fgethash (obj, Vprint_number_table, Qnil); | |
1353 | if (INTEGERP (num)) | |
0f25ecc6 | 1354 | { |
4ae29f89 SM |
1355 | EMACS_INT n = XINT (num); |
1356 | if (n < 0) | |
1357 | { /* Add a prefix #n= if OBJ has not yet been printed; | |
1358 | that is, its status field is nil. */ | |
99027bdd PE |
1359 | int len = sprintf (buf, "#%"pI"d=", -n); |
1360 | strout (buf, len, len, printcharfun); | |
4ae29f89 SM |
1361 | /* OBJ is going to be printed. Remember that fact. */ |
1362 | Fputhash (obj, make_number (- n), Vprint_number_table); | |
1363 | } | |
1364 | else | |
a65b85b5 | 1365 | { |
4ae29f89 | 1366 | /* Just print #n# if OBJ has already been printed. */ |
99027bdd PE |
1367 | int len = sprintf (buf, "#%"pI"d#", n); |
1368 | strout (buf, len, len, printcharfun); | |
4ae29f89 | 1369 | return; |
a65b85b5 | 1370 | } |
0f25ecc6 | 1371 | } |
ec838c39 | 1372 | } |
ec838c39 | 1373 | |
38010d50 JB |
1374 | print_depth++; |
1375 | ||
8e50cc2d | 1376 | switch (XTYPE (obj)) |
38010d50 | 1377 | { |
2de9f71c | 1378 | case_Lisp_Int: |
99027bdd PE |
1379 | { |
1380 | int len = sprintf (buf, "%"pI"d", XINT (obj)); | |
1381 | strout (buf, len, len, printcharfun); | |
1382 | } | |
ca0569ad RS |
1383 | break; |
1384 | ||
ca0569ad RS |
1385 | case Lisp_Float: |
1386 | { | |
6e8e6bf2 | 1387 | char pigbuf[FLOAT_TO_STRING_BUFSIZE]; |
99027bdd PE |
1388 | int len = float_to_string (pigbuf, XFLOAT_DATA (obj)); |
1389 | strout (pigbuf, len, len, printcharfun); | |
ca0569ad RS |
1390 | } |
1391 | break; | |
ca0569ad RS |
1392 | |
1393 | case Lisp_String: | |
38010d50 JB |
1394 | if (!escapeflag) |
1395 | print_string (obj, printcharfun); | |
1396 | else | |
1397 | { | |
d311d28c | 1398 | register ptrdiff_t i_byte; |
38010d50 | 1399 | struct gcpro gcpro1; |
872a36d2 | 1400 | unsigned char *str; |
d311d28c | 1401 | ptrdiff_t size_byte; |
453fa987 RS |
1402 | /* 1 means we must ensure that the next character we output |
1403 | cannot be taken as part of a hex character escape. */ | |
1404 | int need_nonhex = 0; | |
db300f59 | 1405 | int multibyte = STRING_MULTIBYTE (obj); |
38010d50 | 1406 | |
7651e1f5 RS |
1407 | GCPRO1 (obj); |
1408 | ||
71ea13cb KH |
1409 | if (! EQ (Vprint_charset_text_property, Qt)) |
1410 | obj = print_prune_string_charset (obj); | |
1411 | ||
0c94c8d6 | 1412 | if (string_intervals (obj)) |
7651e1f5 RS |
1413 | { |
1414 | PRINTCHAR ('#'); | |
1415 | PRINTCHAR ('('); | |
1416 | } | |
38010d50 JB |
1417 | |
1418 | PRINTCHAR ('\"'); | |
d5db4077 KR |
1419 | str = SDATA (obj); |
1420 | size_byte = SBYTES (obj); | |
dc2a0b79 | 1421 | |
47553fa8 | 1422 | for (i_byte = 0; i_byte < size_byte;) |
38010d50 | 1423 | { |
6ddd6eee RS |
1424 | /* Here, we must convert each multi-byte form to the |
1425 | corresponding character code before handing it to PRINTCHAR. */ | |
1426 | int len; | |
dc2a0b79 RS |
1427 | int c; |
1428 | ||
db300f59 | 1429 | if (multibyte) |
872a36d2 | 1430 | { |
62a6e103 | 1431 | c = STRING_CHAR_AND_LENGTH (str + i_byte, len); |
8b4b4467 | 1432 | i_byte += len; |
872a36d2 | 1433 | } |
dc2a0b79 | 1434 | else |
872a36d2 | 1435 | c = str[i_byte++]; |
dc2a0b79 | 1436 | |
38010d50 | 1437 | QUIT; |
6ddd6eee | 1438 | |
38010d50 JB |
1439 | if (c == '\n' && print_escape_newlines) |
1440 | { | |
1441 | PRINTCHAR ('\\'); | |
1442 | PRINTCHAR ('n'); | |
1443 | } | |
c6f7982f RM |
1444 | else if (c == '\f' && print_escape_newlines) |
1445 | { | |
1446 | PRINTCHAR ('\\'); | |
1447 | PRINTCHAR ('f'); | |
1448 | } | |
ae7367d3 | 1449 | else if (multibyte |
42a5b22f | 1450 | && (CHAR_BYTE8_P (c) |
73af357a | 1451 | || (! ASCII_CHAR_P (c) && print_escape_multibyte))) |
dc2a0b79 RS |
1452 | { |
1453 | /* When multibyte is disabled, | |
ae7367d3 RS |
1454 | print multibyte string chars using hex escapes. |
1455 | For a char code that could be in a unibyte string, | |
1456 | when found in a multibyte string, always use a hex escape | |
1457 | so it reads back as multibyte. */ | |
09125ef8 | 1458 | char outbuf[50]; |
99027bdd | 1459 | int len; |
8b4b4467 KH |
1460 | |
1461 | if (CHAR_BYTE8_P (c)) | |
99027bdd | 1462 | len = sprintf (outbuf, "\\%03o", CHAR_TO_BYTE8 (c)); |
8b4b4467 | 1463 | else |
8f924df7 | 1464 | { |
99027bdd | 1465 | len = sprintf (outbuf, "\\x%04x", c); |
8f924df7 KH |
1466 | need_nonhex = 1; |
1467 | } | |
99027bdd | 1468 | strout (outbuf, len, len, printcharfun); |
dc2a0b79 | 1469 | } |
db300f59 RS |
1470 | else if (! multibyte |
1471 | && SINGLE_BYTE_CHAR_P (c) && ! ASCII_BYTE_P (c) | |
835d0be6 | 1472 | && print_escape_nonascii) |
974a6ff5 | 1473 | { |
835d0be6 RS |
1474 | /* When printing in a multibyte buffer |
1475 | or when explicitly requested, | |
974a6ff5 KH |
1476 | print single-byte non-ASCII string chars |
1477 | using octal escapes. */ | |
09125ef8 | 1478 | char outbuf[5]; |
99027bdd PE |
1479 | int len = sprintf (outbuf, "\\%03o", c); |
1480 | strout (outbuf, len, len, printcharfun); | |
974a6ff5 | 1481 | } |
38010d50 JB |
1482 | else |
1483 | { | |
453fa987 RS |
1484 | /* If we just had a hex escape, and this character |
1485 | could be taken as part of it, | |
1486 | output `\ ' to prevent that. */ | |
1b62edd6 KH |
1487 | if (need_nonhex) |
1488 | { | |
1489 | need_nonhex = 0; | |
1490 | if ((c >= 'a' && c <= 'f') | |
453fa987 | 1491 | || (c >= 'A' && c <= 'F') |
1b62edd6 | 1492 | || (c >= '0' && c <= '9')) |
1db5b1ad | 1493 | strout ("\\ ", -1, -1, printcharfun); |
1b62edd6 | 1494 | } |
453fa987 | 1495 | |
38010d50 JB |
1496 | if (c == '\"' || c == '\\') |
1497 | PRINTCHAR ('\\'); | |
1498 | PRINTCHAR (c); | |
1499 | } | |
1500 | } | |
1501 | PRINTCHAR ('\"'); | |
7651e1f5 | 1502 | |
0c94c8d6 | 1503 | if (string_intervals (obj)) |
7651e1f5 | 1504 | { |
0c94c8d6 | 1505 | traverse_intervals (string_intervals (obj), |
8bbfc258 | 1506 | 0, print_interval, printcharfun); |
7651e1f5 RS |
1507 | PRINTCHAR (')'); |
1508 | } | |
7651e1f5 | 1509 | |
38010d50 JB |
1510 | UNGCPRO; |
1511 | } | |
ca0569ad | 1512 | break; |
38010d50 | 1513 | |
ca0569ad RS |
1514 | case Lisp_Symbol: |
1515 | { | |
1516 | register int confusing; | |
d5db4077 KR |
1517 | register unsigned char *p = SDATA (SYMBOL_NAME (obj)); |
1518 | register unsigned char *end = p + SBYTES (SYMBOL_NAME (obj)); | |
2190a05e | 1519 | register int c; |
d311d28c PE |
1520 | ptrdiff_t i, i_byte; |
1521 | ptrdiff_t size_byte; | |
dc2a0b79 RS |
1522 | Lisp_Object name; |
1523 | ||
76d0b3ae | 1524 | name = SYMBOL_NAME (obj); |
ca0569ad RS |
1525 | |
1526 | if (p != end && (*p == '-' || *p == '+')) p++; | |
1527 | if (p == end) | |
1528 | confusing = 0; | |
d27497e3 RS |
1529 | /* If symbol name begins with a digit, and ends with a digit, |
1530 | and contains nothing but digits and `e', it could be treated | |
1531 | as a number. So set CONFUSING. | |
1532 | ||
1533 | Symbols that contain periods could also be taken as numbers, | |
1534 | but periods are always escaped, so we don't have to worry | |
1535 | about them here. */ | |
1536 | else if (*p >= '0' && *p <= '9' | |
1537 | && end[-1] >= '0' && end[-1] <= '9') | |
ca0569ad | 1538 | { |
e837058b RS |
1539 | while (p != end && ((*p >= '0' && *p <= '9') |
1540 | /* Needed for \2e10. */ | |
01a8d3fa | 1541 | || *p == 'e' || *p == 'E')) |
ca0569ad RS |
1542 | p++; |
1543 | confusing = (end == p); | |
1544 | } | |
d27497e3 RS |
1545 | else |
1546 | confusing = 0; | |
ca0569ad | 1547 | |
9a79b20c AS |
1548 | size_byte = SBYTES (name); |
1549 | ||
bf9f2aab | 1550 | if (! NILP (Vprint_gensym) && !SYMBOL_INTERNED_P (obj)) |
081e0581 | 1551 | { |
081e0581 EN |
1552 | PRINTCHAR ('#'); |
1553 | PRINTCHAR (':'); | |
1554 | } | |
9a79b20c AS |
1555 | else if (size_byte == 0) |
1556 | { | |
1557 | PRINTCHAR ('#'); | |
1558 | PRINTCHAR ('#'); | |
1559 | break; | |
1560 | } | |
dc2a0b79 RS |
1561 | |
1562 | for (i = 0, i_byte = 0; i_byte < size_byte;) | |
ca0569ad | 1563 | { |
6ddd6eee RS |
1564 | /* Here, we must convert each multi-byte form to the |
1565 | corresponding character code before handing it to PRINTCHAR. */ | |
eba90784 | 1566 | FETCH_STRING_CHAR_ADVANCE (c, name, i, i_byte); |
ca0569ad | 1567 | QUIT; |
09eddb56 | 1568 | |
ca0569ad RS |
1569 | if (escapeflag) |
1570 | { | |
09eddb56 RS |
1571 | if (c == '\"' || c == '\\' || c == '\'' |
1572 | || c == ';' || c == '#' || c == '(' || c == ')' | |
9a79b20c | 1573 | || c == ',' || c == '.' || c == '`' |
09eddb56 RS |
1574 | || c == '[' || c == ']' || c == '?' || c <= 040 |
1575 | || confusing) | |
ca0569ad RS |
1576 | PRINTCHAR ('\\'), confusing = 0; |
1577 | } | |
1578 | PRINTCHAR (c); | |
1579 | } | |
1580 | } | |
1581 | break; | |
1582 | ||
1583 | case Lisp_Cons: | |
38010d50 | 1584 | /* If deeper than spec'd depth, print placeholder. */ |
d4ae1f7e | 1585 | if (INTEGERP (Vprint_level) |
38010d50 | 1586 | && print_depth > XINT (Vprint_level)) |
1db5b1ad | 1587 | strout ("...", -1, -1, printcharfun); |
2f100b5c EN |
1588 | else if (print_quoted && CONSP (XCDR (obj)) && NILP (XCDR (XCDR (obj))) |
1589 | && (EQ (XCAR (obj), Qquote))) | |
1590 | { | |
1591 | PRINTCHAR ('\''); | |
0f25ecc6 | 1592 | print_object (XCAR (XCDR (obj)), printcharfun, escapeflag); |
2f100b5c EN |
1593 | } |
1594 | else if (print_quoted && CONSP (XCDR (obj)) && NILP (XCDR (XCDR (obj))) | |
1595 | && (EQ (XCAR (obj), Qfunction))) | |
1596 | { | |
1597 | PRINTCHAR ('#'); | |
1598 | PRINTCHAR ('\''); | |
0f25ecc6 | 1599 | print_object (XCAR (XCDR (obj)), printcharfun, escapeflag); |
2f100b5c EN |
1600 | } |
1601 | else if (print_quoted && CONSP (XCDR (obj)) && NILP (XCDR (XCDR (obj))) | |
a22dec27 SM |
1602 | && ((EQ (XCAR (obj), Qbackquote)))) |
1603 | { | |
1604 | print_object (XCAR (obj), printcharfun, 0); | |
1605 | new_backquote_output++; | |
1606 | print_object (XCAR (XCDR (obj)), printcharfun, escapeflag); | |
1607 | new_backquote_output--; | |
1608 | } | |
1609 | else if (print_quoted && CONSP (XCDR (obj)) && NILP (XCDR (XCDR (obj))) | |
1610 | && new_backquote_output | |
2f100b5c EN |
1611 | && ((EQ (XCAR (obj), Qbackquote) |
1612 | || EQ (XCAR (obj), Qcomma) | |
1613 | || EQ (XCAR (obj), Qcomma_at) | |
1614 | || EQ (XCAR (obj), Qcomma_dot)))) | |
1615 | { | |
0f25ecc6 | 1616 | print_object (XCAR (obj), printcharfun, 0); |
a22dec27 | 1617 | new_backquote_output--; |
0f25ecc6 | 1618 | print_object (XCAR (XCDR (obj)), printcharfun, escapeflag); |
a22dec27 | 1619 | new_backquote_output++; |
2f100b5c | 1620 | } |
e0f93814 | 1621 | else |
38010d50 | 1622 | { |
e0f93814 | 1623 | PRINTCHAR ('('); |
177c0ea7 | 1624 | |
38010d50 | 1625 | { |
aca216ff | 1626 | printmax_t i, print_length; |
1eab22b5 | 1627 | Lisp_Object halftail = obj; |
e0f93814 | 1628 | |
9ab8560d | 1629 | /* Negative values of print-length are invalid in CL. |
42ac1ed4 GM |
1630 | Treat them like nil, as CMUCL does. */ |
1631 | if (NATNUMP (Vprint_length)) | |
1632 | print_length = XFASTINT (Vprint_length); | |
1633 | else | |
aca216ff | 1634 | print_length = TYPE_MAXIMUM (printmax_t); |
42ac1ed4 GM |
1635 | |
1636 | i = 0; | |
e0f93814 | 1637 | while (CONSP (obj)) |
38010d50 | 1638 | { |
1eab22b5 | 1639 | /* Detect circular list. */ |
0f25ecc6 | 1640 | if (NILP (Vprint_circle)) |
1eab22b5 | 1641 | { |
53964682 | 1642 | /* Simple but incomplete way. */ |
0f25ecc6 RS |
1643 | if (i != 0 && EQ (obj, halftail)) |
1644 | { | |
99027bdd PE |
1645 | int len = sprintf (buf, " . #%"pMd, i / 2); |
1646 | strout (buf, len, len, printcharfun); | |
0f25ecc6 RS |
1647 | goto end_of_list; |
1648 | } | |
1649 | } | |
1650 | else | |
1651 | { | |
1652 | /* With the print-circle feature. */ | |
1653 | if (i != 0) | |
1654 | { | |
a65b85b5 SM |
1655 | Lisp_Object num = Fgethash (obj, Vprint_number_table, Qnil); |
1656 | if (INTEGERP (num)) | |
36de6a04 | 1657 | { |
1db5b1ad | 1658 | strout (" . ", 3, 3, printcharfun); |
36de6a04 AS |
1659 | print_object (obj, printcharfun, escapeflag); |
1660 | goto end_of_list; | |
1661 | } | |
0f25ecc6 | 1662 | } |
1eab22b5 | 1663 | } |
177c0ea7 | 1664 | |
aca216ff | 1665 | if (i) |
e0f93814 | 1666 | PRINTCHAR (' '); |
177c0ea7 | 1667 | |
aca216ff | 1668 | if (print_length <= i) |
e0f93814 | 1669 | { |
1db5b1ad | 1670 | strout ("...", 3, 3, printcharfun); |
0f25ecc6 | 1671 | goto end_of_list; |
e0f93814 | 1672 | } |
177c0ea7 | 1673 | |
aca216ff | 1674 | i++; |
0f25ecc6 | 1675 | print_object (XCAR (obj), printcharfun, escapeflag); |
177c0ea7 | 1676 | |
2f100b5c | 1677 | obj = XCDR (obj); |
1eab22b5 RS |
1678 | if (!(i & 1)) |
1679 | halftail = XCDR (halftail); | |
38010d50 | 1680 | } |
38010d50 | 1681 | } |
42ac1ed4 GM |
1682 | |
1683 | /* OBJ non-nil here means it's the end of a dotted list. */ | |
2f100b5c | 1684 | if (!NILP (obj)) |
e0f93814 | 1685 | { |
1db5b1ad | 1686 | strout (" . ", 3, 3, printcharfun); |
0f25ecc6 | 1687 | print_object (obj, printcharfun, escapeflag); |
e0f93814 | 1688 | } |
177c0ea7 | 1689 | |
0f25ecc6 | 1690 | end_of_list: |
e0f93814 | 1691 | PRINTCHAR (')'); |
38010d50 | 1692 | } |
ca0569ad RS |
1693 | break; |
1694 | ||
1695 | case Lisp_Vectorlike: | |
1696 | if (PROCESSP (obj)) | |
1697 | { | |
1698 | if (escapeflag) | |
1699 | { | |
1db5b1ad | 1700 | strout ("#<process ", -1, -1, printcharfun); |
4d2b044c | 1701 | print_string (XPROCESS (obj)->name, printcharfun); |
ca0569ad RS |
1702 | PRINTCHAR ('>'); |
1703 | } | |
1704 | else | |
4d2b044c | 1705 | print_string (XPROCESS (obj)->name, printcharfun); |
ca0569ad | 1706 | } |
ed2c35ef RS |
1707 | else if (BOOL_VECTOR_P (obj)) |
1708 | { | |
29ebea3b | 1709 | ptrdiff_t i; |
99027bdd PE |
1710 | int len; |
1711 | unsigned char c; | |
ed2c35ef | 1712 | struct gcpro gcpro1; |
d311d28c | 1713 | ptrdiff_t size_in_chars |
4b5af5e4 AS |
1714 | = ((XBOOL_VECTOR (obj)->size + BOOL_VECTOR_BITS_PER_CHAR - 1) |
1715 | / BOOL_VECTOR_BITS_PER_CHAR); | |
ed2c35ef RS |
1716 | |
1717 | GCPRO1 (obj); | |
1718 | ||
1719 | PRINTCHAR ('#'); | |
1720 | PRINTCHAR ('&'); | |
99027bdd PE |
1721 | len = sprintf (buf, "%"pI"d", XBOOL_VECTOR (obj)->size); |
1722 | strout (buf, len, len, printcharfun); | |
ed2c35ef | 1723 | PRINTCHAR ('\"'); |
a40384bc | 1724 | |
42ac1ed4 | 1725 | /* Don't print more characters than the specified maximum. |
9ab8560d | 1726 | Negative values of print-length are invalid. Treat them |
42ac1ed4 GM |
1727 | like a print-length of nil. */ |
1728 | if (NATNUMP (Vprint_length) | |
1729 | && XFASTINT (Vprint_length) < size_in_chars) | |
1730 | size_in_chars = XFASTINT (Vprint_length); | |
a40384bc | 1731 | |
ed2c35ef RS |
1732 | for (i = 0; i < size_in_chars; i++) |
1733 | { | |
1734 | QUIT; | |
1735 | c = XBOOL_VECTOR (obj)->data[i]; | |
f10fdbfd | 1736 | if (c == '\n' && print_escape_newlines) |
ed2c35ef RS |
1737 | { |
1738 | PRINTCHAR ('\\'); | |
1739 | PRINTCHAR ('n'); | |
1740 | } | |
1741 | else if (c == '\f' && print_escape_newlines) | |
1742 | { | |
1743 | PRINTCHAR ('\\'); | |
1744 | PRINTCHAR ('f'); | |
1745 | } | |
4b5af5e4 AS |
1746 | else if (c > '\177') |
1747 | { | |
1748 | /* Use octal escapes to avoid encoding issues. */ | |
1749 | PRINTCHAR ('\\'); | |
1750 | PRINTCHAR ('0' + ((c >> 6) & 3)); | |
1751 | PRINTCHAR ('0' + ((c >> 3) & 7)); | |
1752 | PRINTCHAR ('0' + (c & 7)); | |
1753 | } | |
ed2c35ef RS |
1754 | else |
1755 | { | |
1756 | if (c == '\"' || c == '\\') | |
1757 | PRINTCHAR ('\\'); | |
1758 | PRINTCHAR (c); | |
1759 | } | |
1760 | } | |
1761 | PRINTCHAR ('\"'); | |
1762 | ||
1763 | UNGCPRO; | |
1764 | } | |
ca0569ad RS |
1765 | else if (SUBRP (obj)) |
1766 | { | |
1db5b1ad JB |
1767 | strout ("#<subr ", -1, -1, printcharfun); |
1768 | strout (XSUBR (obj)->symbol_name, -1, -1, printcharfun); | |
ca0569ad RS |
1769 | PRINTCHAR ('>'); |
1770 | } | |
ca0569ad RS |
1771 | else if (WINDOWP (obj)) |
1772 | { | |
99027bdd | 1773 | int len; |
1db5b1ad | 1774 | strout ("#<window ", -1, -1, printcharfun); |
45942c7d | 1775 | len = sprintf (buf, "%d", XWINDOW (obj)->sequence_number); |
99027bdd | 1776 | strout (buf, len, len, printcharfun); |
d3d50620 | 1777 | if (!NILP (XWINDOW (obj)->buffer)) |
ca0569ad | 1778 | { |
1db5b1ad | 1779 | strout (" on ", -1, -1, printcharfun); |
d3d50620 | 1780 | print_string (BVAR (XBUFFER (XWINDOW (obj)->buffer), name), |
3a45383a | 1781 | printcharfun); |
ca0569ad RS |
1782 | } |
1783 | PRINTCHAR ('>'); | |
1784 | } | |
f76c8b1e SM |
1785 | else if (TERMINALP (obj)) |
1786 | { | |
99027bdd | 1787 | int len; |
f76c8b1e | 1788 | struct terminal *t = XTERMINAL (obj); |
1db5b1ad | 1789 | strout ("#<terminal ", -1, -1, printcharfun); |
99027bdd PE |
1790 | len = sprintf (buf, "%d", t->id); |
1791 | strout (buf, len, len, printcharfun); | |
f76c8b1e SM |
1792 | if (t->name) |
1793 | { | |
1db5b1ad JB |
1794 | strout (" on ", -1, -1, printcharfun); |
1795 | strout (t->name, -1, -1, printcharfun); | |
f76c8b1e SM |
1796 | } |
1797 | PRINTCHAR ('>'); | |
1798 | } | |
7eb03302 GM |
1799 | else if (HASH_TABLE_P (obj)) |
1800 | { | |
1801 | struct Lisp_Hash_Table *h = XHASH_TABLE (obj); | |
d311d28c PE |
1802 | ptrdiff_t i; |
1803 | ptrdiff_t real_size, size; | |
99027bdd | 1804 | int len; |
f19a0f5b | 1805 | #if 0 |
1db5b1ad | 1806 | strout ("#<hash-table", -1, -1, printcharfun); |
7eb03302 GM |
1807 | if (SYMBOLP (h->test)) |
1808 | { | |
1809 | PRINTCHAR (' '); | |
1810 | PRINTCHAR ('\''); | |
1db5b1ad | 1811 | strout (SDATA (SYMBOL_NAME (h->test)), -1, -1, printcharfun); |
7eb03302 | 1812 | PRINTCHAR (' '); |
1db5b1ad | 1813 | strout (SDATA (SYMBOL_NAME (h->weak)), -1, -1, printcharfun); |
7eb03302 | 1814 | PRINTCHAR (' '); |
99027bdd PE |
1815 | len = sprintf (buf, "%"pD"d/%"pD"d", h->count, ASIZE (h->next)); |
1816 | strout (buf, len, len, printcharfun); | |
7eb03302 | 1817 | } |
99027bdd PE |
1818 | len = sprintf (buf, " %p", h); |
1819 | strout (buf, len, len, printcharfun); | |
7eb03302 | 1820 | PRINTCHAR ('>'); |
f19a0f5b TZ |
1821 | #endif |
1822 | /* Implement a readable output, e.g.: | |
1823 | #s(hash-table size 2 test equal data (k1 v1 k2 v2)) */ | |
1824 | /* Always print the size. */ | |
99027bdd PE |
1825 | len = sprintf (buf, "#s(hash-table size %"pD"d", ASIZE (h->next)); |
1826 | strout (buf, len, len, printcharfun); | |
f19a0f5b TZ |
1827 | |
1828 | if (!NILP (h->test)) | |
1829 | { | |
1db5b1ad | 1830 | strout (" test ", -1, -1, printcharfun); |
617a0e83 | 1831 | print_object (h->test, printcharfun, escapeflag); |
f19a0f5b TZ |
1832 | } |
1833 | ||
1834 | if (!NILP (h->weak)) | |
1835 | { | |
1db5b1ad | 1836 | strout (" weakness ", -1, -1, printcharfun); |
617a0e83 | 1837 | print_object (h->weak, printcharfun, escapeflag); |
f19a0f5b TZ |
1838 | } |
1839 | ||
1840 | if (!NILP (h->rehash_size)) | |
1841 | { | |
1db5b1ad | 1842 | strout (" rehash-size ", -1, -1, printcharfun); |
617a0e83 | 1843 | print_object (h->rehash_size, printcharfun, escapeflag); |
f19a0f5b TZ |
1844 | } |
1845 | ||
1846 | if (!NILP (h->rehash_threshold)) | |
1847 | { | |
1db5b1ad | 1848 | strout (" rehash-threshold ", -1, -1, printcharfun); |
617a0e83 | 1849 | print_object (h->rehash_threshold, printcharfun, escapeflag); |
f19a0f5b TZ |
1850 | } |
1851 | ||
1db5b1ad | 1852 | strout (" data ", -1, -1, printcharfun); |
f19a0f5b TZ |
1853 | |
1854 | /* Print the data here as a plist. */ | |
b0ca0f33 DN |
1855 | real_size = HASH_TABLE_SIZE (h); |
1856 | size = real_size; | |
f19a0f5b TZ |
1857 | |
1858 | /* Don't print more elements than the specified maximum. */ | |
1859 | if (NATNUMP (Vprint_length) | |
1860 | && XFASTINT (Vprint_length) < size) | |
1861 | size = XFASTINT (Vprint_length); | |
42a5b22f | 1862 | |
f19a0f5b TZ |
1863 | PRINTCHAR ('('); |
1864 | for (i = 0; i < size; i++) | |
1865 | if (!NILP (HASH_HASH (h, i))) | |
1866 | { | |
1867 | if (i) PRINTCHAR (' '); | |
617a0e83 | 1868 | print_object (HASH_KEY (h, i), printcharfun, escapeflag); |
f19a0f5b | 1869 | PRINTCHAR (' '); |
617a0e83 | 1870 | print_object (HASH_VALUE (h, i), printcharfun, escapeflag); |
f19a0f5b TZ |
1871 | } |
1872 | ||
1873 | if (size < real_size) | |
1db5b1ad | 1874 | strout (" ...", 4, 4, printcharfun); |
f19a0f5b TZ |
1875 | |
1876 | PRINTCHAR (')'); | |
1877 | PRINTCHAR (')'); | |
1878 | ||
7eb03302 | 1879 | } |
908b0ae5 RS |
1880 | else if (BUFFERP (obj)) |
1881 | { | |
4b4deea2 | 1882 | if (NILP (BVAR (XBUFFER (obj), name))) |
1db5b1ad | 1883 | strout ("#<killed buffer>", -1, -1, printcharfun); |
908b0ae5 RS |
1884 | else if (escapeflag) |
1885 | { | |
1db5b1ad | 1886 | strout ("#<buffer ", -1, -1, printcharfun); |
4b4deea2 | 1887 | print_string (BVAR (XBUFFER (obj), name), printcharfun); |
908b0ae5 RS |
1888 | PRINTCHAR ('>'); |
1889 | } | |
1890 | else | |
4b4deea2 | 1891 | print_string (BVAR (XBUFFER (obj), name), printcharfun); |
908b0ae5 | 1892 | } |
ca0569ad RS |
1893 | else if (WINDOW_CONFIGURATIONP (obj)) |
1894 | { | |
1db5b1ad | 1895 | strout ("#<window-configuration>", -1, -1, printcharfun); |
ca0569ad | 1896 | } |
ca0569ad RS |
1897 | else if (FRAMEP (obj)) |
1898 | { | |
99027bdd | 1899 | int len; |
e69b0960 | 1900 | Lisp_Object frame_name = XFRAME (obj)->name; |
436bc8e0 | 1901 | |
ca0569ad RS |
1902 | strout ((FRAME_LIVE_P (XFRAME (obj)) |
1903 | ? "#<frame " : "#<dead frame "), | |
1db5b1ad | 1904 | -1, -1, printcharfun); |
436bc8e0 EZ |
1905 | if (!STRINGP (frame_name)) |
1906 | { | |
1907 | /* A frame could be too young and have no name yet; | |
1908 | don't crash. */ | |
1909 | if (SYMBOLP (frame_name)) | |
1910 | frame_name = Fsymbol_name (frame_name); | |
1911 | else /* can't happen: name should be either nil or string */ | |
1912 | frame_name = build_string ("*INVALID*FRAME*NAME*"); | |
1913 | } | |
1914 | print_string (frame_name, printcharfun); | |
99027bdd PE |
1915 | len = sprintf (buf, " %p", XFRAME (obj)); |
1916 | strout (buf, len, len, printcharfun); | |
ca0569ad RS |
1917 | PRINTCHAR ('>'); |
1918 | } | |
2a7b7982 KH |
1919 | else if (FONTP (obj)) |
1920 | { | |
d311d28c | 1921 | int i; |
2a7b7982 KH |
1922 | |
1923 | if (! FONT_OBJECT_P (obj)) | |
1924 | { | |
1925 | if (FONT_SPEC_P (obj)) | |
1db5b1ad | 1926 | strout ("#<font-spec", -1, -1, printcharfun); |
2a7b7982 | 1927 | else |
1db5b1ad | 1928 | strout ("#<font-entity", -1, -1, printcharfun); |
2a7b7982 KH |
1929 | for (i = 0; i < FONT_SPEC_MAX; i++) |
1930 | { | |
1931 | PRINTCHAR (' '); | |
1932 | if (i < FONT_WEIGHT_INDEX || i > FONT_WIDTH_INDEX) | |
1933 | print_object (AREF (obj, i), printcharfun, escapeflag); | |
1934 | else | |
1935 | print_object (font_style_symbolic (obj, i, 0), | |
1936 | printcharfun, escapeflag); | |
1937 | } | |
1938 | } | |
1939 | else | |
1940 | { | |
1db5b1ad | 1941 | strout ("#<font-object ", -1, -1, printcharfun); |
2a7b7982 KH |
1942 | print_object (AREF (obj, FONT_NAME_INDEX), printcharfun, |
1943 | escapeflag); | |
1944 | } | |
1945 | PRINTCHAR ('>'); | |
1946 | } | |
ca0569ad RS |
1947 | else |
1948 | { | |
d311d28c | 1949 | ptrdiff_t size = ASIZE (obj); |
ca0569ad RS |
1950 | if (COMPILEDP (obj)) |
1951 | { | |
1952 | PRINTCHAR ('#'); | |
1953 | size &= PSEUDOVECTOR_SIZE_MASK; | |
1954 | } | |
8b4b4467 | 1955 | if (CHAR_TABLE_P (obj) || SUB_CHAR_TABLE_P (obj)) |
ed2c35ef RS |
1956 | { |
1957 | /* We print a char-table as if it were a vector, | |
1958 | lumping the parent and default slots in with the | |
1959 | character slots. But we add #^ as a prefix. */ | |
223509a3 KH |
1960 | |
1961 | /* Make each lowest sub_char_table start a new line. | |
1962 | Otherwise we'll make a line extremely long, which | |
1963 | results in slow redisplay. */ | |
1964 | if (SUB_CHAR_TABLE_P (obj) | |
1965 | && XINT (XSUB_CHAR_TABLE (obj)->depth) == 3) | |
1966 | PRINTCHAR ('\n'); | |
ed2c35ef RS |
1967 | PRINTCHAR ('#'); |
1968 | PRINTCHAR ('^'); | |
3701b5de KH |
1969 | if (SUB_CHAR_TABLE_P (obj)) |
1970 | PRINTCHAR ('^'); | |
ed2c35ef RS |
1971 | size &= PSEUDOVECTOR_SIZE_MASK; |
1972 | } | |
00d76abc KH |
1973 | if (size & PSEUDOVECTOR_FLAG) |
1974 | goto badtype; | |
ca0569ad RS |
1975 | |
1976 | PRINTCHAR ('['); | |
38010d50 | 1977 | { |
ca0569ad RS |
1978 | register int i; |
1979 | register Lisp_Object tem; | |
d311d28c | 1980 | ptrdiff_t real_size = size; |
a40384bc RS |
1981 | |
1982 | /* Don't print more elements than the specified maximum. */ | |
42ac1ed4 GM |
1983 | if (NATNUMP (Vprint_length) |
1984 | && XFASTINT (Vprint_length) < size) | |
1985 | size = XFASTINT (Vprint_length); | |
a40384bc | 1986 | |
ca0569ad RS |
1987 | for (i = 0; i < size; i++) |
1988 | { | |
1989 | if (i) PRINTCHAR (' '); | |
28be1ada | 1990 | tem = AREF (obj, i); |
0f25ecc6 | 1991 | print_object (tem, printcharfun, escapeflag); |
ca0569ad | 1992 | } |
d6ac884e | 1993 | if (size < real_size) |
1db5b1ad | 1994 | strout (" ...", 4, 4, printcharfun); |
38010d50 | 1995 | } |
ca0569ad RS |
1996 | PRINTCHAR (']'); |
1997 | } | |
1998 | break; | |
1999 | ||
ca0569ad | 2000 | case Lisp_Misc: |
5db20f08 | 2001 | switch (XMISCTYPE (obj)) |
38010d50 | 2002 | { |
00d76abc | 2003 | case Lisp_Misc_Marker: |
1db5b1ad | 2004 | strout ("#<marker ", -1, -1, printcharfun); |
087e3c46 KH |
2005 | /* Do you think this is necessary? */ |
2006 | if (XMARKER (obj)->insertion_type != 0) | |
1db5b1ad | 2007 | strout ("(moves after insertion) ", -1, -1, printcharfun); |
cd3587c1 | 2008 | if (! XMARKER (obj)->buffer) |
1db5b1ad | 2009 | strout ("in no buffer", -1, -1, printcharfun); |
ca0569ad RS |
2010 | else |
2011 | { | |
99027bdd PE |
2012 | int len = sprintf (buf, "at %"pD"d", marker_position (obj)); |
2013 | strout (buf, len, len, printcharfun); | |
1db5b1ad | 2014 | strout (" in ", -1, -1, printcharfun); |
4b4deea2 | 2015 | print_string (BVAR (XMARKER (obj)->buffer, name), printcharfun); |
ca0569ad | 2016 | } |
38010d50 | 2017 | PRINTCHAR ('>'); |
908b0ae5 | 2018 | break; |
00d76abc KH |
2019 | |
2020 | case Lisp_Misc_Overlay: | |
1db5b1ad | 2021 | strout ("#<overlay ", -1, -1, printcharfun); |
cd3587c1 | 2022 | if (! XMARKER (OVERLAY_START (obj))->buffer) |
1db5b1ad | 2023 | strout ("in no buffer", -1, -1, printcharfun); |
ca0569ad RS |
2024 | else |
2025 | { | |
99027bdd PE |
2026 | int len = sprintf (buf, "from %"pD"d to %"pD"d in ", |
2027 | marker_position (OVERLAY_START (obj)), | |
2028 | marker_position (OVERLAY_END (obj))); | |
2029 | strout (buf, len, len, printcharfun); | |
4b4deea2 | 2030 | print_string (BVAR (XMARKER (OVERLAY_START (obj))->buffer, name), |
ca0569ad RS |
2031 | printcharfun); |
2032 | } | |
2033 | PRINTCHAR ('>'); | |
908b0ae5 | 2034 | break; |
00d76abc KH |
2035 | |
2036 | /* Remaining cases shouldn't happen in normal usage, but let's print | |
2037 | them anyway for the benefit of the debugger. */ | |
2038 | case Lisp_Misc_Free: | |
1db5b1ad | 2039 | strout ("#<misc free cell>", -1, -1, printcharfun); |
00d76abc KH |
2040 | break; |
2041 | ||
c069fee4 | 2042 | case Lisp_Misc_Save_Value: |
1db5b1ad | 2043 | strout ("#<save_value ", -1, -1, printcharfun); |
99027bdd PE |
2044 | { |
2045 | int len = sprintf (buf, "ptr=%p int=%"pD"d", | |
2046 | XSAVE_VALUE (obj)->pointer, | |
2047 | XSAVE_VALUE (obj)->integer); | |
2048 | strout (buf, len, len, printcharfun); | |
2049 | } | |
c069fee4 KS |
2050 | PRINTCHAR ('>'); |
2051 | break; | |
2052 | ||
00d76abc KH |
2053 | default: |
2054 | goto badtype; | |
e0f93814 | 2055 | } |
00d76abc | 2056 | break; |
ca0569ad RS |
2057 | |
2058 | default: | |
00d76abc | 2059 | badtype: |
ca0569ad | 2060 | { |
99027bdd | 2061 | int len; |
ca0569ad RS |
2062 | /* We're in trouble if this happens! |
2063 | Probably should just abort () */ | |
1db5b1ad | 2064 | strout ("#<EMACS BUG: INVALID DATATYPE ", -1, -1, printcharfun); |
00d76abc | 2065 | if (MISCP (obj)) |
99027bdd | 2066 | len = sprintf (buf, "(MISC 0x%04x)", (int) XMISCTYPE (obj)); |
00d76abc | 2067 | else if (VECTORLIKEP (obj)) |
99027bdd | 2068 | len = sprintf (buf, "(PVEC 0x%08"pD"x)", ASIZE (obj)); |
00d76abc | 2069 | else |
99027bdd PE |
2070 | len = sprintf (buf, "(0x%02x)", (int) XTYPE (obj)); |
2071 | strout (buf, len, len, printcharfun); | |
ca0569ad | 2072 | strout (" Save your buffers immediately and please report this bug>", |
1db5b1ad | 2073 | -1, -1, printcharfun); |
ca0569ad | 2074 | } |
38010d50 JB |
2075 | } |
2076 | ||
2077 | print_depth--; | |
2078 | } | |
2079 | \f | |
7651e1f5 RS |
2080 | |
2081 | /* Print a description of INTERVAL using PRINTCHARFUN. | |
2082 | This is part of printing a string that has text properties. */ | |
2083 | ||
2084 | void | |
971de7fb | 2085 | print_interval (INTERVAL interval, Lisp_Object printcharfun) |
7651e1f5 | 2086 | { |
71ea13cb KH |
2087 | if (NILP (interval->plist)) |
2088 | return; | |
30503c0b | 2089 | PRINTCHAR (' '); |
0f25ecc6 | 2090 | print_object (make_number (interval->position), printcharfun, 1); |
7651e1f5 | 2091 | PRINTCHAR (' '); |
0f25ecc6 | 2092 | print_object (make_number (interval->position + LENGTH (interval)), |
cd3587c1 | 2093 | printcharfun, 1); |
7651e1f5 | 2094 | PRINTCHAR (' '); |
0f25ecc6 | 2095 | print_object (interval->plist, printcharfun, 1); |
7651e1f5 RS |
2096 | } |
2097 | ||
7651e1f5 | 2098 | \f |
38010d50 | 2099 | void |
971de7fb | 2100 | syms_of_print (void) |
38010d50 | 2101 | { |
cd3520a4 | 2102 | DEFSYM (Qtemp_buffer_setup_hook, "temp-buffer-setup-hook"); |
d9c21094 | 2103 | |
29208e82 | 2104 | DEFVAR_LISP ("standard-output", Vstandard_output, |
8c1a1077 PJ |
2105 | doc: /* Output stream `print' uses by default for outputting a character. |
2106 | This may be any function of one argument. | |
2107 | It may also be a buffer (output is inserted before point) | |
2108 | or a marker (output is inserted and the marker is advanced) | |
2109 | or the symbol t (output appears in the echo area). */); | |
38010d50 | 2110 | Vstandard_output = Qt; |
cd3520a4 | 2111 | DEFSYM (Qstandard_output, "standard-output"); |
38010d50 | 2112 | |
29208e82 | 2113 | DEFVAR_LISP ("float-output-format", Vfloat_output_format, |
8c1a1077 PJ |
2114 | doc: /* The format descriptor string used to print floats. |
2115 | This is a %-spec like those accepted by `printf' in C, | |
2116 | but with some restrictions. It must start with the two characters `%.'. | |
2117 | After that comes an integer precision specification, | |
2118 | and then a letter which controls the format. | |
2119 | The letters allowed are `e', `f' and `g'. | |
2120 | Use `e' for exponential notation \"DIG.DIGITSeEXPT\" | |
2121 | Use `f' for decimal point notation \"DIGITS.DIGITS\". | |
2122 | Use `g' to choose the shorter of those two formats for the number at hand. | |
2123 | The precision in any of these cases is the number of digits following | |
2124 | the decimal point. With `f', a precision of 0 means to omit the | |
2125 | decimal point. 0 is not allowed with `e' or `g'. | |
2126 | ||
2127 | A value of nil means to use the shortest notation | |
2128 | that represents the number without losing information. */); | |
38010d50 | 2129 | Vfloat_output_format = Qnil; |
cd3520a4 | 2130 | DEFSYM (Qfloat_output_format, "float-output-format"); |
38010d50 | 2131 | |
29208e82 | 2132 | DEFVAR_LISP ("print-length", Vprint_length, |
8c1a1077 PJ |
2133 | doc: /* Maximum length of list to print before abbreviating. |
2134 | A value of nil means no limit. See also `eval-expression-print-length'. */); | |
38010d50 JB |
2135 | Vprint_length = Qnil; |
2136 | ||
29208e82 | 2137 | DEFVAR_LISP ("print-level", Vprint_level, |
8c1a1077 PJ |
2138 | doc: /* Maximum depth of list nesting to print before abbreviating. |
2139 | A value of nil means no limit. See also `eval-expression-print-level'. */); | |
38010d50 JB |
2140 | Vprint_level = Qnil; |
2141 | ||
29208e82 | 2142 | DEFVAR_BOOL ("print-escape-newlines", print_escape_newlines, |
8c1a1077 PJ |
2143 | doc: /* Non-nil means print newlines in strings as `\\n'. |
2144 | Also print formfeeds as `\\f'. */); | |
38010d50 JB |
2145 | print_escape_newlines = 0; |
2146 | ||
29208e82 | 2147 | DEFVAR_BOOL ("print-escape-nonascii", print_escape_nonascii, |
8c1a1077 PJ |
2148 | doc: /* Non-nil means print unibyte non-ASCII chars in strings as \\OOO. |
2149 | \(OOO is the octal representation of the character code.) | |
249c0f71 RS |
2150 | Only single-byte characters are affected, and only in `prin1'. |
2151 | When the output goes in a multibyte buffer, this feature is | |
2152 | enabled regardless of the value of the variable. */); | |
38940e93 RS |
2153 | print_escape_nonascii = 0; |
2154 | ||
29208e82 | 2155 | DEFVAR_BOOL ("print-escape-multibyte", print_escape_multibyte, |
8c1a1077 PJ |
2156 | doc: /* Non-nil means print multibyte characters in strings as \\xXXXX. |
2157 | \(XXXX is the hex representation of the character code.) | |
2158 | This affects only `prin1'. */); | |
835d0be6 RS |
2159 | print_escape_multibyte = 0; |
2160 | ||
29208e82 | 2161 | DEFVAR_BOOL ("print-quoted", print_quoted, |
8c1a1077 | 2162 | doc: /* Non-nil means print quoted forms with reader syntax. |
db75cb5f | 2163 | I.e., (quote foo) prints as 'foo, (function foo) as #'foo. */); |
2f100b5c EN |
2164 | print_quoted = 0; |
2165 | ||
29208e82 | 2166 | DEFVAR_LISP ("print-gensym", Vprint_gensym, |
8c1a1077 PJ |
2167 | doc: /* Non-nil means print uninterned symbols so they will read as uninterned. |
2168 | I.e., the value of (make-symbol \"foobar\") prints as #:foobar. | |
2169 | When the uninterned symbol appears within a recursive data structure, | |
2170 | and the symbol appears more than once, in addition use the #N# and #N= | |
2171 | constructs as needed, so that multiple references to the same symbol are | |
2172 | shared once again when the text is read back. */); | |
e0f69431 RS |
2173 | Vprint_gensym = Qnil; |
2174 | ||
29208e82 | 2175 | DEFVAR_LISP ("print-circle", Vprint_circle, |
fb7ada5f | 2176 | doc: /* Non-nil means print recursive structures using #N= and #N# syntax. |
8c1a1077 PJ |
2177 | If nil, printing proceeds recursively and may lead to |
2178 | `max-lisp-eval-depth' being exceeded or an error may occur: | |
2179 | \"Apparently circular structure being printed.\" Also see | |
2180 | `print-length' and `print-level'. | |
2181 | If non-nil, shared substructures anywhere in the structure are printed | |
2182 | with `#N=' before the first occurrence (in the order of the print | |
2183 | representation) and `#N#' in place of each subsequent occurrence, | |
2184 | where N is a positive decimal integer. */); | |
0f25ecc6 RS |
2185 | Vprint_circle = Qnil; |
2186 | ||
29208e82 | 2187 | DEFVAR_LISP ("print-continuous-numbering", Vprint_continuous_numbering, |
fb7ada5f | 2188 | doc: /* Non-nil means number continuously across print calls. |
8c1a1077 PJ |
2189 | This affects the numbers printed for #N= labels and #M# references. |
2190 | See also `print-circle', `print-gensym', and `print-number-table'. | |
2191 | This variable should not be set with `setq'; bind it with a `let' instead. */); | |
0f25ecc6 RS |
2192 | Vprint_continuous_numbering = Qnil; |
2193 | ||
29208e82 | 2194 | DEFVAR_LISP ("print-number-table", Vprint_number_table, |
8c1a1077 PJ |
2195 | doc: /* A vector used internally to produce `#N=' labels and `#N#' references. |
2196 | The Lisp printer uses this vector to detect Lisp objects referenced more | |
e6d4cddd RS |
2197 | than once. |
2198 | ||
2199 | When you bind `print-continuous-numbering' to t, you should probably | |
2200 | also bind `print-number-table' to nil. This ensures that the value of | |
2201 | `print-number-table' can be garbage-collected once the printing is | |
2202 | done. If all elements of `print-number-table' are nil, it means that | |
2203 | the printing done so far has not found any shared structure or objects | |
2204 | that need to be recorded in the table. */); | |
0f25ecc6 | 2205 | Vprint_number_table = Qnil; |
081e0581 | 2206 | |
29208e82 | 2207 | DEFVAR_LISP ("print-charset-text-property", Vprint_charset_text_property, |
71ea13cb KH |
2208 | doc: /* A flag to control printing of `charset' text property on printing a string. |
2209 | The value must be nil, t, or `default'. | |
2210 | ||
2211 | If the value is nil, don't print the text property `charset'. | |
2212 | ||
2213 | If the value is t, always print the text property `charset'. | |
2214 | ||
2215 | If the value is `default', print the text property `charset' only when | |
2216 | the value is different from what is guessed in the current charset | |
dedbd91c | 2217 | priorities. */); |
71ea13cb KH |
2218 | Vprint_charset_text_property = Qdefault; |
2219 | ||
38010d50 JB |
2220 | /* prin1_to_string_buffer initialized in init_buffer_once in buffer.c */ |
2221 | staticpro (&Vprin1_to_string_buffer); | |
2222 | ||
2223 | defsubr (&Sprin1); | |
2224 | defsubr (&Sprin1_to_string); | |
113620cc | 2225 | defsubr (&Serror_message_string); |
38010d50 JB |
2226 | defsubr (&Sprinc); |
2227 | defsubr (&Sprint); | |
2228 | defsubr (&Sterpri); | |
2229 | defsubr (&Swrite_char); | |
2230 | defsubr (&Sexternal_debugging_output); | |
6b61353c KH |
2231 | #ifdef WITH_REDIRECT_DEBUGGING_OUTPUT |
2232 | defsubr (&Sredirect_debugging_output); | |
2233 | #endif | |
38010d50 | 2234 | |
cd3520a4 JB |
2235 | DEFSYM (Qexternal_debugging_output, "external-debugging-output"); |
2236 | DEFSYM (Qprint_escape_newlines, "print-escape-newlines"); | |
2237 | DEFSYM (Qprint_escape_multibyte, "print-escape-multibyte"); | |
2238 | DEFSYM (Qprint_escape_nonascii, "print-escape-nonascii"); | |
835d0be6 | 2239 | |
71ea13cb KH |
2240 | print_prune_charset_plist = Qnil; |
2241 | staticpro (&print_prune_charset_plist); | |
38010d50 | 2242 | } |