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