home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / lucid / lemacs-19.6 / src / print.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-12-13  |  32.5 KB  |  1,206 lines

  1. /* Lisp object printing and output streams.
  2.    Copyright (C) 1985, 1986, 1988, 1992 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU Emacs.
  5.  
  6. GNU Emacs is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2, or (at your option)
  9. any later version.
  10.  
  11. GNU Emacs is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GNU Emacs; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20.  
  21. #include "config.h"
  22. #include <stdio.h>
  23.  
  24. #include "lisp.h"
  25.  
  26. #ifndef standalone
  27. #include "buffer.h"
  28. #include "extents.h"
  29. #include "screen.h"
  30. #include "window.h"
  31. #include "process.h"
  32. #include "events.h"
  33. #include "keymap.h"
  34. #include "insdel.h"
  35.  
  36. #endif /* not standalone */
  37.  
  38. Lisp_Object Vstandard_output, Qstandard_output;
  39.  
  40. /* The subroutine object for external-debugging-output is kept here
  41.    for the convenience of the debugger.  */
  42. Lisp_Object Qexternal_debugging_output;
  43.  
  44. #ifdef LISP_FLOAT_TYPE
  45. Lisp_Object Vfloat_output_format, Qfloat_output_format;
  46. #endif /* LISP_FLOAT_TYPE */
  47.  
  48. /* Avoid actual stack overflow in print.  */
  49. int print_depth;
  50.  
  51. /* Maximum length of list to print in full; noninteger means
  52.    effectively infinity */
  53.  
  54. Lisp_Object Vprint_length;
  55.  
  56. /* Maximum depth of list to print in full; noninteger means
  57.    effectively infinity.  */
  58.  
  59. Lisp_Object Vprint_level;
  60.  
  61. /* Nonzero means print newlines in strings as \n.  */
  62.  
  63. int print_escape_newlines;
  64. int print_readably;
  65. int print_gensym;
  66.  
  67. Lisp_Object Qprint_escape_newlines;
  68. Lisp_Object Qprint_readably;
  69.  
  70. /* Nonzero means print newline before next minibuffer message.
  71.    Defined in xdisp.c */
  72.  
  73. extern int noninteractive_need_newline;
  74.  
  75. /* These are only really used if #ifdef MAX_PRINT_CHARS */
  76. static int max_print;
  77. static int print_chars;
  78.  
  79.  
  80. #define PRINTER_BUFFER_SIZE 4096
  81. static char *printer_buffer;
  82. static char *printer_buffer_fill;
  83. static char *printer_buffer_end;
  84.  
  85.  
  86. /* Low level output routines for charaters and strings */
  87.  
  88. /* Lisp functions to do output using a stream
  89.  must have the stream in a variable called printcharfun
  90.  and must start with PRINTPREPARE and end with PRINTFINISH.
  91.  Use PRINTCHAR to output one character,
  92.  or call strout to output a block of characters.
  93.  Also, each one must have the declarations
  94.    struct buffer *old = current_buffer;
  95.    int old_point = -1, start_point;
  96.    Lisp_Object original;
  97. */ 
  98.  
  99. #define PRINTPREPARE \
  100.    count = specpdl_depth; \
  101.    printer_buffer = (char *) alloca (PRINTER_BUFFER_SIZE); \
  102.    printer_buffer_fill = printer_buffer; \
  103.    printer_buffer_end = printer_buffer + PRINTER_BUFFER_SIZE; \
  104.    original = printcharfun; \
  105.    if (NILP (printcharfun)) printcharfun = Qt; \
  106.    if (BUFFERP (printcharfun)) \
  107.      { record_unwind_protect (Fset_buffer, Fcurrent_buffer ()); \
  108.        if (XBUFFER (printcharfun) != current_buffer) \
  109.      Fset_buffer (printcharfun); \
  110.        printcharfun = Qnil;}\
  111.    if (MARKERP (printcharfun)) \
  112.      { if (XMARKER (original)->buffer != current_buffer) \
  113.          internal_set_buffer (XMARKER (original)->buffer); \
  114.        old_point = point; \
  115.        SET_PT (marker_position (printcharfun)); \
  116.        start_point = point; \
  117.        printcharfun = Qnil;}
  118.  
  119. #define PRINTFINISH \
  120.    dump_printer_buffer (); \
  121.    if (MARKERP (original)) \
  122.      Fset_marker (original, make_number (point), Qnil); \
  123.    if (old_point >= 0) \
  124.      SET_PT ((old_point >= start_point ? point - start_point : 0) + old_point); \
  125.    unbind_to (count, Qnil); \
  126.    if (old != current_buffer) \
  127.      abort()
  128.  
  129. #define PRINTCHAR(ch) printchar (ch, printcharfun)
  130.  
  131. static void
  132. insert_in_printer_buffer (string, char_count)
  133.      const char *string;
  134.      int char_count;
  135. {
  136.   char *new_end;
  137.  
  138.    while ((new_end = printer_buffer_fill + char_count) > printer_buffer_end)
  139.      {
  140.        int this_char_count = char_count - (new_end - printer_buffer_end);
  141.  
  142.        memcpy (printer_buffer_fill, string, this_char_count);
  143.        insert_raw_string (printer_buffer, printer_buffer_end - printer_buffer);
  144.        printer_buffer_fill = printer_buffer;
  145.        string += this_char_count;
  146.        char_count -= this_char_count;
  147.      }
  148.   memcpy (printer_buffer_fill, string, char_count);
  149.   printer_buffer_fill += char_count;
  150. }
  151.  
  152. static void
  153. dump_printer_buffer ()
  154. {
  155.   insert_raw_string (printer_buffer, printer_buffer_fill - printer_buffer);
  156.   printer_buffer_fill = printer_buffer;
  157. }
  158.        
  159.   
  160.  
  161. /* Index of first unused element of above */
  162. static int printbufidx;
  163.  
  164. static void
  165. printchar (ch, fun)
  166.      unsigned char ch;
  167.      Lisp_Object fun;
  168. {
  169.   Lisp_Object ch1;
  170.  
  171.   if (max_print != 0)
  172.     print_chars++;
  173.  
  174. #ifndef standalone
  175.   if (EQ (fun, Qnil))
  176.     {
  177.       QUIT;
  178.       insert_in_printer_buffer ((char *) &ch, 1);
  179.       return;
  180.     }
  181.  
  182.   if (EQ (fun, Qt))
  183.     {
  184.       if (noninteractive)
  185.     {
  186.       putchar (ch);
  187.       noninteractive_need_newline = 1;
  188.       return;
  189.     }
  190.  
  191.       if (echo_area_glyphs != SCREEN_MESSAGE_BUF (selected_screen))
  192.     {
  193.       echo_area_glyphs = SCREEN_MESSAGE_BUF (selected_screen);
  194.       printbufidx = 0;
  195.     }
  196.  
  197.       if (! SCREEN_MESSAGE_BUF(selected_screen))
  198.     abort ();
  199.       if (printbufidx < SCREEN_WIDTH (selected_screen) - 1)
  200.     SCREEN_MESSAGE_BUF (selected_screen)[printbufidx++] = ch;
  201.       SCREEN_MESSAGE_BUF (selected_screen)[printbufidx] = 0;
  202.  
  203.       return;
  204.     }
  205. #endif /* not standalone */
  206.  
  207.   XFASTINT (ch1) = ch;
  208.   call1 (fun, ch1);
  209. }
  210.  
  211. static void
  212. strout (ptr, size, printcharfun)
  213.      const char *ptr;
  214.      int size;
  215.      Lisp_Object printcharfun;
  216. {
  217.   int i = 0;
  218.  
  219.   if (EQ (printcharfun, Qnil))
  220.     {
  221.       insert_in_printer_buffer (ptr, size >= 0 ? size : strlen (ptr));
  222.       if (max_print != 0)
  223.         print_chars += ((size >= 0) ? size : strlen(ptr));
  224.       return;
  225.     }
  226.   if (EQ (printcharfun, Qt))
  227.     {
  228.       i = size >= 0 ? size : strlen (ptr);
  229.       if (max_print != 0)
  230.         print_chars += i;
  231.  
  232.       if (noninteractive)
  233.     {
  234.       fwrite (ptr, 1, i, stderr);
  235.       noninteractive_need_newline = 1;
  236.       return;
  237.     }
  238.  
  239.       if (echo_area_glyphs != SCREEN_MESSAGE_BUF (selected_screen))
  240.     {
  241.       echo_area_glyphs = SCREEN_MESSAGE_BUF (selected_screen);
  242.       printbufidx = 0;
  243.     }
  244.  
  245.       if (i > SCREEN_WIDTH (selected_screen) - printbufidx - 1)
  246.     i = SCREEN_WIDTH (selected_screen) - printbufidx - 1;
  247.       memcpy (&SCREEN_MESSAGE_BUF (selected_screen) [printbufidx], ptr, i);
  248.       printbufidx += i;
  249.       SCREEN_MESSAGE_BUF (selected_screen) [printbufidx] = 0;
  250.  
  251.       return;
  252.     }
  253.  
  254.   if (size >= 0)
  255.     while (i < size)
  256.       PRINTCHAR (ptr[i++]);
  257.   else
  258.     while (ptr[i])
  259.       PRINTCHAR (ptr[i++]);
  260. }
  261.  
  262. DEFUN ("write-char", Fwrite_char, Swrite_char, 1, 2, 0,
  263.   "Output character CHAR to stream STREAM.\n\
  264. STREAM defaults to the value of `standard-output' (which see).")
  265.   (ch, printcharfun)
  266.      Lisp_Object ch, printcharfun;
  267. {
  268.   struct buffer *old = current_buffer;
  269.   int old_point = -1;
  270.   int start_point, count;
  271.   Lisp_Object original;
  272.  
  273.   if (NILP (printcharfun))
  274.     printcharfun = Vstandard_output;
  275.   CHECK_FIXNUM (ch, 0);
  276.   PRINTPREPARE;
  277.   PRINTCHAR (XINT (ch));
  278.   PRINTFINISH;
  279.   return ch;
  280. }
  281.  
  282. void
  283. write_string (data, size)
  284.      const char *data;
  285.      int size;
  286. {
  287.   struct buffer *old = current_buffer;
  288.   Lisp_Object printcharfun;
  289.   int old_point = -1;
  290.   int start_point, count;
  291.   Lisp_Object original;
  292.  
  293.   printcharfun = Vstandard_output;
  294.  
  295.   PRINTPREPARE;
  296.   strout (data, size, printcharfun);
  297.   PRINTFINISH;
  298. }
  299.  
  300. void
  301. write_string_1 (data, size, printcharfun)
  302.      char *data;
  303.      int size;
  304.      Lisp_Object printcharfun;
  305. {
  306.   struct buffer *old = current_buffer;
  307.   int old_point = -1;
  308.   int start_point, count;
  309.   Lisp_Object original;
  310.  
  311.   PRINTPREPARE;
  312.   strout (data, size, printcharfun);
  313.   PRINTFINISH;
  314. }
  315.  
  316.  
  317. #ifndef standalone
  318.  
  319. void
  320. temp_output_buffer_setup (bufname)
  321.     const char *bufname;
  322. {
  323.   register struct buffer *old = current_buffer;
  324.   register Lisp_Object buf;
  325.  
  326.   Fset_buffer (Fget_buffer_create (build_string (bufname)));
  327.  
  328.   current_buffer->read_only = Qnil;
  329.   Ferase_buffer ();
  330.  
  331.   XSET (buf, Lisp_Buffer, current_buffer);
  332.   specbind (Qstandard_output, buf);
  333.  
  334.   internal_set_buffer (old);
  335. }
  336.  
  337. Lisp_Object
  338. internal_with_output_to_temp_buffer (bufname, function, args, same_screen)
  339.      const char *bufname;
  340.      Lisp_Object (*function) ();
  341.      Lisp_Object args;
  342.      Lisp_Object same_screen;
  343. {
  344.   int count = specpdl_depth;
  345.   Lisp_Object buf, val;
  346.  
  347.   temp_output_buffer_setup (bufname);
  348.   buf = Vstandard_output;
  349.  
  350.   val = (*function) (args);
  351.  
  352.   temp_output_buffer_show (buf, same_screen);
  353.  
  354.   return unbind_to (count, val);
  355. }
  356.  
  357. DEFUN ("with-output-to-temp-buffer", Fwith_output_to_temp_buffer, Swith_output_to_temp_buffer,
  358.        1, UNEVALLED, 0,
  359.   "Bind `standard-output' to buffer BUFNAME, eval BODY, then show that buffer.\n\
  360. The buffer is cleared out initially, and marked as unmodified when done.\n\
  361. All output done by BODY is inserted in that buffer by default.\n\
  362. The buffer is displayed in another window, but not selected.\n\
  363. The value of the last form in BODY is returned.\n\
  364. If BODY does not finish normally, the buffer BUFNAME is not displayed.\n\n\
  365. If variable `temp-buffer-show-function' is non-nil, call it at the end\n\
  366. to get the buffer displayed.  It gets one argument, the buffer to display.")
  367.   (args)
  368.      Lisp_Object args;
  369. {
  370.   struct gcpro gcpro1;
  371.   Lisp_Object name;
  372.   int count = specpdl_depth;
  373.   Lisp_Object buf, val;
  374.  
  375.   GCPRO1(args);
  376.   name = Feval (Fcar (args));
  377.   UNGCPRO;
  378.  
  379.   CHECK_STRING (name, 0);
  380.   temp_output_buffer_setup ((char *) XSTRING (name)->data);
  381.   buf = Vstandard_output;
  382.  
  383.   val = Fprogn (Fcdr (args));
  384.  
  385.   temp_output_buffer_show (buf, Qnil);
  386.  
  387.   return unbind_to (count, val);
  388. }
  389. #endif /* not standalone */
  390.  
  391. static void print ();
  392.  
  393. DEFUN ("terpri", Fterpri, Sterpri, 0, 1, 0,
  394.   "Output a newline to STREAM.\n\
  395. If STREAM is omitted or nil, the value of `standard-output' is used.")
  396.   (printcharfun)
  397.      Lisp_Object printcharfun;
  398. {
  399.   struct buffer *old = current_buffer;
  400.   int old_point = -1;
  401.   int start_point, count;
  402.   Lisp_Object original;
  403.  
  404.   if (NILP (printcharfun))
  405.     printcharfun = Vstandard_output;
  406.   PRINTPREPARE;
  407.   PRINTCHAR ('\n');
  408.   PRINTFINISH;
  409.   return Qt;
  410. }
  411.  
  412. DEFUN ("prin1", Fprin1, Sprin1, 1, 2, 0,
  413.   "Output the printed representation of OBJECT, any Lisp object.\n\
  414. Quoting characters are printed when needed to make output that `read'\n\
  415. can handle, whenever this is possible.\n\
  416. Output stream is STREAM, or value of `standard-output' (which see).")
  417.   (obj, printcharfun)
  418.      Lisp_Object obj, printcharfun;
  419. {
  420.   struct buffer *old = current_buffer;
  421.   int old_point = -1;
  422.   int start_point, count;
  423.   Lisp_Object original;
  424.  
  425.   max_print = 0;
  426.   if (NILP (printcharfun))
  427.     printcharfun = Vstandard_output;
  428.   PRINTPREPARE;
  429.   print_depth = 0;
  430.   print (obj, printcharfun, 1);
  431.   PRINTFINISH;
  432.   return obj;
  433. }
  434.  
  435. /* a buffer which is used to hold output being built by prin1-to-string */
  436. Lisp_Object Vprin1_to_string_buffer;
  437.  
  438. DEFUN ("prin1-to-string", Fprin1_to_string, Sprin1_to_string, 1, 2, 0,
  439.   "Return a string containing the printed representation of OBJECT,\n\
  440. any Lisp object.  Quoting characters are used when needed to make output\n\
  441. that `read' can handle, whenever this is possible, unless the optional\n\
  442. second argument NOESCAPE is non-nil.")
  443.   (obj, noescape)
  444.      Lisp_Object obj, noescape;
  445. {
  446.   struct buffer *old = current_buffer;
  447.   int old_point = -1;
  448.   int start_point, count;
  449.   Lisp_Object original, printcharfun;
  450.  
  451.   /* erase buffer in case previous prin1-to-string terminated in error */
  452.   internal_set_buffer (XBUFFER (Vprin1_to_string_buffer));
  453.   Ferase_buffer ();
  454.   internal_set_buffer (old);
  455.  
  456.   printcharfun = Vprin1_to_string_buffer;
  457.   PRINTPREPARE;
  458.   print_depth = 0;
  459.   print (obj, printcharfun, NILP (noescape));
  460.   PRINTFINISH;
  461.   {
  462.     struct gcpro gcpro1;
  463.     internal_set_buffer (XBUFFER (Vprin1_to_string_buffer));
  464.     GCPRO1 (obj);
  465.     obj = Fbuffer_string ();
  466.     Ferase_buffer ();
  467.     internal_set_buffer (old);
  468.     UNGCPRO;
  469.   }
  470.   return obj;
  471. }
  472.  
  473. DEFUN ("princ", Fprinc, Sprinc, 1, 2, 0,
  474.   "Output the printed representation of OBJECT, any Lisp object.\n\
  475. No quoting characters are used; no delimiters are printed around\n\
  476. the contents of strings.\n\
  477. Output stream is STREAM, or value of standard-output (which see).")
  478.   (obj, printcharfun)
  479.      Lisp_Object obj, printcharfun;
  480. {
  481.   struct buffer *old = current_buffer;
  482.   int old_point = -1;
  483.   int start_point, count;
  484.   Lisp_Object original;
  485.  
  486.   if (NILP (printcharfun))
  487.     printcharfun = Vstandard_output;
  488.   PRINTPREPARE;
  489.   print_depth = 0;
  490.   print (obj, printcharfun, 0);
  491.   PRINTFINISH;
  492.   return obj;
  493. }
  494.  
  495. DEFUN ("print", Fprint, Sprint, 1, 2, 0,
  496.   "Output the printed representation of OBJECT, with newlines around it.\n\
  497. Quoting characters are printed when needed to make output that `read'\n\
  498. can handle, whenever this is possible.\n\
  499. Output stream is STREAM, or value of `standard-output' (which see).")
  500.   (obj, printcharfun)
  501.      Lisp_Object obj, printcharfun;
  502. {
  503.   struct buffer *old = current_buffer;
  504.   int old_point = -1;
  505.   int start_point, count;
  506.   Lisp_Object original;
  507.  
  508.   print_chars = 0;
  509. #ifdef MAX_PRINT_CHARS
  510.   max_print = MAX_PRINT_CHARS;
  511. #endif
  512.  
  513.   if (NILP (printcharfun))
  514.     printcharfun = Vstandard_output;
  515.   PRINTPREPARE;
  516.   print_depth = 0;
  517.   PRINTCHAR ('\n');
  518.   print (obj, printcharfun, 1);
  519.   PRINTCHAR ('\n');
  520.   PRINTFINISH;
  521.   max_print = 0;
  522.   print_chars = 0;
  523.   return obj;
  524. }
  525.  
  526. #ifdef LISP_FLOAT_TYPE
  527.  
  528. void
  529. float_to_string (buf, data)
  530.      char *buf;
  531. /*
  532.  * This buffer should be at least as large as the max string size of the
  533.  * largest float, printed in the biggest notation.  This is undoubtably
  534.  * 20d float_output_format, with the negative of the C-constant "HUGE"
  535.  * from <math.h>.
  536.  * 
  537.  * On the vax the worst case is -1e38 in 20d format which takes 61 bytes.
  538.  * 
  539.  * I assume that IEEE-754 format numbers can take 329 bytes for the worst
  540.  * case of -1e307 in 20d float_output_format. What is one to do (short of
  541.  * re-writing _doprnt to be more sane)?
  542.  *             -wsr
  543.  */
  544.      double data;
  545. {
  546.   register unsigned char *cp, c;
  547.   register int width;
  548.       
  549.   if (NILP (Vfloat_output_format)
  550.       || !STRINGP (Vfloat_output_format))
  551.   lose:
  552.     sprintf (buf, "%.16g", data);
  553.   else            /* oink oink */
  554.     {
  555.       /* Check that the spec we have is fully valid.
  556.      This means not only valid for printf,
  557.      but meant for floats, and reasonable.  */
  558.       cp = XSTRING (Vfloat_output_format)->data;
  559.  
  560.       if (cp[0] != '%')
  561.     goto lose;
  562.       if (cp[1] != '.')
  563.     goto lose;
  564.  
  565.       cp += 2;
  566.       for (width = 0;
  567.        ((c = *cp) >= '0' && c <= '9');
  568.        cp++)
  569.     {
  570.       width *= 10;
  571.       width += c - '0';
  572.     }
  573.  
  574.       if (*cp != 'e' && *cp != 'f' && *cp != 'g')
  575.     goto lose;
  576.  
  577.       if (width < (*cp != 'e') || width > DBL_DIG)
  578.     goto lose;
  579.  
  580.       if (cp[1] != 0)
  581.     goto lose;
  582.  
  583.       sprintf (buf, (char *)XSTRING (Vfloat_output_format)->data, data);
  584.     }
  585.  
  586.   /* added by jwz: don't allow "1.0" to print as "1"; that destroys
  587.      the read-equivalence of lisp objects.  (* x 1) and (* x 1.0) do
  588.      not do the same thing, so it's important that the printed
  589.      representation of that form not be corrupted by the printer.
  590.    */
  591.   {
  592.     register char *s = buf;
  593.     if (*s == '-') s++;
  594.     for (; *s; s++)
  595.       /* if there's a non-digit, then there is a decimal point, or
  596.      it's in exponential notation, both of which are ok. */
  597.       if (*s < '0' || *s > '9')
  598.     goto DONE;
  599.     /* otherwise, we need to hack it. */
  600.     *s++ = '.';
  601.     *s++ = '0';
  602.     *s = 0;
  603.   }
  604.  DONE:
  605.  
  606.   /* Some machines print "0.4" as ".4".  I don't like that. */
  607.   if (buf [0] == '.' || (buf [0] == '-' && buf [1] == '.'))
  608.     {
  609.       int i;
  610.       for (i = strlen (buf) + 1; i >= 0; i--)
  611.     buf [i+1] = buf [i];
  612.       buf [(buf [0] == '-' ? 1 : 0)] = '0';
  613.     }
  614. }
  615. #endif /* LISP_FLOAT_TYPE */
  616.  
  617. static void
  618. print (obj, printcharfun, escapeflag)
  619. #ifndef RTPC_REGISTER_BUG
  620.      register Lisp_Object obj;
  621. #else
  622.      Lisp_Object obj;
  623. #endif
  624.      register Lisp_Object printcharfun;
  625.      int escapeflag;
  626. {
  627.   char buf[30];
  628.  
  629.   QUIT;
  630.  
  631.   if (noninteractive && EQ (printcharfun, Qt))
  632.     printcharfun = Qexternal_debugging_output;
  633.  
  634.   if (print_depth == 0 && EQ (printcharfun, Qt))
  635.     printbufidx = strlen (SCREEN_MESSAGE_BUF (selected_screen));
  636.  
  637.   print_depth++;
  638.  
  639.   if (print_depth > 200)
  640.     error ("Apparently circular structure being printed");
  641.  
  642. #ifdef MAX_PRINT_CHARS
  643.   if (max_print != 0 && print_chars > max_print)
  644.     {
  645.       PRINTCHAR ('\n');
  646.       print_chars = 0;
  647.     }
  648. #endif /* MAX_PRINT_CHARS */
  649.  
  650. #ifdef SWITCH_ENUM_BUG
  651.   switch ((int) XTYPE (obj))
  652. #else
  653.   switch (XTYPE (obj))
  654. #endif
  655.     {
  656.     default:
  657.       if (print_readably)
  658.         error ("printing illegal data type #o%3o", (int) XTYPE (obj));
  659.  
  660.       /* We're in trouble if this happens!
  661.      Probably should just abort () */
  662.       strout ("#<EMACS BUG: ILLEGAL DATATYPE ", -1, printcharfun);
  663.       sprintf (buf, "(#o%3o)", (int) XTYPE (obj));
  664.       strout (buf, -1, printcharfun);
  665.       strout (" Save your buffers immediately and please report this bug>",
  666.           -1, printcharfun);
  667.       break;
  668.  
  669. #ifdef LISP_FLOAT_TYPE
  670.     case Lisp_Float:
  671.       {
  672.     char pigbuf[350];    /* see comments in float_to_string */
  673.  
  674.     float_to_string (pigbuf, XFLOAT(obj)->data);
  675.     strout (pigbuf, -1, printcharfun);
  676.       }
  677.       break;
  678. #endif /* LISP_FLOAT_TYPE */
  679.  
  680.     case Lisp_Int:
  681.       sprintf (buf, "%d", XINT (obj));
  682.       strout (buf, -1, printcharfun);
  683.       break;
  684.  
  685.     case Lisp_String:
  686.       if (!escapeflag)
  687.     strout ((char *) XSTRING (obj)->data, XSTRING (obj)->size, printcharfun);
  688.       else
  689.     {
  690.       register int i;
  691.       register unsigned char *p = XSTRING (obj)->data;
  692.       register unsigned char c;
  693.  
  694.       PRINTCHAR ('\"');
  695.       for (i = XSTRING (obj)->size; i > 0; i--)
  696.         {
  697.           QUIT;
  698.           c = *p++;
  699.           if (c == '\n' && print_escape_newlines)
  700.         {
  701.           PRINTCHAR ('\\');
  702.           PRINTCHAR ('n');
  703.         }
  704.           else
  705.         {
  706.           if (c == '\"' || c == '\\')
  707.             PRINTCHAR ('\\');
  708.           PRINTCHAR (c);
  709.         }
  710.         }
  711.       PRINTCHAR ('\"');
  712.     }
  713.       break;
  714.  
  715.     case Lisp_Symbol:
  716.       {
  717.     register int confusing;
  718.     register unsigned char *p = XSYMBOL (obj)->name->data;
  719.     register unsigned char *end = p + XSYMBOL (obj)->name->size;
  720.     register unsigned char c;
  721.  
  722.     if (print_gensym) {
  723.       Lisp_Object tem = oblookup (Vobarray, p, end-p);
  724.       if (!EQ (tem, obj))
  725.         /* (read) would return a new symbol with the same name.
  726.            This isn't quite correct, because that symbol might not
  727.            really be uninterned (it might be interned in some other
  728.            obarray) but there's no way to win in that case without
  729.            implementing a real package system.
  730.          */
  731.         strout ("#:", 2, printcharfun);
  732.     }
  733.  
  734.     if (p != end && (*p == '-' || *p == '+')) p++;
  735.         if (p == end)
  736.       confusing = 0;
  737.     else
  738.       {
  739.         while (p != end && *p >= '0' && *p <= '9')
  740.           p++;
  741.         confusing = (end == p);
  742.       }
  743.  
  744.     p = XSYMBOL (obj)->name->data;
  745.     while (p != end)
  746.       {
  747.         QUIT;
  748.         c = *p++;
  749.         if (escapeflag)
  750.           {
  751.         if (c == '\"' || c == '\\' || c == '\'' || c == ';' || c == '#' ||
  752.             c == '(' || c == ')' || c == ',' || c =='.' || c == '`' ||
  753.             c == '[' || c == ']' || c == '?' || c <= 040 || confusing)
  754.           PRINTCHAR ('\\'), confusing = 0;
  755.           }
  756.         PRINTCHAR (c);
  757.       }
  758.       }
  759.       break;
  760.  
  761.     case Lisp_Cons:
  762.       /* If deeper than spec'd depth, print placeholder.  */
  763.       if (FIXNUMP (Vprint_level)
  764.       && print_depth > XINT (Vprint_level))
  765.     {
  766.       strout ("...", -1, printcharfun);
  767.       break;
  768.     }
  769.  
  770.       /* If print_readably is on, print (quote -foo-) as '-foo- */
  771.       if (print_readably &&
  772.       EQ (XCONS (obj)->car, Qquote) &&
  773.       CONSP (XCONS (obj)->cdr) &&
  774.       NILP (XCONS (XCONS (obj)->cdr)->cdr)) {
  775.     PRINTCHAR ('\'');
  776.     print (XCONS (XCONS (obj)->cdr)->car, printcharfun, escapeflag);
  777.     break;
  778.       }
  779.  
  780.       PRINTCHAR ('(');
  781.       {
  782.     register int i = 0;
  783.     register int max = 0;
  784.  
  785.     if (FIXNUMP (Vprint_length))
  786.       max = XINT (Vprint_length);
  787.     while (CONSP (obj))
  788.       {
  789.         if (i++)
  790.           PRINTCHAR (' ');
  791.         if (max && i > max)
  792.           {
  793.         strout ("...", 3, printcharfun);
  794.         break;
  795.           }
  796.         print (Fcar (obj), printcharfun, escapeflag);
  797.         obj = Fcdr (obj);
  798.       }
  799.       }
  800.       if (!NILP (obj) && !CONSP (obj))
  801.     {
  802.       strout (" . ", 3, printcharfun);
  803.       print (obj, printcharfun, escapeflag);
  804.     }
  805.       PRINTCHAR (')');
  806.       break;
  807.  
  808.     case Lisp_Compiled:
  809.       strout ((print_readably ? "#" : "#<byte-code "), -1, printcharfun);
  810.     case Lisp_Vector:
  811.       PRINTCHAR ('[');
  812.       {
  813.     register int i;
  814.     register Lisp_Object tem;
  815.     for (i = 0; i < XVECTOR (obj)->size; i++)
  816.       {
  817.         if (i) PRINTCHAR (' ');
  818.         tem = XVECTOR (obj)->contents[i];
  819.         print (tem, printcharfun, escapeflag);
  820.       }
  821.       }
  822.       PRINTCHAR (']');
  823.       if (!print_readably && COMPILEDP (obj))
  824.     PRINTCHAR ('>');
  825.       break;
  826.  
  827. #ifndef standalone
  828.     case Lisp_Extent:
  829.       if (escapeflag)
  830.     {
  831.           char *title = "";
  832.       char *name = "";
  833.  
  834.           if (BUFFERP (XEXTENT(obj)->buffer))
  835.             {
  836.               struct buffer *buf = XBUFFER(XEXTENT(obj)->buffer);
  837.           if (STRINGP (buf->name))
  838.         {
  839.           name = (char *)&(XSTRING(buf->name)->data[0]);
  840.           title = "buffer ";
  841.         }
  842.           else
  843.         {
  844.           title = "Killed Buffer";
  845.           name = "";
  846.         }
  847.             }
  848.       
  849.       if (print_readably)
  850.             {
  851.           if (EXTENT_FLAGS(XEXTENT(obj)) & EF_DESTROYED)
  852.         error ("printing unreadable object #<destroyed extent>");
  853.           else
  854.         error ("printing unreadable object #<extent [%d, %d)>",
  855.                        XINT (Fextent_start_position (obj)), 
  856.                        XINT (Fextent_end_position (obj)));
  857.         }
  858.       
  859.           if (EXTENT_FLAGS(XEXTENT(obj)) & EF_DESTROYED)
  860.         strout ("#<destroyed extent>", -1, printcharfun);
  861.       else
  862.         {
  863.           strout ("#<extent ", -1, printcharfun);
  864.           if (EXTENT_FLAGS(XEXTENT(obj)) & EF_DETACHED)
  865.         sprintf (buf, "(flags=0x%x) detached from %s%s", 
  866.              EXTENT_FLAGS (XEXTENT(obj)),
  867.              title, name);
  868.           else
  869.         sprintf (buf, "[%d, %d) (flags=0x%x) in %s%s", 
  870.              XINT(Fextent_start_position (obj)), 
  871.              XINT(Fextent_end_position (obj)),
  872.              EXTENT_FLAGS (XEXTENT(obj)),
  873.              title, name);
  874.           strout (buf, -1, printcharfun);
  875.           PRINTCHAR ('>');
  876.         }
  877.     }
  878.       else
  879.     strout ("#<extent>", -1, printcharfun);
  880.       break;
  881.  
  882.     case Lisp_Extent_Replica:
  883.       if (escapeflag)
  884.     {
  885.       if (print_readably)
  886.             {
  887.               error ("printing unreadable object #<dup [%d, %d)>",
  888.                      XDUP(obj)->start, XDUP(obj)->end);
  889.             }
  890.       
  891.       strout ("#<dup ", -1, printcharfun);
  892.           if (EXTENTP (XDUP(obj)->extent))
  893.             {
  894.               Lisp_Object extent_obj = XDUP(obj)->extent;
  895.               int from = XINT(Fextent_start_position (extent_obj));
  896.               int to = XINT(Fextent_end_position (extent_obj));
  897.               sprintf (buf, "[%d, %d) of extent 0x%x [%d, %d)",
  898.                        XDUP(obj)->start, XDUP(obj)->end, XDUP(obj)->extent,
  899.                        from, to);
  900.             }
  901.           else
  902.             sprintf (buf, "[%d, %d) of 0x%x",
  903.                      XDUP(obj)->start, XDUP(obj)->end, XDUP(obj)->extent);
  904.             
  905.           strout (buf, -1, printcharfun);
  906.       PRINTCHAR ('>');
  907.     }
  908.       else
  909.     strout ("#<dup>", -1, printcharfun);
  910.       break;
  911.  
  912.     case Lisp_Buffer:
  913.       if (print_readably)
  914.          {
  915.            if (NILP (XBUFFER (obj)->name))
  916.              error ("printing unreadable object #<killed buffer>");
  917.            else
  918.              error ("printing unreadable object #<buffer %s>",
  919.                     XSTRING (XBUFFER (obj)->name)->data);
  920.          }
  921.       if (NILP (XBUFFER (obj)->name))
  922.     strout ("#<killed buffer>", -1, printcharfun);
  923.       else if (escapeflag)
  924.     {
  925.       strout ("#<buffer ", -1, printcharfun);
  926.       strout ((char *) XSTRING (XBUFFER (obj)->name)->data, -1, printcharfun);
  927.       PRINTCHAR ('>');
  928.     }
  929.       else
  930.     strout ((char *) XSTRING (XBUFFER (obj)->name)->data, -1, printcharfun);
  931.       break;
  932.  
  933.     case Lisp_Process:
  934.       if (escapeflag)
  935.     {
  936.       if (print_readably)
  937.             error ("printing unreadable object #<process %s>",
  938.                    XSTRING (XPROCESS (obj)->name)->data);
  939.       strout ("#<process ", -1, printcharfun);
  940.       strout ((char *) XSTRING (XPROCESS (obj)->name)->data, -1, printcharfun);
  941.       PRINTCHAR ('>');
  942.     }
  943.       else
  944.     strout ((char *) XSTRING (XPROCESS (obj)->name)->data, -1, printcharfun);
  945.       break;
  946.  
  947.     case Lisp_Window:
  948.       if (print_readably)
  949.         error ("printing unreadable object #<window %d>",
  950.                XFASTINT (XWINDOW (obj)->sequence_number));
  951.  
  952.       strout ("#<window ", -1, printcharfun);
  953.       sprintf (buf, "%d", XFASTINT (XWINDOW (obj)->sequence_number));
  954.       strout (buf, -1, printcharfun);
  955.       if (!NILP (XWINDOW (obj)->buffer))
  956.     {
  957.       unsigned char *p = XSTRING (XBUFFER (XWINDOW (obj)->buffer)->name)->data;
  958.       strout (" on ", -1, printcharfun);
  959.       strout ((char *) p, -1, printcharfun);
  960.     }
  961.       PRINTCHAR ('>');
  962.       break;
  963.  
  964.     case Lisp_Window_Configuration:
  965.       if (print_readably)
  966.         error ("printing unreadable object #<window-configuration>");
  967.       strout ("#<window-configuration>", -1, printcharfun);
  968.       break;
  969.  
  970. #ifdef MULTI_SCREEN
  971.     case Lisp_Screen:
  972.       if (print_readably)
  973.         error ("printing unreadable object #<screen %s 0x%x>",
  974.                XSTRING (XSCREEN (obj)->name)->data,
  975.                XFASTINT (XSCREEN (obj)));
  976.       strout ("#<screen ", -1, printcharfun);
  977.       strout ((char *) XSTRING (XSCREEN (obj)->name)->data, -1, printcharfun);
  978.       sprintf (buf, " 0x%x", XFASTINT (XSCREEN (obj)));
  979.       strout (buf, -1, printcharfun);
  980.       strout (">", -1, printcharfun);
  981.       break;
  982. #endif /* MULTI_SCREEN */
  983.  
  984.     case Lisp_Marker:
  985.       if (print_readably)
  986.         error ("printing unreadable object #<marker>");
  987.       strout ("#<marker ", -1, printcharfun);
  988.       if (!(XMARKER (obj)->buffer))
  989.     strout ("in no buffer", -1, printcharfun);
  990.       else
  991.     {
  992.       sprintf (buf, "at %d", marker_position (obj));
  993.       strout (buf, -1, printcharfun);
  994.       strout (" in ", -1, printcharfun);
  995.       strout ((char *) XSTRING (XMARKER (obj)->buffer->name)->data, -1,
  996.           printcharfun);
  997.     }
  998.       PRINTCHAR ('>');
  999.       break;
  1000.  
  1001.     case Lisp_Event:
  1002.       if (print_readably)
  1003.         error ("printing unreadable object #<event>");
  1004.       switch (XEVENT (obj)->event_type) {
  1005.       case key_press_event:
  1006.       case button_press_event:
  1007.       case button_release_event:
  1008.       case magic_event:
  1009.     switch (XEVENT (obj)->event_type) {
  1010.     case key_press_event:
  1011.       strout ("#<keypress-event ",   -1, printcharfun); break;
  1012.     case button_press_event:
  1013.       strout ("#<buttondown-event ", -1, printcharfun); break;
  1014.     case button_release_event:
  1015.       strout ("#<buttonup-event ",   -1, printcharfun); break;
  1016.     case magic_event:
  1017.       strout ("#<magic-event ",      -1, printcharfun); break;
  1018.     default: abort ();
  1019.     }
  1020.     format_event_object (buf, XEVENT (obj), 0);
  1021.     strout (buf, -1, printcharfun);
  1022.     PRINTCHAR ('>');
  1023.     break;
  1024.  
  1025.       case pointer_motion_event:
  1026.     sprintf (buf, "#<motion-event %d, %d>",
  1027.          XEVENT (obj)->event.motion.x, XEVENT (obj)->event.motion.y);
  1028.     strout (buf, -1, printcharfun);
  1029.     break;
  1030.  
  1031.       case process_event:
  1032.     strout ("#<process-event ", -1, printcharfun);
  1033.     print (XEVENT (obj)->event.process.process, printcharfun, 1);
  1034.     PRINTCHAR ('>');
  1035.     break;
  1036.  
  1037.       case timeout_event:
  1038.     strout ("#<timeout-event ", -1, printcharfun);
  1039.     print (XEVENT (obj)->event.timeout.object, printcharfun, 1);
  1040.     PRINTCHAR ('>');
  1041.     break;
  1042.  
  1043.       case empty_event:
  1044.     strout ("#<empty-event>", -1, printcharfun);
  1045.     break;
  1046.  
  1047.       case menu_event:
  1048.       case eval_event:
  1049.     strout ("#<", -1, printcharfun);
  1050.     if (XEVENT (obj)->event_type == menu_event)
  1051.       strout ("menu", -1, printcharfun);
  1052.     else
  1053.       strout ("eval", -1, printcharfun);
  1054.     strout ("-event (", -1, printcharfun);
  1055.     print (XEVENT (obj)->event.eval.function, printcharfun, 1);
  1056.     PRINTCHAR (' ');
  1057.     print (XEVENT (obj)->event.eval.object, printcharfun, 1);
  1058.     strout (")>", -1, printcharfun);
  1059.     break;
  1060.  
  1061.       case dead_event:
  1062.     strout ("#<DEALLOCATED-EVENT>", -1, printcharfun);
  1063.     break;
  1064.  
  1065.       default:
  1066.     strout ("#<UNKNOWN-EVENT-TYPE>", -1, printcharfun);
  1067.     break;
  1068.       }
  1069.       break;
  1070.  
  1071.     case Lisp_Keymap:
  1072.       {
  1073.         struct Lisp_Keymap *keymap = XKEYMAP (obj);
  1074.         Lisp_Object name = keymap->name;
  1075.     int size = XFASTINT (Fkeymap_fullness (obj));
  1076.         strout ("#<keymap ", -1, printcharfun);
  1077.         if (!NILP (name))
  1078.           print (name, printcharfun, 1);
  1079.     sprintf (buf, "%s%d entr%s>", 
  1080.                  ((NILP (keymap->name)) ? "" : " "),
  1081.                  size, ((size == 1) ? "y" : "ies"));
  1082.     strout (buf, -1, printcharfun);
  1083.       }
  1084.       break;
  1085. #endif /* standalone */
  1086.  
  1087.     case Lisp_Subr:
  1088.       if (print_readably)
  1089.         error ("printing unreadable object #<subr %s>",
  1090.                XSUBR (obj)->symbol_name);
  1091.       strout ("#<subr ", -1, printcharfun);
  1092.       strout (XSUBR (obj)->symbol_name, -1, printcharfun);
  1093.       PRINTCHAR ('>');
  1094.       break;
  1095.     }
  1096.  
  1097.   print_depth--;
  1098. }
  1099.  
  1100.  
  1101. DEFUN ("external-debugging-output", Fexternal_debugging_output,
  1102.        Sexternal_debugging_output, 1, 1, 0,
  1103.   "Write CHARACTER to stderr.\n\
  1104. You can call print while debugging emacs, and pass it this function\n\
  1105. to make it write to the debugging output.\n")
  1106.   (character)
  1107.      Lisp_Object character;
  1108. {
  1109.   CHECK_FIXNUM (character, 0);
  1110.   putc (XINT (character), stderr);
  1111.   return character;
  1112. }
  1113.  
  1114. void
  1115. syms_of_print ()
  1116. {
  1117.   staticpro (&Qprint_escape_newlines);
  1118.   Qprint_escape_newlines = intern ("print-escape-newlines");
  1119.   staticpro (&Qprint_readably);
  1120.   Qprint_readably = intern ("print-readably");
  1121.  
  1122.   defsubr (&Sexternal_debugging_output);
  1123.   Qexternal_debugging_output = intern ("external-debugging-output");
  1124.   staticpro (&Qexternal_debugging_output);
  1125.  
  1126.   DEFVAR_LISP ("standard-output", &Vstandard_output,
  1127.     "Output stream `print' uses by default for outputting a character.\n\
  1128. This may be any function of one argument.\n\
  1129. It may also be a buffer (output is inserted before point)\n\
  1130. or a marker (output is inserted and the marker is advanced)\n\
  1131. or the symbol t (output appears in the minibuffer line).");
  1132.   Vstandard_output = Qt;
  1133.   Qstandard_output = intern ("standard-output");
  1134.   staticpro (&Qstandard_output);
  1135.  
  1136. #ifdef LISP_FLOAT_TYPE
  1137.   DEFVAR_LISP ("float-output-format", &Vfloat_output_format,
  1138.     "The format descriptor string that lisp uses to print floats.\n\
  1139. This is a %-spec like those accepted by `printf' in C,\n\
  1140. but with some restrictions.  It must start with the two characters `%.'.\n\
  1141. After that comes an integer precision specification,\n\
  1142. and then a letter which controls the format.\n\
  1143. The letters allowed are `e', `f' and `g'.\n\
  1144. Use `e' for exponential notation \"DIG.DIGITSeEXPT\"\n\
  1145. Use `f' for decimal point notation \"DIGITS.DIGITS\".\n\
  1146. Use `g' to choose the shorter of those two formats for the number at hand.\n\
  1147. The precision in any of these cases is the number of digits following\n\
  1148. the decimal point.  With `f', a precision of 0 means to omit the\n\
  1149. decimal point.  0 is not allowed with `f' or `g'.\n\n\
  1150. A value of nil means to use `%.16g'.\n\
  1151. \n\
  1152. Regardless of the value of `float-output-format', a floating point number\n\
  1153. will never be printed in such a way that it is ambiguous with an integer;\n\
  1154. that is, a floating-point number will always be printed with a decimal\n\
  1155. point and/or an exponent, even if the digits following the decimal point\n\
  1156. are all zero.  This is to preserve read-equivalence.");
  1157.   Vfloat_output_format = Qnil;
  1158.   Qfloat_output_format = intern ("float-output-format");
  1159.   staticpro (&Qfloat_output_format);
  1160. #endif /* LISP_FLOAT_TYPE */
  1161.  
  1162.   DEFVAR_LISP ("print-length", &Vprint_length,
  1163.     "Maximum length of list to print before abbreviating.\
  1164. A value of nil means no limit.");
  1165.   Vprint_length = Qnil;
  1166.  
  1167.   DEFVAR_LISP ("print-level", &Vprint_level,
  1168.     "Maximum depth of list nesting to print before abbreviating.\
  1169. A value of nil means no limit.");
  1170.   Vprint_level = Qnil;
  1171.  
  1172.   DEFVAR_BOOL ("print-escape-newlines", &print_escape_newlines,
  1173.     "Non-nil means print newlines in strings as backslash-n.");
  1174.   print_escape_newlines = 0;
  1175.  
  1176.   DEFVAR_BOOL ("print-readably", &print_readably,
  1177.     "If non-nil, then all objects will be printed in a readable form.\n\
  1178. If an object has no readable representation, then an error is signalled.\n\
  1179. When this is true, compiled-function objects will be written in #[...] form\n\
  1180. instead of in #<byte-code [...]> form.\n\
  1181. Do not SET this variable; bind it instead.");
  1182.   print_readably = 0;
  1183.  
  1184.   DEFVAR_BOOL ("print-gensym", &print_gensym,
  1185.     "If non-nil, then uninterned symbols (those made with `make-symbol'\n\
  1186. instead of `intern') will be preceeded by \"#:\", which tells the reader to\n\
  1187. create a new symbol instead of interning.  Beware: the #: syntax creates a\n\
  1188. new symbol each time it is seen, so if you print an object which contains\n\
  1189. two pointers to the same uninterned symbol, `read' will not duplicate that\n\
  1190. structure.");
  1191.   print_gensym = 0;
  1192.  
  1193.   /* prin1_to_string_buffer initialized in init_buffer_once in buffer.c */
  1194.   staticpro (&Vprin1_to_string_buffer);
  1195.  
  1196.   defsubr (&Sprin1);
  1197.   defsubr (&Sprin1_to_string);
  1198.   defsubr (&Sprinc);
  1199.   defsubr (&Sprint);
  1200.   defsubr (&Sterpri);
  1201.   defsubr (&Swrite_char);
  1202. #ifndef standalone
  1203.   defsubr (&Swith_output_to_temp_buffer);
  1204. #endif /* not standalone */
  1205. }
  1206.