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