428
|
1 /* Lisp object printing and output streams.
|
|
2 Copyright (C) 1985, 1986, 1988, 1992-1995 Free Software Foundation, Inc.
|
793
|
3 Copyright (C) 1995, 1996, 2000, 2001, 2002 Ben Wing.
|
428
|
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
|
771
|
26 /* Seriously divergent from FSF by this point.
|
|
27
|
|
28 Seriously hacked on by Ben Wing for Mule. All stdio code also by Ben,
|
|
29 as well as the debugging code (initial version of debug_print(), though,
|
|
30 by Jamie Zawinski) and the _fmt interfaces. Also a fair amount of work
|
|
31 by Hrvoje, e.g. floating-point code and rewriting to avoid O(N^2)
|
|
32 consing when outputting to the echo area. Print-circularity code by
|
|
33 Martin? */
|
428
|
34
|
|
35 #include <config.h>
|
|
36 #include "lisp.h"
|
|
37
|
|
38 #include "backtrace.h"
|
|
39 #include "buffer.h"
|
|
40 #include "bytecode.h"
|
800
|
41 #include "device.h"
|
428
|
42 #include "extents.h"
|
|
43 #include "frame.h"
|
|
44 #include "insdel.h"
|
|
45 #include "lstream.h"
|
771
|
46 #include "opaque.h"
|
800
|
47
|
|
48 #include "console-tty.h"
|
|
49 #include "console-stream.h"
|
442
|
50 #ifdef WIN32_NATIVE
|
|
51 #include "console-msw.h"
|
|
52 #endif
|
428
|
53
|
800
|
54 #include "sysfile.h"
|
|
55
|
428
|
56 #include <float.h>
|
|
57 /* Define if not in float.h */
|
|
58 #ifndef DBL_DIG
|
|
59 #define DBL_DIG 16
|
|
60 #endif
|
|
61
|
|
62 Lisp_Object Vstandard_output, Qstandard_output;
|
|
63
|
|
64 /* The subroutine object for external-debugging-output is kept here
|
|
65 for the convenience of the debugger. */
|
442
|
66 Lisp_Object Qexternal_debugging_output, Qalternate_debugging_output;
|
|
67
|
|
68 #ifdef HAVE_MS_WINDOWS
|
|
69 Lisp_Object Qmswindows_debugging_output;
|
|
70 #endif
|
428
|
71
|
|
72 /* Avoid actual stack overflow in print. */
|
|
73 static int print_depth;
|
|
74
|
|
75 /* Detect most circularities to print finite output. */
|
|
76 #define PRINT_CIRCLE 200
|
|
77 static Lisp_Object being_printed[PRINT_CIRCLE];
|
|
78
|
|
79 /* Maximum length of list or vector to print in full; noninteger means
|
|
80 effectively infinity */
|
|
81
|
|
82 Lisp_Object Vprint_length;
|
|
83 Lisp_Object Qprint_length;
|
|
84
|
|
85 /* Maximum length of string to print in full; noninteger means
|
|
86 effectively infinity */
|
|
87
|
|
88 Lisp_Object Vprint_string_length;
|
|
89 Lisp_Object Qprint_string_length;
|
|
90
|
|
91 /* Maximum depth of list to print in full; noninteger means
|
|
92 effectively infinity. */
|
|
93
|
|
94 Lisp_Object Vprint_level;
|
|
95
|
|
96 /* Label to use when making echo-area messages. */
|
|
97
|
|
98 Lisp_Object Vprint_message_label;
|
|
99
|
|
100 /* Nonzero means print newlines in strings as \n. */
|
|
101
|
|
102 int print_escape_newlines;
|
|
103 int print_readably;
|
|
104
|
|
105 /* Non-nil means print #: before uninterned symbols.
|
|
106 Neither t nor nil means so that and don't clear Vprint_gensym_alist
|
|
107 on entry to and exit from print functions. */
|
|
108 Lisp_Object Vprint_gensym;
|
|
109 Lisp_Object Vprint_gensym_alist;
|
|
110
|
|
111 Lisp_Object Qdisplay_error;
|
|
112 Lisp_Object Qprint_message_label;
|
|
113
|
|
114 /* Force immediate output of all printed data. Used for debugging. */
|
|
115 int print_unbuffered;
|
|
116
|
|
117 FILE *termscript; /* Stdio stream being used for copy of all output. */
|
|
118
|
771
|
119 static void write_string_to_alternate_debugging_output (Intbyte *str,
|
|
120 Bytecount len);
|
|
121
|
428
|
122
|
|
123
|
|
124 int stdout_needs_newline;
|
|
125
|
771
|
126 /* Basic function to actually write to a stdio stream or TTY console. */
|
|
127
|
442
|
128 static void
|
771
|
129 write_string_to_stdio_stream (FILE *stream, struct console *con,
|
|
130 const Intbyte *ptr, Bytecount len,
|
|
131 int must_flush)
|
428
|
132 {
|
771
|
133 Extbyte *extptr = 0;
|
|
134 Bytecount extlen = 0;
|
|
135 int output_is_std_handle =
|
|
136 stream ? stream == stdout || stream == stderr :
|
|
137 CONSOLE_TTY_DATA (con)->is_stdio;
|
|
138
|
|
139 if (stream || output_is_std_handle)
|
|
140 {
|
|
141 if (initialized && !inhibit_non_essential_printing_operations)
|
|
142 TO_EXTERNAL_FORMAT (DATA, (ptr, len),
|
|
143 ALLOCA, (extptr, extlen),
|
|
144 Qterminal);
|
|
145 else
|
|
146 {
|
|
147 extptr = (Extbyte *) ptr;
|
|
148 extlen = (Bytecount) len;
|
|
149 }
|
|
150 }
|
|
151
|
428
|
152 if (stream)
|
|
153 {
|
442
|
154 #ifdef WIN32_NATIVE
|
|
155 HANDLE errhand = GetStdHandle (STD_INPUT_HANDLE);
|
|
156 int no_useful_stderr = errhand == 0 || errhand == INVALID_HANDLE_VALUE;
|
|
157
|
|
158 if (!no_useful_stderr)
|
|
159 no_useful_stderr = !PeekNamedPipe (errhand, 0, 0, 0, 0, 0);
|
|
160 /* we typically have no useful stdout/stderr under windows if we're
|
|
161 being invoked graphically. */
|
|
162 if (no_useful_stderr)
|
771
|
163 mswindows_output_console_string (ptr, len);
|
442
|
164 else
|
428
|
165 #endif
|
442
|
166 {
|
771
|
167 retry_fwrite (extptr, 1, extlen, stream);
|
442
|
168 #ifdef WIN32_NATIVE
|
|
169 /* Q122442 says that pipes are "treated as files, not as
|
|
170 devices", and that this is a feature. Before I found that
|
|
171 article, I thought it was a bug. Thanks MS, I feel much
|
|
172 better now. - kkm */
|
|
173 must_flush = 1;
|
|
174 #endif
|
|
175 if (must_flush)
|
|
176 fflush (stream);
|
|
177 }
|
428
|
178 }
|
|
179 else
|
771
|
180 /* The stream itself does conversion to external format */
|
|
181 Lstream_write (XLSTREAM (CONSOLE_TTY_DATA (con)->outstream), ptr, len);
|
442
|
182
|
|
183 if (output_is_std_handle)
|
428
|
184 {
|
|
185 if (termscript)
|
|
186 {
|
771
|
187 retry_fwrite (extptr, 1, extlen, termscript);
|
428
|
188 fflush (termscript);
|
|
189 }
|
771
|
190 stdout_needs_newline = (ptr[extlen - 1] != '\n');
|
428
|
191 }
|
|
192 }
|
|
193
|
442
|
194 /* #### The following function should be replaced a call to the
|
771
|
195 emacs_vsprintf_*() functions. This is the only way to ensure that
|
442
|
196 I18N3 works properly (many implementations of the *printf()
|
|
197 functions, including the ones included in glibc, do not implement
|
|
198 the %###$ argument-positioning syntax).
|
|
199
|
|
200 Note, however, that to do this, we'd have to
|
|
201
|
|
202 1) pre-allocate all the lstreams and do whatever else was necessary
|
|
203 to make sure that no allocation occurs, since these functions may be
|
|
204 called from fatal_error_signal().
|
|
205
|
|
206 2) (to be really correct) make a new lstream that outputs using
|
|
207 mswindows_output_console_string(). */
|
|
208
|
771
|
209 static void
|
|
210 std_handle_out_va (FILE *stream, const CIntbyte *fmt, va_list args,
|
|
211 int debug_output_as_well)
|
442
|
212 {
|
665
|
213 Intbyte kludge[8192];
|
771
|
214 Bytecount kludgelen;
|
|
215
|
|
216 if (initialized && !inhibit_non_essential_printing_operations)
|
|
217 fmt = GETTEXT (fmt);
|
|
218 vsprintf ((CIntbyte *) kludge, fmt, args);
|
|
219 kludgelen = qxestrlen (kludge);
|
442
|
220
|
771
|
221 write_string_to_stdio_stream (stream, 0, kludge, kludgelen, 1);
|
|
222 if (debug_output_as_well)
|
442
|
223 {
|
771
|
224 write_string_to_alternate_debugging_output (kludge, kludgelen);
|
|
225 #ifdef WIN32_NATIVE
|
|
226 write_string_to_mswindows_debugging_output (kludge, kludgelen);
|
|
227 #endif
|
442
|
228 }
|
|
229 }
|
|
230
|
771
|
231 /* Output portably to stderr or its equivalent (i.e. may be a console
|
|
232 window under MS Windows); do external-format conversion and call GETTEXT
|
|
233 on the format string. Automatically flush when done.
|
442
|
234
|
771
|
235 This is safe even when not initialized or when dying -- we don't do
|
|
236 conversion in such cases. */
|
|
237
|
|
238 void
|
|
239 stderr_out (const CIntbyte *fmt, ...)
|
442
|
240 {
|
|
241 va_list args;
|
|
242 va_start (args, fmt);
|
771
|
243 std_handle_out_va (stderr, fmt, args, 0);
|
442
|
244 va_end (args);
|
|
245 }
|
|
246
|
771
|
247 /* Output portably to stdout or its equivalent (i.e. may be a console
|
|
248 window under MS Windows). Works like stderr_out(). */
|
442
|
249
|
771
|
250 void
|
|
251 stdout_out (const CIntbyte *fmt, ...)
|
442
|
252 {
|
|
253 va_list args;
|
|
254 va_start (args, fmt);
|
771
|
255 std_handle_out_va (stdout, fmt, args, 0);
|
442
|
256 va_end (args);
|
771
|
257 }
|
|
258
|
|
259 /* Output portably to stderr or its equivalent (i.e. may be a console
|
|
260 window under MS Windows), as well as alternate-debugging-output and
|
|
261 (under MS Windows) the C debugging output, i.e. OutputDebugString().
|
|
262 Works like stderr_out(). */
|
|
263
|
|
264 void
|
|
265 debug_out (const CIntbyte *fmt, ...)
|
|
266 {
|
|
267 va_list args;
|
|
268 va_start (args, fmt);
|
|
269 std_handle_out_va (stderr, fmt, args, 1);
|
|
270 va_end (args);
|
442
|
271 }
|
|
272
|
|
273 DOESNT_RETURN
|
771
|
274 fatal (const CIntbyte *fmt, ...)
|
442
|
275 {
|
|
276 va_list args;
|
|
277 va_start (args, fmt);
|
|
278
|
771
|
279 stderr_out ("\nXEmacs: fatal error: ");
|
|
280 std_handle_out_va (stderr, fmt, args, 0);
|
442
|
281 stderr_out ("\n");
|
|
282
|
|
283 va_end (args);
|
|
284 exit (1);
|
|
285 }
|
|
286
|
428
|
287 /* Write a string to the output location specified in FUNCTION.
|
|
288 Arguments NONRELOC, RELOC, OFFSET, and LEN are as in
|
771
|
289 buffer_insert_string_1() in insdel.c.
|
|
290
|
|
291 FUNCTION is one of
|
|
292
|
|
293 -- an lstream
|
|
294 -- a buffer (insert at point and advance point)
|
|
295 -- a marker (insert at marker and advance marker)
|
|
296 -- a frame (append to echo area; clear echo area first if
|
|
297 `print-message-label' has changed since the last time)
|
|
298 -- t or nil (send to stdout)
|
|
299 -- a Lisp function of one argument (call to get data output)
|
|
300
|
|
301 Use Qexternal_debugging_output to get output to stderr.
|
|
302 */
|
428
|
303
|
|
304 static void
|
665
|
305 output_string (Lisp_Object function, const Intbyte *nonreloc,
|
428
|
306 Lisp_Object reloc, Bytecount offset, Bytecount len)
|
|
307 {
|
|
308 /* This function can GC */
|
|
309 Charcount cclen;
|
|
310 /* We change the value of nonreloc (fetching it from reloc as
|
|
311 necessary), but we don't want to pass this changed value on to
|
|
312 other functions that take both a nonreloc and a reloc, or things
|
|
313 may get confused and an assertion failure in
|
|
314 fixup_internal_substring() may get triggered. */
|
665
|
315 const Intbyte *newnonreloc = nonreloc;
|
428
|
316 struct gcpro gcpro1, gcpro2;
|
|
317
|
|
318 /* Emacs won't print while GCing, but an external debugger might */
|
771
|
319 #ifdef NO_PRINT_DURING_GC
|
428
|
320 if (gc_in_progress) return;
|
771
|
321 #endif
|
428
|
322
|
|
323 /* Perhaps not necessary but probably safer. */
|
|
324 GCPRO2 (function, reloc);
|
|
325
|
|
326 fixup_internal_substring (newnonreloc, reloc, offset, &len);
|
|
327
|
|
328 if (STRINGP (reloc))
|
771
|
329 {
|
793
|
330 cclen = string_offset_byte_to_char_len (reloc, offset, len);
|
771
|
331 newnonreloc = XSTRING_DATA (reloc);
|
|
332 }
|
|
333 else
|
|
334 cclen = bytecount_to_charcount (newnonreloc + offset, len);
|
428
|
335
|
|
336 if (LSTREAMP (function))
|
|
337 {
|
|
338 if (STRINGP (reloc))
|
|
339 {
|
|
340 /* Protect against Lstream_write() causing a GC and
|
|
341 relocating the string. For small strings, we do it by
|
|
342 alloc'ing the string and using a copy; for large strings,
|
|
343 we inhibit GC. */
|
|
344 if (len < 65536)
|
|
345 {
|
665
|
346 Intbyte *copied = alloca_array (Intbyte, len);
|
428
|
347 memcpy (copied, newnonreloc + offset, len);
|
|
348 Lstream_write (XLSTREAM (function), copied, len);
|
|
349 }
|
|
350 else
|
|
351 {
|
771
|
352 int speccount = begin_gc_forbidden ();
|
428
|
353 Lstream_write (XLSTREAM (function), newnonreloc + offset, len);
|
771
|
354 unbind_to (speccount);
|
428
|
355 }
|
|
356 }
|
|
357 else
|
|
358 Lstream_write (XLSTREAM (function), newnonreloc + offset, len);
|
|
359
|
|
360 if (print_unbuffered)
|
|
361 Lstream_flush (XLSTREAM (function));
|
|
362 }
|
|
363 else if (BUFFERP (function))
|
|
364 {
|
|
365 CHECK_LIVE_BUFFER (function);
|
|
366 buffer_insert_string (XBUFFER (function), nonreloc, reloc, offset, len);
|
|
367 }
|
|
368 else if (MARKERP (function))
|
|
369 {
|
|
370 /* marker_position() will err if marker doesn't point anywhere. */
|
665
|
371 Charbpos spoint = marker_position (function);
|
428
|
372
|
|
373 buffer_insert_string_1 (XMARKER (function)->buffer,
|
|
374 spoint, nonreloc, reloc, offset, len,
|
|
375 0);
|
|
376 Fset_marker (function, make_int (spoint + cclen),
|
|
377 Fmarker_buffer (function));
|
|
378 }
|
|
379 else if (FRAMEP (function))
|
|
380 {
|
|
381 /* This gets used by functions not invoking print_prepare(),
|
|
382 such as Fwrite_char, Fterpri, etc.. */
|
|
383 struct frame *f = XFRAME (function);
|
|
384 CHECK_LIVE_FRAME (function);
|
|
385
|
|
386 if (!EQ (Vprint_message_label, echo_area_status (f)))
|
|
387 clear_echo_area_from_print (f, Qnil, 1);
|
|
388 echo_area_append (f, nonreloc, reloc, offset, len, Vprint_message_label);
|
|
389 }
|
|
390 else if (EQ (function, Qt) || EQ (function, Qnil))
|
|
391 {
|
771
|
392 write_string_to_stdio_stream (stdout, 0, newnonreloc + offset, len,
|
|
393 print_unbuffered);
|
|
394 }
|
|
395 else if (EQ (function, Qexternal_debugging_output))
|
|
396 {
|
|
397 /* This is not strictly necessary, and somewhat of a hack, but it
|
|
398 avoids having each character passed separately to
|
|
399 `external-debugging-output'. #### Why do we pass each character
|
|
400 separately, anyway?
|
|
401 */
|
|
402 write_string_to_stdio_stream (stderr, 0, newnonreloc + offset, len,
|
|
403 print_unbuffered);
|
428
|
404 }
|
|
405 else
|
|
406 {
|
771
|
407 Charcount ccoff;
|
428
|
408 Charcount iii;
|
|
409
|
771
|
410 if (STRINGP (reloc))
|
793
|
411 ccoff = string_index_byte_to_char (reloc, offset);
|
771
|
412 else
|
|
413 ccoff = bytecount_to_charcount (newnonreloc, offset);
|
|
414
|
|
415 if (STRINGP (reloc))
|
428
|
416 {
|
771
|
417 for (iii = ccoff; iii < cclen + ccoff; iii++)
|
|
418 {
|
|
419 call1 (function, make_char (XSTRING_CHAR (reloc, iii)));
|
|
420 if (STRINGP (reloc))
|
|
421 newnonreloc = XSTRING_DATA (reloc);
|
|
422 }
|
|
423 }
|
|
424 else
|
|
425 {
|
|
426 for (iii = ccoff; iii < cclen + ccoff; iii++)
|
|
427 {
|
|
428 call1 (function,
|
|
429 make_char (charptr_emchar_n (newnonreloc, iii)));
|
|
430 }
|
428
|
431 }
|
|
432 }
|
|
433
|
|
434 UNGCPRO;
|
|
435 }
|
|
436
|
|
437 #define RESET_PRINT_GENSYM do { \
|
|
438 if (!CONSP (Vprint_gensym)) \
|
|
439 Vprint_gensym_alist = Qnil; \
|
|
440 } while (0)
|
|
441
|
|
442 static Lisp_Object
|
|
443 canonicalize_printcharfun (Lisp_Object printcharfun)
|
|
444 {
|
|
445 if (NILP (printcharfun))
|
|
446 printcharfun = Vstandard_output;
|
|
447
|
|
448 if (EQ (printcharfun, Qt) || NILP (printcharfun))
|
|
449 printcharfun = Fselected_frame (Qnil); /* print to minibuffer */
|
|
450
|
|
451 return printcharfun;
|
|
452 }
|
|
453
|
|
454 static Lisp_Object
|
|
455 print_prepare (Lisp_Object printcharfun, Lisp_Object *frame_kludge)
|
|
456 {
|
|
457 /* Emacs won't print while GCing, but an external debugger might */
|
771
|
458 #ifdef NO_PRINT_DURING_GC
|
428
|
459 if (gc_in_progress)
|
|
460 return Qnil;
|
771
|
461 #endif
|
|
462
|
428
|
463 RESET_PRINT_GENSYM;
|
|
464
|
|
465 printcharfun = canonicalize_printcharfun (printcharfun);
|
|
466
|
|
467 /* Here we could safely return the canonicalized PRINTCHARFUN.
|
|
468 However, if PRINTCHARFUN is a frame, printing of complex
|
|
469 structures becomes very expensive, because `append-message'
|
|
470 (called by echo_area_append) gets called as many times as
|
|
471 output_string() is called (and that's a *lot*). append-message
|
|
472 tries to keep top of the message-stack in sync with the contents
|
|
473 of " *Echo Area" buffer, consing a new string for each component
|
|
474 of the printed structure. For instance, if you print (a a),
|
|
475 append-message will cons up the following strings:
|
|
476
|
|
477 "("
|
|
478 "(a"
|
|
479 "(a "
|
|
480 "(a a"
|
|
481 "(a a)"
|
|
482
|
|
483 and will use only the last one. With larger objects, this turns
|
|
484 into an O(n^2) consing frenzy that locks up XEmacs in incessant
|
|
485 garbage collection.
|
|
486
|
|
487 We prevent this by creating a resizing_buffer stream and letting
|
|
488 the printer write into it. print_finish() will notice this
|
|
489 stream, and invoke echo_area_append() with the stream's buffer,
|
|
490 only once. */
|
|
491 if (FRAMEP (printcharfun))
|
|
492 {
|
|
493 CHECK_LIVE_FRAME (printcharfun);
|
|
494 *frame_kludge = printcharfun;
|
|
495 printcharfun = make_resizing_buffer_output_stream ();
|
|
496 }
|
|
497
|
|
498 return printcharfun;
|
|
499 }
|
|
500
|
|
501 static void
|
|
502 print_finish (Lisp_Object stream, Lisp_Object frame_kludge)
|
|
503 {
|
|
504 /* Emacs won't print while GCing, but an external debugger might */
|
771
|
505 #ifdef NO_PRINT_DURING_GC
|
428
|
506 if (gc_in_progress)
|
|
507 return;
|
771
|
508 #endif
|
|
509
|
428
|
510 RESET_PRINT_GENSYM;
|
|
511
|
|
512 /* See the comment in print_prepare(). */
|
|
513 if (FRAMEP (frame_kludge))
|
|
514 {
|
|
515 struct frame *f = XFRAME (frame_kludge);
|
|
516 Lstream *str = XLSTREAM (stream);
|
|
517 CHECK_LIVE_FRAME (frame_kludge);
|
|
518
|
|
519 Lstream_flush (str);
|
|
520 if (!EQ (Vprint_message_label, echo_area_status (f)))
|
|
521 clear_echo_area_from_print (f, Qnil, 1);
|
|
522 echo_area_append (f, resizing_buffer_stream_ptr (str),
|
|
523 Qnil, 0, Lstream_byte_count (str),
|
|
524 Vprint_message_label);
|
|
525 Lstream_delete (str);
|
|
526 }
|
|
527 }
|
|
528
|
|
529 /* Used for printing a single-byte character (*not* any Emchar). */
|
|
530 #define write_char_internal(string_of_length_1, stream) \
|
665
|
531 output_string (stream, (const Intbyte *) (string_of_length_1), \
|
428
|
532 Qnil, 0, 1)
|
|
533
|
771
|
534 /* Write internal-format data to STREAM. See output_string() for
|
|
535 interpretation of STREAM.
|
|
536
|
|
537 NOTE: Do not call this with the data of a Lisp_String, as
|
428
|
538 printcharfun might cause a GC, which might cause the string's data
|
|
539 to be relocated. To princ a Lisp string, use:
|
|
540
|
|
541 print_internal (string, printcharfun, 0);
|
|
542
|
|
543 Also note that STREAM should be the result of
|
|
544 canonicalize_printcharfun() (i.e. Qnil means stdout, not
|
|
545 Vstandard_output, etc.) */
|
|
546 void
|
665
|
547 write_string_1 (const Intbyte *str, Bytecount size, Lisp_Object stream)
|
428
|
548 {
|
|
549 /* This function can GC */
|
800
|
550 #ifdef ERROR_CHECK_TEXT
|
428
|
551 assert (size >= 0);
|
|
552 #endif
|
|
553 output_string (stream, str, Qnil, 0, size);
|
|
554 }
|
|
555
|
|
556 void
|
771
|
557 write_string (const Intbyte *str, Lisp_Object stream)
|
|
558 {
|
|
559 /* This function can GC */
|
|
560 write_string_1 (str, qxestrlen (str), stream);
|
|
561 }
|
|
562
|
|
563 void
|
|
564 write_c_string (const CIntbyte *str, Lisp_Object stream)
|
428
|
565 {
|
|
566 /* This function can GC */
|
665
|
567 write_string_1 ((const Intbyte *) str, strlen (str), stream);
|
428
|
568 }
|
|
569
|
793
|
570 void
|
|
571 write_eistring (const Eistring *ei, Lisp_Object stream)
|
|
572 {
|
|
573 write_string_1 (eidata (ei), eilen (ei), stream);
|
|
574 }
|
|
575
|
771
|
576 /* Write a printf-style string to STREAM; see output_string(). */
|
|
577
|
|
578 void
|
|
579 write_fmt_string (Lisp_Object stream, const CIntbyte *fmt, ...)
|
|
580 {
|
|
581 va_list va;
|
|
582 Intbyte *str;
|
|
583 Bytecount len;
|
|
584 int count;
|
|
585
|
|
586 va_start (va, fmt);
|
|
587 str = emacs_vsprintf_malloc (fmt, va, &len);
|
|
588 va_end (va);
|
|
589 count = record_unwind_protect_freeing (str);
|
|
590 write_string_1 (str, len, stream);
|
|
591 unbind_to (count);
|
|
592 }
|
|
593
|
|
594 /* Write a printf-style string to STREAM, where the arguments are
|
|
595 Lisp objects and not C strings or integers; see output_string().
|
|
596
|
|
597 #### It shouldn't be necessary to specify the number of arguments.
|
|
598 This would require some rewriting of the doprnt() functions, though. */
|
|
599
|
|
600 void
|
|
601 write_fmt_string_lisp (Lisp_Object stream, const CIntbyte *fmt, int nargs, ...)
|
|
602 {
|
|
603 Lisp_Object *args = alloca_array (Lisp_Object, nargs);
|
|
604 va_list va;
|
|
605 int i;
|
|
606 Intbyte *str;
|
|
607 Bytecount len;
|
|
608 int count;
|
|
609
|
|
610 va_start (va, nargs);
|
|
611 for (i = 0; i < nargs; i++)
|
|
612 args[i] = va_arg (va, Lisp_Object);
|
|
613 va_end (va);
|
|
614 str = emacs_vsprintf_malloc_lisp (fmt, Qnil, nargs, args, &len);
|
|
615 count = record_unwind_protect_freeing (str);
|
|
616 write_string_1 (str, len, stream);
|
|
617 unbind_to (count);
|
|
618 }
|
|
619
|
|
620 void
|
|
621 stderr_out_lisp (const CIntbyte *fmt, int nargs, ...)
|
|
622 {
|
|
623 Lisp_Object *args = alloca_array (Lisp_Object, nargs);
|
|
624 va_list va;
|
|
625 int i;
|
|
626 Intbyte *str;
|
|
627 Bytecount len;
|
|
628 int count;
|
|
629
|
|
630 va_start (va, nargs);
|
|
631 for (i = 0; i < nargs; i++)
|
|
632 args[i] = va_arg (va, Lisp_Object);
|
|
633 va_end (va);
|
|
634 str = emacs_vsprintf_malloc_lisp (fmt, Qnil, nargs, args, &len);
|
|
635 count = record_unwind_protect_freeing (str);
|
|
636 write_string_1 (str, len, Qexternal_debugging_output);
|
|
637 unbind_to (count);
|
|
638 }
|
|
639
|
428
|
640
|
|
641 DEFUN ("write-char", Fwrite_char, 1, 2, 0, /*
|
444
|
642 Output character CHARACTER to stream STREAM.
|
428
|
643 STREAM defaults to the value of `standard-output' (which see).
|
|
644 */
|
444
|
645 (character, stream))
|
428
|
646 {
|
|
647 /* This function can GC */
|
665
|
648 Intbyte str[MAX_EMCHAR_LEN];
|
428
|
649 Bytecount len;
|
|
650
|
444
|
651 CHECK_CHAR_COERCE_INT (character);
|
|
652 len = set_charptr_emchar (str, XCHAR (character));
|
428
|
653 output_string (canonicalize_printcharfun (stream), str, Qnil, 0, len);
|
444
|
654 return character;
|
428
|
655 }
|
|
656
|
|
657 void
|
|
658 temp_output_buffer_setup (Lisp_Object bufname)
|
|
659 {
|
|
660 /* This function can GC */
|
|
661 struct buffer *old = current_buffer;
|
|
662 Lisp_Object buf;
|
|
663
|
|
664 #ifdef I18N3
|
|
665 /* #### This function should accept a Lisp_Object instead of a char *,
|
|
666 so that proper translation on the buffer name can occur. */
|
|
667 #endif
|
|
668
|
|
669 Fset_buffer (Fget_buffer_create (bufname));
|
|
670
|
|
671 current_buffer->read_only = Qnil;
|
|
672 Ferase_buffer (Qnil);
|
|
673
|
793
|
674 buf = wrap_buffer (current_buffer);
|
428
|
675 specbind (Qstandard_output, buf);
|
|
676
|
|
677 set_buffer_internal (old);
|
|
678 }
|
|
679
|
|
680 Lisp_Object
|
|
681 internal_with_output_to_temp_buffer (Lisp_Object bufname,
|
|
682 Lisp_Object (*function) (Lisp_Object arg),
|
|
683 Lisp_Object arg,
|
|
684 Lisp_Object same_frame)
|
|
685 {
|
|
686 int speccount = specpdl_depth ();
|
|
687 struct gcpro gcpro1, gcpro2, gcpro3;
|
|
688 Lisp_Object buf = Qnil;
|
|
689
|
|
690 GCPRO3 (buf, arg, same_frame);
|
|
691
|
|
692 temp_output_buffer_setup (bufname);
|
|
693 buf = Vstandard_output;
|
|
694
|
|
695 arg = (*function) (arg);
|
|
696
|
|
697 temp_output_buffer_show (buf, same_frame);
|
|
698 UNGCPRO;
|
|
699
|
771
|
700 return unbind_to_1 (speccount, arg);
|
428
|
701 }
|
|
702
|
|
703 DEFUN ("with-output-to-temp-buffer", Fwith_output_to_temp_buffer, 1, UNEVALLED, 0, /*
|
|
704 Bind `standard-output' to buffer BUFNAME, eval BODY, then show that buffer.
|
|
705 The buffer is cleared out initially, and marked as unmodified when done.
|
|
706 All output done by BODY is inserted in that buffer by default.
|
|
707 The buffer is displayed in another window, but not selected.
|
|
708 The value of the last form in BODY is returned.
|
|
709 If BODY does not finish normally, the buffer BUFNAME is not displayed.
|
|
710
|
|
711 If variable `temp-buffer-show-function' is non-nil, call it at the end
|
|
712 to get the buffer displayed. It gets one argument, the buffer to display.
|
|
713 */
|
|
714 (args))
|
|
715 {
|
|
716 /* This function can GC */
|
|
717 Lisp_Object name = Qnil;
|
|
718 int speccount = specpdl_depth ();
|
|
719 struct gcpro gcpro1, gcpro2;
|
|
720 Lisp_Object val = Qnil;
|
|
721
|
|
722 #ifdef I18N3
|
|
723 /* #### should set the buffer to be translating. See print_internal(). */
|
|
724 #endif
|
|
725
|
|
726 GCPRO2 (name, val);
|
|
727 name = Feval (XCAR (args));
|
|
728
|
|
729 CHECK_STRING (name);
|
|
730
|
|
731 temp_output_buffer_setup (name);
|
|
732 UNGCPRO;
|
|
733
|
|
734 val = Fprogn (XCDR (args));
|
|
735
|
|
736 temp_output_buffer_show (Vstandard_output, Qnil);
|
|
737
|
771
|
738 return unbind_to_1 (speccount, val);
|
428
|
739 }
|
|
740
|
|
741 DEFUN ("terpri", Fterpri, 0, 1, 0, /*
|
|
742 Output a newline to STREAM.
|
|
743 If STREAM is omitted or nil, the value of `standard-output' is used.
|
|
744 */
|
|
745 (stream))
|
|
746 {
|
|
747 /* This function can GC */
|
|
748 write_char_internal ("\n", canonicalize_printcharfun (stream));
|
|
749 return Qt;
|
|
750 }
|
|
751
|
|
752 DEFUN ("prin1", Fprin1, 1, 2, 0, /*
|
|
753 Output the printed representation of OBJECT, any Lisp object.
|
|
754 Quoting characters are printed when needed to make output that `read'
|
|
755 can handle, whenever this is possible.
|
|
756 Output stream is STREAM, or value of `standard-output' (which see).
|
|
757 */
|
|
758 (object, stream))
|
|
759 {
|
|
760 /* This function can GC */
|
|
761 Lisp_Object frame = Qnil;
|
|
762 struct gcpro gcpro1, gcpro2;
|
|
763 GCPRO2 (object, stream);
|
|
764
|
|
765 stream = print_prepare (stream, &frame);
|
|
766 print_internal (object, stream, 1);
|
|
767 print_finish (stream, frame);
|
|
768
|
|
769 UNGCPRO;
|
|
770 return object;
|
|
771 }
|
|
772
|
|
773 DEFUN ("prin1-to-string", Fprin1_to_string, 1, 2, 0, /*
|
|
774 Return a string containing the printed representation of OBJECT,
|
|
775 any Lisp object. Quoting characters are used when needed to make output
|
|
776 that `read' can handle, whenever this is possible, unless the optional
|
|
777 second argument NOESCAPE is non-nil.
|
|
778 */
|
|
779 (object, noescape))
|
|
780 {
|
|
781 /* This function can GC */
|
|
782 Lisp_Object result = Qnil;
|
|
783 Lisp_Object stream = make_resizing_buffer_output_stream ();
|
|
784 Lstream *str = XLSTREAM (stream);
|
|
785 /* gcpro OBJECT in case a caller forgot to do so */
|
|
786 struct gcpro gcpro1, gcpro2, gcpro3;
|
|
787 GCPRO3 (object, stream, result);
|
|
788
|
|
789 RESET_PRINT_GENSYM;
|
|
790 print_internal (object, stream, NILP (noescape));
|
|
791 RESET_PRINT_GENSYM;
|
|
792 Lstream_flush (str);
|
|
793 UNGCPRO;
|
|
794 result = make_string (resizing_buffer_stream_ptr (str),
|
|
795 Lstream_byte_count (str));
|
|
796 Lstream_delete (str);
|
|
797 return result;
|
|
798 }
|
|
799
|
|
800 DEFUN ("princ", Fprinc, 1, 2, 0, /*
|
|
801 Output the printed representation of OBJECT, any Lisp object.
|
|
802 No quoting characters are used; no delimiters are printed around
|
|
803 the contents of strings.
|
444
|
804 Output stream is STREAM, or value of `standard-output' (which see).
|
428
|
805 */
|
|
806 (object, stream))
|
|
807 {
|
|
808 /* This function can GC */
|
|
809 Lisp_Object frame = Qnil;
|
|
810 struct gcpro gcpro1, gcpro2;
|
|
811
|
|
812 GCPRO2 (object, stream);
|
|
813 stream = print_prepare (stream, &frame);
|
|
814 print_internal (object, stream, 0);
|
|
815 print_finish (stream, frame);
|
|
816 UNGCPRO;
|
|
817 return object;
|
|
818 }
|
|
819
|
|
820 DEFUN ("print", Fprint, 1, 2, 0, /*
|
|
821 Output the printed representation of OBJECT, with newlines around it.
|
|
822 Quoting characters are printed when needed to make output that `read'
|
|
823 can handle, whenever this is possible.
|
|
824 Output stream is STREAM, or value of `standard-output' (which see).
|
|
825 */
|
|
826 (object, stream))
|
|
827 {
|
|
828 /* This function can GC */
|
|
829 Lisp_Object frame = Qnil;
|
|
830 struct gcpro gcpro1, gcpro2;
|
|
831
|
|
832 GCPRO2 (object, stream);
|
|
833 stream = print_prepare (stream, &frame);
|
|
834 write_char_internal ("\n", stream);
|
|
835 print_internal (object, stream, 1);
|
|
836 write_char_internal ("\n", stream);
|
|
837 print_finish (stream, frame);
|
|
838 UNGCPRO;
|
|
839 return object;
|
|
840 }
|
|
841
|
|
842 /* Print an error message for the error DATA to STREAM. This is a
|
|
843 complete implementation of `display-error', which used to be in
|
|
844 Lisp (see prim/cmdloop.el). It was ported to C so it can be used
|
|
845 efficiently by Ferror_message_string. Fdisplay_error and
|
|
846 Ferror_message_string are trivial wrappers around this function.
|
|
847
|
|
848 STREAM should be the result of canonicalize_printcharfun(). */
|
|
849 static void
|
|
850 print_error_message (Lisp_Object error_object, Lisp_Object stream)
|
|
851 {
|
|
852 /* This function can GC */
|
|
853 Lisp_Object type = Fcar_safe (error_object);
|
|
854 Lisp_Object method = Qnil;
|
|
855 Lisp_Object tail;
|
|
856
|
|
857 /* No need to GCPRO anything under the assumption that ERROR_OBJECT
|
|
858 is GCPRO'd. */
|
|
859
|
|
860 if (! (CONSP (error_object) && SYMBOLP (type)
|
|
861 && CONSP (Fget (type, Qerror_conditions, Qnil))))
|
|
862 goto error_throw;
|
|
863
|
|
864 tail = XCDR (error_object);
|
|
865 while (!NILP (tail))
|
|
866 {
|
|
867 if (CONSP (tail))
|
|
868 tail = XCDR (tail);
|
|
869 else
|
|
870 goto error_throw;
|
|
871 }
|
|
872 tail = Fget (type, Qerror_conditions, Qnil);
|
|
873 while (!NILP (tail))
|
|
874 {
|
|
875 if (!(CONSP (tail) && SYMBOLP (XCAR (tail))))
|
|
876 goto error_throw;
|
|
877 else if (!NILP (Fget (XCAR (tail), Qdisplay_error, Qnil)))
|
|
878 {
|
|
879 method = Fget (XCAR (tail), Qdisplay_error, Qnil);
|
|
880 goto error_throw;
|
|
881 }
|
|
882 else
|
|
883 tail = XCDR (tail);
|
|
884 }
|
|
885 /* Default method */
|
|
886 {
|
|
887 int first = 1;
|
|
888 int speccount = specpdl_depth ();
|
438
|
889 Lisp_Object frame = Qnil;
|
|
890 struct gcpro gcpro1;
|
|
891 GCPRO1 (stream);
|
428
|
892
|
|
893 specbind (Qprint_message_label, Qerror);
|
438
|
894 stream = print_prepare (stream, &frame);
|
|
895
|
428
|
896 tail = Fcdr (error_object);
|
|
897 if (EQ (type, Qerror))
|
|
898 {
|
|
899 print_internal (Fcar (tail), stream, 0);
|
|
900 tail = Fcdr (tail);
|
|
901 }
|
|
902 else
|
|
903 {
|
|
904 Lisp_Object errmsg = Fget (type, Qerror_message, Qnil);
|
|
905 if (NILP (errmsg))
|
|
906 print_internal (type, stream, 0);
|
|
907 else
|
|
908 print_internal (LISP_GETTEXT (errmsg), stream, 0);
|
|
909 }
|
|
910 while (!NILP (tail))
|
|
911 {
|
|
912 write_c_string (first ? ": " : ", ", stream);
|
563
|
913 /* Most errors have an explanatory string as their first argument,
|
|
914 and it looks better not to put the quotes around it. */
|
|
915 print_internal (Fcar (tail), stream,
|
|
916 !(first && STRINGP (Fcar (tail))) ||
|
|
917 !NILP (Fget (type, Qerror_lacks_explanatory_string,
|
|
918 Qnil)));
|
428
|
919 tail = Fcdr (tail);
|
|
920 first = 0;
|
|
921 }
|
438
|
922 print_finish (stream, frame);
|
|
923 UNGCPRO;
|
771
|
924 unbind_to (speccount);
|
428
|
925 return;
|
|
926 /* not reached */
|
|
927 }
|
|
928
|
|
929 error_throw:
|
|
930 if (NILP (method))
|
|
931 {
|
|
932 write_c_string (GETTEXT ("Peculiar error "), stream);
|
|
933 print_internal (error_object, stream, 1);
|
|
934 return;
|
|
935 }
|
|
936 else
|
|
937 {
|
|
938 call2 (method, error_object, stream);
|
|
939 }
|
|
940 }
|
|
941
|
|
942 DEFUN ("error-message-string", Ferror_message_string, 1, 1, 0, /*
|
|
943 Convert ERROR-OBJECT to an error message, and return it.
|
|
944
|
|
945 The format of ERROR-OBJECT should be (ERROR-SYMBOL . DATA). The
|
|
946 message is equivalent to the one that would be issued by
|
|
947 `display-error' with the same argument.
|
|
948 */
|
|
949 (error_object))
|
|
950 {
|
|
951 /* This function can GC */
|
|
952 Lisp_Object result = Qnil;
|
|
953 Lisp_Object stream = make_resizing_buffer_output_stream ();
|
|
954 struct gcpro gcpro1;
|
|
955 GCPRO1 (stream);
|
|
956
|
|
957 print_error_message (error_object, stream);
|
|
958 Lstream_flush (XLSTREAM (stream));
|
|
959 result = make_string (resizing_buffer_stream_ptr (XLSTREAM (stream)),
|
|
960 Lstream_byte_count (XLSTREAM (stream)));
|
|
961 Lstream_delete (XLSTREAM (stream));
|
|
962
|
|
963 UNGCPRO;
|
|
964 return result;
|
|
965 }
|
|
966
|
|
967 DEFUN ("display-error", Fdisplay_error, 2, 2, 0, /*
|
|
968 Display ERROR-OBJECT on STREAM in a user-friendly way.
|
|
969 */
|
|
970 (error_object, stream))
|
|
971 {
|
|
972 /* This function can GC */
|
|
973 print_error_message (error_object, canonicalize_printcharfun (stream));
|
|
974 return Qnil;
|
|
975 }
|
|
976
|
|
977
|
|
978 #ifdef LISP_FLOAT_TYPE
|
|
979
|
|
980 Lisp_Object Vfloat_output_format;
|
|
981
|
|
982 /*
|
|
983 * This buffer should be at least as large as the max string size of the
|
440
|
984 * largest float, printed in the biggest notation. This is undoubtedly
|
428
|
985 * 20d float_output_format, with the negative of the C-constant "HUGE"
|
|
986 * from <math.h>.
|
|
987 *
|
|
988 * On the vax the worst case is -1e38 in 20d format which takes 61 bytes.
|
|
989 *
|
|
990 * I assume that IEEE-754 format numbers can take 329 bytes for the worst
|
|
991 * case of -1e307 in 20d float_output_format. What is one to do (short of
|
|
992 * re-writing _doprnt to be more sane)?
|
|
993 * -wsr
|
|
994 */
|
|
995 void
|
|
996 float_to_string (char *buf, double data)
|
|
997 {
|
665
|
998 Intbyte *cp, c;
|
428
|
999 int width;
|
|
1000
|
|
1001 if (NILP (Vfloat_output_format)
|
|
1002 || !STRINGP (Vfloat_output_format))
|
|
1003 lose:
|
|
1004 sprintf (buf, "%.16g", data);
|
|
1005 else /* oink oink */
|
|
1006 {
|
|
1007 /* Check that the spec we have is fully valid.
|
|
1008 This means not only valid for printf,
|
|
1009 but meant for floats, and reasonable. */
|
|
1010 cp = XSTRING_DATA (Vfloat_output_format);
|
|
1011
|
|
1012 if (cp[0] != '%')
|
|
1013 goto lose;
|
|
1014 if (cp[1] != '.')
|
|
1015 goto lose;
|
|
1016
|
|
1017 cp += 2;
|
|
1018 for (width = 0; (c = *cp, isdigit (c)); cp++)
|
|
1019 {
|
|
1020 width *= 10;
|
|
1021 width += c - '0';
|
|
1022 }
|
|
1023
|
|
1024 if (*cp != 'e' && *cp != 'f' && *cp != 'g' && *cp != 'E' && *cp != 'G')
|
|
1025 goto lose;
|
|
1026
|
|
1027 if (width < (int) (*cp != 'e' && *cp != 'E') || width > DBL_DIG)
|
|
1028 goto lose;
|
|
1029
|
|
1030 if (cp[1] != 0)
|
|
1031 goto lose;
|
|
1032
|
|
1033 sprintf (buf, (char *) XSTRING_DATA (Vfloat_output_format),
|
|
1034 data);
|
|
1035 }
|
|
1036
|
|
1037 /* added by jwz: don't allow "1.0" to print as "1"; that destroys
|
|
1038 the read-equivalence of lisp objects. (* x 1) and (* x 1.0) do
|
|
1039 not do the same thing, so it's important that the printed
|
|
1040 representation of that form not be corrupted by the printer.
|
|
1041 */
|
|
1042 {
|
665
|
1043 Intbyte *s = (Intbyte *) buf; /* don't use signed chars here!
|
428
|
1044 isdigit() can't hack them! */
|
|
1045 if (*s == '-') s++;
|
|
1046 for (; *s; s++)
|
|
1047 /* if there's a non-digit, then there is a decimal point, or
|
|
1048 it's in exponential notation, both of which are ok. */
|
|
1049 if (!isdigit (*s))
|
|
1050 goto DONE_LABEL;
|
|
1051 /* otherwise, we need to hack it. */
|
|
1052 *s++ = '.';
|
|
1053 *s++ = '0';
|
|
1054 *s = 0;
|
|
1055 }
|
|
1056 DONE_LABEL:
|
|
1057
|
|
1058 /* Some machines print "0.4" as ".4". I don't like that. */
|
|
1059 if (buf [0] == '.' || (buf [0] == '-' && buf [1] == '.'))
|
|
1060 {
|
|
1061 int i;
|
|
1062 for (i = strlen (buf) + 1; i >= 0; i--)
|
|
1063 buf [i+1] = buf [i];
|
|
1064 buf [(buf [0] == '-' ? 1 : 0)] = '0';
|
|
1065 }
|
|
1066 }
|
|
1067 #endif /* LISP_FLOAT_TYPE */
|
|
1068
|
577
|
1069 #define ONE_DIGIT(figure) *p++ = n / (figure) + '0'
|
|
1070 #define ONE_DIGIT_ADVANCE(figure) (ONE_DIGIT (figure), n %= (figure))
|
|
1071
|
|
1072 #define DIGITS_1(figure) ONE_DIGIT (figure)
|
|
1073 #define DIGITS_2(figure) ONE_DIGIT_ADVANCE (figure); DIGITS_1 ((figure) / 10)
|
|
1074 #define DIGITS_3(figure) ONE_DIGIT_ADVANCE (figure); DIGITS_2 ((figure) / 10)
|
|
1075 #define DIGITS_4(figure) ONE_DIGIT_ADVANCE (figure); DIGITS_3 ((figure) / 10)
|
|
1076 #define DIGITS_5(figure) ONE_DIGIT_ADVANCE (figure); DIGITS_4 ((figure) / 10)
|
|
1077 #define DIGITS_6(figure) ONE_DIGIT_ADVANCE (figure); DIGITS_5 ((figure) / 10)
|
|
1078 #define DIGITS_7(figure) ONE_DIGIT_ADVANCE (figure); DIGITS_6 ((figure) / 10)
|
|
1079 #define DIGITS_8(figure) ONE_DIGIT_ADVANCE (figure); DIGITS_7 ((figure) / 10)
|
|
1080 #define DIGITS_9(figure) ONE_DIGIT_ADVANCE (figure); DIGITS_8 ((figure) / 10)
|
|
1081 #define DIGITS_10(figure) ONE_DIGIT_ADVANCE (figure); DIGITS_9 ((figure) / 10)
|
|
1082
|
|
1083 /* DIGITS_<11-20> are only used on machines with 64-bit longs. */
|
428
|
1084
|
577
|
1085 #define DIGITS_11(figure) ONE_DIGIT_ADVANCE (figure); DIGITS_10 ((figure) / 10)
|
|
1086 #define DIGITS_12(figure) ONE_DIGIT_ADVANCE (figure); DIGITS_11 ((figure) / 10)
|
|
1087 #define DIGITS_13(figure) ONE_DIGIT_ADVANCE (figure); DIGITS_12 ((figure) / 10)
|
|
1088 #define DIGITS_14(figure) ONE_DIGIT_ADVANCE (figure); DIGITS_13 ((figure) / 10)
|
|
1089 #define DIGITS_15(figure) ONE_DIGIT_ADVANCE (figure); DIGITS_14 ((figure) / 10)
|
|
1090 #define DIGITS_16(figure) ONE_DIGIT_ADVANCE (figure); DIGITS_15 ((figure) / 10)
|
|
1091 #define DIGITS_17(figure) ONE_DIGIT_ADVANCE (figure); DIGITS_16 ((figure) / 10)
|
|
1092 #define DIGITS_18(figure) ONE_DIGIT_ADVANCE (figure); DIGITS_17 ((figure) / 10)
|
|
1093 #define DIGITS_19(figure) ONE_DIGIT_ADVANCE (figure); DIGITS_18 ((figure) / 10)
|
|
1094
|
|
1095 /* Print NUMBER to BUFFER in base 10. This is completely equivalent
|
|
1096 to `sprintf(buffer, "%ld", number)', only much faster.
|
|
1097
|
|
1098 The speedup may make a difference in programs that frequently
|
|
1099 convert numbers to strings. Some implementations of sprintf,
|
|
1100 particularly the one in GNU libc, have been known to be extremely
|
|
1101 slow compared to this function.
|
|
1102
|
|
1103 BUFFER should accept as many bytes as you expect the number to take
|
|
1104 up. On machines with 64-bit longs the maximum needed size is 24
|
|
1105 bytes. That includes the worst-case digits, the optional `-' sign,
|
|
1106 and the trailing \0. */
|
|
1107
|
|
1108 void
|
428
|
1109 long_to_string (char *buffer, long number)
|
|
1110 {
|
577
|
1111 char *p = buffer;
|
|
1112 long n = number;
|
|
1113
|
428
|
1114 #if (SIZEOF_LONG != 4) && (SIZEOF_LONG != 8)
|
577
|
1115 /* We are running in a strange or misconfigured environment. Let
|
|
1116 sprintf cope with it. */
|
|
1117 sprintf (buffer, "%ld", n);
|
|
1118 #else /* (SIZEOF_LONG == 4) || (SIZEOF_LONG == 8) */
|
428
|
1119
|
577
|
1120 if (n < 0)
|
428
|
1121 {
|
|
1122 *p++ = '-';
|
577
|
1123 n = -n;
|
428
|
1124 }
|
|
1125
|
577
|
1126 if (n < 10) { DIGITS_1 (1); }
|
|
1127 else if (n < 100) { DIGITS_2 (10); }
|
|
1128 else if (n < 1000) { DIGITS_3 (100); }
|
|
1129 else if (n < 10000) { DIGITS_4 (1000); }
|
|
1130 else if (n < 100000) { DIGITS_5 (10000); }
|
|
1131 else if (n < 1000000) { DIGITS_6 (100000); }
|
|
1132 else if (n < 10000000) { DIGITS_7 (1000000); }
|
|
1133 else if (n < 100000000) { DIGITS_8 (10000000); }
|
|
1134 else if (n < 1000000000) { DIGITS_9 (100000000); }
|
|
1135 #if SIZEOF_LONG == 4
|
|
1136 /* ``if (1)'' serves only to preserve editor indentation. */
|
|
1137 else if (1) { DIGITS_10 (1000000000); }
|
|
1138 #else /* SIZEOF_LONG != 4 */
|
|
1139 else if (n < 10000000000L) { DIGITS_10 (1000000000L); }
|
|
1140 else if (n < 100000000000L) { DIGITS_11 (10000000000L); }
|
|
1141 else if (n < 1000000000000L) { DIGITS_12 (100000000000L); }
|
|
1142 else if (n < 10000000000000L) { DIGITS_13 (1000000000000L); }
|
|
1143 else if (n < 100000000000000L) { DIGITS_14 (10000000000000L); }
|
|
1144 else if (n < 1000000000000000L) { DIGITS_15 (100000000000000L); }
|
|
1145 else if (n < 10000000000000000L) { DIGITS_16 (1000000000000000L); }
|
|
1146 else if (n < 100000000000000000L) { DIGITS_17 (10000000000000000L); }
|
|
1147 else if (n < 1000000000000000000L) { DIGITS_18 (100000000000000000L); }
|
|
1148 else { DIGITS_19 (1000000000000000000L); }
|
|
1149 #endif /* SIZEOF_LONG != 4 */
|
|
1150
|
428
|
1151 *p = '\0';
|
|
1152 #endif /* (SIZEOF_LONG == 4) || (SIZEOF_LONG == 8) */
|
|
1153 }
|
577
|
1154
|
|
1155 #undef ONE_DIGIT
|
|
1156 #undef ONE_DIGIT_ADVANCE
|
|
1157
|
|
1158 #undef DIGITS_1
|
|
1159 #undef DIGITS_2
|
|
1160 #undef DIGITS_3
|
|
1161 #undef DIGITS_4
|
|
1162 #undef DIGITS_5
|
|
1163 #undef DIGITS_6
|
|
1164 #undef DIGITS_7
|
|
1165 #undef DIGITS_8
|
|
1166 #undef DIGITS_9
|
|
1167 #undef DIGITS_10
|
|
1168 #undef DIGITS_11
|
|
1169 #undef DIGITS_12
|
|
1170 #undef DIGITS_13
|
|
1171 #undef DIGITS_14
|
|
1172 #undef DIGITS_15
|
|
1173 #undef DIGITS_16
|
|
1174 #undef DIGITS_17
|
|
1175 #undef DIGITS_18
|
|
1176 #undef DIGITS_19
|
428
|
1177
|
|
1178 static void
|
442
|
1179 print_vector_internal (const char *start, const char *end,
|
428
|
1180 Lisp_Object obj,
|
|
1181 Lisp_Object printcharfun, int escapeflag)
|
|
1182 {
|
|
1183 /* This function can GC */
|
|
1184 int i;
|
|
1185 int len = XVECTOR_LENGTH (obj);
|
|
1186 int last = len;
|
|
1187 struct gcpro gcpro1, gcpro2;
|
|
1188 GCPRO2 (obj, printcharfun);
|
|
1189
|
|
1190 if (INTP (Vprint_length))
|
|
1191 {
|
|
1192 int max = XINT (Vprint_length);
|
|
1193 if (max < len) last = max;
|
|
1194 }
|
|
1195
|
|
1196 write_c_string (start, printcharfun);
|
|
1197 for (i = 0; i < last; i++)
|
|
1198 {
|
|
1199 Lisp_Object elt = XVECTOR_DATA (obj)[i];
|
|
1200 if (i != 0) write_char_internal (" ", printcharfun);
|
|
1201 print_internal (elt, printcharfun, escapeflag);
|
|
1202 }
|
|
1203 UNGCPRO;
|
|
1204 if (last != len)
|
|
1205 write_c_string (" ...", printcharfun);
|
|
1206 write_c_string (end, printcharfun);
|
|
1207 }
|
|
1208
|
|
1209 void
|
|
1210 print_cons (Lisp_Object obj, Lisp_Object printcharfun, int escapeflag)
|
|
1211 {
|
|
1212 /* This function can GC */
|
|
1213 struct gcpro gcpro1, gcpro2;
|
|
1214
|
|
1215 /* If print_readably is on, print (quote -foo-) as '-foo-
|
|
1216 (Yeah, this should really be what print-pretty does, but we
|
|
1217 don't have the rest of a pretty printer, and this actually
|
|
1218 has non-negligible impact on size/speed of .elc files.)
|
|
1219 */
|
|
1220 if (print_readably &&
|
|
1221 EQ (XCAR (obj), Qquote) &&
|
|
1222 CONSP (XCDR (obj)) &&
|
|
1223 NILP (XCDR (XCDR (obj))))
|
|
1224 {
|
|
1225 obj = XCAR (XCDR (obj));
|
|
1226 GCPRO2 (obj, printcharfun);
|
|
1227 write_char_internal ("\'", printcharfun);
|
|
1228 UNGCPRO;
|
|
1229 print_internal (obj, printcharfun, escapeflag);
|
|
1230 return;
|
|
1231 }
|
|
1232
|
|
1233 GCPRO2 (obj, printcharfun);
|
|
1234 write_char_internal ("(", printcharfun);
|
|
1235
|
|
1236 {
|
|
1237 int len;
|
|
1238 int max = INTP (Vprint_length) ? XINT (Vprint_length) : INT_MAX;
|
|
1239 Lisp_Object tortoise;
|
|
1240 /* Use tortoise/hare to make sure circular lists don't infloop */
|
|
1241
|
|
1242 for (tortoise = obj, len = 0;
|
|
1243 CONSP (obj);
|
|
1244 obj = XCDR (obj), len++)
|
|
1245 {
|
|
1246 if (len > 0)
|
|
1247 write_char_internal (" ", printcharfun);
|
|
1248 if (EQ (obj, tortoise) && len > 0)
|
|
1249 {
|
|
1250 if (print_readably)
|
563
|
1251 printing_unreadable_object ("circular list");
|
428
|
1252 else
|
|
1253 write_c_string ("... <circular list>", printcharfun);
|
|
1254 break;
|
|
1255 }
|
|
1256 if (len & 1)
|
|
1257 tortoise = XCDR (tortoise);
|
|
1258 if (len > max)
|
|
1259 {
|
|
1260 write_c_string ("...", printcharfun);
|
|
1261 break;
|
|
1262 }
|
|
1263 print_internal (XCAR (obj), printcharfun, escapeflag);
|
|
1264 }
|
|
1265 }
|
|
1266 if (!LISTP (obj))
|
|
1267 {
|
|
1268 write_c_string (" . ", printcharfun);
|
|
1269 print_internal (obj, printcharfun, escapeflag);
|
|
1270 }
|
|
1271 UNGCPRO;
|
|
1272
|
|
1273 write_char_internal (")", printcharfun);
|
|
1274 return;
|
|
1275 }
|
|
1276
|
|
1277 void
|
|
1278 print_vector (Lisp_Object obj, Lisp_Object printcharfun, int escapeflag)
|
|
1279 {
|
|
1280 print_vector_internal ("[", "]", obj, printcharfun, escapeflag);
|
|
1281 }
|
|
1282
|
|
1283 void
|
|
1284 print_string (Lisp_Object obj, Lisp_Object printcharfun, int escapeflag)
|
|
1285 {
|
|
1286 /* We distinguish between Bytecounts and Charcounts, to make
|
|
1287 Vprint_string_length work correctly under Mule. */
|
793
|
1288 Charcount size = XSTRING_CHAR_LENGTH (obj);
|
428
|
1289 Charcount max = size;
|
793
|
1290 Bytecount bcmax = XSTRING_LENGTH (obj);
|
428
|
1291 struct gcpro gcpro1, gcpro2;
|
|
1292 GCPRO2 (obj, printcharfun);
|
|
1293
|
|
1294 if (INTP (Vprint_string_length) &&
|
|
1295 XINT (Vprint_string_length) < max)
|
|
1296 {
|
|
1297 max = XINT (Vprint_string_length);
|
793
|
1298 bcmax = string_index_char_to_byte (obj, max);
|
428
|
1299 }
|
|
1300 if (max < 0)
|
|
1301 {
|
|
1302 max = 0;
|
|
1303 bcmax = 0;
|
|
1304 }
|
|
1305
|
|
1306 if (!escapeflag)
|
|
1307 {
|
|
1308 /* This deals with GC-relocation and Mule. */
|
|
1309 output_string (printcharfun, 0, obj, 0, bcmax);
|
|
1310 if (max < size)
|
|
1311 write_c_string (" ...", printcharfun);
|
|
1312 }
|
|
1313 else
|
|
1314 {
|
|
1315 Bytecount i, last = 0;
|
|
1316
|
|
1317 write_char_internal ("\"", printcharfun);
|
|
1318 for (i = 0; i < bcmax; i++)
|
|
1319 {
|
793
|
1320 Intbyte ch = XSTRING_BYTE (obj, i);
|
428
|
1321 if (ch == '\"' || ch == '\\'
|
|
1322 || (ch == '\n' && print_escape_newlines))
|
|
1323 {
|
|
1324 if (i > last)
|
|
1325 {
|
|
1326 output_string (printcharfun, 0, obj, last,
|
|
1327 i - last);
|
|
1328 }
|
|
1329 if (ch == '\n')
|
|
1330 {
|
|
1331 write_c_string ("\\n", printcharfun);
|
|
1332 }
|
|
1333 else
|
|
1334 {
|
|
1335 write_char_internal ("\\", printcharfun);
|
|
1336 /* This is correct for Mule because the
|
|
1337 character is either \ or " */
|
793
|
1338 write_char_internal (XSTRING_DATA (obj) + i, printcharfun);
|
428
|
1339 }
|
|
1340 last = i + 1;
|
|
1341 }
|
|
1342 }
|
|
1343 if (bcmax > last)
|
|
1344 {
|
|
1345 output_string (printcharfun, 0, obj, last,
|
|
1346 bcmax - last);
|
|
1347 }
|
|
1348 if (max < size)
|
|
1349 write_c_string (" ...", printcharfun);
|
|
1350 write_char_internal ("\"", printcharfun);
|
|
1351 }
|
|
1352 UNGCPRO;
|
|
1353 }
|
|
1354
|
|
1355 static void
|
|
1356 default_object_printer (Lisp_Object obj, Lisp_Object printcharfun,
|
|
1357 int escapeflag)
|
|
1358 {
|
|
1359 struct lcrecord_header *header =
|
|
1360 (struct lcrecord_header *) XPNTR (obj);
|
|
1361
|
|
1362 if (print_readably)
|
563
|
1363 printing_unreadable_object
|
|
1364 ("#<%s 0x%x>",
|
|
1365 LHEADER_IMPLEMENTATION (&header->lheader)->name,
|
|
1366 header->uid);
|
428
|
1367
|
800
|
1368 write_fmt_string (printcharfun, "#<%s 0x%x>",
|
|
1369 LHEADER_IMPLEMENTATION (&header->lheader)->name,
|
|
1370 header->uid);
|
428
|
1371 }
|
|
1372
|
|
1373 void
|
|
1374 internal_object_printer (Lisp_Object obj, Lisp_Object printcharfun,
|
|
1375 int escapeflag)
|
|
1376 {
|
800
|
1377 write_fmt_string (printcharfun,
|
|
1378 "#<INTERNAL OBJECT (XEmacs bug?) (%s) 0x%lx>",
|
|
1379 XRECORD_LHEADER_IMPLEMENTATION (obj)->name,
|
|
1380 (unsigned long) XPNTR (obj));
|
428
|
1381 }
|
|
1382
|
|
1383 void
|
|
1384 print_internal (Lisp_Object obj, Lisp_Object printcharfun, int escapeflag)
|
|
1385 {
|
|
1386 /* This function can GC */
|
802
|
1387 int specdepth;
|
428
|
1388
|
|
1389 QUIT;
|
|
1390
|
771
|
1391 #ifdef NO_PRINT_DURING_GC
|
428
|
1392 /* Emacs won't print while GCing, but an external debugger might */
|
|
1393 if (gc_in_progress) return;
|
771
|
1394 #endif
|
|
1395
|
|
1396 /* Try to check for a bogus pointer if we're in a situation where it may
|
|
1397 be likely. In such cases, crashing is counterproductive. */
|
|
1398 if (inhibit_non_essential_printing_operations || print_unbuffered)
|
|
1399 {
|
|
1400 if (XTYPE (obj) == Lisp_Type_Record)
|
|
1401 {
|
|
1402 struct lrecord_header *lheader = XRECORD_LHEADER (obj);
|
|
1403
|
|
1404 if (!debug_can_access_memory (lheader, sizeof (*lheader)))
|
|
1405 {
|
800
|
1406 write_fmt_string (printcharfun, "#<EMACS BUG: BAD MEMORY %p>",
|
|
1407 lheader);
|
771
|
1408 return;
|
|
1409 }
|
|
1410 else
|
|
1411 {
|
|
1412 const struct lrecord_implementation *impl;
|
|
1413
|
|
1414 if ((int) lheader->type >= lrecord_type_count)
|
|
1415 {
|
800
|
1416 write_fmt_string (printcharfun,
|
|
1417 "#<EMACS BUG: bad type %d BAD MEMORY %p>",
|
|
1418 lheader->type, lheader);
|
771
|
1419 return;
|
|
1420 }
|
|
1421
|
|
1422 impl = LHEADER_IMPLEMENTATION (lheader);
|
|
1423 if (!debug_can_access_memory
|
|
1424 (lheader,
|
|
1425 (impl->size_in_bytes_method ?
|
|
1426 impl->size_in_bytes_method (lheader) :
|
|
1427 impl->static_size)))
|
|
1428 {
|
800
|
1429 write_fmt_string (printcharfun,
|
|
1430 "#<EMACS BUG: type %s BAD MEMORY %p>",
|
|
1431 impl->name, lheader);
|
771
|
1432 return;
|
|
1433 }
|
|
1434
|
|
1435 if (STRINGP (obj))
|
|
1436 {
|
|
1437 Lisp_String *l = (Lisp_String *) lheader;
|
|
1438 if (!debug_can_access_memory
|
793
|
1439 (l->data_, l->size_))
|
771
|
1440 {
|
800
|
1441 write_fmt_string
|
|
1442 (printcharfun,
|
|
1443 "#<EMACS BUG: %p (BAD STRING DATA %p)>",
|
|
1444 lheader, l->data_);
|
771
|
1445 return;
|
|
1446 }
|
|
1447 }
|
|
1448 }
|
|
1449 }
|
|
1450 }
|
428
|
1451
|
|
1452 #ifdef I18N3
|
|
1453 /* #### Both input and output streams should have a flag associated
|
|
1454 with them indicating whether output to that stream, or strings
|
|
1455 read from the stream, get translated using Fgettext(). Such a
|
|
1456 stream is called a "translating stream". For the minibuffer and
|
|
1457 external-debugging-output this is always true on output, and
|
|
1458 with-output-to-temp-buffer sets the flag to true for the buffer
|
|
1459 it creates. This flag should also be user-settable. Perhaps it
|
|
1460 should be split up into two flags, one for input and one for
|
|
1461 output. */
|
|
1462 #endif
|
|
1463
|
|
1464 /* Detect circularities and truncate them.
|
|
1465 No need to offer any alternative--this is better than an error. */
|
|
1466 if (CONSP (obj) || VECTORP (obj) || COMPILED_FUNCTIONP (obj))
|
|
1467 {
|
|
1468 int i;
|
|
1469 for (i = 0; i < print_depth; i++)
|
|
1470 if (EQ (obj, being_printed[i]))
|
|
1471 {
|
603
|
1472 char buf[DECIMAL_PRINT_SIZE (long) + 1];
|
428
|
1473 *buf = '#';
|
|
1474 long_to_string (buf + 1, i);
|
|
1475 write_c_string (buf, printcharfun);
|
|
1476 return;
|
|
1477 }
|
|
1478 }
|
|
1479
|
|
1480 being_printed[print_depth] = obj;
|
802
|
1481 specdepth = internal_bind_int (&print_depth, print_depth + 1);
|
428
|
1482
|
|
1483 if (print_depth > PRINT_CIRCLE)
|
563
|
1484 signal_error (Qstack_overflow, "Apparently circular structure being printed", Qunbound);
|
428
|
1485
|
|
1486 switch (XTYPE (obj))
|
|
1487 {
|
|
1488 case Lisp_Type_Int_Even:
|
|
1489 case Lisp_Type_Int_Odd:
|
|
1490 {
|
603
|
1491 char buf[DECIMAL_PRINT_SIZE (EMACS_INT)];
|
428
|
1492 long_to_string (buf, XINT (obj));
|
|
1493 write_c_string (buf, printcharfun);
|
|
1494 break;
|
|
1495 }
|
|
1496
|
|
1497 case Lisp_Type_Char:
|
|
1498 {
|
|
1499 /* God intended that this be #\..., you know. */
|
|
1500 char buf[16];
|
|
1501 Emchar ch = XCHAR (obj);
|
|
1502 char *p = buf;
|
|
1503 *p++ = '?';
|
434
|
1504 if (ch < 32)
|
|
1505 {
|
|
1506 *p++ = '\\';
|
|
1507 switch (ch)
|
|
1508 {
|
|
1509 case '\t': *p++ = 't'; break;
|
|
1510 case '\n': *p++ = 'n'; break;
|
|
1511 case '\r': *p++ = 'r'; break;
|
|
1512 default:
|
|
1513 *p++ = '^';
|
|
1514 *p++ = ch + 64;
|
|
1515 if ((ch + 64) == '\\')
|
|
1516 *p++ = '\\';
|
|
1517 break;
|
|
1518 }
|
|
1519 }
|
|
1520 else if (ch < 127)
|
428
|
1521 {
|
434
|
1522 /* syntactically special characters should be escaped. */
|
|
1523 switch (ch)
|
|
1524 {
|
|
1525 case ' ':
|
|
1526 case '"':
|
|
1527 case '#':
|
|
1528 case '\'':
|
|
1529 case '(':
|
|
1530 case ')':
|
|
1531 case ',':
|
|
1532 case '.':
|
|
1533 case ';':
|
|
1534 case '?':
|
|
1535 case '[':
|
|
1536 case '\\':
|
|
1537 case ']':
|
|
1538 case '`':
|
|
1539 *p++ = '\\';
|
|
1540 }
|
|
1541 *p++ = ch;
|
428
|
1542 }
|
|
1543 else if (ch == 127)
|
434
|
1544 {
|
|
1545 *p++ = '\\', *p++ = '^', *p++ = '?';
|
|
1546 }
|
|
1547 else if (ch < 160)
|
428
|
1548 {
|
|
1549 *p++ = '\\', *p++ = '^';
|
665
|
1550 p += set_charptr_emchar ((Intbyte *) p, ch + 64);
|
428
|
1551 }
|
|
1552 else
|
434
|
1553 {
|
665
|
1554 p += set_charptr_emchar ((Intbyte *) p, ch);
|
434
|
1555 }
|
440
|
1556
|
665
|
1557 output_string (printcharfun, (Intbyte *) buf, Qnil, 0, p - buf);
|
434
|
1558
|
428
|
1559 break;
|
|
1560 }
|
|
1561
|
|
1562 case Lisp_Type_Record:
|
|
1563 {
|
|
1564 struct lrecord_header *lheader = XRECORD_LHEADER (obj);
|
|
1565 struct gcpro gcpro1, gcpro2;
|
|
1566
|
|
1567 if (CONSP (obj) || VECTORP(obj))
|
|
1568 {
|
|
1569 /* If deeper than spec'd depth, print placeholder. */
|
|
1570 if (INTP (Vprint_level)
|
|
1571 && print_depth > XINT (Vprint_level))
|
|
1572 {
|
|
1573 GCPRO2 (obj, printcharfun);
|
|
1574 write_c_string ("...", printcharfun);
|
|
1575 UNGCPRO;
|
|
1576 break;
|
|
1577 }
|
|
1578 }
|
|
1579
|
|
1580 GCPRO2 (obj, printcharfun);
|
|
1581 if (LHEADER_IMPLEMENTATION (lheader)->printer)
|
|
1582 ((LHEADER_IMPLEMENTATION (lheader)->printer)
|
|
1583 (obj, printcharfun, escapeflag));
|
|
1584 else
|
|
1585 default_object_printer (obj, printcharfun, escapeflag);
|
|
1586 UNGCPRO;
|
|
1587 break;
|
|
1588 }
|
|
1589
|
|
1590 default:
|
|
1591 {
|
800
|
1592 #ifdef ERROR_CHECK_TYPES
|
428
|
1593 abort ();
|
800
|
1594 #else /* not ERROR_CHECK_TYPES */
|
428
|
1595 /* We're in trouble if this happens! */
|
|
1596 if (print_readably)
|
571
|
1597 signal_error (Qinternal_error, "printing illegal data type #o%03o",
|
579
|
1598 make_int (XTYPE (obj)));
|
428
|
1599 write_c_string ("#<EMACS BUG: ILLEGAL DATATYPE ",
|
|
1600 printcharfun);
|
800
|
1601 write_fmt_string (printcharfun, "(#o%3o)", (int) XTYPE (obj));
|
428
|
1602 write_c_string
|
|
1603 (" Save your buffers immediately and please report this bug>",
|
|
1604 printcharfun);
|
800
|
1605 #endif /* not ERROR_CHECK_TYPES */
|
428
|
1606 break;
|
|
1607 }
|
|
1608 }
|
|
1609
|
802
|
1610 unbind_to (specdepth);
|
428
|
1611 }
|
|
1612
|
|
1613
|
|
1614 #ifdef LISP_FLOAT_TYPE
|
|
1615 void
|
|
1616 print_float (Lisp_Object obj, Lisp_Object printcharfun, int escapeflag)
|
|
1617 {
|
|
1618 char pigbuf[350]; /* see comments in float_to_string */
|
|
1619
|
|
1620 float_to_string (pigbuf, XFLOAT_DATA (obj));
|
|
1621 write_c_string (pigbuf, printcharfun);
|
|
1622 }
|
|
1623 #endif /* LISP_FLOAT_TYPE */
|
|
1624
|
|
1625 void
|
|
1626 print_symbol (Lisp_Object obj, Lisp_Object printcharfun, int escapeflag)
|
|
1627 {
|
|
1628 /* This function can GC */
|
|
1629 /* #### Bug!! (intern "") isn't printed in some distinguished way */
|
|
1630 /* #### (the reader also loses on it) */
|
793
|
1631 Lisp_Object name = symbol_name (XSYMBOL (obj));
|
|
1632 Bytecount size = XSTRING_LENGTH (name);
|
428
|
1633 struct gcpro gcpro1, gcpro2;
|
|
1634
|
|
1635 if (!escapeflag)
|
|
1636 {
|
|
1637 /* This deals with GC-relocation */
|
793
|
1638 output_string (printcharfun, 0, name, 0, size);
|
428
|
1639 return;
|
|
1640 }
|
|
1641 GCPRO2 (obj, printcharfun);
|
|
1642
|
|
1643 /* If we print an uninterned symbol as part of a complex object and
|
|
1644 the flag print-gensym is non-nil, prefix it with #n= to read the
|
|
1645 object back with the #n# reader syntax later if needed. */
|
|
1646 if (!NILP (Vprint_gensym)
|
442
|
1647 /* #### Test whether this produces a noticeable slow-down for
|
428
|
1648 printing when print-gensym is non-nil. */
|
|
1649 && !EQ (obj, oblookup (Vobarray,
|
793
|
1650 XSTRING_DATA (symbol_name (XSYMBOL (obj))),
|
|
1651 XSTRING_LENGTH (symbol_name (XSYMBOL (obj))))))
|
428
|
1652 {
|
|
1653 if (print_depth > 1)
|
|
1654 {
|
|
1655 Lisp_Object tem = Fassq (obj, Vprint_gensym_alist);
|
|
1656 if (CONSP (tem))
|
|
1657 {
|
|
1658 write_char_internal ("#", printcharfun);
|
|
1659 print_internal (XCDR (tem), printcharfun, escapeflag);
|
|
1660 write_char_internal ("#", printcharfun);
|
446
|
1661 UNGCPRO;
|
428
|
1662 return;
|
|
1663 }
|
|
1664 else
|
|
1665 {
|
|
1666 if (CONSP (Vprint_gensym_alist))
|
|
1667 {
|
|
1668 /* Vprint_gensym_alist is exposed to Lisp, so we
|
|
1669 have to be careful. */
|
|
1670 CHECK_CONS (XCAR (Vprint_gensym_alist));
|
|
1671 CHECK_INT (XCDR (XCAR (Vprint_gensym_alist)));
|
793
|
1672 tem = make_int (XINT (XCDR (XCAR (Vprint_gensym_alist))) + 1);
|
428
|
1673 }
|
|
1674 else
|
793
|
1675 tem = make_int (1);
|
428
|
1676 Vprint_gensym_alist = Fcons (Fcons (obj, tem), Vprint_gensym_alist);
|
|
1677
|
|
1678 write_char_internal ("#", printcharfun);
|
|
1679 print_internal (tem, printcharfun, escapeflag);
|
|
1680 write_char_internal ("=", printcharfun);
|
|
1681 }
|
|
1682 }
|
|
1683 write_c_string ("#:", printcharfun);
|
|
1684 }
|
|
1685
|
|
1686 /* Does it look like an integer or a float? */
|
|
1687 {
|
793
|
1688 Intbyte *data = XSTRING_DATA (name);
|
428
|
1689 Bytecount confusing = 0;
|
|
1690
|
|
1691 if (size == 0)
|
|
1692 goto not_yet_confused; /* Really confusing */
|
|
1693 else if (isdigit (data[0]))
|
|
1694 confusing = 0;
|
|
1695 else if (size == 1)
|
|
1696 goto not_yet_confused;
|
|
1697 else if (data[0] == '-' || data[0] == '+')
|
|
1698 confusing = 1;
|
|
1699 else
|
|
1700 goto not_yet_confused;
|
|
1701
|
|
1702 for (; confusing < size; confusing++)
|
|
1703 {
|
|
1704 if (!isdigit (data[confusing]))
|
|
1705 {
|
|
1706 confusing = 0;
|
|
1707 break;
|
|
1708 }
|
|
1709 }
|
|
1710 not_yet_confused:
|
|
1711
|
|
1712 #ifdef LISP_FLOAT_TYPE
|
|
1713 if (!confusing)
|
|
1714 /* #### Ugh, this is needlessly complex and slow for what we
|
|
1715 need here. It might be a good idea to copy equivalent code
|
|
1716 from FSF. --hniksic */
|
|
1717 confusing = isfloat_string ((char *) data);
|
|
1718 #endif
|
|
1719 if (confusing)
|
|
1720 write_char_internal ("\\", printcharfun);
|
|
1721 }
|
|
1722
|
|
1723 {
|
|
1724 Bytecount i;
|
|
1725 Bytecount last = 0;
|
|
1726
|
|
1727 for (i = 0; i < size; i++)
|
|
1728 {
|
793
|
1729 switch (XSTRING_BYTE (name, i))
|
428
|
1730 {
|
|
1731 case 0: case 1: case 2: case 3:
|
|
1732 case 4: case 5: case 6: case 7:
|
|
1733 case 8: case 9: case 10: case 11:
|
|
1734 case 12: case 13: case 14: case 15:
|
|
1735 case 16: case 17: case 18: case 19:
|
|
1736 case 20: case 21: case 22: case 23:
|
|
1737 case 24: case 25: case 26: case 27:
|
|
1738 case 28: case 29: case 30: case 31:
|
|
1739 case ' ': case '\"': case '\\': case '\'':
|
|
1740 case ';': case '#' : case '(' : case ')':
|
|
1741 case ',': case '.' : case '`' :
|
|
1742 case '[': case ']' : case '?' :
|
|
1743 if (i > last)
|
793
|
1744 output_string (printcharfun, 0, name, last, i - last);
|
428
|
1745 write_char_internal ("\\", printcharfun);
|
|
1746 last = i;
|
|
1747 }
|
|
1748 }
|
793
|
1749 output_string (printcharfun, 0, name, last, size - last);
|
428
|
1750 }
|
|
1751 UNGCPRO;
|
|
1752 }
|
|
1753
|
|
1754
|
442
|
1755 /* Useful on systems or in places where writing to stdout is unavailable or
|
|
1756 not working. */
|
428
|
1757
|
|
1758 static int alternate_do_pointer;
|
|
1759 static char alternate_do_string[5000];
|
|
1760
|
|
1761 DEFUN ("alternate-debugging-output", Falternate_debugging_output, 1, 1, 0, /*
|
|
1762 Append CHARACTER to the array `alternate_do_string'.
|
|
1763 This can be used in place of `external-debugging-output' as a function
|
|
1764 to be passed to `print'. Before calling `print', set `alternate_do_pointer'
|
|
1765 to 0.
|
|
1766 */
|
|
1767 (character))
|
|
1768 {
|
665
|
1769 Intbyte str[MAX_EMCHAR_LEN];
|
428
|
1770 Bytecount len;
|
|
1771
|
|
1772 CHECK_CHAR_COERCE_INT (character);
|
|
1773 len = set_charptr_emchar (str, XCHAR (character));
|
771
|
1774 write_string_to_alternate_debugging_output (str, len);
|
|
1775
|
|
1776 return character;
|
|
1777 }
|
|
1778
|
|
1779 static void
|
|
1780 write_string_to_alternate_debugging_output (Intbyte *str, Bytecount len)
|
|
1781 {
|
|
1782 int extlen;
|
|
1783 const Extbyte *extptr;
|
|
1784 #if 0 /* We want to see the internal representation, don't we? */
|
|
1785 if (initialized && !inhibit_non_essential_printing_operations)
|
|
1786 TO_EXTERNAL_FORMAT (DATA, (str, len),
|
|
1787 ALLOCA, (extptr, extlen),
|
|
1788 Qterminal);
|
|
1789 else
|
|
1790 #endif /* 0 */
|
|
1791 {
|
|
1792 extlen = len;
|
|
1793 extptr = (Extbyte *) str;
|
|
1794 }
|
428
|
1795 memcpy (alternate_do_string + alternate_do_pointer, extptr, extlen);
|
|
1796 alternate_do_pointer += extlen;
|
|
1797 alternate_do_string[alternate_do_pointer] = 0;
|
|
1798 }
|
|
1799
|
|
1800 DEFUN ("external-debugging-output", Fexternal_debugging_output, 1, 3, 0, /*
|
|
1801 Write CHAR-OR-STRING to stderr or stdout.
|
|
1802 If optional arg STDOUT-P is non-nil, write to stdout; otherwise, write
|
|
1803 to stderr. You can use this function to write directly to the terminal.
|
|
1804 This function can be used as the STREAM argument of Fprint() or the like.
|
|
1805
|
442
|
1806 Under MS Windows, this writes output to the console window (which is
|
|
1807 created, if necessary), unless XEmacs is being run noninteractively
|
|
1808 \(i.e. using the `-batch' argument).
|
|
1809
|
428
|
1810 If you have opened a termscript file (using `open-termscript'), then
|
|
1811 the output also will be logged to this file.
|
|
1812 */
|
|
1813 (char_or_string, stdout_p, device))
|
|
1814 {
|
|
1815 FILE *file = 0;
|
|
1816 struct console *con = 0;
|
|
1817
|
|
1818 if (NILP (device))
|
|
1819 {
|
|
1820 if (!NILP (stdout_p))
|
|
1821 file = stdout;
|
|
1822 else
|
|
1823 file = stderr;
|
|
1824 }
|
|
1825 else
|
|
1826 {
|
|
1827 CHECK_LIVE_DEVICE (device);
|
|
1828 if (!DEVICE_TTY_P (XDEVICE (device)) &&
|
|
1829 !DEVICE_STREAM_P (XDEVICE (device)))
|
563
|
1830 wtaerror ("Must be tty or stream device", device);
|
428
|
1831 con = XCONSOLE (DEVICE_CONSOLE (XDEVICE (device)));
|
|
1832 if (DEVICE_TTY_P (XDEVICE (device)))
|
|
1833 file = 0;
|
|
1834 else if (!NILP (stdout_p))
|
|
1835 file = CONSOLE_STREAM_DATA (con)->out;
|
|
1836 else
|
|
1837 file = CONSOLE_STREAM_DATA (con)->err;
|
|
1838 }
|
|
1839
|
|
1840 if (STRINGP (char_or_string))
|
|
1841 write_string_to_stdio_stream (file, con,
|
|
1842 XSTRING_DATA (char_or_string),
|
771
|
1843 XSTRING_LENGTH (char_or_string),
|
|
1844 print_unbuffered);
|
428
|
1845 else
|
|
1846 {
|
665
|
1847 Intbyte str[MAX_EMCHAR_LEN];
|
428
|
1848 Bytecount len;
|
|
1849
|
|
1850 CHECK_CHAR_COERCE_INT (char_or_string);
|
|
1851 len = set_charptr_emchar (str, XCHAR (char_or_string));
|
771
|
1852 write_string_to_stdio_stream (file, con, str, len, print_unbuffered);
|
428
|
1853 }
|
|
1854
|
|
1855 return char_or_string;
|
|
1856 }
|
|
1857
|
|
1858 DEFUN ("open-termscript", Fopen_termscript, 1, 1, "FOpen termscript file: ", /*
|
444
|
1859 Start writing all terminal output to FILENAME as well as the terminal.
|
|
1860 FILENAME = nil means just close any termscript file currently open.
|
428
|
1861 */
|
444
|
1862 (filename))
|
428
|
1863 {
|
|
1864 /* This function can GC */
|
|
1865 if (termscript != 0)
|
|
1866 {
|
771
|
1867 retry_fclose (termscript);
|
444
|
1868 termscript = 0;
|
|
1869 }
|
|
1870
|
|
1871 if (! NILP (filename))
|
|
1872 {
|
|
1873 filename = Fexpand_file_name (filename, Qnil);
|
771
|
1874 termscript = qxe_fopen (XSTRING_DATA (filename), "w");
|
428
|
1875 if (termscript == NULL)
|
563
|
1876 report_file_error ("Opening termscript", filename);
|
428
|
1877 }
|
|
1878 return Qnil;
|
|
1879 }
|
|
1880
|
440
|
1881 static int debug_print_length = 50;
|
|
1882 static int debug_print_level = 15;
|
|
1883 static int debug_print_readably = -1;
|
428
|
1884
|
771
|
1885 /* Debugging kludge -- unbuffered */
|
428
|
1886 static void
|
|
1887 debug_print_no_newline (Lisp_Object debug_print_obj)
|
|
1888 {
|
|
1889 /* This function can GC */
|
802
|
1890 int specdepth = internal_bind_int (&print_depth, 0);
|
|
1891 internal_bind_int (&print_readably,
|
|
1892 debug_print_readably != -1 ? debug_print_readably : 0);
|
|
1893 internal_bind_int (&print_unbuffered, print_unbuffered + 1);
|
428
|
1894 if (debug_print_length > 0)
|
802
|
1895 internal_bind_lisp_object (&Vprint_length, make_int (debug_print_length));
|
428
|
1896 if (debug_print_level > 0)
|
802
|
1897 internal_bind_lisp_object (&Vprint_level, make_int (debug_print_level));
|
|
1898 /* #### Do we need this? It was in the old code. */
|
|
1899 internal_bind_lisp_object (&Vinhibit_quit, Vinhibit_quit);
|
|
1900
|
428
|
1901 print_internal (debug_print_obj, Qexternal_debugging_output, 1);
|
442
|
1902 alternate_do_pointer = 0;
|
|
1903 print_internal (debug_print_obj, Qalternate_debugging_output, 1);
|
|
1904 #ifdef WIN32_NATIVE
|
|
1905 /* Write out to the debugger, as well */
|
|
1906 print_internal (debug_print_obj, Qmswindows_debugging_output, 1);
|
|
1907 #endif
|
440
|
1908
|
802
|
1909 unbind_to (specdepth);
|
428
|
1910 }
|
|
1911
|
|
1912 void
|
|
1913 debug_print (Lisp_Object debug_print_obj)
|
|
1914 {
|
|
1915 debug_print_no_newline (debug_print_obj);
|
771
|
1916 debug_out ("\n");
|
428
|
1917 }
|
|
1918
|
|
1919 /* Debugging kludge -- unbuffered */
|
|
1920 /* This function provided for the benefit of the debugger. */
|
|
1921 void
|
|
1922 debug_backtrace (void)
|
|
1923 {
|
|
1924 /* This function can GC */
|
802
|
1925 int specdepth = internal_bind_int (&print_depth, 0);
|
|
1926 internal_bind_int (&print_readably, 0);
|
|
1927 internal_bind_int (&print_unbuffered, print_unbuffered + 1);
|
428
|
1928 if (debug_print_length > 0)
|
802
|
1929 internal_bind_lisp_object (&Vprint_length, make_int (debug_print_length));
|
428
|
1930 if (debug_print_level > 0)
|
802
|
1931 internal_bind_lisp_object (&Vprint_level, make_int (debug_print_level));
|
|
1932 /* #### Do we need this? It was in the old code. */
|
|
1933 internal_bind_lisp_object (&Vinhibit_quit, Vinhibit_quit);
|
428
|
1934
|
|
1935 Fbacktrace (Qexternal_debugging_output, Qt);
|
|
1936 stderr_out ("\n");
|
|
1937
|
802
|
1938 unbind_to (specdepth);
|
428
|
1939 }
|
|
1940
|
|
1941 void
|
|
1942 debug_short_backtrace (int length)
|
|
1943 {
|
|
1944 int first = 1;
|
|
1945 struct backtrace *bt = backtrace_list;
|
771
|
1946 debug_out (" [");
|
428
|
1947 while (length > 0 && bt)
|
|
1948 {
|
|
1949 if (!first)
|
|
1950 {
|
771
|
1951 debug_out (", ");
|
428
|
1952 }
|
|
1953 if (COMPILED_FUNCTIONP (*bt->function))
|
|
1954 {
|
|
1955 #if defined(COMPILED_FUNCTION_ANNOTATION_HACK)
|
|
1956 Lisp_Object ann =
|
|
1957 compiled_function_annotation (XCOMPILED_FUNCTION (*bt->function));
|
|
1958 #else
|
|
1959 Lisp_Object ann = Qnil;
|
|
1960 #endif
|
|
1961 if (!NILP (ann))
|
|
1962 {
|
771
|
1963 debug_out ("<compiled-function from ");
|
428
|
1964 debug_print_no_newline (ann);
|
771
|
1965 debug_out (">");
|
428
|
1966 }
|
|
1967 else
|
|
1968 {
|
771
|
1969 debug_out ("<compiled-function of unknown origin>");
|
428
|
1970 }
|
|
1971 }
|
|
1972 else
|
|
1973 debug_print_no_newline (*bt->function);
|
|
1974 first = 0;
|
|
1975 length--;
|
|
1976 bt = bt->next;
|
|
1977 }
|
771
|
1978 debug_out ("]\n");
|
428
|
1979 }
|
|
1980
|
|
1981
|
|
1982 void
|
|
1983 syms_of_print (void)
|
|
1984 {
|
563
|
1985 DEFSYMBOL (Qstandard_output);
|
428
|
1986
|
563
|
1987 DEFSYMBOL (Qprint_length);
|
428
|
1988
|
563
|
1989 DEFSYMBOL (Qprint_string_length);
|
428
|
1990
|
563
|
1991 DEFSYMBOL (Qdisplay_error);
|
|
1992 DEFSYMBOL (Qprint_message_label);
|
428
|
1993
|
|
1994 DEFSUBR (Fprin1);
|
|
1995 DEFSUBR (Fprin1_to_string);
|
|
1996 DEFSUBR (Fprinc);
|
|
1997 DEFSUBR (Fprint);
|
|
1998 DEFSUBR (Ferror_message_string);
|
|
1999 DEFSUBR (Fdisplay_error);
|
|
2000 DEFSUBR (Fterpri);
|
|
2001 DEFSUBR (Fwrite_char);
|
|
2002 DEFSUBR (Falternate_debugging_output);
|
|
2003 DEFSUBR (Fexternal_debugging_output);
|
|
2004 DEFSUBR (Fopen_termscript);
|
563
|
2005 DEFSYMBOL (Qexternal_debugging_output);
|
|
2006 DEFSYMBOL (Qalternate_debugging_output);
|
442
|
2007 #ifdef HAVE_MS_WINDOWS
|
563
|
2008 DEFSYMBOL (Qmswindows_debugging_output);
|
442
|
2009 #endif
|
428
|
2010 DEFSUBR (Fwith_output_to_temp_buffer);
|
|
2011 }
|
|
2012
|
|
2013 void
|
|
2014 reinit_vars_of_print (void)
|
|
2015 {
|
|
2016 alternate_do_pointer = 0;
|
|
2017 }
|
|
2018
|
|
2019 void
|
|
2020 vars_of_print (void)
|
|
2021 {
|
|
2022 reinit_vars_of_print ();
|
|
2023
|
|
2024 DEFVAR_LISP ("standard-output", &Vstandard_output /*
|
|
2025 Output stream `print' uses by default for outputting a character.
|
|
2026 This may be any function of one argument.
|
|
2027 It may also be a buffer (output is inserted before point)
|
|
2028 or a marker (output is inserted and the marker is advanced)
|
|
2029 or the symbol t (output appears in the minibuffer line).
|
|
2030 */ );
|
|
2031 Vstandard_output = Qt;
|
|
2032
|
|
2033 #ifdef LISP_FLOAT_TYPE
|
|
2034 DEFVAR_LISP ("float-output-format", &Vfloat_output_format /*
|
|
2035 The format descriptor string that lisp uses to print floats.
|
|
2036 This is a %-spec like those accepted by `printf' in C,
|
|
2037 but with some restrictions. It must start with the two characters `%.'.
|
|
2038 After that comes an integer precision specification,
|
|
2039 and then a letter which controls the format.
|
|
2040 The letters allowed are `e', `f' and `g'.
|
|
2041 Use `e' for exponential notation "DIG.DIGITSeEXPT"
|
|
2042 Use `f' for decimal point notation "DIGITS.DIGITS".
|
|
2043 Use `g' to choose the shorter of those two formats for the number at hand.
|
|
2044 The precision in any of these cases is the number of digits following
|
|
2045 the decimal point. With `f', a precision of 0 means to omit the
|
|
2046 decimal point. 0 is not allowed with `f' or `g'.
|
|
2047
|
|
2048 A value of nil means to use `%.16g'.
|
|
2049
|
|
2050 Regardless of the value of `float-output-format', a floating point number
|
|
2051 will never be printed in such a way that it is ambiguous with an integer;
|
|
2052 that is, a floating-point number will always be printed with a decimal
|
|
2053 point and/or an exponent, even if the digits following the decimal point
|
|
2054 are all zero. This is to preserve read-equivalence.
|
|
2055 */ );
|
|
2056 Vfloat_output_format = Qnil;
|
|
2057 #endif /* LISP_FLOAT_TYPE */
|
|
2058
|
|
2059 DEFVAR_LISP ("print-length", &Vprint_length /*
|
|
2060 Maximum length of list or vector to print before abbreviating.
|
|
2061 A value of nil means no limit.
|
|
2062 */ );
|
|
2063 Vprint_length = Qnil;
|
|
2064
|
|
2065 DEFVAR_LISP ("print-string-length", &Vprint_string_length /*
|
|
2066 Maximum length of string to print before abbreviating.
|
|
2067 A value of nil means no limit.
|
|
2068 */ );
|
|
2069 Vprint_string_length = Qnil;
|
|
2070
|
|
2071 DEFVAR_LISP ("print-level", &Vprint_level /*
|
|
2072 Maximum depth of list nesting to print before abbreviating.
|
|
2073 A value of nil means no limit.
|
|
2074 */ );
|
|
2075 Vprint_level = Qnil;
|
|
2076
|
|
2077 DEFVAR_BOOL ("print-escape-newlines", &print_escape_newlines /*
|
|
2078 Non-nil means print newlines in strings as backslash-n.
|
|
2079 */ );
|
|
2080 print_escape_newlines = 0;
|
|
2081
|
|
2082 DEFVAR_BOOL ("print-readably", &print_readably /*
|
|
2083 If non-nil, then all objects will be printed in a readable form.
|
|
2084 If an object has no readable representation, then an error is signalled.
|
|
2085 When print-readably is true, compiled-function objects will be written in
|
|
2086 #[...] form instead of in #<compiled-function [...]> form, and two-element
|
|
2087 lists of the form (quote object) will be written as the equivalent 'object.
|
|
2088 Do not SET this variable; bind it instead.
|
|
2089 */ );
|
|
2090 print_readably = 0;
|
|
2091
|
|
2092 /* #### I think this should default to t. But we'd better wait
|
|
2093 until we see that it works out. */
|
|
2094 DEFVAR_LISP ("print-gensym", &Vprint_gensym /*
|
|
2095 If non-nil, then uninterned symbols will be printed specially.
|
|
2096 Uninterned symbols are those which are not present in `obarray', that is,
|
|
2097 those which were made with `make-symbol' or by calling `intern' with a
|
|
2098 second argument.
|
|
2099
|
|
2100 When print-gensym is true, such symbols will be preceded by "#:",
|
|
2101 which causes the reader to create a new symbol instead of interning
|
|
2102 and returning an existing one. Beware: the #: syntax creates a new
|
|
2103 symbol each time it is seen, so if you print an object which contains
|
|
2104 two pointers to the same uninterned symbol, `read' will not duplicate
|
|
2105 that structure.
|
|
2106
|
|
2107 If the value of `print-gensym' is a cons cell, then in addition
|
|
2108 refrain from clearing `print-gensym-alist' on entry to and exit from
|
|
2109 printing functions, so that the use of #...# and #...= can carry over
|
|
2110 for several separately printed objects.
|
|
2111 */ );
|
|
2112 Vprint_gensym = Qnil;
|
|
2113
|
|
2114 DEFVAR_LISP ("print-gensym-alist", &Vprint_gensym_alist /*
|
|
2115 Association list of elements (GENSYM . N) to guide use of #N# and #N=.
|
|
2116 In each element, GENSYM is an uninterned symbol that has been associated
|
|
2117 with #N= for the specified value of N.
|
|
2118 */ );
|
|
2119 Vprint_gensym_alist = Qnil;
|
|
2120
|
|
2121 DEFVAR_LISP ("print-message-label", &Vprint_message_label /*
|
|
2122 Label for minibuffer messages created with `print'. This should
|
|
2123 generally be bound with `let' rather than set. (See `display-message'.)
|
|
2124 */ );
|
|
2125 Vprint_message_label = Qprint;
|
|
2126 }
|