0
|
1 /* Lisp object printing and output streams.
|
|
2 Copyright (C) 1985, 1986, 1988, 1992-1995 Free Software Foundation, Inc.
|
|
3 Copyright (C) 1995, 1996 Ben Wing.
|
|
4
|
|
5 This file is part of XEmacs.
|
|
6
|
|
7 XEmacs is free software; you can redistribute it and/or modify it
|
|
8 under the terms of the GNU General Public License as published by the
|
|
9 Free Software Foundation; either version 2, or (at your option) any
|
|
10 later version.
|
|
11
|
|
12 XEmacs is distributed in the hope that it will be useful, but WITHOUT
|
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
15 for more details.
|
|
16
|
|
17 You should have received a copy of the GNU General Public License
|
|
18 along with XEmacs; see the file COPYING. If not, write to
|
|
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
20 Boston, MA 02111-1307, USA. */
|
|
21
|
|
22 /* Synched up with: Not synched with FSF. */
|
|
23
|
|
24 /* This file has been Mule-ized. */
|
|
25
|
|
26 /* Seriously hacked on by Ben Wing for Mule. */
|
|
27
|
|
28 #include <config.h>
|
|
29 #include "lisp.h"
|
|
30
|
|
31 #ifndef standalone
|
|
32 #include "backtrace.h"
|
|
33 #include "buffer.h"
|
|
34 #include "bytecode.h"
|
|
35 #include "console-tty.h"
|
|
36 #include "console-stream.h"
|
|
37 #include "extents.h"
|
|
38 #include "frame.h"
|
|
39 #include "emacsfns.h"
|
|
40 #include "insdel.h"
|
|
41 #include "lstream.h"
|
|
42
|
|
43 #endif /* not standalone */
|
|
44
|
|
45 Lisp_Object Vstandard_output, Qstandard_output;
|
|
46
|
|
47 /* The subroutine object for external-debugging-output is kept here
|
|
48 for the convenience of the debugger. */
|
|
49 Lisp_Object Qexternal_debugging_output;
|
|
50 Lisp_Object Qalternate_debugging_output;
|
|
51
|
|
52 /* Avoid actual stack overflow in print. */
|
|
53 static int print_depth;
|
|
54
|
|
55 /* Maximum length of list or vector to print in full; noninteger means
|
|
56 effectively infinity */
|
|
57
|
|
58 Lisp_Object Vprint_length;
|
|
59 Lisp_Object Qprint_length;
|
|
60
|
|
61 /* Maximum length of string to print in full; noninteger means
|
|
62 effectively infinity */
|
|
63
|
|
64 Lisp_Object Vprint_string_length;
|
|
65 Lisp_Object Qprint_string_length;
|
|
66
|
|
67 /* Maximum depth of list to print in full; noninteger means
|
|
68 effectively infinity. */
|
|
69
|
|
70 Lisp_Object Vprint_level;
|
|
71
|
|
72 /* Label to use when making echo-area messages. */
|
|
73
|
|
74 Lisp_Object Vprint_message_label;
|
|
75
|
|
76 /* Nonzero means print newlines in strings as \n. */
|
|
77
|
|
78 int print_escape_newlines;
|
|
79 int print_readably;
|
|
80 int print_gensym;
|
|
81
|
|
82 Lisp_Object Qprint_escape_newlines;
|
|
83 Lisp_Object Qprint_readably;
|
|
84
|
|
85 /* Force immediate output of all printed data. Used for debugging. */
|
|
86 int print_unbuffered;
|
|
87
|
|
88 FILE *termscript; /* Stdio stream being used for copy of all output. */
|
|
89
|
|
90
|
|
91
|
|
92 int stdout_needs_newline;
|
|
93
|
|
94 /* Write a string (in internal format) to stdio stream STREAM. */
|
|
95
|
|
96 void
|
|
97 write_string_to_stdio_stream (FILE *stream, struct console *con,
|
|
98 CONST Bufbyte *str,
|
|
99 Bytecount offset, Bytecount len,
|
|
100 enum external_data_format fmt)
|
|
101 {
|
|
102 int extlen;
|
|
103 CONST Extbyte *extptr;
|
|
104
|
|
105 GET_CHARPTR_EXT_DATA_ALLOCA (str + offset, len, fmt, extptr, extlen);
|
|
106 if (stream)
|
|
107 fwrite (extptr, 1, extlen, stream);
|
|
108 else
|
|
109 {
|
|
110 assert (CONSOLE_TTY_P (con));
|
|
111 Lstream_write (XLSTREAM (CONSOLE_TTY_DATA (con)->outstream),
|
|
112 extptr, extlen);
|
|
113 }
|
|
114 if (stream == stdout || stream == stderr ||
|
|
115 (!stream && CONSOLE_TTY_DATA (con)->is_stdio))
|
|
116 {
|
|
117 if (termscript)
|
|
118 {
|
|
119 fwrite (extptr, 1, extlen, termscript);
|
|
120 fflush (termscript);
|
|
121 }
|
|
122 stdout_needs_newline = (extptr[extlen - 1] != '\n');
|
|
123 }
|
|
124 }
|
|
125
|
|
126 /* Write a string to the output location specified in FUNCTION.
|
|
127 Arguments NONRELOC, RELOC, OFFSET, and LEN are as in
|
|
128 buffer_insert_string_1() in insdel.c. */
|
|
129
|
|
130 static void
|
|
131 output_string (Lisp_Object function, CONST Bufbyte *nonreloc,
|
|
132 Lisp_Object reloc, Bytecount offset, Bytecount len)
|
|
133 {
|
|
134 /* This function can GC */
|
|
135 Charcount ccoff, cclen;
|
|
136 /* We change the value of nonreloc (fetching it from reloc as
|
|
137 necessary), but we don't want to pass this changed value on to
|
|
138 other functions that take both a nonreloc and a reloc, or things
|
|
139 may get confused and an assertion failure in
|
|
140 fixup_internal_substring() may get triggered. */
|
|
141 CONST Bufbyte *newnonreloc = nonreloc;
|
|
142 struct gcpro gcpro1, gcpro2;
|
|
143
|
|
144 /* Emacs won't print whilst GCing, but an external debugger might */
|
|
145 if (gc_in_progress) return;
|
|
146
|
|
147 /* Perhaps not necessary but probably safer. */
|
|
148 GCPRO2 (function, reloc);
|
|
149
|
|
150 fixup_internal_substring (newnonreloc, reloc, offset, &len);
|
|
151
|
|
152 if (STRINGP (reloc))
|
16
|
153 newnonreloc = XSTRING_DATA (reloc);
|
0
|
154
|
|
155 ccoff = bytecount_to_charcount (newnonreloc, offset);
|
|
156 cclen = bytecount_to_charcount (newnonreloc + offset, len);
|
|
157
|
|
158 if (LSTREAMP (function))
|
|
159 {
|
|
160 /* Lstream_write() could easily cause GC inside of it, if the
|
|
161 stream is a print-stream. (It will call output_string()
|
|
162 recursively.) This is probably the fastest way to fix this
|
|
163 problem. (alloca() is very fast on machines that have it
|
|
164 built-in, and you avoid some nasty problems with recursion
|
|
165 that could result from using a static buffer somewhere.)
|
|
166
|
|
167 The other possibility is to inhibit GC, but that of course
|
|
168 would require an unwind-protect, which is usually a lot
|
|
169 slower than the small amount of memcpy()ing that happens
|
|
170 here. */
|
|
171 if (STRINGP (reloc))
|
|
172 {
|
|
173 Bufbyte *copied = (Bufbyte *) alloca (len);
|
|
174 memcpy (copied, newnonreloc + offset, len);
|
|
175 Lstream_write (XLSTREAM (function), copied, len);
|
|
176 }
|
|
177 else
|
|
178 Lstream_write (XLSTREAM (function), newnonreloc + offset, len);
|
|
179
|
|
180 if (print_unbuffered)
|
|
181 Lstream_flush (XLSTREAM (function));
|
|
182 }
|
|
183
|
|
184 #ifndef standalone
|
|
185 else if (BUFFERP (function))
|
|
186 {
|
|
187 CHECK_LIVE_BUFFER (function);
|
|
188 buffer_insert_string (XBUFFER (function), nonreloc, reloc, offset, len);
|
|
189 }
|
|
190 else if (MARKERP (function))
|
|
191 {
|
|
192 /* marker_position will err if marker doesn't point anywhere */
|
|
193 Bufpos spoint = marker_position (function);
|
|
194
|
|
195 buffer_insert_string_1 (XBUFFER (Fmarker_buffer (function)),
|
|
196 spoint, nonreloc, reloc, offset, len,
|
|
197 0);
|
|
198 Fset_marker (function, make_int (spoint + cclen),
|
|
199 Fmarker_buffer (function));
|
|
200 }
|
|
201 else if (FRAMEP (function))
|
|
202 {
|
|
203 struct frame *f = XFRAME (function);
|
|
204 if (!EQ (Vprint_message_label, echo_area_status (f)))
|
|
205 clear_echo_area_from_print (f, Qnil, 1);
|
|
206 echo_area_append (f, nonreloc, reloc, offset, len, Vprint_message_label);
|
|
207 }
|
|
208 #endif /* not standalone */
|
|
209 else if (EQ (function, Qt) || EQ (function, Qnil))
|
|
210 {
|
|
211 write_string_to_stdio_stream (stdout, 0, newnonreloc, offset, len,
|
16
|
212 FORMAT_TERMINAL);
|
0
|
213 }
|
|
214 else
|
|
215 {
|
|
216 Charcount iii;
|
|
217
|
|
218 for (iii = ccoff; iii < cclen + ccoff; iii++)
|
|
219 {
|
|
220 call1 (function,
|
|
221 make_char (charptr_emchar_n (newnonreloc, iii)));
|
|
222 if (STRINGP (reloc))
|
16
|
223 newnonreloc = XSTRING_DATA (reloc);
|
0
|
224 }
|
|
225 }
|
|
226
|
|
227 UNGCPRO;
|
|
228 }
|
|
229
|
|
230 struct print_stream
|
|
231 {
|
|
232 FILE *file;
|
|
233 Lisp_Object fun;
|
|
234 };
|
|
235
|
|
236 #define get_print_stream(stream) \
|
|
237 ((struct print_stream *) Lstream_data (stream))
|
|
238
|
|
239 DEFINE_LSTREAM_IMPLEMENTATION ("print", lstream_print,
|
|
240 sizeof (struct print_stream));
|
|
241
|
|
242 static Lisp_Object
|
|
243 make_print_output_stream (FILE *file, Lisp_Object fun)
|
|
244 {
|
|
245 Lstream *str = Lstream_new (lstream_print, "w");
|
|
246 struct print_stream *ps = get_print_stream (str);
|
|
247 Lisp_Object val = Qnil;
|
|
248
|
|
249 Lstream_set_character_mode (str);
|
|
250 ps->file = file;
|
|
251 ps->fun = fun;
|
|
252 XSETLSTREAM (val, str);
|
|
253 return val;
|
|
254 }
|
|
255
|
|
256 /* #### This isn't being used anywhere at the moment. Is it supposed
|
|
257 to be? */
|
|
258 #if 0
|
|
259 static void
|
|
260 reset_print_stream (Lstream *str, FILE *file, Lisp_Object fun)
|
|
261 {
|
|
262 struct print_stream *ps = get_print_stream (str);
|
|
263
|
|
264 Lstream_reopen (str);
|
|
265 ps->file = file;
|
|
266 ps->fun = fun;
|
|
267 }
|
|
268 #endif
|
|
269
|
|
270 static Lisp_Object
|
|
271 print_marker (Lisp_Object obj, void (*markobj) (Lisp_Object))
|
|
272 {
|
|
273 return get_print_stream (XLSTREAM (obj))->fun;
|
|
274 }
|
|
275
|
|
276 static int
|
|
277 print_writer (Lstream *stream, CONST unsigned char *data, int size)
|
|
278 {
|
|
279 struct print_stream *ps = get_print_stream (stream);
|
|
280
|
|
281 if (ps->file)
|
|
282 {
|
|
283 write_string_to_stdio_stream (ps->file, 0, data, 0, size,
|
16
|
284 FORMAT_TERMINAL);
|
0
|
285 /* Make sure it really gets written now. */
|
|
286 if (print_unbuffered)
|
|
287 fflush (ps->file);
|
|
288 }
|
|
289 else
|
|
290 output_string (ps->fun, data, Qnil, 0, size);
|
|
291 return size;
|
|
292 }
|
|
293
|
|
294 static Lisp_Object
|
|
295 canonicalize_printcharfun (Lisp_Object printcharfun)
|
|
296 {
|
|
297 if (NILP (printcharfun))
|
|
298 printcharfun = Vstandard_output;
|
|
299
|
|
300 if (EQ (printcharfun, Qt) || NILP (printcharfun))
|
|
301 {
|
|
302 #ifndef standalone
|
|
303 printcharfun = Fselected_frame (Qnil); /* print to minibuffer */
|
|
304 #endif
|
|
305 }
|
|
306 return (printcharfun);
|
|
307 }
|
|
308
|
|
309
|
|
310 static Lisp_Object
|
|
311 print_prepare (Lisp_Object printcharfun)
|
|
312 {
|
|
313 FILE *stdio_stream = 0;
|
|
314
|
|
315 /* Emacs won't print whilst GCing, but an external debugger might */
|
|
316 if (gc_in_progress)
|
|
317 return (Qnil);
|
|
318
|
|
319 printcharfun = canonicalize_printcharfun (printcharfun);
|
|
320 if (EQ (printcharfun, Qnil))
|
|
321 {
|
|
322 stdio_stream = stdout;
|
|
323 }
|
|
324 #if 0 /* Don't bother */
|
|
325 else if (SUBRP (indirect_function (printcharfun, 0))
|
|
326 && (XSUBR (indirect_function (printcharfun, 0))
|
|
327 == Sexternal_debugging_output))
|
|
328 {
|
|
329 stdio_stream = stderr;
|
|
330 }
|
|
331 #endif
|
|
332
|
|
333 return make_print_output_stream (stdio_stream, printcharfun);
|
|
334 }
|
|
335
|
|
336 static void
|
|
337 print_finish (Lisp_Object stream)
|
|
338 {
|
|
339 /* Emacs won't print whilst GCing, but an external debugger might */
|
|
340 if (gc_in_progress)
|
|
341 return;
|
|
342
|
|
343 Lstream_delete (XLSTREAM (stream));
|
|
344 }
|
|
345
|
|
346 #if 1 /* Prefer space over "speed" */
|
|
347 #define write_char_internal(string_of_length_1, stream) \
|
|
348 write_string_1 ((CONST Bufbyte *) (string_of_length_1), 1, (stream))
|
|
349 #else
|
|
350 #define write_char_internal(string_of_length_1, stream) \
|
|
351 output_string ((stream), (CONST Bufbyte *) (string_of_length_1), Qnil, 0, 1)
|
|
352 #endif
|
|
353
|
|
354 /* NOTE: Do not call this with the data of a Lisp_String,
|
|
355 * as printcharfun might cause a GC, which might cause
|
|
356 * the string's data to be relocated.
|
|
357 * Use print_object_internal (string, printcharfun, 0)
|
|
358 * to princ a Lisp_String
|
|
359 * Note: "stream" should be the result of "canonicalize_printcharfun"
|
|
360 * (ie Qnil means stdout, not Vstandard_output, etc)
|
|
361 */
|
|
362 void
|
|
363 write_string_1 (CONST Bufbyte *str, Bytecount size, Lisp_Object stream)
|
|
364 {
|
|
365 /* This function can GC */
|
|
366 assert (size >= 0);
|
|
367 output_string (stream, str, Qnil, 0, size);
|
|
368 }
|
|
369
|
|
370 void
|
|
371 write_c_string (CONST char *str, Lisp_Object stream)
|
|
372 {
|
|
373 /* This function can GC */
|
|
374 write_string_1 ((CONST Bufbyte *) str, strlen (str), stream);
|
|
375 }
|
|
376
|
|
377
|
20
|
378 DEFUN ("write-char", Fwrite_char, 1, 2, 0, /*
|
0
|
379 Output character CH to stream STREAM.
|
|
380 STREAM defaults to the value of `standard-output' (which see).
|
20
|
381 */
|
|
382 (ch, stream))
|
0
|
383 {
|
|
384 /* This function can GC */
|
|
385 Bufbyte str[MAX_EMCHAR_LEN];
|
|
386 Bytecount len;
|
|
387
|
|
388 CHECK_CHAR_COERCE_INT (ch);
|
|
389 len = set_charptr_emchar (str, XCHAR (ch));
|
|
390 output_string (canonicalize_printcharfun (stream), str, Qnil, 0, len);
|
|
391 return ch;
|
|
392 }
|
|
393
|
|
394 #ifndef standalone
|
|
395
|
|
396 void
|
|
397 temp_output_buffer_setup (CONST char *bufname)
|
|
398 {
|
|
399 /* This function can GC */
|
|
400 struct buffer *old = current_buffer;
|
|
401 Lisp_Object buf;
|
|
402
|
|
403 #ifdef I18N3
|
|
404 /* #### This function should accept a Lisp_Object instead of a char *,
|
|
405 so that proper translation on the buffer name can occur. */
|
|
406 #endif
|
|
407
|
|
408 Fset_buffer (Fget_buffer_create (build_string (bufname)));
|
|
409
|
|
410 current_buffer->read_only = Qnil;
|
|
411 Ferase_buffer (Fcurrent_buffer ());
|
|
412
|
|
413 XSETBUFFER (buf, current_buffer);
|
|
414 specbind (Qstandard_output, buf);
|
|
415
|
|
416 set_buffer_internal (old);
|
|
417 }
|
|
418
|
|
419 Lisp_Object
|
|
420 internal_with_output_to_temp_buffer (CONST char *bufname,
|
|
421 Lisp_Object (*function) (Lisp_Object arg),
|
|
422 Lisp_Object arg,
|
|
423 Lisp_Object same_frame)
|
|
424 {
|
|
425 int speccount = specpdl_depth ();
|
|
426 struct gcpro gcpro1, gcpro2, gcpro3;
|
|
427 Lisp_Object buf = Qnil;
|
|
428
|
|
429 GCPRO3 (buf, arg, same_frame);
|
|
430
|
|
431 temp_output_buffer_setup (GETTEXT (bufname));
|
|
432 buf = Vstandard_output;
|
|
433
|
|
434 arg = (*function) (arg);
|
|
435
|
|
436 temp_output_buffer_show (buf, same_frame);
|
|
437 UNGCPRO;
|
|
438
|
|
439 return unbind_to (speccount, arg);
|
|
440 }
|
|
441
|
20
|
442 DEFUN ("with-output-to-temp-buffer", Fwith_output_to_temp_buffer, 1, UNEVALLED, 0, /*
|
0
|
443 Bind `standard-output' to buffer BUFNAME, eval BODY, then show that buffer.
|
|
444 The buffer is cleared out initially, and marked as unmodified when done.
|
|
445 All output done by BODY is inserted in that buffer by default.
|
|
446 The buffer is displayed in another window, but not selected.
|
|
447 The value of the last form in BODY is returned.
|
|
448 If BODY does not finish normally, the buffer BUFNAME is not displayed.
|
|
449
|
|
450 If variable `temp-buffer-show-function' is non-nil, call it at the end
|
|
451 to get the buffer displayed. It gets one argument, the buffer to display.
|
20
|
452 */
|
|
453 (args))
|
0
|
454 {
|
|
455 /* This function can GC */
|
|
456 struct gcpro gcpro1;
|
|
457 Lisp_Object name;
|
|
458 int speccount = specpdl_depth ();
|
|
459 Lisp_Object buf, val;
|
|
460
|
|
461 #ifdef I18N3
|
|
462 /* #### should set the buffer to be translating. See print_internal(). */
|
|
463 #endif
|
|
464
|
|
465 GCPRO1 (args);
|
|
466 name = Feval (Fcar (args));
|
|
467 UNGCPRO;
|
|
468
|
|
469 CHECK_STRING (name);
|
16
|
470 temp_output_buffer_setup ((char *) XSTRING_DATA (name));
|
0
|
471 buf = Vstandard_output;
|
|
472
|
|
473 val = Fprogn (Fcdr (args));
|
|
474
|
|
475 temp_output_buffer_show (buf, Qnil);
|
|
476
|
|
477 return unbind_to (speccount, val);
|
|
478 }
|
|
479 #endif /* not standalone */
|
|
480
|
20
|
481 DEFUN ("terpri", Fterpri, 0, 1, 0, /*
|
0
|
482 Output a newline to STREAM.
|
|
483 If STREAM is omitted or nil, the value of `standard-output' is used.
|
20
|
484 */
|
|
485 (stream))
|
0
|
486 {
|
|
487 /* This function can GC */
|
|
488 Bufbyte str[1];
|
|
489 str[0] = '\n';
|
|
490 output_string (canonicalize_printcharfun (stream), str, Qnil, 0, 1);
|
|
491 return Qt;
|
|
492 }
|
|
493
|
20
|
494 DEFUN ("prin1", Fprin1, 1, 2, 0, /*
|
0
|
495 Output the printed representation of OBJECT, any Lisp object.
|
|
496 Quoting characters are printed when needed to make output that `read'
|
|
497 can handle, whenever this is possible.
|
|
498 Output stream is STREAM, or value of `standard-output' (which see).
|
20
|
499 */
|
|
500 (object, stream))
|
0
|
501 {
|
|
502 /* This function can GC */
|
|
503 Lisp_Object the_stream = Qnil;
|
|
504 struct gcpro gcpro1, gcpro2, gcpro3;
|
|
505
|
|
506 GCPRO3 (object, stream, the_stream);
|
|
507 print_depth = 0;
|
|
508 the_stream = print_prepare (stream);
|
|
509 print_internal (object, the_stream, 1);
|
|
510 print_finish (the_stream);
|
|
511 UNGCPRO;
|
|
512 return object;
|
|
513 }
|
|
514
|
|
515 /* a buffer which is used to hold output being built by prin1-to-string */
|
|
516 Lisp_Object Vprin1_to_string_buffer;
|
|
517
|
20
|
518 DEFUN ("prin1-to-string", Fprin1_to_string, 1, 2, 0, /*
|
0
|
519 Return a string containing the printed representation of OBJECT,
|
|
520 any Lisp object. Quoting characters are used when needed to make output
|
|
521 that `read' can handle, whenever this is possible, unless the optional
|
|
522 second argument NOESCAPE is non-nil.
|
20
|
523 */
|
|
524 (object, noescape))
|
0
|
525 {
|
|
526 /* This function can GC */
|
|
527 Lisp_Object old = Fcurrent_buffer ();
|
|
528 struct buffer *out = XBUFFER (Vprin1_to_string_buffer);
|
|
529 Lisp_Object stream = Qnil;
|
|
530 struct gcpro gcpro1, gcpro2, gcpro3;
|
|
531
|
|
532 GCPRO3 (object, old, stream);
|
|
533 stream = print_prepare (Vprin1_to_string_buffer);
|
|
534 set_buffer_internal (out);
|
|
535 Ferase_buffer (Fcurrent_buffer ());
|
|
536 print_depth = 0;
|
|
537 print_internal (object, stream, NILP (noescape));
|
|
538 print_finish (stream);
|
|
539 stream = Qnil; /* No GC surprises! */
|
|
540 object = make_string_from_buffer (out,
|
|
541 BUF_BEG (out),
|
|
542 BUF_Z (out) - 1);
|
|
543 Ferase_buffer (Fcurrent_buffer ());
|
|
544 Fset_buffer (old);
|
|
545 UNGCPRO;
|
|
546 return (object);
|
|
547 }
|
|
548
|
20
|
549 DEFUN ("princ", Fprinc, 1, 2, 0, /*
|
0
|
550 Output the printed representation of OBJECT, any Lisp object.
|
|
551 No quoting characters are used; no delimiters are printed around
|
|
552 the contents of strings.
|
|
553 Output stream is STREAM, or value of standard-output (which see).
|
20
|
554 */
|
|
555 (obj, stream))
|
0
|
556 {
|
|
557 /* This function can GC */
|
|
558 Lisp_Object the_stream = Qnil;
|
|
559 struct gcpro gcpro1, gcpro2, gcpro3;
|
|
560
|
|
561 GCPRO3 (obj, stream, the_stream);
|
|
562 the_stream = print_prepare (stream);
|
|
563 print_depth = 0;
|
|
564 print_internal (obj, the_stream, 0);
|
|
565 print_finish (the_stream);
|
|
566 UNGCPRO;
|
|
567 return (obj);
|
|
568 }
|
|
569
|
20
|
570 DEFUN ("print", Fprint, 1, 2, 0, /*
|
0
|
571 Output the printed representation of OBJECT, with newlines around it.
|
|
572 Quoting characters are printed when needed to make output that `read'
|
|
573 can handle, whenever this is possible.
|
|
574 Output stream is STREAM, or value of `standard-output' (which see).
|
20
|
575 */
|
|
576 (obj, stream))
|
0
|
577 {
|
|
578 /* This function can GC */
|
|
579 Lisp_Object the_stream = Qnil;
|
|
580 struct gcpro gcpro1, gcpro2, gcpro3;
|
|
581
|
|
582 GCPRO3 (obj, stream, the_stream);
|
|
583 the_stream = print_prepare (stream);
|
|
584 print_depth = 0;
|
|
585 write_char_internal ("\n", the_stream);
|
|
586 print_internal (obj, the_stream, 1);
|
|
587 write_char_internal ("\n", the_stream);
|
|
588 print_finish (the_stream);
|
|
589 UNGCPRO;
|
|
590 return obj;
|
|
591 }
|
|
592
|
100
|
593 #include "emacsfns.h"
|
|
594 /* Synched with Emacs 19.34 */
|
|
595 DEFUN ("error-message-string", Ferror_message_string, 1, 1, 0, /*
|
|
596 Convert an error value (ERROR-SYMBOL . DATA) to an error message.
|
|
597 */
|
|
598 (obj))
|
|
599 {
|
|
600 struct buffer *old = XBUFFER(Fcurrent_buffer());
|
|
601 Lisp_Object original, printcharfun, value;
|
|
602 struct gcpro gcpro1;
|
|
603
|
|
604 print_error_message (obj, Vprin1_to_string_buffer, NULL);
|
|
605
|
|
606 set_buffer_internal (XBUFFER (Vprin1_to_string_buffer));
|
|
607 value = Fbuffer_substring (Fpoint_min(Fcurrent_buffer()),
|
|
608 Fpoint_max(Fcurrent_buffer()),
|
|
609 Fcurrent_buffer());
|
|
610
|
|
611 GCPRO1 (value);
|
|
612 Ferase_buffer (Fcurrent_buffer());
|
|
613 set_buffer_internal (old);
|
|
614 UNGCPRO;
|
|
615
|
|
616 return value;
|
|
617 }
|
|
618
|
|
619 /* Print an error message for the error DATA
|
|
620 onto Lisp output stream STREAM (suitable for the print functions). */
|
|
621
|
|
622 print_error_message (data, stream)
|
|
623 Lisp_Object data, stream;
|
|
624 {
|
|
625 Lisp_Object errname, errmsg, file_error, tail;
|
|
626 struct gcpro gcpro1;
|
|
627 int i;
|
|
628
|
|
629 errname = Fcar (data);
|
|
630
|
|
631 if (EQ (errname, Qerror))
|
|
632 {
|
|
633 data = Fcdr (data);
|
|
634 if (!CONSP (data)) data = Qnil;
|
|
635 errmsg = Fcar (data);
|
|
636 file_error = Qnil;
|
|
637 }
|
|
638 else
|
|
639 {
|
|
640 errmsg = Fget (errname, Qerror_message, Qnil);
|
|
641 file_error = Fmemq (Qfile_error,
|
|
642 Fget (errname, Qerror_conditions, Qnil));
|
|
643 }
|
|
644
|
|
645 /* Print an error message including the data items. */
|
|
646
|
|
647 tail = Fcdr_safe (data);
|
|
648 GCPRO1 (tail);
|
|
649
|
|
650 /* For file-error, make error message by concatenating
|
|
651 all the data items. They are all strings. */
|
|
652 if (!NILP (file_error) && !NILP (tail))
|
|
653 errmsg = XCONS (tail)->car, tail = XCONS (tail)->cdr;
|
|
654
|
|
655 if (STRINGP (errmsg))
|
|
656 Fprinc (errmsg, stream);
|
|
657 else
|
102
|
658 write_string_1 ((CONST Bufbyte *)"Peculiar error", 14, stream);
|
100
|
659
|
|
660 for (i = 0; CONSP (tail); tail = Fcdr (tail), i++)
|
|
661 {
|
102
|
662 write_string_1 ((CONST Bufbyte *)(i ? ", " : ": "), 2, stream);
|
100
|
663 if (!NILP (file_error))
|
|
664 Fprinc (Fcar (tail), stream);
|
|
665 else
|
|
666 Fprin1 (Fcar (tail), stream);
|
|
667 }
|
|
668 UNGCPRO;
|
|
669 }
|
|
670
|
0
|
671 #ifdef LISP_FLOAT_TYPE
|
|
672
|
|
673 Lisp_Object Vfloat_output_format;
|
|
674 Lisp_Object Qfloat_output_format;
|
|
675
|
|
676 void
|
|
677 float_to_string (char *buf, double data)
|
|
678 /*
|
|
679 * This buffer should be at least as large as the max string size of the
|
|
680 * largest float, printed in the biggest notation. This is undoubtably
|
|
681 * 20d float_output_format, with the negative of the C-constant "HUGE"
|
|
682 * from <math.h>.
|
|
683 *
|
|
684 * On the vax the worst case is -1e38 in 20d format which takes 61 bytes.
|
|
685 *
|
|
686 * I assume that IEEE-754 format numbers can take 329 bytes for the worst
|
|
687 * case of -1e307 in 20d float_output_format. What is one to do (short of
|
|
688 * re-writing _doprnt to be more sane)?
|
|
689 * -wsr
|
|
690 */
|
|
691 {
|
|
692 Bufbyte *cp, c;
|
|
693 int width;
|
|
694
|
|
695 if (NILP (Vfloat_output_format)
|
|
696 || !STRINGP (Vfloat_output_format))
|
|
697 lose:
|
|
698 sprintf (buf, "%.16g", data);
|
|
699 else /* oink oink */
|
|
700 {
|
|
701 /* Check that the spec we have is fully valid.
|
|
702 This means not only valid for printf,
|
|
703 but meant for floats, and reasonable. */
|
16
|
704 cp = XSTRING_DATA (Vfloat_output_format);
|
0
|
705
|
|
706 if (cp[0] != '%')
|
|
707 goto lose;
|
|
708 if (cp[1] != '.')
|
|
709 goto lose;
|
|
710
|
|
711 cp += 2;
|
|
712 for (width = 0; (c = *cp, isdigit (c)); cp++)
|
|
713 {
|
|
714 width *= 10;
|
|
715 width += c - '0';
|
|
716 }
|
|
717
|
|
718 if (*cp != 'e' && *cp != 'f' && *cp != 'g' && *cp != 'E' && *cp != 'G')
|
|
719 goto lose;
|
|
720
|
|
721 if (width < (int) (*cp != 'e' && *cp != 'E') || width > DBL_DIG)
|
|
722 goto lose;
|
|
723
|
|
724 if (cp[1] != 0)
|
|
725 goto lose;
|
|
726
|
16
|
727 sprintf (buf, (char *) XSTRING_DATA (Vfloat_output_format),
|
0
|
728 data);
|
|
729 }
|
|
730
|
|
731 /* added by jwz: don't allow "1.0" to print as "1"; that destroys
|
|
732 the read-equivalence of lisp objects. (* x 1) and (* x 1.0) do
|
|
733 not do the same thing, so it's important that the printed
|
|
734 representation of that form not be corrupted by the printer.
|
|
735 */
|
|
736 {
|
|
737 Bufbyte *s = (Bufbyte *) buf; /* don't use signed chars here!
|
|
738 isdigit() can't hack them! */
|
|
739 if (*s == '-') s++;
|
|
740 for (; *s; s++)
|
|
741 /* if there's a non-digit, then there is a decimal point, or
|
|
742 it's in exponential notation, both of which are ok. */
|
|
743 if (!isdigit (*s))
|
|
744 goto DONE_LABEL;
|
|
745 /* otherwise, we need to hack it. */
|
|
746 *s++ = '.';
|
|
747 *s++ = '0';
|
|
748 *s = 0;
|
|
749 }
|
|
750 DONE_LABEL:
|
|
751
|
|
752 /* Some machines print "0.4" as ".4". I don't like that. */
|
|
753 if (buf [0] == '.' || (buf [0] == '-' && buf [1] == '.'))
|
|
754 {
|
|
755 int i;
|
|
756 for (i = strlen (buf) + 1; i >= 0; i--)
|
|
757 buf [i+1] = buf [i];
|
|
758 buf [(buf [0] == '-' ? 1 : 0)] = '0';
|
|
759 }
|
|
760 }
|
|
761 #endif /* LISP_FLOAT_TYPE */
|
|
762
|
|
763 static void
|
|
764 print_vector_internal (CONST char *start, CONST char *end,
|
|
765 Lisp_Object obj,
|
|
766 Lisp_Object printcharfun, int escapeflag)
|
|
767 {
|
|
768 /* This function can GC */
|
|
769 int i;
|
|
770 int len = vector_length (XVECTOR (obj));
|
|
771 int last = len;
|
|
772 struct gcpro gcpro1, gcpro2;
|
|
773 GCPRO2 (obj, printcharfun);
|
|
774
|
|
775 if (INTP (Vprint_length))
|
|
776 {
|
|
777 int max = XINT (Vprint_length);
|
|
778 if (max < len) last = max;
|
|
779 }
|
|
780
|
|
781 write_c_string (start, printcharfun);
|
|
782 for (i = 0; i < last; i++)
|
|
783 {
|
|
784 Lisp_Object elt = vector_data (XVECTOR (obj))[i];
|
|
785 if (i != 0) write_char_internal (" ", printcharfun);
|
|
786 print_internal (elt, printcharfun, escapeflag);
|
|
787 }
|
|
788 UNGCPRO;
|
|
789 if (last != len)
|
|
790 write_c_string (" ...", printcharfun);
|
|
791 write_c_string (end, printcharfun);
|
|
792 }
|
|
793
|
|
794 static void
|
|
795 default_object_printer (Lisp_Object obj, Lisp_Object printcharfun,
|
|
796 int escapeflag)
|
|
797 {
|
|
798 struct lcrecord_header *header =
|
|
799 (struct lcrecord_header *) XPNTR (obj);
|
|
800 char buf[200];
|
|
801
|
|
802 if (print_readably)
|
|
803 error ("printing unreadable object #<%s 0x%x>",
|
|
804 header->lheader.implementation->name, header->uid);
|
|
805
|
|
806 sprintf (buf, "#<%s 0x%x>", header->lheader.implementation->name,
|
|
807 header->uid);
|
|
808 write_c_string (buf, printcharfun);
|
|
809 }
|
|
810
|
|
811 void
|
|
812 internal_object_printer (Lisp_Object obj, Lisp_Object printcharfun,
|
|
813 int escapeflag)
|
|
814 {
|
|
815 char buf[200];
|
|
816 sprintf (buf, "#<INTERNAL OBJECT (XEmacs bug?) (%s) 0x%x>",
|
|
817 XRECORD_LHEADER (obj)->implementation->name,
|
|
818 (EMACS_INT) XPNTR (obj));
|
|
819 write_c_string (buf, printcharfun);
|
|
820 }
|
|
821
|
|
822 void
|
|
823 print_internal (Lisp_Object obj, Lisp_Object printcharfun, int escapeflag)
|
|
824 {
|
|
825 /* This function can GC */
|
|
826 char buf[256];
|
|
827
|
|
828 QUIT;
|
|
829
|
|
830 /* Emacs won't print whilst GCing, but an external debugger might */
|
|
831 if (gc_in_progress) return;
|
|
832
|
|
833 #ifdef I18N3
|
|
834 /* #### Both input and output streams should have a flag associated
|
|
835 with them indicating whether output to that stream, or strings
|
|
836 read from the stream, get translated using Fgettext(). Such a
|
|
837 stream is called a "translating stream". For the minibuffer and
|
|
838 external-debugging-output this is always true on output, and
|
|
839 with-output-to-temp-buffer sets the flag to true for the buffer
|
|
840 it creates. This flag should also be user-settable. Perhaps it
|
|
841 should be split up into two flags, one for input and one for
|
|
842 output. */
|
|
843 #endif
|
|
844
|
|
845 print_depth++;
|
|
846
|
|
847 if (print_depth > 200)
|
|
848 error ("Apparently circular structure being printed");
|
|
849
|
|
850 switch (XTYPE (obj))
|
|
851 {
|
|
852 case Lisp_Int:
|
|
853 {
|
|
854 sprintf (buf, "%d", XINT (obj));
|
|
855 write_c_string (buf, printcharfun);
|
|
856 break;
|
|
857 }
|
|
858
|
70
|
859 case Lisp_Char:
|
|
860 {
|
|
861 /* God intended that this be #\..., you know. */
|
|
862 Emchar ch = XCHAR (obj);
|
|
863 write_c_string ("?", printcharfun);
|
|
864 if (ch == '\n')
|
|
865 strcpy (buf, "\\n");
|
|
866 else if (ch == '\r')
|
|
867 strcpy (buf, "\\r");
|
|
868 else if (ch == '\t')
|
|
869 strcpy (buf, "\\t");
|
|
870 else if (ch < 32)
|
|
871 sprintf (buf, "\\^%c", ch + 64);
|
|
872 else if (ch == 127)
|
|
873 strcpy (buf, "\\^?");
|
|
874 else if (ch >= 128 && ch < 160)
|
|
875 {
|
|
876 Bytecount i;
|
|
877 strcpy (buf, "\\^");
|
|
878 i = set_charptr_emchar ((unsigned char *) (buf + 2), ch + 64);
|
|
879 buf[2+i] = '\0';
|
|
880 }
|
|
881 else if (ch < 127
|
|
882 && !isdigit (ch)
|
|
883 && !isalpha (ch)
|
|
884 && ch != '^') /* must not backslash this or it will
|
|
885 be interpreted as the start of a
|
|
886 control char */
|
|
887 sprintf (buf, "\\%c", ch);
|
|
888 else
|
|
889 {
|
|
890 Bytecount i;
|
|
891 i = set_charptr_emchar ((unsigned char *) buf, ch);
|
|
892 buf[i] = '\0';
|
|
893 }
|
|
894 write_c_string (buf, printcharfun);
|
|
895 break;
|
|
896 }
|
|
897
|
0
|
898 case Lisp_String:
|
|
899 {
|
16
|
900 Bytecount size = XSTRING_LENGTH (obj);
|
0
|
901 struct gcpro gcpro1, gcpro2;
|
|
902 int max = size;
|
|
903 GCPRO2 (obj, printcharfun);
|
|
904
|
|
905 if (INTP (Vprint_string_length) &&
|
|
906 XINT (Vprint_string_length) < max)
|
|
907 max = XINT (Vprint_string_length);
|
|
908 if (max < 0)
|
|
909 max = 0;
|
|
910
|
|
911 /* !!#### This handles MAX incorrectly for Mule. */
|
|
912 if (!escapeflag)
|
|
913 {
|
|
914 /* This deals with GC-relocation */
|
|
915 output_string (printcharfun, 0, obj, 0, max);
|
|
916 if (max < size)
|
|
917 write_c_string (" ...", printcharfun);
|
|
918 }
|
|
919 else
|
|
920 {
|
|
921 Bytecount i;
|
|
922 struct Lisp_String *s = XSTRING (obj);
|
|
923 Bytecount last = 0;
|
|
924
|
|
925 write_char_internal ("\"", printcharfun);
|
|
926 for (i = 0; i < max; i++)
|
|
927 {
|
|
928 Bufbyte ch = string_byte (s, i);
|
|
929 if (ch == '\"' || ch == '\\'
|
|
930 || (ch == '\n' && print_escape_newlines))
|
|
931 {
|
|
932 if (i > last)
|
|
933 {
|
|
934 output_string (printcharfun, 0, obj, last,
|
|
935 i - last);
|
|
936 }
|
|
937 if (ch == '\n')
|
|
938 {
|
|
939 write_c_string ("\\n", printcharfun);
|
|
940 }
|
|
941 else
|
|
942 {
|
|
943 write_char_internal ("\\", printcharfun);
|
|
944 /* This is correct for Mule because the
|
|
945 character is either \ or " */
|
|
946 write_char_internal ((char *) (string_data (s) + i),
|
|
947 printcharfun);
|
|
948 }
|
|
949 last = i + 1;
|
|
950 }
|
|
951 }
|
|
952 if (max > last)
|
|
953 {
|
|
954 output_string (printcharfun, 0, obj, last,
|
|
955 max - last);
|
|
956 }
|
|
957 if (max < size)
|
|
958 write_c_string (" ...", printcharfun);
|
|
959 write_char_internal ("\"", printcharfun);
|
|
960 }
|
|
961 UNGCPRO;
|
|
962 break;
|
|
963 }
|
|
964
|
|
965 case Lisp_Cons:
|
|
966 {
|
|
967 struct gcpro gcpro1, gcpro2;
|
|
968
|
|
969 /* If deeper than spec'd depth, print placeholder. */
|
|
970 if (INTP (Vprint_level)
|
|
971 && print_depth > XINT (Vprint_level))
|
|
972 {
|
|
973 write_c_string ("...", printcharfun);
|
|
974 break;
|
|
975 }
|
|
976
|
|
977 /* If print_readably is on, print (quote -foo-) as '-foo-
|
|
978 (Yeah, this should really be what print-pretty does, but we
|
|
979 don't have the rest of a pretty printer, and this actually
|
|
980 has non-negligible impact on size/speed of .elc files.)
|
|
981 */
|
|
982 if (print_readably &&
|
|
983 EQ (XCAR (obj), Qquote) &&
|
|
984 CONSP (XCDR (obj)) &&
|
|
985 NILP (XCDR (XCDR (obj))))
|
|
986 {
|
|
987 obj = XCAR (XCDR (obj));
|
|
988 GCPRO2 (obj, printcharfun);
|
|
989 write_char_internal ("'", printcharfun);
|
|
990 UNGCPRO;
|
|
991 print_internal (obj, printcharfun, escapeflag);
|
|
992 break;
|
|
993 }
|
|
994
|
|
995 GCPRO2 (obj, printcharfun);
|
|
996 write_char_internal ("(", printcharfun);
|
|
997 {
|
|
998 int i = 0;
|
|
999 int max = 0;
|
|
1000
|
|
1001 if (INTP (Vprint_length))
|
|
1002 max = XINT (Vprint_length);
|
|
1003 while (CONSP (obj))
|
|
1004 {
|
|
1005 if (i++)
|
|
1006 write_char_internal (" ", printcharfun);
|
|
1007 if (max && i > max)
|
|
1008 {
|
|
1009 write_c_string ("...", printcharfun);
|
|
1010 break;
|
|
1011 }
|
|
1012 print_internal (Fcar (obj), printcharfun,
|
|
1013 escapeflag);
|
|
1014 obj = Fcdr (obj);
|
|
1015 }
|
|
1016 }
|
|
1017 if (!NILP (obj) && !CONSP (obj))
|
|
1018 {
|
|
1019 write_c_string (" . ", printcharfun);
|
|
1020 print_internal (obj, printcharfun, escapeflag);
|
|
1021 }
|
|
1022 UNGCPRO;
|
|
1023 write_char_internal (")", printcharfun);
|
|
1024 break;
|
|
1025 }
|
|
1026
|
|
1027 #ifndef LRECORD_VECTOR
|
|
1028 case Lisp_Vector:
|
|
1029 {
|
|
1030 /* If deeper than spec'd depth, print placeholder. */
|
|
1031 if (INTP (Vprint_level)
|
|
1032 && print_depth > XINT (Vprint_level))
|
|
1033 {
|
|
1034 write_c_string ("...", printcharfun);
|
|
1035 break;
|
|
1036 }
|
|
1037
|
|
1038 /* God intended that this be #(...), you know. */
|
|
1039 print_vector_internal ("[", "]", obj, printcharfun, escapeflag);
|
|
1040 break;
|
|
1041 }
|
|
1042 #endif /* !LRECORD_VECTOR */
|
|
1043
|
|
1044 #ifndef LRECORD_SYMBOL
|
|
1045 case Lisp_Symbol:
|
|
1046 {
|
|
1047 print_symbol (obj, printcharfun, escapeflag);
|
|
1048 break;
|
|
1049 }
|
|
1050 #endif /* !LRECORD_SYMBOL */
|
|
1051
|
|
1052 case Lisp_Record:
|
|
1053 {
|
|
1054 struct lrecord_header *lheader = XRECORD_LHEADER (obj);
|
|
1055 struct gcpro gcpro1, gcpro2;
|
|
1056
|
|
1057 GCPRO2 (obj, printcharfun);
|
|
1058 if (lheader->implementation->printer)
|
|
1059 ((lheader->implementation->printer)
|
|
1060 (obj, printcharfun, escapeflag));
|
|
1061 else
|
|
1062 default_object_printer (obj, printcharfun, escapeflag);
|
|
1063 UNGCPRO;
|
|
1064 break;
|
|
1065 }
|
|
1066
|
|
1067 default:
|
|
1068 {
|
|
1069 /* We're in trouble if this happens!
|
|
1070 Probably should just abort () */
|
|
1071 if (print_readably)
|
|
1072 error ("printing illegal data type #o%03o",
|
|
1073 (int) XTYPE (obj));
|
|
1074 write_c_string ("#<EMACS BUG: ILLEGAL DATATYPE ",
|
|
1075 printcharfun);
|
|
1076 sprintf (buf, "(#o%3o)", (int) XTYPE (obj));
|
|
1077 write_c_string (buf, printcharfun);
|
|
1078 write_c_string
|
|
1079 (" Save your buffers immediately and please report this bug>",
|
|
1080 printcharfun);
|
|
1081 break;
|
|
1082 }
|
|
1083 }
|
|
1084
|
|
1085 print_depth--;
|
|
1086 }
|
|
1087
|
|
1088 static void
|
|
1089 print_compiled_function_internal (CONST char *start, CONST char *end,
|
|
1090 Lisp_Object obj,
|
|
1091 Lisp_Object printcharfun, int escapeflag)
|
|
1092 {
|
|
1093 /* This function can GC */
|
|
1094 struct Lisp_Compiled_Function *b =
|
|
1095 XCOMPILED_FUNCTION (obj); /* GC doesn't relocate */
|
|
1096 int docp = b->flags.documentationp;
|
|
1097 int intp = b->flags.interactivep;
|
|
1098 struct gcpro gcpro1, gcpro2;
|
|
1099 char buf[100];
|
|
1100 GCPRO2 (obj, printcharfun);
|
|
1101
|
|
1102 write_c_string (start, printcharfun);
|
|
1103 #ifdef COMPILED_FUNCTION_ANNOTATION_HACK
|
|
1104 if (!print_readably)
|
|
1105 {
|
|
1106 Lisp_Object ann = compiled_function_annotation (b);
|
|
1107 if (!NILP (ann))
|
|
1108 {
|
|
1109 write_c_string ("(from ", printcharfun);
|
|
1110 print_internal (ann, printcharfun, 1);
|
|
1111 write_c_string (") ", printcharfun);
|
|
1112 }
|
|
1113 }
|
|
1114 #endif /* COMPILED_FUNCTION_ANNOTATION_HACK */
|
|
1115 /* COMPILED_ARGLIST = 0 */
|
|
1116 print_internal (b->arglist, printcharfun, escapeflag);
|
|
1117 /* COMPILED_BYTECODE = 1 */
|
|
1118 write_char_internal (" ", printcharfun);
|
|
1119 /* we don't really want to see that junk in the bytecode instructions. */
|
|
1120 if (STRINGP (b->bytecodes) && !print_readably)
|
|
1121 {
|
16
|
1122 sprintf (buf, "\"...(%ld)\"", (long) XSTRING_LENGTH (b->bytecodes));
|
0
|
1123 write_c_string (buf, printcharfun);
|
|
1124 }
|
|
1125 else
|
|
1126 print_internal (b->bytecodes, printcharfun, escapeflag);
|
|
1127 /* COMPILED_CONSTANTS = 2 */
|
|
1128 write_char_internal (" ", printcharfun);
|
|
1129 print_internal (b->constants, printcharfun, escapeflag);
|
|
1130 /* COMPILED_STACK_DEPTH = 3 */
|
|
1131 sprintf (buf, " %d", b->maxdepth);
|
|
1132 write_c_string (buf, printcharfun);
|
|
1133 /* COMPILED_DOC_STRING = 4 */
|
|
1134 if (docp || intp)
|
|
1135 {
|
|
1136 write_char_internal (" ", printcharfun);
|
|
1137 print_internal (compiled_function_documentation (b), printcharfun,
|
|
1138 escapeflag);
|
|
1139 }
|
|
1140 /* COMPILED_INTERACTIVE = 5 */
|
|
1141 if (intp)
|
|
1142 {
|
|
1143 write_char_internal (" ", printcharfun);
|
|
1144 print_internal (compiled_function_interactive (b), printcharfun,
|
|
1145 escapeflag);
|
|
1146 }
|
|
1147 UNGCPRO;
|
|
1148 write_c_string (end, printcharfun);
|
|
1149 }
|
|
1150
|
|
1151 void
|
|
1152 print_compiled_function (Lisp_Object obj, Lisp_Object printcharfun,
|
|
1153 int escapeflag)
|
|
1154 {
|
|
1155 /* This function can GC */
|
|
1156 print_compiled_function_internal (((print_readably) ? "#[" :
|
|
1157 "#<compiled-function "),
|
|
1158 ((print_readably) ? "]" : ">"),
|
|
1159 obj, printcharfun, escapeflag);
|
|
1160 }
|
|
1161
|
|
1162 #ifdef LISP_FLOAT_TYPE
|
|
1163 void
|
|
1164 print_float (Lisp_Object obj, Lisp_Object printcharfun, int escapeflag)
|
|
1165 {
|
|
1166 char pigbuf[350]; /* see comments in float_to_string */
|
|
1167
|
|
1168 float_to_string (pigbuf, float_data (XFLOAT (obj)));
|
|
1169 write_c_string (pigbuf, printcharfun);
|
|
1170 }
|
|
1171 #endif /* LISP_FLOAT_TYPE */
|
|
1172
|
|
1173 void
|
|
1174 print_symbol (Lisp_Object obj, Lisp_Object printcharfun, int escapeflag)
|
|
1175 {
|
|
1176 /* This function can GC */
|
|
1177 /* #### Bug!! (intern "") isn't printed in some distinguished way */
|
|
1178 /* #### (the reader also loses on it) */
|
|
1179 struct Lisp_String *name = XSYMBOL (obj)->name;
|
|
1180 Bytecount size = string_length (name);
|
|
1181 struct gcpro gcpro1, gcpro2;
|
|
1182
|
|
1183 if (!escapeflag)
|
|
1184 {
|
|
1185 /* This deals with GC-relocation */
|
|
1186 Lisp_Object nameobj;
|
|
1187 XSETSTRING (nameobj, name);
|
|
1188 output_string (printcharfun, 0, nameobj, 0, size);
|
|
1189 return;
|
|
1190 }
|
|
1191 GCPRO2 (obj, printcharfun);
|
|
1192
|
|
1193 if (print_gensym)
|
|
1194 {
|
|
1195 Lisp_Object tem = oblookup (Vobarray, string_data (name), size);
|
|
1196 if (!EQ (tem, obj))
|
|
1197 /* (read) would return a new symbol with the same name.
|
|
1198 This isn't quite correct, because that symbol might not
|
|
1199 really be uninterned (it might be interned in some other
|
|
1200 obarray) but there's no way to win in that case without
|
|
1201 implementing a real package system.
|
|
1202 */
|
|
1203 write_c_string ("#:", printcharfun);
|
|
1204 }
|
|
1205
|
|
1206 /* Does it look like an integer or a float? */
|
|
1207 {
|
|
1208 Bufbyte *data = string_data (name);
|
|
1209 Bytecount confusing = 0;
|
|
1210
|
|
1211 if (size == 0)
|
|
1212 goto not_yet_confused; /* Really confusing */
|
|
1213 else if (isdigit (data[0]))
|
|
1214 confusing = 0;
|
|
1215 else if (size == 1)
|
|
1216 goto not_yet_confused;
|
|
1217 else if (data[0] == '-' || data[0] == '+')
|
|
1218 confusing = 1;
|
|
1219 else
|
|
1220 goto not_yet_confused;
|
|
1221
|
|
1222 for (; confusing < size; confusing++)
|
|
1223 {
|
|
1224 if (!isdigit (data[confusing]))
|
|
1225 {
|
|
1226 confusing = 0;
|
|
1227 break;
|
|
1228 }
|
|
1229 }
|
|
1230 not_yet_confused:
|
|
1231
|
|
1232 #ifdef LISP_FLOAT_TYPE
|
|
1233 if (!confusing)
|
|
1234 confusing = isfloat_string ((char *) data);
|
|
1235 #endif
|
|
1236 if (confusing)
|
|
1237 write_char_internal ("\\", printcharfun);
|
|
1238 }
|
|
1239
|
|
1240 {
|
|
1241 Lisp_Object nameobj;
|
|
1242 Bytecount i;
|
|
1243 Bytecount last = 0;
|
|
1244
|
|
1245 XSETSTRING (nameobj, name);
|
|
1246 for (i = 0; i < size; i++)
|
|
1247 {
|
|
1248 Bufbyte c = string_byte (name, i);
|
|
1249
|
|
1250 if (c == '\"' || c == '\\' || c == '\'' || c == ';' || c == '#' ||
|
|
1251 c == '(' || c == ')' || c == ',' || c =='.' || c == '`' ||
|
|
1252 c == '[' || c == ']' || c == '?' || c <= 040)
|
|
1253 {
|
|
1254 if (i > last)
|
|
1255 {
|
|
1256 output_string (printcharfun, 0, nameobj, last,
|
|
1257 i - last);
|
|
1258 }
|
|
1259 write_char_internal ("\\", printcharfun);
|
|
1260 last = i;
|
|
1261 }
|
|
1262 }
|
|
1263 output_string (printcharfun, 0, nameobj, last, size - last);
|
|
1264 }
|
|
1265 UNGCPRO;
|
|
1266 }
|
|
1267
|
|
1268
|
|
1269 int alternate_do_pointer;
|
|
1270 char alternate_do_string[5000];
|
|
1271
|
20
|
1272 DEFUN ("alternate-debugging-output", Falternate_debugging_output, 1, 1, 0, /*
|
0
|
1273 Append CHARACTER to the array `alternate_do_string'.
|
|
1274 This can be used in place of `external-debugging-output' as a function
|
|
1275 to be passed to `print'. Before calling `print', set `alternate_do_pointer'
|
|
1276 to 0.
|
|
1277
|
20
|
1278 */
|
|
1279 (character))
|
0
|
1280 {
|
|
1281 Bufbyte str[MAX_EMCHAR_LEN];
|
|
1282 Bytecount len;
|
|
1283 int extlen;
|
|
1284 CONST Extbyte *extptr;
|
|
1285
|
|
1286 CHECK_CHAR_COERCE_INT (character);
|
|
1287 len = set_charptr_emchar (str, XCHAR (character));
|
16
|
1288 GET_CHARPTR_EXT_DATA_ALLOCA (str, len, FORMAT_TERMINAL, extptr, extlen);
|
0
|
1289 memcpy (alternate_do_string + alternate_do_pointer, extptr, extlen);
|
|
1290 alternate_do_pointer += extlen;
|
|
1291 alternate_do_string[alternate_do_pointer] = 0;
|
|
1292 return character;
|
|
1293 }
|
|
1294
|
20
|
1295 DEFUN ("external-debugging-output", Fexternal_debugging_output, 1, 3, 0, /*
|
0
|
1296 Write CHAR-OR-STRING to stderr or stdout.
|
|
1297 If optional arg STDOUT-P is non-nil, write to stdout; otherwise, write
|
|
1298 to stderr. You can use this function to write directly to the terminal.
|
|
1299 This function can be used as the STREAM argument of Fprint() or the like.
|
|
1300
|
|
1301 If you have opened a termscript file (using `open-termscript'), then
|
|
1302 the output also will be logged to this file.
|
20
|
1303 */
|
|
1304 (char_or_string, stdout_p, device))
|
0
|
1305 {
|
|
1306 FILE *file = 0;
|
|
1307 struct console *con = 0;
|
|
1308
|
|
1309 if (NILP (device))
|
|
1310 {
|
|
1311 if (!NILP (stdout_p))
|
|
1312 file = stdout;
|
|
1313 else
|
|
1314 file = stderr;
|
|
1315 }
|
|
1316 else
|
|
1317 {
|
|
1318 CHECK_LIVE_DEVICE (device);
|
|
1319 if (!DEVICE_TTY_P (XDEVICE (device)) &&
|
|
1320 !DEVICE_STREAM_P (XDEVICE (device)))
|
|
1321 signal_simple_error ("Must be tty or stream device", device);
|
|
1322 con = XCONSOLE (DEVICE_CONSOLE (XDEVICE (device)));
|
|
1323 if (DEVICE_TTY_P (XDEVICE (device)))
|
|
1324 file = 0;
|
|
1325 else if (!NILP (stdout_p))
|
|
1326 file = CONSOLE_STREAM_DATA (con)->outfd;
|
|
1327 else
|
|
1328 file = CONSOLE_STREAM_DATA (con)->errfd;
|
|
1329 }
|
|
1330
|
|
1331 if (STRINGP (char_or_string))
|
|
1332 write_string_to_stdio_stream (file, con,
|
16
|
1333 XSTRING_DATA (char_or_string),
|
|
1334 0, XSTRING_LENGTH (char_or_string),
|
|
1335 FORMAT_TERMINAL);
|
0
|
1336 else
|
|
1337 {
|
|
1338 Bufbyte str[MAX_EMCHAR_LEN];
|
|
1339 Bytecount len;
|
|
1340
|
|
1341 CHECK_CHAR_COERCE_INT (char_or_string);
|
|
1342 len = set_charptr_emchar (str, XCHAR (char_or_string));
|
16
|
1343 write_string_to_stdio_stream (file, con, str, 0, len, FORMAT_TERMINAL);
|
0
|
1344 }
|
|
1345
|
|
1346 return char_or_string;
|
|
1347 }
|
|
1348
|
20
|
1349 DEFUN ("open-termscript", Fopen_termscript, 1, 1, "FOpen termscript file: ", /*
|
0
|
1350 Start writing all terminal output to FILE as well as the terminal.
|
|
1351 FILE = nil means just close any termscript file currently open.
|
20
|
1352 */
|
|
1353 (file))
|
0
|
1354 {
|
|
1355 /* This function can GC */
|
|
1356 if (termscript != 0)
|
|
1357 fclose (termscript);
|
|
1358 termscript = 0;
|
|
1359
|
|
1360 if (! NILP (file))
|
|
1361 {
|
|
1362 file = Fexpand_file_name (file, Qnil);
|
16
|
1363 termscript = fopen ((char *) XSTRING_DATA (file), "w");
|
0
|
1364 if (termscript == 0)
|
|
1365 report_file_error ("Opening termscript", Fcons (file, Qnil));
|
|
1366 }
|
|
1367 return Qnil;
|
|
1368 }
|
|
1369
|
|
1370 #if 1
|
|
1371 /* Debugging kludge -- unbuffered */
|
|
1372 static int debug_print_length = 50;
|
|
1373 static int debug_print_level = 15;
|
|
1374 Lisp_Object debug_temp;
|
|
1375 void debug_print_no_newline (Lisp_Object debug_print_obj);
|
|
1376 void
|
|
1377 debug_print_no_newline (Lisp_Object debug_print_obj)
|
|
1378 {
|
|
1379 /* This function can GC */
|
|
1380 int old_print_readably = print_readably;
|
|
1381 int old_print_depth = print_depth;
|
|
1382 Lisp_Object old_print_length = Vprint_length;
|
|
1383 Lisp_Object old_print_level = Vprint_level;
|
|
1384 Lisp_Object old_inhibit_quit = Vinhibit_quit;
|
|
1385 struct gcpro gcpro1, gcpro2, gcpro3;
|
|
1386 GCPRO3 (old_print_level, old_print_length, old_inhibit_quit);
|
|
1387
|
|
1388 if (gc_in_progress)
|
|
1389 stderr_out ("** gc-in-progress! Bad idea to print anything! **\n");
|
|
1390
|
|
1391 print_depth = 0;
|
|
1392 print_readably = 0;
|
|
1393 print_unbuffered++;
|
|
1394 /* Could use unwind-protect, but why bother? */
|
|
1395 if (debug_print_length > 0)
|
|
1396 Vprint_length = make_int (debug_print_length);
|
|
1397 if (debug_print_level > 0)
|
|
1398 Vprint_level = make_int (debug_print_level);
|
|
1399 print_internal (debug_print_obj, Qexternal_debugging_output, 1);
|
|
1400 Vinhibit_quit = old_inhibit_quit;
|
|
1401 Vprint_level = old_print_level;
|
|
1402 Vprint_length = old_print_length;
|
|
1403 print_depth = old_print_depth;
|
|
1404 print_readably = old_print_readably;
|
|
1405 print_unbuffered--;
|
|
1406 UNGCPRO;
|
|
1407 }
|
|
1408
|
|
1409 void debug_print (Lisp_Object debug_print_obj);
|
|
1410 void
|
|
1411 debug_print (Lisp_Object debug_print_obj)
|
|
1412 {
|
|
1413 debug_print_no_newline (debug_print_obj);
|
|
1414 stderr_out ("\n");
|
|
1415 fflush (stderr);
|
|
1416 }
|
|
1417
|
|
1418 /* Debugging kludge -- unbuffered */
|
|
1419 void
|
|
1420 debug_backtrace (void)
|
|
1421 {
|
|
1422 /* This function can GC */
|
|
1423 int old_print_readably = print_readably;
|
|
1424 int old_print_depth = print_depth;
|
|
1425 Lisp_Object old_print_length = Vprint_length;
|
|
1426 Lisp_Object old_print_level = Vprint_level;
|
|
1427 Lisp_Object old_inhibit_quit = Vinhibit_quit;
|
|
1428 struct gcpro gcpro1, gcpro2, gcpro3;
|
|
1429 GCPRO3 (old_print_level, old_print_length, old_inhibit_quit);
|
|
1430
|
|
1431 if (gc_in_progress)
|
|
1432 stderr_out ("** gc-in-progress! Bad idea to print anything! **\n");
|
|
1433
|
|
1434 print_depth = 0;
|
|
1435 print_readably = 0;
|
|
1436 print_unbuffered++;
|
|
1437 /* Could use unwind-protect, but why bother? */
|
|
1438 if (debug_print_length > 0)
|
|
1439 Vprint_length = make_int (debug_print_length);
|
|
1440 if (debug_print_level > 0)
|
|
1441 Vprint_level = make_int (debug_print_level);
|
|
1442 Fbacktrace (Qexternal_debugging_output, Qt);
|
|
1443 stderr_out ("\n");
|
|
1444 fflush (stderr);
|
|
1445 Vinhibit_quit = old_inhibit_quit;
|
|
1446 Vprint_level = old_print_level;
|
|
1447 Vprint_length = old_print_length;
|
|
1448 print_depth = old_print_depth;
|
|
1449 print_readably = old_print_readably;
|
|
1450 print_unbuffered--;
|
|
1451 UNGCPRO;
|
|
1452 }
|
|
1453
|
|
1454 void
|
|
1455 debug_short_backtrace (int length)
|
|
1456 {
|
|
1457 int first = 1;
|
|
1458 struct backtrace *bt = backtrace_list;
|
|
1459 stderr_out (" [");
|
|
1460 fflush (stderr);
|
|
1461 while (length > 0 && bt)
|
|
1462 {
|
|
1463 if (!first)
|
|
1464 {
|
|
1465 stderr_out (", ");
|
|
1466 fflush (stderr);
|
|
1467 }
|
|
1468 if (COMPILED_FUNCTIONP (*bt->function))
|
|
1469 {
|
|
1470 Lisp_Object ann = Fcompiled_function_annotation (*bt->function);
|
|
1471 if (!NILP (ann))
|
|
1472 {
|
|
1473 stderr_out ("<compiled-function from ");
|
|
1474 fflush (stderr);
|
|
1475 debug_print_no_newline (ann);
|
|
1476 stderr_out (">");
|
|
1477 fflush (stderr);
|
|
1478 }
|
|
1479 else
|
|
1480 {
|
|
1481 stderr_out ("<compiled-function of unknown origin>");
|
|
1482 fflush (stderr);
|
|
1483 }
|
|
1484 }
|
|
1485 else
|
|
1486 debug_print_no_newline (*bt->function);
|
|
1487 first = 0;
|
|
1488 length--;
|
|
1489 bt = bt->next;
|
|
1490 }
|
|
1491 stderr_out ("]\n");
|
|
1492 fflush (stderr);
|
|
1493 }
|
|
1494
|
|
1495 #endif /* debugging kludge */
|
|
1496
|
|
1497
|
|
1498 void
|
|
1499 syms_of_print (void)
|
|
1500 {
|
|
1501 defsymbol (&Qprint_escape_newlines, "print-escape-newlines");
|
|
1502 defsymbol (&Qprint_readably, "print-readably");
|
|
1503
|
|
1504 defsymbol (&Qstandard_output, "standard-output");
|
|
1505
|
|
1506 #ifdef LISP_FLOAT_TYPE
|
|
1507 defsymbol (&Qfloat_output_format, "float-output-format");
|
|
1508 #endif
|
|
1509
|
|
1510 defsymbol (&Qprint_length, "print-length");
|
|
1511
|
|
1512 defsymbol (&Qprint_string_length, "print-string-length");
|
20
|
1513 DEFSUBR (Fprin1);
|
|
1514 DEFSUBR (Fprin1_to_string);
|
|
1515 DEFSUBR (Fprinc);
|
|
1516 DEFSUBR (Fprint);
|
100
|
1517 DEFSUBR (Ferror_message_string);
|
20
|
1518 DEFSUBR (Fterpri);
|
|
1519 DEFSUBR (Fwrite_char);
|
|
1520 DEFSUBR (Falternate_debugging_output);
|
0
|
1521 defsymbol (&Qalternate_debugging_output, "alternate-debugging-output");
|
20
|
1522 DEFSUBR (Fexternal_debugging_output);
|
|
1523 DEFSUBR (Fopen_termscript);
|
0
|
1524 defsymbol (&Qexternal_debugging_output, "external-debugging-output");
|
|
1525 #ifndef standalone
|
20
|
1526 DEFSUBR (Fwith_output_to_temp_buffer);
|
0
|
1527 #endif /* not standalone */
|
|
1528 }
|
|
1529
|
|
1530 void
|
|
1531 lstream_type_create_print (void)
|
|
1532 {
|
|
1533 LSTREAM_HAS_METHOD (print, writer);
|
|
1534 LSTREAM_HAS_METHOD (print, marker);
|
|
1535 }
|
|
1536
|
|
1537 void
|
|
1538 vars_of_print (void)
|
|
1539 {
|
|
1540 alternate_do_pointer = 0;
|
|
1541
|
|
1542 DEFVAR_LISP ("standard-output", &Vstandard_output /*
|
|
1543 Output stream `print' uses by default for outputting a character.
|
|
1544 This may be any function of one argument.
|
|
1545 It may also be a buffer (output is inserted before point)
|
|
1546 or a marker (output is inserted and the marker is advanced)
|
|
1547 or the symbol t (output appears in the minibuffer line).
|
|
1548 */ );
|
|
1549 Vstandard_output = Qt;
|
|
1550
|
|
1551 #ifdef LISP_FLOAT_TYPE
|
|
1552 DEFVAR_LISP ("float-output-format", &Vfloat_output_format /*
|
|
1553 The format descriptor string that lisp uses to print floats.
|
|
1554 This is a %-spec like those accepted by `printf' in C,
|
|
1555 but with some restrictions. It must start with the two characters `%.'.
|
|
1556 After that comes an integer precision specification,
|
|
1557 and then a letter which controls the format.
|
|
1558 The letters allowed are `e', `f' and `g'.
|
|
1559 Use `e' for exponential notation \"DIG.DIGITSeEXPT\"
|
|
1560 Use `f' for decimal point notation \"DIGITS.DIGITS\".
|
|
1561 Use `g' to choose the shorter of those two formats for the number at hand.
|
|
1562 The precision in any of these cases is the number of digits following
|
|
1563 the decimal point. With `f', a precision of 0 means to omit the
|
|
1564 decimal point. 0 is not allowed with `f' or `g'.
|
|
1565
|
|
1566 A value of nil means to use `%.16g'.
|
|
1567
|
|
1568 Regardless of the value of `float-output-format', a floating point number
|
|
1569 will never be printed in such a way that it is ambiguous with an integer;
|
|
1570 that is, a floating-point number will always be printed with a decimal
|
|
1571 point and/or an exponent, even if the digits following the decimal point
|
|
1572 are all zero. This is to preserve read-equivalence.
|
|
1573 */ );
|
|
1574 Vfloat_output_format = Qnil;
|
|
1575 #endif /* LISP_FLOAT_TYPE */
|
|
1576
|
|
1577 DEFVAR_LISP ("print-length", &Vprint_length /*
|
|
1578 Maximum length of list or vector to print before abbreviating.
|
|
1579 A value of nil means no limit.
|
|
1580 */ );
|
|
1581 Vprint_length = Qnil;
|
|
1582
|
|
1583 DEFVAR_LISP ("print-string-length", &Vprint_string_length /*
|
|
1584 Maximum length of string to print before abbreviating.
|
|
1585 A value of nil means no limit.
|
|
1586 */ );
|
|
1587 Vprint_string_length = Qnil;
|
|
1588
|
|
1589 DEFVAR_LISP ("print-level", &Vprint_level /*
|
|
1590 Maximum depth of list nesting to print before abbreviating.
|
|
1591 A value of nil means no limit.
|
|
1592 */ );
|
|
1593 Vprint_level = Qnil;
|
|
1594
|
|
1595 DEFVAR_BOOL ("print-escape-newlines", &print_escape_newlines /*
|
|
1596 Non-nil means print newlines in strings as backslash-n.
|
|
1597 */ );
|
|
1598 print_escape_newlines = 0;
|
|
1599
|
|
1600 DEFVAR_BOOL ("print-readably", &print_readably /*
|
|
1601 If non-nil, then all objects will be printed in a readable form.
|
|
1602 If an object has no readable representation, then an error is signalled.
|
|
1603 When print-readably is true, compiled-function objects will be written in
|
|
1604 #[...] form instead of in #<compiled-function [...]> form, and two-element
|
|
1605 lists of the form (quote object) will be written as the equivalent 'object.
|
|
1606 Do not SET this variable; bind it instead.
|
|
1607 */ );
|
|
1608 print_readably = 0;
|
|
1609
|
|
1610 DEFVAR_BOOL ("print-gensym", &print_gensym /*
|
|
1611 If non-nil, then uninterned symbols will be printed specially.
|
|
1612 Uninterned symbols are those which are not present in `obarray', that is,
|
|
1613 those which were made with `make-symbol' or by calling `intern' with a
|
|
1614 second argument.
|
|
1615
|
110
|
1616 When print-gensym is true, such symbols will be preceded by \"#:\", which
|
0
|
1617 causes the reader to create a new symbol instead of interning and returning
|
|
1618 an existing one. Beware: the #: syntax creates a new symbol each time it is
|
|
1619 seen, so if you print an object which contains two pointers to the same
|
|
1620 uninterned symbol, `read' will not duplicate that structure.
|
|
1621
|
|
1622 Also, since XEmacs has no real notion of packages, there is no way for the
|
|
1623 printer to distinguish between symbols interned in no obarray, and symbols
|
|
1624 interned in an alternate obarray.
|
|
1625 */ );
|
|
1626 print_gensym = 0;
|
|
1627
|
|
1628 DEFVAR_LISP ("print-message-label", &Vprint_message_label /*
|
|
1629 Label for minibuffer messages created with `print'. This should
|
|
1630 generally be bound with `let' rather than set. (See `display-message'.)
|
|
1631 */ );
|
|
1632 Vprint_message_label = Qprint;
|
|
1633
|
|
1634 /* prin1_to_string_buffer initialized in init_buffer_once in buffer.c */
|
|
1635 staticpro (&Vprin1_to_string_buffer);
|
|
1636 }
|