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