comparison src/print.c @ 412:697ef44129c6 r21-2-14

Import from CVS: tag r21-2-14
author cvs
date Mon, 13 Aug 2007 11:20:41 +0200
parents de805c49cfc1
children da8ed4261e83
comparison
equal deleted inserted replaced
411:12e008d41344 412:697ef44129c6
1 /* Lisp object printing and output streams. 1 /* Lisp object printing and output streams.
2 Copyright (C) 1985, 1986, 1988, 1992-1995 Free Software Foundation, Inc. 2 Copyright (C) 1985, 1986, 1988, 1992-1995 Free Software Foundation, Inc.
3 Copyright (C) 1995, 1996, 2000 Ben Wing. 3 Copyright (C) 1995, 1996 Ben Wing.
4 4
5 This file is part of XEmacs. 5 This file is part of XEmacs.
6 6
7 XEmacs is free software; you can redistribute it and/or modify it 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 8 under the terms of the GNU General Public License as published by the
36 #include "extents.h" 36 #include "extents.h"
37 #include "frame.h" 37 #include "frame.h"
38 #include "insdel.h" 38 #include "insdel.h"
39 #include "lstream.h" 39 #include "lstream.h"
40 #include "sysfile.h" 40 #include "sysfile.h"
41 #ifdef WIN32_NATIVE 41
42 #include "console-msw.h" 42 #include <limits.h>
43 #endif
44
45 #include <float.h> 43 #include <float.h>
46 /* Define if not in float.h */ 44 /* Define if not in float.h */
47 #ifndef DBL_DIG 45 #ifndef DBL_DIG
48 #define DBL_DIG 16 46 #define DBL_DIG 16
49 #endif 47 #endif
51 Lisp_Object Vstandard_output, Qstandard_output; 49 Lisp_Object Vstandard_output, Qstandard_output;
52 50
53 /* The subroutine object for external-debugging-output is kept here 51 /* The subroutine object for external-debugging-output is kept here
54 for the convenience of the debugger. */ 52 for the convenience of the debugger. */
55 Lisp_Object Qexternal_debugging_output; 53 Lisp_Object Qexternal_debugging_output;
54 Lisp_Object Qalternate_debugging_output;
56 55
57 /* Avoid actual stack overflow in print. */ 56 /* Avoid actual stack overflow in print. */
58 static int print_depth; 57 static int print_depth;
59 58
60 /* Detect most circularities to print finite output. */ 59 /* Detect most circularities to print finite output. */
61 #define PRINT_CIRCLE 200 60 #define PRINT_CIRCLE 200
62 static Lisp_Object being_printed[PRINT_CIRCLE]; 61 Lisp_Object being_printed[PRINT_CIRCLE];
63 62
64 /* Maximum length of list or vector to print in full; noninteger means 63 /* Maximum length of list or vector to print in full; noninteger means
65 effectively infinity */ 64 effectively infinity */
66 65
67 Lisp_Object Vprint_length; 66 Lisp_Object Vprint_length;
91 Neither t nor nil means so that and don't clear Vprint_gensym_alist 90 Neither t nor nil means so that and don't clear Vprint_gensym_alist
92 on entry to and exit from print functions. */ 91 on entry to and exit from print functions. */
93 Lisp_Object Vprint_gensym; 92 Lisp_Object Vprint_gensym;
94 Lisp_Object Vprint_gensym_alist; 93 Lisp_Object Vprint_gensym_alist;
95 94
95 Lisp_Object Qprint_escape_newlines;
96 Lisp_Object Qprint_readably;
97
96 Lisp_Object Qdisplay_error; 98 Lisp_Object Qdisplay_error;
97 Lisp_Object Qprint_message_label; 99 Lisp_Object Qprint_message_label;
98 100
99 /* Force immediate output of all printed data. Used for debugging. */ 101 /* Force immediate output of all printed data. Used for debugging. */
100 int print_unbuffered; 102 int print_unbuffered;
103 105
104 106
105 107
106 int stdout_needs_newline; 108 int stdout_needs_newline;
107 109
108 #ifdef WIN32_NATIVE 110 /* Write a string (in internal format) to stdio stream STREAM. */
109 static int no_useful_stderr; 111
112 void
113 write_string_to_stdio_stream (FILE *stream, struct console *con,
114 CONST Bufbyte *str,
115 Bytecount offset, Bytecount len,
116 enum external_data_format fmt)
117 {
118 int extlen;
119 CONST Extbyte *extptr;
120
121 GET_CHARPTR_EXT_DATA_ALLOCA (str + offset, len, fmt, extptr, extlen);
122 if (stream)
123 {
124 fwrite (extptr, 1, extlen, stream);
125 #ifdef WINDOWSNT
126 /* Q122442 says that pipes are "treated as files, not as
127 devices", and that this is a feature. Before I found that
128 article, I thought it was a bug. Thanks MS, I feel much
129 better now. - kkm */
130 if (stream == stdout || stream == stderr)
131 fflush (stream);
110 #endif 132 #endif
111
112 static void
113 std_handle_out_external (FILE *stream, Lisp_Object lstream,
114 const Extbyte *extptr, Extcount extlen,
115 /* is this really stdout/stderr?
116 (controls termscript writing) */
117 int output_is_std_handle,
118 int must_flush)
119 {
120 if (stream)
121 {
122 #ifdef WIN32_NATIVE
123 if (!no_useful_stderr)
124 no_useful_stderr = GetStdHandle (STD_ERROR_HANDLE) == 0 ? 1 : -1;
125
126 /* we typically have no useful stdout/stderr under windows if we're
127 being invoked graphically. */
128 if (!noninteractive || no_useful_stderr > 0)
129 mswindows_output_console_string (extptr, extlen);
130 else
131 #endif
132 {
133 fwrite (extptr, 1, extlen, stream);
134 #ifdef WIN32_NATIVE
135 /* Q122442 says that pipes are "treated as files, not as
136 devices", and that this is a feature. Before I found that
137 article, I thought it was a bug. Thanks MS, I feel much
138 better now. - kkm */
139 must_flush = 1;
140 #endif
141 if (must_flush)
142 fflush (stream);
143 }
144 } 133 }
145 else 134 else
146 Lstream_write (XLSTREAM (lstream), extptr, extlen); 135 {
147 136 assert (CONSOLE_TTY_P (con));
148 if (output_is_std_handle) 137 Lstream_write (XLSTREAM (CONSOLE_TTY_DATA (con)->outstream),
138 extptr, extlen);
139 }
140 if (stream == stdout || stream == stderr ||
141 (!stream && CONSOLE_TTY_DATA (con)->is_stdio))
149 { 142 {
150 if (termscript) 143 if (termscript)
151 { 144 {
152 fwrite (extptr, 1, extlen, termscript); 145 fwrite (extptr, 1, extlen, termscript);
153 fflush (termscript); 146 fflush (termscript);
154 } 147 }
155 stdout_needs_newline = (extptr[extlen - 1] != '\n'); 148 stdout_needs_newline = (extptr[extlen - 1] != '\n');
156 } 149 }
157 } 150 }
158 151
159 /* #### The following function should be replaced a call to the
160 emacs_doprnt_*() functions. This is the only way to ensure that
161 I18N3 works properly (many implementations of the *printf()
162 functions, including the ones included in glibc, do not implement
163 the %###$ argument-positioning syntax).
164
165 Note, however, that to do this, we'd have to
166
167 1) pre-allocate all the lstreams and do whatever else was necessary
168 to make sure that no allocation occurs, since these functions may be
169 called from fatal_error_signal().
170
171 2) (to be really correct) make a new lstream that outputs using
172 mswindows_output_console_string(). */
173
174 static int
175 std_handle_out_va (FILE *stream, const char *fmt, va_list args)
176 {
177 Bufbyte kludge[8192];
178 Extbyte *extptr;
179 Extcount extlen;
180 int retval;
181
182 retval = vsprintf ((char *) kludge, fmt, args);
183 if (initialized && !fatal_error_in_progress)
184 TO_EXTERNAL_FORMAT (DATA, (kludge, strlen ((char *) kludge)),
185 ALLOCA, (extptr, extlen),
186 Qnative);
187 else
188 {
189 extptr = (Extbyte *) kludge;
190 extlen = (Extcount) strlen ((char *) kludge);
191 }
192
193 std_handle_out_external (stream, Qnil, extptr, extlen, 1, 1);
194 return retval;
195 }
196
197 /* Output portably to stderr or its equivalent; call GETTEXT on the
198 format string. Automatically flush when done. */
199
200 int
201 stderr_out (const char *fmt, ...)
202 {
203 int retval;
204 va_list args;
205 va_start (args, fmt);
206 retval =
207 std_handle_out_va
208 (stderr, initialized && !fatal_error_in_progress ? GETTEXT (fmt) : fmt,
209 args);
210 va_end (args);
211 return retval;
212 }
213
214 /* Output portably to stdout or its equivalent; call GETTEXT on the
215 format string. Automatically flush when done. */
216
217 int
218 stdout_out (const char *fmt, ...)
219 {
220 int retval;
221 va_list args;
222 va_start (args, fmt);
223 retval =
224 std_handle_out_va
225 (stdout, initialized && !fatal_error_in_progress ? GETTEXT (fmt) : fmt,
226 args);
227 va_end (args);
228 return retval;
229 }
230
231 DOESNT_RETURN
232 fatal (const char *fmt, ...)
233 {
234 va_list args;
235 va_start (args, fmt);
236
237 stderr_out ("\nXEmacs: ");
238 std_handle_out_va (stderr, GETTEXT (fmt), args);
239 stderr_out ("\n");
240
241 va_end (args);
242 exit (1);
243 }
244
245 /* Write a string (in internal format) to stdio stream STREAM. */
246
247 void
248 write_string_to_stdio_stream (FILE *stream, struct console *con,
249 const Bufbyte *str,
250 Bytecount offset, Bytecount len,
251 Lisp_Object coding_system,
252 int must_flush)
253 {
254 Extcount extlen;
255 const Extbyte *extptr;
256
257 /* #### yuck! sometimes this function is called with string data,
258 and the following call may gc. */
259 {
260 Bufbyte *puta = (Bufbyte *) alloca (len);
261 memcpy (puta, str + offset, len);
262 TO_EXTERNAL_FORMAT (DATA, (puta, len),
263 ALLOCA, (extptr, extlen),
264 coding_system);
265 }
266
267 if (stream)
268 std_handle_out_external (stream, Qnil, extptr, extlen,
269 stream == stdout || stream == stderr, must_flush);
270 else
271 {
272 assert (CONSOLE_TTY_P (con));
273 std_handle_out_external (0, CONSOLE_TTY_DATA (con)->outstream,
274 extptr, extlen,
275 CONSOLE_TTY_DATA (con)->is_stdio, must_flush);
276 }
277 }
278
279 /* Write a string to the output location specified in FUNCTION. 152 /* Write a string to the output location specified in FUNCTION.
280 Arguments NONRELOC, RELOC, OFFSET, and LEN are as in 153 Arguments NONRELOC, RELOC, OFFSET, and LEN are as in
281 buffer_insert_string_1() in insdel.c. */ 154 buffer_insert_string_1() in insdel.c. */
282 155
283 static void 156 static void
284 output_string (Lisp_Object function, const Bufbyte *nonreloc, 157 output_string (Lisp_Object function, CONST Bufbyte *nonreloc,
285 Lisp_Object reloc, Bytecount offset, Bytecount len) 158 Lisp_Object reloc, Bytecount offset, Bytecount len)
286 { 159 {
287 /* This function can GC */ 160 /* This function can GC */
288 Charcount cclen; 161 Charcount cclen;
289 /* We change the value of nonreloc (fetching it from reloc as 162 /* We change the value of nonreloc (fetching it from reloc as
290 necessary), but we don't want to pass this changed value on to 163 necessary), but we don't want to pass this changed value on to
291 other functions that take both a nonreloc and a reloc, or things 164 other functions that take both a nonreloc and a reloc, or things
292 may get confused and an assertion failure in 165 may get confused and an assertion failure in
293 fixup_internal_substring() may get triggered. */ 166 fixup_internal_substring() may get triggered. */
294 const Bufbyte *newnonreloc = nonreloc; 167 CONST Bufbyte *newnonreloc = nonreloc;
295 struct gcpro gcpro1, gcpro2; 168 struct gcpro gcpro1, gcpro2;
296 169
297 /* Emacs won't print while GCing, but an external debugger might */ 170 /* Emacs won't print while GCing, but an external debugger might */
298 if (gc_in_progress) return; 171 if (gc_in_progress) return;
299 172
365 echo_area_append (f, nonreloc, reloc, offset, len, Vprint_message_label); 238 echo_area_append (f, nonreloc, reloc, offset, len, Vprint_message_label);
366 } 239 }
367 else if (EQ (function, Qt) || EQ (function, Qnil)) 240 else if (EQ (function, Qt) || EQ (function, Qnil))
368 { 241 {
369 write_string_to_stdio_stream (stdout, 0, newnonreloc, offset, len, 242 write_string_to_stdio_stream (stdout, 0, newnonreloc, offset, len,
370 Qterminal, print_unbuffered); 243 FORMAT_TERMINAL);
371 } 244 }
372 else 245 else
373 { 246 {
374 Charcount ccoff = bytecount_to_charcount (newnonreloc, offset); 247 Charcount ccoff = bytecount_to_charcount (newnonreloc, offset);
375 Charcount iii; 248 Charcount iii;
474 } 347 }
475 } 348 }
476 349
477 /* Used for printing a single-byte character (*not* any Emchar). */ 350 /* Used for printing a single-byte character (*not* any Emchar). */
478 #define write_char_internal(string_of_length_1, stream) \ 351 #define write_char_internal(string_of_length_1, stream) \
479 output_string (stream, (const Bufbyte *) (string_of_length_1), \ 352 output_string (stream, (CONST Bufbyte *) (string_of_length_1), \
480 Qnil, 0, 1) 353 Qnil, 0, 1)
481 354
482 /* NOTE: Do not call this with the data of a Lisp_String, as 355 /* NOTE: Do not call this with the data of a Lisp_String, as
483 printcharfun might cause a GC, which might cause the string's data 356 printcharfun might cause a GC, which might cause the string's data
484 to be relocated. To princ a Lisp string, use: 357 to be relocated. To princ a Lisp string, use:
487 360
488 Also note that STREAM should be the result of 361 Also note that STREAM should be the result of
489 canonicalize_printcharfun() (i.e. Qnil means stdout, not 362 canonicalize_printcharfun() (i.e. Qnil means stdout, not
490 Vstandard_output, etc.) */ 363 Vstandard_output, etc.) */
491 void 364 void
492 write_string_1 (const Bufbyte *str, Bytecount size, Lisp_Object stream) 365 write_string_1 (CONST Bufbyte *str, Bytecount size, Lisp_Object stream)
493 { 366 {
494 /* This function can GC */ 367 /* This function can GC */
495 #ifdef ERROR_CHECK_BUFPOS 368 #ifdef ERROR_CHECK_BUFPOS
496 assert (size >= 0); 369 assert (size >= 0);
497 #endif 370 #endif
498 output_string (stream, str, Qnil, 0, size); 371 output_string (stream, str, Qnil, 0, size);
499 } 372 }
500 373
501 void 374 void
502 write_c_string (const char *str, Lisp_Object stream) 375 write_c_string (CONST char *str, Lisp_Object stream)
503 { 376 {
504 /* This function can GC */ 377 /* This function can GC */
505 write_string_1 ((const Bufbyte *) str, strlen (str), stream); 378 write_string_1 ((CONST Bufbyte *) str, strlen (str), stream);
506 } 379 }
507 380
508 381
509 DEFUN ("write-char", Fwrite_char, 1, 2, 0, /* 382 DEFUN ("write-char", Fwrite_char, 1, 2, 0, /*
510 Output character CH to stream STREAM. 383 Output character CH to stream STREAM.
756 } 629 }
757 /* Default method */ 630 /* Default method */
758 { 631 {
759 int first = 1; 632 int first = 1;
760 int speccount = specpdl_depth (); 633 int speccount = specpdl_depth ();
761 Lisp_Object frame = Qnil;
762 struct gcpro gcpro1;
763 GCPRO1 (stream);
764 634
765 specbind (Qprint_message_label, Qerror); 635 specbind (Qprint_message_label, Qerror);
766 stream = print_prepare (stream, &frame);
767
768 tail = Fcdr (error_object); 636 tail = Fcdr (error_object);
769 if (EQ (type, Qerror)) 637 if (EQ (type, Qerror))
770 { 638 {
771 print_internal (Fcar (tail), stream, 0); 639 print_internal (Fcar (tail), stream, 0);
772 tail = Fcdr (tail); 640 tail = Fcdr (tail);
784 write_c_string (first ? ": " : ", ", stream); 652 write_c_string (first ? ": " : ", ", stream);
785 print_internal (Fcar (tail), stream, 1); 653 print_internal (Fcar (tail), stream, 1);
786 tail = Fcdr (tail); 654 tail = Fcdr (tail);
787 first = 0; 655 first = 0;
788 } 656 }
789 print_finish (stream, frame);
790 UNGCPRO;
791 unbind_to (speccount, Qnil); 657 unbind_to (speccount, Qnil);
792 return; 658 return;
793 /* not reached */ 659 /* not reached */
794 } 660 }
795 661
843 709
844 710
845 #ifdef LISP_FLOAT_TYPE 711 #ifdef LISP_FLOAT_TYPE
846 712
847 Lisp_Object Vfloat_output_format; 713 Lisp_Object Vfloat_output_format;
714 Lisp_Object Qfloat_output_format;
848 715
849 /* 716 /*
850 * This buffer should be at least as large as the max string size of the 717 * This buffer should be at least as large as the max string size of the
851 * largest float, printed in the biggest notation. This is undoubtedly 718 * largest float, printed in the biggest notation. This is undoubtably
852 * 20d float_output_format, with the negative of the C-constant "HUGE" 719 * 20d float_output_format, with the negative of the C-constant "HUGE"
853 * from <math.h>. 720 * from <math.h>.
854 * 721 *
855 * On the vax the worst case is -1e38 in 20d format which takes 61 bytes. 722 * On the vax the worst case is -1e38 in 20d format which takes 61 bytes.
856 * 723 *
931 buf [(buf [0] == '-' ? 1 : 0)] = '0'; 798 buf [(buf [0] == '-' ? 1 : 0)] = '0';
932 } 799 }
933 } 800 }
934 #endif /* LISP_FLOAT_TYPE */ 801 #endif /* LISP_FLOAT_TYPE */
935 802
936 /* Print NUMBER to BUFFER. This is equivalent to sprintf(buffer, 803 /* Print NUMBER to BUFFER. The digits are first written in reverse
937 "%ld", number), only much faster. 804 order (the least significant digit first), and are then reversed.
805 This is equivalent to sprintf(buffer, "%ld", number), only much
806 faster.
938 807
939 BUFFER should accept 24 bytes. This should suffice for the longest 808 BUFFER should accept 24 bytes. This should suffice for the longest
940 numbers on 64-bit machines, including the `-' sign and the trailing 809 numbers on 64-bit machines. */
941 \0. */
942 void 810 void
943 long_to_string (char *buffer, long number) 811 long_to_string (char *buffer, long number)
944 { 812 {
945 #if (SIZEOF_LONG != 4) && (SIZEOF_LONG != 8) 813 char *p;
946 /* Huh? */ 814 int i, len;
947 sprintf (buffer, "%ld", number);
948 #else /* (SIZEOF_LONG == 4) || (SIZEOF_LONG == 8) */
949 char *p = buffer;
950 int force = 0;
951 815
952 if (number < 0) 816 if (number < 0)
953 { 817 {
954 *p++ = '-'; 818 *buffer++ = '-';
955 number = -number; 819 number = -number;
956 } 820 }
957 821 p = buffer;
958 #define FROB(figure) do { \ 822
959 if (force || number >= figure) \ 823 /* Print the digits to the string. */
960 *p++ = number / figure + '0', number %= figure, force = 1; \ 824 do
961 } while (0) 825 {
962 #if SIZEOF_LONG == 8 826 *p++ = number % 10 + '0';
963 FROB (1000000000000000000L); 827 number /= 10;
964 FROB (100000000000000000L); 828 }
965 FROB (10000000000000000L); 829 while (number);
966 FROB (1000000000000000L); 830
967 FROB (100000000000000L); 831 /* And reverse them. */
968 FROB (10000000000000L); 832 len = p - buffer - 1;
969 FROB (1000000000000L); 833 for (i = len / 2; i >= 0; i--)
970 FROB (100000000000L); 834 {
971 FROB (10000000000L); 835 char c = buffer[i];
972 #endif /* SIZEOF_LONG == 8 */ 836 buffer[i] = buffer[len - i];
973 FROB (1000000000); 837 buffer[len - i] = c;
974 FROB (100000000); 838 }
975 FROB (10000000); 839 buffer[len + 1] = '\0';
976 FROB (1000000);
977 FROB (100000);
978 FROB (10000);
979 FROB (1000);
980 FROB (100);
981 FROB (10);
982 #undef FROB
983 *p++ = number + '0';
984 *p = '\0';
985 #endif /* (SIZEOF_LONG == 4) || (SIZEOF_LONG == 8) */
986 } 840 }
987 841
988 static void 842 static void
989 print_vector_internal (const char *start, const char *end, 843 print_vector_internal (CONST char *start, CONST char *end,
990 Lisp_Object obj, 844 Lisp_Object obj,
991 Lisp_Object printcharfun, int escapeflag) 845 Lisp_Object printcharfun, int escapeflag)
992 { 846 {
993 /* This function can GC */ 847 /* This function can GC */
994 int i; 848 int i;
1091 } 945 }
1092 946
1093 void 947 void
1094 print_string (Lisp_Object obj, Lisp_Object printcharfun, int escapeflag) 948 print_string (Lisp_Object obj, Lisp_Object printcharfun, int escapeflag)
1095 { 949 {
1096 Lisp_String *s = XSTRING (obj); 950 struct Lisp_String *s = XSTRING (obj);
1097 /* We distinguish between Bytecounts and Charcounts, to make 951 /* We distinguish between Bytecounts and Charcounts, to make
1098 Vprint_string_length work correctly under Mule. */ 952 Vprint_string_length work correctly under Mule. */
1099 Charcount size = string_char_length (s); 953 Charcount size = string_char_length (s);
1100 Charcount max = size; 954 Charcount max = size;
1101 Bytecount bcmax = string_length (s); 955 Bytecount bcmax = string_length (s);
1240 switch (XTYPE (obj)) 1094 switch (XTYPE (obj))
1241 { 1095 {
1242 case Lisp_Type_Int_Even: 1096 case Lisp_Type_Int_Even:
1243 case Lisp_Type_Int_Odd: 1097 case Lisp_Type_Int_Odd:
1244 { 1098 {
1245 /* ASCII Decimal representation uses 2.4 times as many bits as 1099 char buf[24];
1246 machine binary. */
1247 char buf[3 * sizeof (EMACS_INT) + 5];
1248 long_to_string (buf, XINT (obj)); 1100 long_to_string (buf, XINT (obj));
1249 write_c_string (buf, printcharfun); 1101 write_c_string (buf, printcharfun);
1250 break; 1102 break;
1251 } 1103 }
1252 1104
1255 /* God intended that this be #\..., you know. */ 1107 /* God intended that this be #\..., you know. */
1256 char buf[16]; 1108 char buf[16];
1257 Emchar ch = XCHAR (obj); 1109 Emchar ch = XCHAR (obj);
1258 char *p = buf; 1110 char *p = buf;
1259 *p++ = '?'; 1111 *p++ = '?';
1260 if (ch < 32) 1112 if (ch == '\n')
1113 *p++ = '\\', *p++ = 'n';
1114 else if (ch == '\r')
1115 *p++ = '\\', *p++ = 'r';
1116 else if (ch == '\t')
1117 *p++ = '\\', *p++ = 't';
1118 else if (ch < 32)
1261 { 1119 {
1262 *p++ = '\\'; 1120 *p++ = '\\', *p++ = '^';
1263 switch (ch) 1121 *p++ = ch + 64;
1264 { 1122 if ((ch + 64) == '\\')
1265 case '\t': *p++ = 't'; break; 1123 *p++ = '\\';
1266 case '\n': *p++ = 'n'; break;
1267 case '\r': *p++ = 'r'; break;
1268 default:
1269 *p++ = '^';
1270 *p++ = ch + 64;
1271 if ((ch + 64) == '\\')
1272 *p++ = '\\';
1273 break;
1274 }
1275 }
1276 else if (ch < 127)
1277 {
1278 /* syntactically special characters should be escaped. */
1279 switch (ch)
1280 {
1281 case ' ':
1282 case '"':
1283 case '#':
1284 case '\'':
1285 case '(':
1286 case ')':
1287 case ',':
1288 case '.':
1289 case ';':
1290 case '?':
1291 case '[':
1292 case '\\':
1293 case ']':
1294 case '`':
1295 *p++ = '\\';
1296 }
1297 *p++ = ch;
1298 } 1124 }
1299 else if (ch == 127) 1125 else if (ch == 127)
1300 { 1126 *p++ = '\\', *p++ = '^', *p++ = '?';
1301 *p++ = '\\', *p++ = '^', *p++ = '?'; 1127 else if (ch >= 128 && ch < 160)
1302 }
1303 else if (ch < 160)
1304 { 1128 {
1305 *p++ = '\\', *p++ = '^'; 1129 *p++ = '\\', *p++ = '^';
1306 p += set_charptr_emchar ((Bufbyte *) p, ch + 64); 1130 p += set_charptr_emchar ((Bufbyte *)p, ch + 64);
1307 } 1131 }
1132 else if (ch < 127
1133 && !isdigit (ch)
1134 && !isalpha (ch)
1135 && ch != '^') /* must not backslash this or it will
1136 be interpreted as the start of a
1137 control char */
1138 *p++ = '\\', *p++ = ch;
1308 else 1139 else
1309 { 1140 p += set_charptr_emchar ((Bufbyte *)p, ch);
1310 p += set_charptr_emchar ((Bufbyte *) p, ch); 1141 output_string (printcharfun, (Bufbyte *)buf, Qnil, 0, p - buf);
1311 }
1312
1313 output_string (printcharfun, (Bufbyte *) buf, Qnil, 0, p - buf);
1314
1315 break; 1142 break;
1316 } 1143 }
1317 1144
1318 case Lisp_Type_Record: 1145 case Lisp_Type_Record:
1319 { 1146 {
1384 print_symbol (Lisp_Object obj, Lisp_Object printcharfun, int escapeflag) 1211 print_symbol (Lisp_Object obj, Lisp_Object printcharfun, int escapeflag)
1385 { 1212 {
1386 /* This function can GC */ 1213 /* This function can GC */
1387 /* #### Bug!! (intern "") isn't printed in some distinguished way */ 1214 /* #### Bug!! (intern "") isn't printed in some distinguished way */
1388 /* #### (the reader also loses on it) */ 1215 /* #### (the reader also loses on it) */
1389 Lisp_String *name = symbol_name (XSYMBOL (obj)); 1216 struct Lisp_String *name = symbol_name (XSYMBOL (obj));
1390 Bytecount size = string_length (name); 1217 Bytecount size = string_length (name);
1391 struct gcpro gcpro1, gcpro2; 1218 struct gcpro gcpro1, gcpro2;
1392 1219
1393 if (!escapeflag) 1220 if (!escapeflag)
1394 { 1221 {
1518 alternate-debugging-output @ 429542' -slb */ 1345 alternate-debugging-output @ 429542' -slb */
1519 /* #### Eek! Any clue how to get rid of it? In fact, how about 1346 /* #### Eek! Any clue how to get rid of it? In fact, how about
1520 getting rid of this function altogether? Does anything actually 1347 getting rid of this function altogether? Does anything actually
1521 *use* it? --hniksic */ 1348 *use* it? --hniksic */
1522 1349
1523 static int alternate_do_pointer; 1350 int alternate_do_pointer;
1524 static char alternate_do_string[5000]; 1351 char alternate_do_string[5000];
1525 1352
1526 DEFUN ("alternate-debugging-output", Falternate_debugging_output, 1, 1, 0, /* 1353 DEFUN ("alternate-debugging-output", Falternate_debugging_output, 1, 1, 0, /*
1527 Append CHARACTER to the array `alternate_do_string'. 1354 Append CHARACTER to the array `alternate_do_string'.
1528 This can be used in place of `external-debugging-output' as a function 1355 This can be used in place of `external-debugging-output' as a function
1529 to be passed to `print'. Before calling `print', set `alternate_do_pointer' 1356 to be passed to `print'. Before calling `print', set `alternate_do_pointer'
1532 (character)) 1359 (character))
1533 { 1360 {
1534 Bufbyte str[MAX_EMCHAR_LEN]; 1361 Bufbyte str[MAX_EMCHAR_LEN];
1535 Bytecount len; 1362 Bytecount len;
1536 int extlen; 1363 int extlen;
1537 const Extbyte *extptr; 1364 CONST Extbyte *extptr;
1538 1365
1539 CHECK_CHAR_COERCE_INT (character); 1366 CHECK_CHAR_COERCE_INT (character);
1540 len = set_charptr_emchar (str, XCHAR (character)); 1367 len = set_charptr_emchar (str, XCHAR (character));
1541 TO_EXTERNAL_FORMAT (DATA, (str, len), 1368 GET_CHARPTR_EXT_DATA_ALLOCA (str, len, FORMAT_TERMINAL, extptr, extlen);
1542 ALLOCA, (extptr, extlen),
1543 Qterminal);
1544 memcpy (alternate_do_string + alternate_do_pointer, extptr, extlen); 1369 memcpy (alternate_do_string + alternate_do_pointer, extptr, extlen);
1545 alternate_do_pointer += extlen; 1370 alternate_do_pointer += extlen;
1546 alternate_do_string[alternate_do_pointer] = 0; 1371 alternate_do_string[alternate_do_pointer] = 0;
1547 return character; 1372 return character;
1548 } 1373 }
1551 DEFUN ("external-debugging-output", Fexternal_debugging_output, 1, 3, 0, /* 1376 DEFUN ("external-debugging-output", Fexternal_debugging_output, 1, 3, 0, /*
1552 Write CHAR-OR-STRING to stderr or stdout. 1377 Write CHAR-OR-STRING to stderr or stdout.
1553 If optional arg STDOUT-P is non-nil, write to stdout; otherwise, write 1378 If optional arg STDOUT-P is non-nil, write to stdout; otherwise, write
1554 to stderr. You can use this function to write directly to the terminal. 1379 to stderr. You can use this function to write directly to the terminal.
1555 This function can be used as the STREAM argument of Fprint() or the like. 1380 This function can be used as the STREAM argument of Fprint() or the like.
1556
1557 Under MS Windows, this writes output to the console window (which is
1558 created, if necessary), unless XEmacs is being run noninteractively
1559 \(i.e. using the `-batch' argument).
1560 1381
1561 If you have opened a termscript file (using `open-termscript'), then 1382 If you have opened a termscript file (using `open-termscript'), then
1562 the output also will be logged to this file. 1383 the output also will be logged to this file.
1563 */ 1384 */
1564 (char_or_string, stdout_p, device)) 1385 (char_or_string, stdout_p, device))
1590 1411
1591 if (STRINGP (char_or_string)) 1412 if (STRINGP (char_or_string))
1592 write_string_to_stdio_stream (file, con, 1413 write_string_to_stdio_stream (file, con,
1593 XSTRING_DATA (char_or_string), 1414 XSTRING_DATA (char_or_string),
1594 0, XSTRING_LENGTH (char_or_string), 1415 0, XSTRING_LENGTH (char_or_string),
1595 Qterminal, 1); 1416 FORMAT_TERMINAL);
1596 else 1417 else
1597 { 1418 {
1598 Bufbyte str[MAX_EMCHAR_LEN]; 1419 Bufbyte str[MAX_EMCHAR_LEN];
1599 Bytecount len; 1420 Bytecount len;
1600 1421
1601 CHECK_CHAR_COERCE_INT (char_or_string); 1422 CHECK_CHAR_COERCE_INT (char_or_string);
1602 len = set_charptr_emchar (str, XCHAR (char_or_string)); 1423 len = set_charptr_emchar (str, XCHAR (char_or_string));
1603 write_string_to_stdio_stream (file, con, str, 0, len, Qterminal, 1); 1424 write_string_to_stdio_stream (file, con, str, 0, len, FORMAT_TERMINAL);
1604 } 1425 }
1605 1426
1606 return char_or_string; 1427 return char_or_string;
1607 } 1428 }
1608 1429
1627 return Qnil; 1448 return Qnil;
1628 } 1449 }
1629 1450
1630 #if 1 1451 #if 1
1631 /* Debugging kludge -- unbuffered */ 1452 /* Debugging kludge -- unbuffered */
1632 static int debug_print_length = 50; 1453 static int debug_print_length = 50;
1633 static int debug_print_level = 15; 1454 static int debug_print_level = 15;
1634 static int debug_print_readably = -1; 1455 Lisp_Object debug_temp;
1635 1456
1636 static void 1457 static void
1637 debug_print_no_newline (Lisp_Object debug_print_obj) 1458 debug_print_no_newline (Lisp_Object debug_print_obj)
1638 { 1459 {
1639 /* This function can GC */ 1460 /* This function can GC */
1640 int save_print_readably = print_readably; 1461 int old_print_readably = print_readably;
1641 int save_print_depth = print_depth; 1462 int old_print_depth = print_depth;
1642 Lisp_Object save_Vprint_length = Vprint_length; 1463 Lisp_Object old_print_length = Vprint_length;
1643 Lisp_Object save_Vprint_level = Vprint_level; 1464 Lisp_Object old_print_level = Vprint_level;
1644 Lisp_Object save_Vinhibit_quit = Vinhibit_quit; 1465 Lisp_Object old_inhibit_quit = Vinhibit_quit;
1645 struct gcpro gcpro1, gcpro2, gcpro3;
1646 GCPRO3 (save_Vprint_level, save_Vprint_length, save_Vinhibit_quit);
1647
1648 if (gc_in_progress)
1649 stderr_out ("** gc-in-progress! Bad idea to print anything! **\n");
1650
1651 print_depth = 0;
1652 print_readably = debug_print_readably != -1 ? debug_print_readably : 0;
1653 print_unbuffered++;
1654 /* Could use unwind-protect, but why bother? */
1655 if (debug_print_length > 0)
1656 Vprint_length = make_int (debug_print_length);
1657 if (debug_print_level > 0)
1658 Vprint_level = make_int (debug_print_level);
1659
1660 print_internal (debug_print_obj, Qexternal_debugging_output, 1);
1661
1662 Vinhibit_quit = save_Vinhibit_quit;
1663 Vprint_level = save_Vprint_level;
1664 Vprint_length = save_Vprint_length;
1665 print_depth = save_print_depth;
1666 print_readably = save_print_readably;
1667 print_unbuffered--;
1668 UNGCPRO;
1669 }
1670
1671 void
1672 debug_print (Lisp_Object debug_print_obj)
1673 {
1674 debug_print_no_newline (debug_print_obj);
1675 stderr_out ("\n");
1676 }
1677
1678 /* Debugging kludge -- unbuffered */
1679 /* This function provided for the benefit of the debugger. */
1680 void debug_backtrace (void);
1681 void
1682 debug_backtrace (void)
1683 {
1684 /* This function can GC */
1685 int old_print_readably = print_readably;
1686 int old_print_depth = print_depth;
1687 Lisp_Object old_print_length = Vprint_length;
1688 Lisp_Object old_print_level = Vprint_level;
1689 Lisp_Object old_inhibit_quit = Vinhibit_quit;
1690
1691 struct gcpro gcpro1, gcpro2, gcpro3; 1466 struct gcpro gcpro1, gcpro2, gcpro3;
1692 GCPRO3 (old_print_level, old_print_length, old_inhibit_quit); 1467 GCPRO3 (old_print_level, old_print_length, old_inhibit_quit);
1693 1468
1694 if (gc_in_progress) 1469 if (gc_in_progress)
1695 stderr_out ("** gc-in-progress! Bad idea to print anything! **\n"); 1470 stderr_out ("** gc-in-progress! Bad idea to print anything! **\n");
1700 /* Could use unwind-protect, but why bother? */ 1475 /* Could use unwind-protect, but why bother? */
1701 if (debug_print_length > 0) 1476 if (debug_print_length > 0)
1702 Vprint_length = make_int (debug_print_length); 1477 Vprint_length = make_int (debug_print_length);
1703 if (debug_print_level > 0) 1478 if (debug_print_level > 0)
1704 Vprint_level = make_int (debug_print_level); 1479 Vprint_level = make_int (debug_print_level);
1480 print_internal (debug_print_obj, Qexternal_debugging_output, 1);
1481 Vinhibit_quit = old_inhibit_quit;
1482 Vprint_level = old_print_level;
1483 Vprint_length = old_print_length;
1484 print_depth = old_print_depth;
1485 print_readably = old_print_readably;
1486 print_unbuffered--;
1487 UNGCPRO;
1488 }
1489
1490 void
1491 debug_print (Lisp_Object debug_print_obj)
1492 {
1493 debug_print_no_newline (debug_print_obj);
1494 stderr_out ("\n");
1495 fflush (stderr);
1496 }
1497
1498 /* Debugging kludge -- unbuffered */
1499 /* This function provided for the benefit of the debugger. */
1500 void debug_backtrace (void);
1501 void
1502 debug_backtrace (void)
1503 {
1504 /* This function can GC */
1505 int old_print_readably = print_readably;
1506 int old_print_depth = print_depth;
1507 Lisp_Object old_print_length = Vprint_length;
1508 Lisp_Object old_print_level = Vprint_level;
1509 Lisp_Object old_inhibit_quit = Vinhibit_quit;
1510
1511 struct gcpro gcpro1, gcpro2, gcpro3;
1512 GCPRO3 (old_print_level, old_print_length, old_inhibit_quit);
1513
1514 if (gc_in_progress)
1515 stderr_out ("** gc-in-progress! Bad idea to print anything! **\n");
1516
1517 print_depth = 0;
1518 print_readably = 0;
1519 print_unbuffered++;
1520 /* Could use unwind-protect, but why bother? */
1521 if (debug_print_length > 0)
1522 Vprint_length = make_int (debug_print_length);
1523 if (debug_print_level > 0)
1524 Vprint_level = make_int (debug_print_level);
1705 1525
1706 Fbacktrace (Qexternal_debugging_output, Qt); 1526 Fbacktrace (Qexternal_debugging_output, Qt);
1707 stderr_out ("\n"); 1527 stderr_out ("\n");
1528 fflush (stderr);
1708 1529
1709 Vinhibit_quit = old_inhibit_quit; 1530 Vinhibit_quit = old_inhibit_quit;
1710 Vprint_level = old_print_level; 1531 Vprint_level = old_print_level;
1711 Vprint_length = old_print_length; 1532 Vprint_length = old_print_length;
1712 print_depth = old_print_depth; 1533 print_depth = old_print_depth;
1720 debug_short_backtrace (int length) 1541 debug_short_backtrace (int length)
1721 { 1542 {
1722 int first = 1; 1543 int first = 1;
1723 struct backtrace *bt = backtrace_list; 1544 struct backtrace *bt = backtrace_list;
1724 stderr_out (" ["); 1545 stderr_out (" [");
1546 fflush (stderr);
1725 while (length > 0 && bt) 1547 while (length > 0 && bt)
1726 { 1548 {
1727 if (!first) 1549 if (!first)
1728 { 1550 {
1729 stderr_out (", "); 1551 stderr_out (", ");
1552 fflush (stderr);
1730 } 1553 }
1731 if (COMPILED_FUNCTIONP (*bt->function)) 1554 if (COMPILED_FUNCTIONP (*bt->function))
1732 { 1555 {
1733 #if defined(COMPILED_FUNCTION_ANNOTATION_HACK) 1556 #if defined(COMPILED_FUNCTION_ANNOTATION_HACK)
1734 Lisp_Object ann = 1557 Lisp_Object ann =
1737 Lisp_Object ann = Qnil; 1560 Lisp_Object ann = Qnil;
1738 #endif 1561 #endif
1739 if (!NILP (ann)) 1562 if (!NILP (ann))
1740 { 1563 {
1741 stderr_out ("<compiled-function from "); 1564 stderr_out ("<compiled-function from ");
1565 fflush (stderr);
1742 debug_print_no_newline (ann); 1566 debug_print_no_newline (ann);
1743 stderr_out (">"); 1567 stderr_out (">");
1568 fflush (stderr);
1744 } 1569 }
1745 else 1570 else
1746 { 1571 {
1747 stderr_out ("<compiled-function of unknown origin>"); 1572 stderr_out ("<compiled-function of unknown origin>");
1573 fflush (stderr);
1748 } 1574 }
1749 } 1575 }
1750 else 1576 else
1751 debug_print_no_newline (*bt->function); 1577 debug_print_no_newline (*bt->function);
1752 first = 0; 1578 first = 0;
1753 length--; 1579 length--;
1754 bt = bt->next; 1580 bt = bt->next;
1755 } 1581 }
1756 stderr_out ("]\n"); 1582 stderr_out ("]\n");
1583 fflush (stderr);
1757 } 1584 }
1758 1585
1759 #endif /* debugging kludge */ 1586 #endif /* debugging kludge */
1760 1587
1761 1588
1762 void 1589 void
1763 syms_of_print (void) 1590 syms_of_print (void)
1764 { 1591 {
1592 defsymbol (&Qprint_escape_newlines, "print-escape-newlines");
1593 defsymbol (&Qprint_readably, "print-readably");
1594
1765 defsymbol (&Qstandard_output, "standard-output"); 1595 defsymbol (&Qstandard_output, "standard-output");
1596
1597 #ifdef LISP_FLOAT_TYPE
1598 defsymbol (&Qfloat_output_format, "float-output-format");
1599 #endif
1766 1600
1767 defsymbol (&Qprint_length, "print-length"); 1601 defsymbol (&Qprint_length, "print-length");
1768 1602
1769 defsymbol (&Qprint_string_length, "print-string-length"); 1603 defsymbol (&Qprint_string_length, "print-string-length");
1770 1604
1778 DEFSUBR (Ferror_message_string); 1612 DEFSUBR (Ferror_message_string);
1779 DEFSUBR (Fdisplay_error); 1613 DEFSUBR (Fdisplay_error);
1780 DEFSUBR (Fterpri); 1614 DEFSUBR (Fterpri);
1781 DEFSUBR (Fwrite_char); 1615 DEFSUBR (Fwrite_char);
1782 DEFSUBR (Falternate_debugging_output); 1616 DEFSUBR (Falternate_debugging_output);
1617 defsymbol (&Qalternate_debugging_output, "alternate-debugging-output");
1783 DEFSUBR (Fexternal_debugging_output); 1618 DEFSUBR (Fexternal_debugging_output);
1784 DEFSUBR (Fopen_termscript); 1619 DEFSUBR (Fopen_termscript);
1785 defsymbol (&Qexternal_debugging_output, "external-debugging-output"); 1620 defsymbol (&Qexternal_debugging_output, "external-debugging-output");
1786 DEFSUBR (Fwith_output_to_temp_buffer); 1621 DEFSUBR (Fwith_output_to_temp_buffer);
1787 } 1622 }
1788 1623
1789 void 1624 void
1790 reinit_vars_of_print (void) 1625 vars_of_print (void)
1791 { 1626 {
1792 alternate_do_pointer = 0; 1627 alternate_do_pointer = 0;
1793 }
1794
1795 void
1796 vars_of_print (void)
1797 {
1798 reinit_vars_of_print ();
1799 1628
1800 DEFVAR_LISP ("standard-output", &Vstandard_output /* 1629 DEFVAR_LISP ("standard-output", &Vstandard_output /*
1801 Output stream `print' uses by default for outputting a character. 1630 Output stream `print' uses by default for outputting a character.
1802 This may be any function of one argument. 1631 This may be any function of one argument.
1803 It may also be a buffer (output is inserted before point) 1632 It may also be a buffer (output is inserted before point)