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