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