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