home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / programming / source / a2.0bemacs-src.lha / Emacs-19.25 / src / dispnew.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-25  |  70.8 KB  |  2,554 lines

  1. /* Updating of data structures for redisplay.
  2.    Copyright (C) 1985, 86, 87, 88, 93, 94 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 <signal.h>
  22.  
  23. #include <config.h>
  24. #include <stdio.h>
  25. #include <ctype.h>
  26.  
  27. #include "lisp.h"
  28. #include "termchar.h"
  29. #include "termopts.h"
  30. #include "termhooks.h"
  31. #include "cm.h"
  32. #include "dispextern.h"
  33. #include "buffer.h"
  34. #include "frame.h"
  35. #include "window.h"
  36. #include "commands.h"
  37. #include "disptab.h"
  38. #include "indent.h"
  39. #include "intervals.h"
  40.  
  41. #include "systty.h"
  42. #include "systime.h"
  43.  
  44. #ifdef HAVE_X_WINDOWS
  45. #include "xterm.h"
  46. #endif    /* HAVE_X_WINDOWS */
  47.  
  48. #include <errno.h>
  49.  
  50. #ifdef USE_PROTOS
  51. #include "protos.h"
  52. #endif
  53.  
  54. #define max(a, b) ((a) > (b) ? (a) : (b))
  55. #define min(a, b) ((a) < (b) ? (a) : (b))
  56.  
  57. /* Get number of chars of output now in the buffer of a stdio stream.
  58.    This ought to be built in in stdio, but it isn't.
  59.    Some s- files override this because their stdio internals differ.  */
  60. #ifdef __GNU_LIBRARY__
  61. /* The s- file might have overridden the definition with one that works for
  62.    the system's C library.  But we are using the GNU C library, so this is
  63.    the right definition for every system.  */
  64. #ifdef GNU_LIBRARY_PENDING_OUTPUT_COUNT
  65. #define PENDING_OUTPUT_COUNT GNU_LIBRARY_PENDING_OUTPUT_COUNT
  66. #else
  67. #undef    PENDING_OUTPUT_COUNT
  68. #define    PENDING_OUTPUT_COUNT(FILE) ((FILE)->__bufp - (FILE)->__buffer)
  69. #endif
  70. #else /* not __GNU_LIBRARY__ */
  71. #ifndef PENDING_OUTPUT_COUNT
  72. #define PENDING_OUTPUT_COUNT(FILE) ((FILE)->_ptr - (FILE)->_base)
  73. #endif
  74. #endif
  75.  
  76. /* Nonzero upon entry to redisplay means do not assume anything about
  77.    current contents of actual terminal frame; clear and redraw it.  */
  78.  
  79. int frame_garbaged;
  80.  
  81. /* Nonzero means last display completed.  Zero means it was preempted. */
  82.  
  83. int display_completed;
  84.  
  85. /* Lisp variable visible-bell; enables use of screen-flash
  86.    instead of audible bell.  */
  87.  
  88. int visible_bell;
  89.  
  90. /* Invert the color of the whole frame, at a low level.  */
  91.  
  92. int inverse_video;
  93.  
  94. /* Line speed of the terminal.  */
  95.  
  96. int baud_rate;
  97.  
  98. /* nil or a symbol naming the window system under which emacs is
  99.    running ('x is the only current possibility).  */
  100.  
  101. Lisp_Object Vwindow_system;
  102.  
  103. /* Version number of X windows: 10, 11 or nil.  */
  104. Lisp_Object Vwindow_system_version;
  105.  
  106. /* Vector of glyph definitions.  Indexed by glyph number,
  107.    the contents are a string which is how to output the glyph.
  108.  
  109.    If Vglyph_table is nil, a glyph is output by using its low 8 bits
  110.    as a character code.  */
  111.  
  112. Lisp_Object Vglyph_table;
  113.  
  114. /* Display table to use for vectors that don't specify their own.  */
  115.  
  116. Lisp_Object Vstandard_display_table;
  117.  
  118. /* Nonzero means reading single-character input with prompt
  119.    so put cursor on minibuffer after the prompt.
  120.    positive means at end of text in echo area;
  121.    negative means at beginning of line.  */
  122. int cursor_in_echo_area;
  123.  
  124. /* The currently selected frame.
  125.    In a single-frame version, this variable always remains 0.  */
  126.  
  127. FRAME_PTR selected_frame;
  128.  
  129. /* A frame which is not just a minibuffer, or 0 if there are no such
  130.    frames.  This is usually the most recent such frame that was
  131.    selected.  In a single-frame version, this variable always remains 0.  */
  132. FRAME_PTR last_nonminibuf_frame;
  133.  
  134. /* In a single-frame version, the information that would otherwise
  135.    exist inside frame objects lives in the following structure instead.
  136.  
  137.    NOTE: the_only_frame is not checked for garbage collection; don't
  138.    store collectible objects in any of its fields!
  139.  
  140.    You're not/The only frame in town/...  */
  141.  
  142. #ifndef MULTI_FRAME
  143. struct frame the_only_frame;
  144. #endif
  145.  
  146. /* This is a vector, made larger whenever it isn't large enough,
  147.    which is used inside `update_frame' to hold the old contents
  148.    of the FRAME_PHYS_LINES of the frame being updated.  */
  149. struct frame_glyphs **ophys_lines;
  150. /* Length of vector currently allocated.  */
  151. int ophys_lines_length;
  152.  
  153. FILE *termscript;    /* Stdio stream being used for copy of all output.  */
  154.  
  155. struct cm Wcm;        /* Structure for info on cursor positioning */
  156.  
  157. extern short ospeed;    /* Output speed (from sg_ospeed) */
  158.  
  159. int delayed_size_change;  /* 1 means SIGWINCH happened when not safe.  */
  160.  
  161. #ifdef MULTI_FRAME
  162.  
  163. DEFUN ("redraw-frame", Fredraw_frame, Sredraw_frame, 1, 1, 0,
  164.   "Clear frame FRAME and output again what is supposed to appear on it.")
  165.   (frame)
  166.      Lisp_Object frame;
  167. {
  168.   FRAME_PTR f;
  169.  
  170.   CHECK_LIVE_FRAME (frame, 0);
  171.   f = XFRAME (frame);
  172.   update_begin (f);
  173.   /*  set_terminal_modes (); */
  174.   clear_frame ();
  175.   clear_frame_records (f);
  176.   update_end (f);
  177.   fflush (stdout);
  178.   windows_or_buffers_changed++;
  179.   /* Mark all windows as INaccurate,
  180.      so that every window will have its redisplay done.  */
  181.   mark_window_display_accurate (FRAME_ROOT_WINDOW (f), 0);
  182.   f->garbaged = 0;
  183.   return Qnil;
  184. }
  185.  
  186. redraw_frame (f)
  187.      FRAME_PTR f;
  188. {
  189.   Lisp_Object frame;
  190.   XSET (frame, Lisp_Frame, f);
  191.   Fredraw_frame (frame);
  192. }
  193.  
  194. #else
  195.  
  196. DEFUN ("redraw-frame", Fredraw_frame, Sredraw_frame, 1, 1, 0,
  197.   "Clear frame FRAME and output again what is supposed to appear on it.")
  198.   (frame)
  199.      Lisp_Object frame;
  200. {
  201.   update_begin (0);
  202.   set_terminal_modes ();
  203.   clear_frame ();
  204.   update_end (0);
  205.   fflush (stdout);
  206.   clear_frame_records (0);
  207.   windows_or_buffers_changed++;
  208.   /* Mark all windows as INaccurate,
  209.      so that every window will have its redisplay done.  */
  210.   mark_window_display_accurate (FRAME_ROOT_WINDOW (0), 0);
  211.   return Qnil;
  212. }
  213.  
  214. #endif
  215.  
  216. DEFUN ("redraw-display", Fredraw_display, Sredraw_display, 0, 0, "",
  217.   "Clear and redisplay all visible frames.")
  218.   ()
  219. {
  220.   Lisp_Object tail, frame;
  221.  
  222.   FOR_EACH_FRAME (tail, frame)
  223.     if (FRAME_VISIBLE_P (XFRAME (frame)))
  224.       Fredraw_frame (frame);
  225.  
  226.   return Qnil;
  227. }
  228.  
  229. /* This is used when frame_garbaged is set.
  230.    Redraw the individual frames marked as garbaged.  */
  231.  
  232. void
  233. redraw_garbaged_frames ()
  234. {
  235.   Lisp_Object tail, frame;
  236.  
  237.   FOR_EACH_FRAME (tail, frame)
  238.     if (FRAME_VISIBLE_P (XFRAME (frame))
  239.     && FRAME_GARBAGED_P (XFRAME (frame)))
  240.       Fredraw_frame (frame);
  241. }
  242.  
  243.  
  244. static struct frame_glyphs *
  245. make_frame_glyphs (frame, empty)
  246.      register FRAME_PTR frame;
  247.      int empty;
  248. {
  249.   register int i;
  250.   register width = FRAME_WIDTH (frame);
  251.   register height = FRAME_HEIGHT (frame);
  252.   register struct frame_glyphs *new
  253.     = (struct frame_glyphs *) xmalloc (sizeof (struct frame_glyphs));
  254.  
  255.   SET_GLYPHS_FRAME (new, frame);
  256.   new->height = height;
  257.   new->width = width;
  258.   new->used = (int *) xmalloc (height * sizeof (int));
  259.   new->glyphs = (GLYPH **) xmalloc (height * sizeof (GLYPH *));
  260.   new->charstarts = (int **) xmalloc (height * sizeof (int *));
  261.   new->highlight = (char *) xmalloc (height * sizeof (char));
  262.   new->enable = (char *) xmalloc (height * sizeof (char));
  263.   bzero (new->enable, height * sizeof (char));
  264.   new->bufp = (int *) xmalloc (height * sizeof (int));
  265.  
  266. #ifdef HAVE_X_WINDOWS
  267.   if (FRAME_X_P (frame))
  268.     {
  269.       new->top_left_x = (short *) xmalloc (height * sizeof (short));
  270.       new->top_left_y = (short *) xmalloc (height * sizeof (short));
  271.       new->pix_width = (short *) xmalloc (height * sizeof (short));
  272.       new->pix_height = (short *) xmalloc (height * sizeof (short));
  273.       new->max_ascent = (short *) xmalloc (height * sizeof (short));
  274.     }
  275. #endif
  276.  
  277.   if (empty)
  278.     {
  279.       /* Make the buffer used by decode_mode_spec.  This buffer is also
  280.          used as temporary storage when updating the frame.  See scroll.c. */
  281.       unsigned int total_glyphs = (width + 2) * sizeof (GLYPH);
  282.       unsigned int total_charstarts = (width + 2) * sizeof (int);
  283.  
  284.       new->total_contents = (GLYPH *) xmalloc (total_glyphs);
  285.       bzero (new->total_contents, total_glyphs);
  286.  
  287.       new->total_charstarts = (int *) xmalloc (total_charstarts);
  288.       bzero (new->total_charstarts, total_glyphs);
  289.     }
  290.   else
  291.     {
  292.       unsigned int total_glyphs = height * (width + 2) * sizeof (GLYPH);
  293.  
  294.       new->total_contents = (GLYPH *) xmalloc (total_glyphs);
  295.       bzero (new->total_contents, total_glyphs);
  296.       for (i = 0; i < height; i++)
  297.     new->glyphs[i] = new->total_contents + i * (width + 2) + 1;
  298.  
  299.       if (!FRAME_TERMCAP_P (frame))
  300.     {
  301.       unsigned int total_charstarts = height * (width + 2) * sizeof (int);
  302.  
  303.       new->total_charstarts = (int *) xmalloc (total_charstarts);
  304.       bzero (new->total_charstarts, total_charstarts);
  305.       for (i = 0; i < height; i++)
  306.         new->charstarts[i] = new->total_charstarts + i * (width + 2) + 1;
  307.     }
  308.       else
  309.     {
  310.       /* Without a window system, we don't really need charstarts.
  311.          So use a small amount of space to make enough data structure
  312.          to prevent crashes in display_text_line.  */
  313.       new->total_charstarts = (int *) xmalloc ((width + 2) * sizeof (int));
  314.       for (i = 0; i < height; i++)
  315.         new->charstarts[i] = new->total_charstarts;
  316.     }
  317.     }
  318.  
  319.   return new;
  320. }
  321.  
  322. static void
  323. free_frame_glyphs (frame, glyphs)
  324.      FRAME_PTR frame;
  325.      struct frame_glyphs *glyphs;
  326. {
  327.   if (glyphs->total_contents)
  328.     xfree (glyphs->total_contents);
  329.   if (glyphs->total_charstarts)
  330.     xfree (glyphs->total_charstarts);
  331.  
  332.   xfree (glyphs->used);
  333.   xfree (glyphs->glyphs);
  334.   xfree (glyphs->highlight);
  335.   xfree (glyphs->enable);
  336.   xfree (glyphs->bufp);
  337.   if (glyphs->charstarts)
  338.     xfree (glyphs->charstarts);
  339.  
  340. #ifdef HAVE_X_WINDOWS
  341.   if (FRAME_X_P (frame))
  342.     {
  343.       xfree (glyphs->top_left_x);
  344.       xfree (glyphs->top_left_y);
  345.       xfree (glyphs->pix_width);
  346.       xfree (glyphs->pix_height);
  347.       xfree (glyphs->max_ascent);
  348.     }
  349. #endif
  350.  
  351.   xfree (glyphs);
  352. }
  353.  
  354. static void
  355. remake_frame_glyphs (frame)
  356.      FRAME_PTR frame;
  357. {
  358.   if (FRAME_CURRENT_GLYPHS (frame))
  359.     free_frame_glyphs (frame, FRAME_CURRENT_GLYPHS (frame));
  360.   if (FRAME_DESIRED_GLYPHS (frame))
  361.     free_frame_glyphs (frame, FRAME_DESIRED_GLYPHS (frame));
  362.   if (FRAME_TEMP_GLYPHS (frame))
  363.     free_frame_glyphs (frame, FRAME_TEMP_GLYPHS (frame));
  364.  
  365.   if (FRAME_MESSAGE_BUF (frame))
  366.     {
  367.       /* Reallocate the frame's message buffer; remember that
  368.      echo_area_glyphs may be pointing here.  */
  369.       char *old_message_buf = FRAME_MESSAGE_BUF (frame);
  370.  
  371.       FRAME_MESSAGE_BUF (frame)
  372.     = (char *) xrealloc (FRAME_MESSAGE_BUF (frame),
  373.                  FRAME_WIDTH (frame) + 1);
  374.  
  375.       if (echo_area_glyphs == old_message_buf)
  376.     echo_area_glyphs = FRAME_MESSAGE_BUF (frame);
  377.       if (previous_echo_glyphs == old_message_buf)
  378.     previous_echo_glyphs = FRAME_MESSAGE_BUF (frame);
  379.     }
  380.   else
  381.     FRAME_MESSAGE_BUF (frame)
  382.       = (char *) xmalloc (FRAME_WIDTH (frame) + 1);
  383.  
  384.   FRAME_CURRENT_GLYPHS (frame) = make_frame_glyphs (frame, 0);
  385.   FRAME_DESIRED_GLYPHS (frame) = make_frame_glyphs (frame, 0);
  386.   FRAME_TEMP_GLYPHS (frame) = make_frame_glyphs (frame, 1);
  387.   SET_FRAME_GARBAGED (frame);
  388. }
  389.  
  390. /* Return the hash code of contents of line VPOS in frame-matrix M.  */
  391.  
  392. static int
  393. line_hash_code (m, vpos)
  394.      register struct frame_glyphs *m;
  395.      int vpos;
  396. {
  397.   register GLYPH *body, *end;
  398.   register int h = 0;
  399.  
  400.   if (!m->enable[vpos])
  401.     return 0;
  402.  
  403.   /* Give all highlighted lines the same hash code
  404.      so as to encourage scrolling to leave them in place.  */
  405.   if (m->highlight[vpos])
  406.     return -1;
  407.  
  408.   body = m->glyphs[vpos];
  409.  
  410.   if (must_write_spaces)
  411.     while (1)
  412.       {
  413.     GLYPH g = *body++;
  414.  
  415.     if (g == 0)
  416.       break;
  417.     h = (((h << 4) + (h >> 24)) & 0x0fffffff) + g - SPACEGLYPH;
  418.       }
  419.   else
  420.     while (1)
  421.       {
  422.     GLYPH g = *body++;
  423.  
  424.     if (g == 0)
  425.       break;
  426.     h = (((h << 4) + (h >> 24)) & 0x0fffffff) + g;
  427.       }
  428.  
  429.   if (h)
  430.     return h;
  431.   return 1;
  432. }
  433.  
  434. /* Return number of characters in line in M at vpos VPOS,
  435.    except don't count leading and trailing spaces
  436.    unless the terminal requires those to be explicitly output.  */
  437.  
  438. static unsigned int
  439. line_draw_cost (m, vpos)
  440.      struct frame_glyphs *m;
  441.      int vpos;
  442. {
  443.   register GLYPH *beg = m->glyphs[vpos];
  444.   register GLYPH *end = m->glyphs[vpos] + m->used[vpos];
  445.   register int i;
  446.   register int tlen = GLYPH_TABLE_LENGTH;
  447.   register Lisp_Object *tbase = GLYPH_TABLE_BASE;
  448.  
  449.   /* Ignore trailing and leading spaces if we can.  */
  450.   if (!must_write_spaces)
  451.     {
  452.       while ((end != beg) && (*end == SPACEGLYPH))
  453.     --end;
  454.       if (end == beg)
  455.     return (0); /* All blank line. */
  456.  
  457.       while (*beg == SPACEGLYPH)
  458.     ++beg;
  459.     }
  460.  
  461.   /* If we don't have a glyph-table, each glyph is one character,
  462.      so return the number of glyphs.  */
  463.   if (tbase == 0)
  464.     return end - beg;
  465.  
  466.   /* Otherwise, scan the glyphs and accumulate their total size in I.  */
  467.   i = 0;
  468.   while ((beg <= end) && *beg)
  469.     {
  470.       register GLYPH g = *beg++;
  471.  
  472.       if (GLYPH_SIMPLE_P (tbase, tlen, g))
  473.     i += 1;
  474.       else
  475.     i += GLYPH_LENGTH (tbase, g);
  476.     }
  477.   return i;
  478. }
  479.  
  480. /* The functions on this page are the interface from xdisp.c to redisplay.
  481.  
  482.    The only other interface into redisplay is through setting
  483.    FRAME_CURSOR_X (frame) and FRAME_CURSOR_Y (frame)
  484.    and SET_FRAME_GARBAGED (frame).  */
  485.  
  486. /* cancel_line eliminates any request to display a line at position `vpos' */
  487.  
  488. cancel_line (vpos, frame)
  489.      int vpos;
  490.      register FRAME_PTR frame;
  491. {
  492.   FRAME_DESIRED_GLYPHS (frame)->enable[vpos] = 0;
  493. }
  494.  
  495. clear_frame_records (frame)
  496.      register FRAME_PTR frame;
  497. {
  498.   if(FRAME_CURRENT_GLYPHS (frame))
  499.       bzero (FRAME_CURRENT_GLYPHS (frame)->enable, FRAME_HEIGHT (frame));
  500. }
  501.  
  502. /* Prepare to display on line VPOS starting at HPOS within it.  */
  503.  
  504. void
  505. get_display_line (frame, vpos, hpos)
  506.      register FRAME_PTR frame;
  507.      int vpos;
  508.      register int hpos;
  509. {
  510.   register struct frame_glyphs *glyphs;
  511.   register struct frame_glyphs *desired_glyphs = FRAME_DESIRED_GLYPHS (frame);
  512.   register GLYPH *p;
  513.  
  514.   if (vpos < 0)
  515.     abort ();
  516.  
  517.   if ((desired_glyphs->enable[vpos]) && desired_glyphs->used[vpos] > hpos)
  518.     abort ();
  519.  
  520.   if (! desired_glyphs->enable[vpos])
  521.     {
  522.       desired_glyphs->used[vpos] = 0;
  523.       desired_glyphs->highlight[vpos] = 0;
  524.       desired_glyphs->enable[vpos] = 1;
  525.     }
  526.  
  527.   if (hpos > desired_glyphs->used[vpos])
  528.     {
  529.       GLYPH *g = desired_glyphs->glyphs[vpos] + desired_glyphs->used[vpos];
  530.       GLYPH *end = desired_glyphs->glyphs[vpos] + hpos;
  531.  
  532.       desired_glyphs->used[vpos] = hpos;
  533.       while (g != end)
  534.     *g++ = SPACEGLYPH;
  535.     }
  536. }
  537.  
  538. /* Like bcopy except never gets confused by overlap.  */
  539.  
  540. void
  541. safe_bcopy (from, to, size)
  542.      char *from, *to;
  543.      int size;
  544. {
  545.   if (size <= 0 || from == to)
  546.     return;
  547.  
  548.   /* If the source and destination don't overlap, then bcopy can
  549.      handle it.  If they do overlap, but the destination is lower in
  550.      memory than the source, we'll assume bcopy can handle that.  */
  551.   if (to < from || from + size <= to)
  552.     bcopy (from, to, size);
  553.  
  554.   /* Otherwise, we'll copy from the end.  */
  555.   else
  556.     {
  557.       register char *endf = from + size;
  558.       register char *endt = to + size;
  559.  
  560.       /* If TO - FROM is large, then we should break the copy into
  561.      nonoverlapping chunks of TO - FROM bytes each.  However, if
  562.      TO - FROM is small, then the bcopy function call overhead
  563.      makes this not worth it.  The crossover point could be about
  564.      anywhere.  Since I don't think the obvious copy loop is too
  565.      bad, I'm trying to err in its favor.  */
  566.       if (to - from < 64)
  567.     {
  568.       do
  569.         *--endt = *--endf;
  570.       while (endf != from);
  571.     }
  572.       else
  573.     {
  574.       for (;;)
  575.         {
  576.           endt -= (to - from);
  577.           endf -= (to - from);
  578.  
  579.           if (endt < to)
  580.         break;
  581.  
  582.           bcopy (endf, endt, to - from);
  583.         }
  584.  
  585.       /* If SIZE wasn't a multiple of TO - FROM, there will be a
  586.          little left over.  The amount left over is
  587.          (endt + (to - from)) - to, which is endt - from.  */
  588.       bcopy (from, to, endt - from);
  589.     }
  590.     }
  591. }     
  592.  
  593. /* Rotate a vector of SIZE bytes right, by DISTANCE bytes.
  594.    DISTANCE may be negative.  */
  595.  
  596. static void
  597. rotate_vector (vector, size, distance)
  598.      char *vector;
  599.      int size;
  600.      int distance;
  601. {
  602.   char *temp = (char *) alloca (size);
  603.  
  604.   if (distance < 0)
  605.     distance += size;
  606.  
  607.   bcopy (vector, temp + distance, size - distance);
  608.   bcopy (vector + size - distance, temp, distance);
  609.   bcopy (temp, vector, size);
  610. }
  611.  
  612. /* Scroll lines from vpos FROM up to but not including vpos END
  613.    down by AMOUNT lines (AMOUNT may be negative).
  614.    Returns nonzero if done, zero if terminal cannot scroll them.  */
  615.  
  616. int
  617. scroll_frame_lines (frame, from, end, amount, newpos)
  618.      register FRAME_PTR frame;
  619.      int from, end, amount, newpos;
  620. {
  621.   register int i;
  622.   register struct frame_glyphs *current_frame
  623.     = FRAME_CURRENT_GLYPHS (frame);
  624.   int pos_adjust;
  625.   int width = FRAME_WIDTH (frame);
  626.  
  627.   if (!line_ins_del_ok)
  628.     return 0;
  629.  
  630.   if (amount == 0)
  631.     return 1;
  632.  
  633.   if (amount > 0)
  634.     {
  635.       update_begin (frame);
  636.       set_terminal_window (end + amount);
  637.       if (!scroll_region_ok)
  638.     ins_del_lines (end, -amount);
  639.       ins_del_lines (from, amount);
  640.       set_terminal_window (0);
  641.  
  642.       rotate_vector (current_frame->glyphs + from,
  643.              sizeof (GLYPH *) * (end + amount - from),
  644.              amount * sizeof (GLYPH *));
  645.  
  646.       rotate_vector (current_frame->charstarts + from,
  647.              sizeof (int *) * (end + amount - from),
  648.              amount * sizeof (int *));
  649.  
  650.       safe_bcopy (current_frame->used + from,
  651.           current_frame->used + from + amount,
  652.           (end - from) * sizeof current_frame->used[0]);
  653.  
  654.       safe_bcopy (current_frame->highlight + from,
  655.           current_frame->highlight + from + amount,
  656.           (end - from) * sizeof current_frame->highlight[0]);
  657.  
  658.       safe_bcopy (current_frame->enable + from,
  659.           current_frame->enable + from + amount,
  660.           (end - from) * sizeof current_frame->enable[0]);
  661.  
  662.       /* Adjust the lines by an amount
  663.      that puts the first of them at NEWPOS.  */
  664.       pos_adjust = newpos - current_frame->charstarts[from + amount][0];
  665.  
  666.       /* Offset each char position in the charstarts lines we moved
  667.      by pos_adjust.  */
  668.       for (i = from + amount; i < end + amount; i++)
  669.     {
  670.       int *line = current_frame->charstarts[i];
  671.       int col;
  672.       for (col = 0; col < width; col++)
  673.         if (line[col] > 0)
  674.           line[col] += pos_adjust;
  675.     }
  676.       for (i = from; i < from + amount; i++)
  677.     {
  678.       int *line = current_frame->charstarts[i];
  679.       int col;
  680.       line[0] = -1;
  681.       for (col = 0; col < width; col++)
  682.         line[col] = 0;
  683.     }
  684.  
  685.       /* Mark the lines made empty by scrolling as enabled, empty and
  686.      normal video.  */
  687.       bzero (current_frame->used + from,
  688.          amount * sizeof current_frame->used[0]);
  689.       bzero (current_frame->highlight + from,
  690.          amount * sizeof current_frame->highlight[0]);
  691.       for (i = from; i < from + amount; i++)
  692.     {
  693.       current_frame->glyphs[i][0] = '\0';
  694.       current_frame->charstarts[i][0] = -1;
  695.       current_frame->enable[i] = 1;
  696.     }
  697.  
  698.       safe_bcopy (current_frame->bufp + from,
  699.           current_frame->bufp + from + amount,
  700.           (end - from) * sizeof current_frame->bufp[0]);
  701.  
  702. #ifdef HAVE_X_WINDOWS
  703.       if (FRAME_X_P (frame))
  704.     {
  705.       safe_bcopy (current_frame->top_left_x + from,
  706.               current_frame->top_left_x + from + amount,
  707.               (end - from) * sizeof current_frame->top_left_x[0]);
  708.  
  709.       safe_bcopy (current_frame->top_left_y + from,
  710.               current_frame->top_left_y + from + amount,
  711.               (end - from) * sizeof current_frame->top_left_y[0]);
  712.  
  713.       safe_bcopy (current_frame->pix_width + from,
  714.               current_frame->pix_width + from + amount,
  715.               (end - from) * sizeof current_frame->pix_width[0]);
  716.  
  717.       safe_bcopy (current_frame->pix_height + from,
  718.               current_frame->pix_height + from + amount,
  719.               (end - from) * sizeof current_frame->pix_height[0]);
  720.  
  721.       safe_bcopy (current_frame->max_ascent + from,
  722.               current_frame->max_ascent + from + amount,
  723.               (end - from) * sizeof current_frame->max_ascent[0]);
  724.     }
  725. #endif                /* HAVE_X_WINDOWS */
  726.  
  727.       update_end (frame);
  728.     }
  729.   if (amount < 0)
  730.     {
  731.       update_begin (frame);
  732.       set_terminal_window (end);
  733.       ins_del_lines (from + amount, amount);
  734.       if (!scroll_region_ok)
  735.     ins_del_lines (end + amount, -amount);
  736.       set_terminal_window (0);
  737.  
  738.       rotate_vector (current_frame->glyphs + from + amount,
  739.              sizeof (GLYPH *) * (end - from - amount),
  740.              amount * sizeof (GLYPH *));
  741.  
  742.       rotate_vector (current_frame->charstarts + from + amount,
  743.              sizeof (int *) * (end - from - amount),
  744.              amount * sizeof (int *));
  745.  
  746.       safe_bcopy (current_frame->used + from,
  747.           current_frame->used + from + amount,
  748.           (end - from) * sizeof current_frame->used[0]);
  749.  
  750.       safe_bcopy (current_frame->highlight + from,
  751.           current_frame->highlight + from + amount,
  752.           (end - from) * sizeof current_frame->highlight[0]);
  753.  
  754.       safe_bcopy (current_frame->enable + from,
  755.           current_frame->enable + from + amount,
  756.           (end - from) * sizeof current_frame->enable[0]);
  757.  
  758.       /* Adjust the lines by an amount
  759.      that puts the first of them at NEWPOS.  */
  760.       pos_adjust = newpos - current_frame->charstarts[from + amount][0];
  761.  
  762.       /* Offset each char position in the charstarts lines we moved
  763.      by pos_adjust.  */
  764.       for (i = from + amount; i < end + amount; i++)
  765.     {
  766.       int *line = current_frame->charstarts[i];
  767.       int col;
  768.       for (col = 0; col < width; col++)
  769.         if (line[col] > 0)
  770.           line[col] += pos_adjust;
  771.     }
  772.       for (i = end + amount; i < end; i++)
  773.     {
  774.       int *line = current_frame->charstarts[i];
  775.       int col;
  776.       line[0] = -1;
  777.       for (col = 0; col < width; col++)
  778.         line[col] = 0;
  779.     }
  780.  
  781.       /* Mark the lines made empty by scrolling as enabled, empty and
  782.      normal video.  */
  783.       bzero (current_frame->used + end + amount,
  784.          - amount * sizeof current_frame->used[0]);
  785.       bzero (current_frame->highlight + end + amount,
  786.          - amount * sizeof current_frame->highlight[0]);
  787.       for (i = end + amount; i < end; i++)
  788.     {
  789.       current_frame->glyphs[i][0] = '\0';
  790.       current_frame->charstarts[i][0] = 0;
  791.       current_frame->enable[i] = 1;
  792.     }
  793.  
  794.       safe_bcopy (current_frame->bufp + from,
  795.           current_frame->bufp + from + amount,
  796.           (end - from) * sizeof current_frame->bufp[0]);
  797.  
  798. #ifdef HAVE_X_WINDOWS
  799.       if (FRAME_X_P (frame))
  800.     {
  801.       safe_bcopy (current_frame->top_left_x + from,
  802.               current_frame->top_left_x + from + amount,
  803.               (end - from) * sizeof current_frame->top_left_x[0]);
  804.  
  805.       safe_bcopy (current_frame->top_left_y + from,
  806.               current_frame->top_left_y + from + amount,
  807.               (end - from) * sizeof current_frame->top_left_y[0]);
  808.  
  809.       safe_bcopy (current_frame->pix_width + from,
  810.               current_frame->pix_width + from + amount,
  811.               (end - from) * sizeof current_frame->pix_width[0]);
  812.  
  813.       safe_bcopy (current_frame->pix_height + from,
  814.               current_frame->pix_height + from + amount,
  815.               (end - from) * sizeof current_frame->pix_height[0]);
  816.  
  817.       safe_bcopy (current_frame->max_ascent + from,
  818.               current_frame->max_ascent + from + amount,
  819.               (end - from) * sizeof current_frame->max_ascent[0]);
  820.     }
  821. #endif                /* HAVE_X_WINDOWS */
  822.  
  823.       update_end (frame);
  824.     }
  825.   return 1;
  826. }
  827.  
  828. /* After updating a window W that isn't the full frame wide,
  829.    copy all the columns that W does not occupy
  830.    into the FRAME_DESIRED_GLYPHS (frame) from the FRAME_PHYS_GLYPHS (frame)
  831.    so that update_frame will not change those columns.  */
  832.  
  833. preserve_other_columns (w)
  834.      struct window *w;
  835. {
  836.   register int vpos;
  837.   register struct frame_glyphs *current_frame, *desired_frame;
  838.   register FRAME_PTR frame = XFRAME (w->frame);
  839.   int start = XFASTINT (w->left);
  840.   int end = XFASTINT (w->left) + XFASTINT (w->width);
  841.   int bot = XFASTINT (w->top) + XFASTINT (w->height);
  842.  
  843.   current_frame = FRAME_CURRENT_GLYPHS (frame);
  844.   desired_frame = FRAME_DESIRED_GLYPHS (frame);
  845.  
  846.   for (vpos = XFASTINT (w->top); vpos < bot; vpos++)
  847.     {
  848.       if (current_frame->enable[vpos] && desired_frame->enable[vpos])
  849.     {
  850.       if (start > 0)
  851.         {
  852.           int len;
  853.  
  854.           bcopy (current_frame->glyphs[vpos],
  855.              desired_frame->glyphs[vpos],
  856.              start * sizeof (current_frame->glyphs[vpos]));
  857.           bcopy (current_frame->charstarts[vpos],
  858.              desired_frame->charstarts[vpos],
  859.              start * sizeof (current_frame->charstarts[vpos]));
  860.           len = min (start, current_frame->used[vpos]);
  861.           if (desired_frame->used[vpos] < len)
  862.         desired_frame->used[vpos] = len;
  863.         }
  864.       if (current_frame->used[vpos] > end
  865.           && desired_frame->used[vpos] < current_frame->used[vpos])
  866.         {
  867.           while (desired_frame->used[vpos] < end)
  868.         {
  869.           int used = desired_frame->used[vpos]++;
  870.           desired_frame->glyphs[vpos][used] = SPACEGLYPH;
  871.           desired_frame->glyphs[vpos][used] = 0;
  872.         }
  873.           bcopy (current_frame->glyphs[vpos] + end,
  874.              desired_frame->glyphs[vpos] + end,
  875.              ((current_frame->used[vpos] - end)
  876.               * sizeof (current_frame->glyphs[vpos])));
  877.           bcopy (current_frame->charstarts[vpos] + end,
  878.              desired_frame->charstarts[vpos] + end,
  879.              ((current_frame->used[vpos] - end)
  880.               * sizeof (current_frame->charstarts[vpos])));
  881.           desired_frame->used[vpos] = current_frame->used[vpos];
  882.         }
  883.     }
  884.     }
  885. }
  886.  
  887. #if 0
  888.  
  889. /* If window w does not need to be updated and isn't the full frame wide,
  890.  copy all the columns that w does occupy
  891.  into the FRAME_DESIRED_LINES (frame) from the FRAME_PHYS_LINES (frame)
  892.  so that update_frame will not change those columns.
  893.  
  894.  Have not been able to figure out how to use this correctly.  */
  895.  
  896. preserve_my_columns (w)
  897.      struct window *w;
  898. {
  899.   register int vpos, fin;
  900.   register struct frame_glyphs *l1, *l2;
  901.   register FRAME_PTR frame = XFRAME (w->frame);
  902.   int start = XFASTINT (w->left);
  903.   int end = XFASTINT (w->left) + XFASTINT (w->width);
  904.   int bot = XFASTINT (w->top) + XFASTINT (w->height);
  905.  
  906.   for (vpos = XFASTINT (w->top); vpos < bot; vpos++)
  907.     {
  908.       if ((l1 = FRAME_DESIRED_GLYPHS (frame)->glyphs[vpos + 1])
  909.       && (l2 = FRAME_PHYS_GLYPHS (frame)->glyphs[vpos + 1]))
  910.     {
  911.       if (l2->length > start && l1->length < l2->length)
  912.         {
  913.           fin = l2->length;
  914.           if (fin > end) fin = end;
  915.           while (l1->length < start)
  916.         l1->body[l1->length++] = ' ';
  917.           bcopy (l2->body + start, l1->body + start, fin - start);
  918.           l1->length = fin;
  919.         }
  920.     }
  921.     }
  922. }
  923.  
  924. #endif
  925.  
  926. /* Adjust by ADJUST the charstart values in window W
  927.    after vpos VPOS, which counts relative to the frame
  928.    (not relative to W itself).  */
  929.  
  930. void
  931. adjust_window_charstarts (w, vpos, adjust)
  932.      struct window *w;
  933.      int vpos;
  934.      int adjust;
  935. {
  936.   int left = XFASTINT (w->left);
  937.   int top = XFASTINT (w->top);
  938.   int right = left + window_internal_width (w);
  939.   int bottom = top + window_internal_height (w);
  940.   int i;
  941.  
  942.   for (i = vpos + 1; i < bottom; i++)
  943.     {
  944.       int *charstart
  945.     = FRAME_CURRENT_GLYPHS (XFRAME (WINDOW_FRAME (w)))->charstarts[i];
  946.       int j;
  947.       for (j = left; j < right; j++)
  948.     if (charstart[j] > 0)
  949.       charstart[j] += adjust;
  950.     }
  951. }
  952.  
  953. /* Check the charstarts values in the area of window W
  954.    for internal consistency.  We cannot check that they are "right";
  955.    we can only look for something nonsensical.  */
  956.  
  957. verify_charstarts (w)
  958.      struct window *w;
  959. {
  960.   FRAME_PTR f = XFRAME (WINDOW_FRAME (w));
  961.   int i;
  962.   int top = XFASTINT (w->top);
  963.   int bottom = top + window_internal_height (w);
  964.   int left = XFASTINT (w->left);
  965.   int right = left + window_internal_width (w);
  966.   int next_line;
  967.   int truncate = (XINT (w->hscroll)
  968.           || (truncate_partial_width_windows
  969.               && (XFASTINT (w->width) < FRAME_WIDTH (f)))
  970.           || !NILP (XBUFFER (w->buffer)->truncate_lines));
  971.  
  972.   for (i = top; i < bottom; i++)
  973.     {
  974.       int j;
  975.       int last;
  976.       int *charstart = FRAME_CURRENT_GLYPHS (f)->charstarts[i];
  977.  
  978.       if (i != top)
  979.     {
  980.       if (truncate)
  981.         {
  982.           /* If we are truncating lines, allow a jump
  983.          in charstarts from one line to the next.  */
  984.           if (charstart[left] < next_line)
  985.         abort ();
  986.         }
  987.       else
  988.         {
  989.           if (charstart[left] != next_line)
  990.         abort ();
  991.         }
  992.     }
  993.  
  994.       for (j = left; j < right; j++)
  995.     if (charstart[j] > 0)
  996.       last = charstart[j];
  997.       /* Record where the next line should start.  */
  998.       next_line = last;
  999.       if (BUF_ZV (XBUFFER (w->buffer)) != last)
  1000.     {
  1001.       /* If there's a newline between the two lines, count that.  */
  1002.       int endchar = *BUF_CHAR_ADDRESS (XBUFFER (w->buffer), last);
  1003.       if (endchar == '\n')
  1004.         next_line++;
  1005.     }
  1006.     }
  1007. }
  1008.  
  1009. /* On discovering that the redisplay for a window was no good,
  1010.    cancel the columns of that window, so that when the window is
  1011.    displayed over again get_display_line will not complain.  */
  1012.  
  1013. cancel_my_columns (w)
  1014.      struct window *w;
  1015. {
  1016.   register int vpos;
  1017.   register struct frame_glyphs *desired_glyphs
  1018.     = FRAME_DESIRED_GLYPHS (XFRAME (w->frame));
  1019.   register int start = XFASTINT (w->left);
  1020.   register int bot = XFASTINT (w->top) + XFASTINT (w->height);
  1021.  
  1022.   for (vpos = XFASTINT (w->top); vpos < bot; vpos++)
  1023.     if (desired_glyphs->enable[vpos]
  1024.     && desired_glyphs->used[vpos] >= start)
  1025.       desired_glyphs->used[vpos] = start;
  1026. }
  1027.  
  1028. /* These functions try to perform directly and immediately on the frame
  1029.    the necessary output for one change in the buffer.
  1030.    They may return 0 meaning nothing was done if anything is difficult,
  1031.    or 1 meaning the output was performed properly.
  1032.    They assume that the frame was up to date before the buffer
  1033.    change being displayed.  They make various other assumptions too;
  1034.    see command_loop_1 where these are called.  */
  1035.  
  1036. int
  1037. direct_output_for_insert (g)
  1038.      int g;
  1039. {
  1040.   register FRAME_PTR frame = selected_frame;
  1041.   register struct frame_glyphs *current_frame
  1042.     = FRAME_CURRENT_GLYPHS (frame);
  1043.  
  1044. #ifndef COMPILER_REGISTER_BUG
  1045.   register
  1046. #endif /* COMPILER_REGISTER_BUG */
  1047.     struct window *w = XWINDOW (selected_window);
  1048. #ifndef COMPILER_REGISTER_BUG
  1049.   register
  1050. #endif /* COMPILER_REGISTER_BUG */
  1051.     int hpos = FRAME_CURSOR_X (frame);
  1052. #ifndef COMPILER_REGISTER_BUG
  1053.   register
  1054. #endif /* COMPILER_REGISTER_BUG */
  1055.     int vpos = FRAME_CURSOR_Y (frame);
  1056.  
  1057.   /* Give up if about to continue line.  */
  1058.   if (hpos >= XFASTINT (w->left) + window_internal_width (w) - 1
  1059.     
  1060.   /* Avoid losing if cursor is in invisible text off left margin */
  1061.       || (XINT (w->hscroll) && hpos == XFASTINT (w->left))
  1062.     
  1063.   /* Give up if cursor outside window (in minibuf, probably) */
  1064.       || cursor_in_echo_area
  1065.       || FRAME_CURSOR_Y (frame) < XFASTINT (w->top)
  1066.       || FRAME_CURSOR_Y (frame) >= XFASTINT (w->top) + XFASTINT (w->height)
  1067.  
  1068.   /* Give up if cursor not really at FRAME_CURSOR_X, FRAME_CURSOR_Y */
  1069.       || !display_completed
  1070.  
  1071.   /* Give up if buffer appears in two places.  */
  1072.       || buffer_shared > 1
  1073.  
  1074. #ifdef USE_TEXT_PROPERTIES
  1075.   /* Intervals have already been adjusted, point is after the
  1076.      character that was just inserted. */
  1077.   /* Give up if character is invisible. */
  1078.   /* Give up if character has a face property.
  1079.      At the moment we only lose at end of line or end of buffer
  1080.      and only with faces that have some background */
  1081.   /* Instead of wasting time, give up if character has any text properties */
  1082.       || ! NILP (Ftext_properties_at (XFASTINT (point - 1), Qnil))
  1083. #endif
  1084.  
  1085.   /* Give up if w is minibuffer and a message is being displayed there */
  1086.       || (MINI_WINDOW_P (w) && echo_area_glyphs))
  1087.     return 0;
  1088.  
  1089.   {
  1090. #ifdef HAVE_X_WINDOWS
  1091.     int dummy;
  1092.     int face = compute_char_face (frame, w, point - 1, -1, -1, &dummy, point);
  1093. #endif
  1094.     current_frame->glyphs[vpos][hpos] = MAKE_GLYPH (frame, g, face);
  1095.     current_frame->charstarts[vpos][hpos] = point - 1;
  1096.     /* Record the entry for after the newly inserted character.  */
  1097.     current_frame->charstarts[vpos][hpos + 1] = point;
  1098.     adjust_window_charstarts (w, vpos, 1);
  1099.   }
  1100.   unchanged_modified = MODIFF;
  1101.   beg_unchanged = GPT - BEG;
  1102.   XFASTINT (w->last_point) = point;
  1103.   XFASTINT (w->last_point_x) = hpos;
  1104.   XFASTINT (w->last_modified) = MODIFF;
  1105.  
  1106.   reassert_line_highlight (0, vpos);
  1107.   write_glyphs (¤t_frame->glyphs[vpos][hpos], 1);
  1108.   fflush (stdout);
  1109.   ++FRAME_CURSOR_X (frame);
  1110.   if (hpos == current_frame->used[vpos])
  1111.     {
  1112.       current_frame->used[vpos] = hpos + 1;
  1113.       current_frame->glyphs[vpos][hpos + 1] = 0;
  1114.     }
  1115.  
  1116.   return 1;
  1117. }
  1118.  
  1119. int
  1120. direct_output_forward_char (n)
  1121.      int n;
  1122. {
  1123.   register FRAME_PTR frame = selected_frame;
  1124.   register struct window *w = XWINDOW (selected_window);
  1125.   int position;
  1126.   int hpos = FRAME_CURSOR_X (frame);
  1127.  
  1128.   /* Give up if in truncated text at end of line.  */
  1129.   if (hpos >= XFASTINT (w->left) + window_internal_width (w) - 1)
  1130.     return 0;
  1131.  
  1132.   /* Avoid losing if cursor is in invisible text off left margin
  1133.      or about to go off either side of window.  */
  1134.   if ((FRAME_CURSOR_X (frame) == XFASTINT (w->left)
  1135.        && (XINT (w->hscroll) || n < 0))
  1136.       || (n > 0
  1137.       && (FRAME_CURSOR_X (frame) + 1 >= window_internal_width (w) - 1))
  1138.       || cursor_in_echo_area)
  1139.     return 0;
  1140.   
  1141.   /* Can't use direct output if highlighting a region.  */
  1142.   if (!NILP (Vtransient_mark_mode) && !NILP (current_buffer->mark_active))
  1143.     return 0;
  1144.  
  1145. #ifdef USE_TEXT_PROPERTIES
  1146.   /* Don't use direct output next to an invisible character
  1147.      since we might need to do something special.  */
  1148.  
  1149.   XFASTINT (position) = point;
  1150.   if (XFASTINT (position) < ZV
  1151.       && ! NILP (Fget_char_property (position,
  1152.                      Qinvisible,
  1153.                      selected_window)))
  1154.     return 0;
  1155.  
  1156.   XFASTINT (position) = point - 1;
  1157.   if (XFASTINT (position) >= BEGV
  1158.       && ! NILP (Fget_char_property (position,
  1159.                      Qinvisible,
  1160.                      selected_window)))
  1161.     return 0;
  1162. #endif
  1163.  
  1164.   FRAME_CURSOR_X (frame) += n;
  1165.   XFASTINT (w->last_point_x) = FRAME_CURSOR_X (frame);
  1166.   XFASTINT (w->last_point) = point;
  1167.   cursor_to (FRAME_CURSOR_Y (frame), FRAME_CURSOR_X (frame));
  1168.   fflush (stdout);
  1169.  
  1170.   return 1;
  1171. }
  1172.  
  1173. static void update_line ();
  1174.  
  1175. /* Update frame F based on the data in FRAME_DESIRED_GLYPHS.
  1176.    Value is nonzero if redisplay stopped due to pending input.
  1177.    FORCE nonzero means do not stop for pending input.  */
  1178.  
  1179. int
  1180. update_frame (f, force, inhibit_hairy_id)
  1181.      FRAME_PTR f;
  1182.      int force;
  1183.      int inhibit_hairy_id;
  1184. {
  1185.   register struct frame_glyphs *current_frame;
  1186.   register struct frame_glyphs *desired_frame = 0;
  1187.   register int i;
  1188.   int pause;
  1189.   int preempt_count = baud_rate / 2400 + 1;
  1190.   extern input_pending;
  1191. #ifdef HAVE_X_WINDOWS
  1192.   register int downto, leftmost;
  1193. #endif
  1194.  
  1195.   if (preempt_count <= 0)
  1196.     preempt_count = 1;
  1197.  
  1198. #ifdef AMIGA
  1199.   start_count(0); /* CHFIXME: check, evt. remove? define? */
  1200. #endif
  1201.  
  1202.   if (FRAME_HEIGHT (f) == 0) abort (); /* Some bug zeros some core */
  1203.  
  1204. #ifdef FAST_DISPLAY
  1205.   /* Don't compute for i/d line if just want cursor motion. */
  1206.   /* Don't allow preemption, etc either */
  1207.   for (i = 0; i < FRAME_HEIGHT (f); i++)
  1208.     if (desired_frame->enable[i])
  1209.       break;
  1210.  
  1211.   if (i >= FRAME_HEIGHT (f))
  1212.     {
  1213.       update_begin();
  1214.       goto update_done;
  1215.     }
  1216. #endif
  1217.  
  1218.   detect_input_pending ();
  1219.   if (input_pending && !force)
  1220.     {
  1221.       pause = 1;
  1222.       goto do_pause;
  1223.     }
  1224.  
  1225.   update_begin (f);
  1226.  
  1227.   if (!line_ins_del_ok)
  1228.     inhibit_hairy_id = 1;
  1229.  
  1230.   /* These are separate to avoid a possible bug in the AIX C compiler.  */
  1231.   current_frame = FRAME_CURRENT_GLYPHS (f);
  1232.   desired_frame = FRAME_DESIRED_GLYPHS (f);
  1233.  
  1234. #ifndef FAST_DISPLAY
  1235.   /* See if any of the desired lines are enabled; don't compute for
  1236.      i/d line if just want cursor motion. */
  1237.   for (i = 0; i < FRAME_HEIGHT (f); i++)
  1238.     if (desired_frame->enable[i])
  1239.       break;
  1240. #endif
  1241.   
  1242.   /* Try doing i/d line, if not yet inhibited.  */
  1243.   if (!inhibit_hairy_id && i < FRAME_HEIGHT (f))
  1244.     force |= scrolling (f);
  1245.  
  1246.   /* Update the individual lines as needed.  Do bottom line first.  */
  1247.  
  1248.   if (desired_frame->enable[FRAME_HEIGHT (f) - 1])
  1249.     update_line (f, FRAME_HEIGHT (f) - 1);
  1250.  
  1251. #ifdef HAVE_X_WINDOWS
  1252.   if (FRAME_X_P (f))
  1253.     {
  1254.       leftmost = downto = f->display.x->internal_border_width;
  1255.       if (desired_frame->enable[0])
  1256.     {
  1257.       current_frame->top_left_x[FRAME_HEIGHT (f) - 1] = leftmost;
  1258.       current_frame->top_left_y[FRAME_HEIGHT (f) - 1]
  1259.         = PIXEL_HEIGHT (f) - f->display.x->internal_border_width
  1260.           - current_frame->pix_height[FRAME_HEIGHT (f) - 1];
  1261.       current_frame->top_left_x[0] = leftmost;
  1262.       current_frame->top_left_y[0] = downto;
  1263.     }
  1264.     }
  1265. #endif /* HAVE_X_WINDOWS */
  1266.  
  1267.   /* Now update the rest of the lines. */
  1268.   for (i = 0; i < FRAME_HEIGHT (f) - 1 && (force || !input_pending); i++)
  1269.     {
  1270.       if (desired_frame->enable[i])
  1271.     {
  1272.       if (FRAME_TERMCAP_P (f))
  1273.         {
  1274.           /* Flush out every so many lines.
  1275.          Also flush out if likely to have more than 1k buffered
  1276.          otherwise.   I'm told that some telnet connections get
  1277.          really screwed by more than 1k output at once.  */
  1278.           int outq = PENDING_OUTPUT_COUNT (stdout);
  1279.           if (outq > 900
  1280.           || (outq > 20 && ((i - 1) % preempt_count == 0)))
  1281.         {
  1282.           fflush (stdout);
  1283.           if (preempt_count == 1)
  1284.             {
  1285. #ifdef EMACS_OUTQSIZE
  1286.               if (EMACS_OUTQSIZE (0, &outq) < 0)
  1287.             /* Probably not a tty.  Ignore the error and reset
  1288.              * the outq count. */
  1289.             outq = PENDING_OUTPUT_COUNT (stdout);
  1290. #endif
  1291.               outq *= 10;
  1292.               if (baud_rate <= outq && baud_rate > 0)
  1293.             sleep (outq / baud_rate);
  1294.             }
  1295.         }
  1296.           if ((i - 1) % preempt_count == 0)
  1297.         detect_input_pending ();
  1298.         }
  1299.  
  1300.       update_line (f, i);
  1301. #ifdef HAVE_X_WINDOWS
  1302.       if (FRAME_X_P (f))
  1303.         {
  1304.           current_frame->top_left_y[i] = downto;
  1305.           current_frame->top_left_x[i] = leftmost;
  1306.         }
  1307. #endif /* HAVE_X_WINDOWS */
  1308.     }
  1309.  
  1310. #ifdef HAVE_X_WINDOWS
  1311.       if (FRAME_X_P (f))
  1312.     downto += current_frame->pix_height[i];
  1313. #endif
  1314.     }
  1315. #ifdef FAST_DISPLAY
  1316. update_done:
  1317. #endif
  1318.   pause = (i < FRAME_HEIGHT (f) - 1) ? i : 0;
  1319.  
  1320.   /* Now just clean up termcap drivers and set cursor, etc.  */
  1321.   if (!pause)
  1322.     {
  1323.       if (cursor_in_echo_area
  1324.       && FRAME_HAS_MINIBUF_P (f))
  1325.     {
  1326.       int top = XINT (XWINDOW (FRAME_MINIBUF_WINDOW (f))->top);
  1327.       int row, col;
  1328.  
  1329.       if (cursor_in_echo_area < 0)
  1330.         {
  1331.           row = top;
  1332.           col = 0;
  1333.         }
  1334.       else
  1335.         {
  1336.           /* If the minibuffer is several lines high, find the last
  1337.          line that has any text on it.  */
  1338.           row = FRAME_HEIGHT (f);
  1339.           do 
  1340.         {
  1341.           row--;
  1342.           if (current_frame->enable[row])
  1343.             col = current_frame->used[row];
  1344.           else
  1345.             col = 0;
  1346.         }
  1347.           while (row > top && col == 0);
  1348.  
  1349.           if (col >= FRAME_WIDTH (f))
  1350.         {
  1351.           col = 0;
  1352.           if (row < FRAME_HEIGHT (f) - 1)
  1353.             row++;
  1354.         }
  1355.         }
  1356.  
  1357.       cursor_to (row, col);
  1358.     }
  1359.       else
  1360.     cursor_to (FRAME_CURSOR_Y (f), max (min (FRAME_CURSOR_X (f),
  1361.                           FRAME_WIDTH (f) - 1), 0));
  1362.     }
  1363.  
  1364.   update_end (f);
  1365.  
  1366.   if (termscript)
  1367.     fflush (termscript);
  1368.   fflush (stdout);
  1369.  
  1370.   /* Here if output is preempted because input is detected.  */
  1371.  do_pause:
  1372.  
  1373.   if (FRAME_HEIGHT (f) == 0) abort (); /* Some bug zeros some core */
  1374.   display_completed = !pause;
  1375.   
  1376.   if(FRAME_DESIRED_GLYPHS (f))
  1377.       bzero (FRAME_DESIRED_GLYPHS (f)->enable, FRAME_HEIGHT (f));
  1378. #ifdef AMIGA
  1379.   stop_count(0);
  1380. #endif
  1381.   return pause;
  1382. }
  1383.  
  1384. /* Called when about to quit, to check for doing so
  1385.    at an improper time.  */
  1386.  
  1387. void
  1388. quit_error_check ()
  1389. {
  1390.   if (FRAME_DESIRED_GLYPHS (selected_frame) == 0)
  1391.     return;
  1392.   if (FRAME_DESIRED_GLYPHS (selected_frame)->enable[0])
  1393.     abort ();
  1394.   if (FRAME_DESIRED_GLYPHS (selected_frame)->enable[FRAME_HEIGHT (selected_frame) - 1])
  1395.     abort ();
  1396. }
  1397.  
  1398. /* Decide what insert/delete line to do, and do it */
  1399.  
  1400. extern void scrolling_1 ();
  1401.  
  1402. scrolling (frame)
  1403.      FRAME_PTR frame;
  1404. {
  1405.   int unchanged_at_top, unchanged_at_bottom;
  1406.   int window_size;
  1407.   int changed_lines;
  1408.   int *old_hash = (int *) alloca (FRAME_HEIGHT (frame) * sizeof (int));
  1409.   int *new_hash = (int *) alloca (FRAME_HEIGHT (frame) * sizeof (int));
  1410.   int *draw_cost = (int *) alloca (FRAME_HEIGHT (frame) * sizeof (int));
  1411.   register int i;
  1412.   int free_at_end_vpos = FRAME_HEIGHT (frame);
  1413.   register struct frame_glyphs *current_frame = FRAME_CURRENT_GLYPHS (frame);
  1414.   register struct frame_glyphs *desired_frame = FRAME_DESIRED_GLYPHS (frame);
  1415.  
  1416.   /* Compute hash codes of all the lines.
  1417.      Also calculate number of changed lines,
  1418.      number of unchanged lines at the beginning,
  1419.      and number of unchanged lines at the end.  */
  1420.  
  1421.   changed_lines = 0;
  1422.   unchanged_at_top = 0;
  1423.   unchanged_at_bottom = FRAME_HEIGHT (frame);
  1424.   for (i = 0; i < FRAME_HEIGHT (frame); i++)
  1425.     {
  1426.       /* Give up on this scrolling if some old lines are not enabled.  */
  1427.       if (!current_frame->enable[i])
  1428.     return 0;
  1429.       old_hash[i] = line_hash_code (current_frame, i);
  1430.       if (! desired_frame->enable[i])
  1431.     new_hash[i] = old_hash[i];
  1432.       else
  1433.     new_hash[i] = line_hash_code (desired_frame, i);
  1434.  
  1435.       if (old_hash[i] != new_hash[i])
  1436.     {
  1437.       changed_lines++;
  1438.       unchanged_at_bottom = FRAME_HEIGHT (frame) - i - 1;
  1439.     }
  1440.       else if (i == unchanged_at_top)
  1441.     unchanged_at_top++;
  1442.       draw_cost[i] = line_draw_cost (desired_frame, i);
  1443.     }
  1444.  
  1445.   /* If changed lines are few, don't allow preemption, don't scroll.  */
  1446.   if (changed_lines < baud_rate / 2400
  1447.       || unchanged_at_bottom == FRAME_HEIGHT (frame))
  1448.     return 1;
  1449.  
  1450.   window_size = (FRAME_HEIGHT (frame) - unchanged_at_top
  1451.          - unchanged_at_bottom);
  1452.  
  1453.   if (scroll_region_ok)
  1454.     free_at_end_vpos -= unchanged_at_bottom;
  1455.   else if (memory_below_frame)
  1456.     free_at_end_vpos = -1;
  1457.  
  1458.   /* If large window, fast terminal and few lines in common between
  1459.      current frame and desired frame, don't bother with i/d calc. */
  1460.   if (window_size >= 18 && baud_rate > 2400
  1461.       && (window_size >=
  1462.       10 * scrolling_max_lines_saved (unchanged_at_top,
  1463.                       FRAME_HEIGHT (frame) - unchanged_at_bottom,
  1464.                       old_hash, new_hash, draw_cost)))
  1465.     return 0;
  1466.  
  1467.   scrolling_1 (frame, window_size, unchanged_at_top, unchanged_at_bottom,
  1468.            draw_cost + unchanged_at_top - 1,
  1469.            old_hash + unchanged_at_top - 1,
  1470.            new_hash + unchanged_at_top - 1,
  1471.            free_at_end_vpos - unchanged_at_top);
  1472.  
  1473.   return 0;
  1474. }
  1475.  
  1476. /* Return the offset in its buffer of the character at location col, line
  1477.    in the given window.  */
  1478. int
  1479. buffer_posn_from_coords (window, col, line)
  1480.      struct window *window;
  1481.      int col, line;
  1482. {
  1483.   int hscroll = XINT (window->hscroll);
  1484.   int window_left = XFASTINT (window->left);
  1485.  
  1486.   /* The actual width of the window is window->width less one for the
  1487.      DISP_CONTINUE_GLYPH, and less one if it's not the rightmost
  1488.      window.  */
  1489.   int window_width = window_internal_width (window) - 1;
  1490.  
  1491.   int startp = marker_position (window->start);
  1492.  
  1493.   /* Since compute_motion will only operate on the current buffer,
  1494.      we need to save the old one and restore it when we're done.  */
  1495.   struct buffer *old_current_buffer = current_buffer;
  1496.   struct position *posn;
  1497.  
  1498.   current_buffer = XBUFFER (window->buffer);
  1499.  
  1500.   /* It would be nice if we could use FRAME_CURRENT_GLYPHS (XFRAME
  1501.      (window->frame))->bufp to avoid scanning from the very top of
  1502.      the window, but it isn't maintained correctly, and I'm not even
  1503.      sure I will keep it.  */
  1504.   posn = compute_motion (startp, 0,
  1505.              (window == XWINDOW (minibuf_window) && startp == 1
  1506.               ? minibuf_prompt_width : 0)
  1507.              + (hscroll ? 1 - hscroll : 0),
  1508.              ZV, line, col,
  1509.              window_width, hscroll, 0, window);
  1510.  
  1511.   current_buffer = old_current_buffer;
  1512.  
  1513.   /* compute_motion considers frame points past the end of a line
  1514.      to be *after* the newline, i.e. at the start of the next line.
  1515.      This is reasonable, but not really what we want.  So if the
  1516.      result is on a line below LINE, back it up one character.  */
  1517.   if (posn->vpos > line)
  1518.     return posn->bufpos - 1;
  1519.   else
  1520.     return posn->bufpos;
  1521. }
  1522.  
  1523. static int
  1524. count_blanks (r)
  1525.      register GLYPH *r;
  1526. {
  1527.   register GLYPH *p = r;
  1528.   while (*p++ == SPACEGLYPH);
  1529.   return p - r - 1;
  1530. }
  1531.  
  1532. static int
  1533. count_match (str1, str2)
  1534.      GLYPH *str1, *str2;
  1535. {
  1536.   register GLYPH *p1 = str1;
  1537.   register GLYPH *p2 = str2;
  1538.   while (*p1++ == *p2++);
  1539.   return p1 - str1 - 1;
  1540. }
  1541.  
  1542. /* Char insertion/deletion cost vector, from term.c */
  1543. extern int *char_ins_del_vector;
  1544.  
  1545. #define char_ins_del_cost(f) (&char_ins_del_vector[FRAME_WIDTH((f))])
  1546.  
  1547. static void
  1548. update_line (frame, vpos)
  1549.      register FRAME_PTR frame;
  1550.      int vpos;
  1551. {
  1552.   register GLYPH *obody, *nbody, *op1, *op2, *np1, *temp;
  1553.   int *temp1;
  1554.   int tem;
  1555.   int osp, nsp, begmatch, endmatch, olen, nlen;
  1556.   int save;
  1557.   register struct frame_glyphs *current_frame
  1558.     = FRAME_CURRENT_GLYPHS (frame);
  1559.   register struct frame_glyphs *desired_frame
  1560.     = FRAME_DESIRED_GLYPHS (frame);
  1561.  
  1562.   if (desired_frame->highlight[vpos]
  1563.       != (current_frame->enable[vpos] && current_frame->highlight[vpos]))
  1564.     {
  1565.       change_line_highlight (desired_frame->highlight[vpos], vpos,
  1566.                  (current_frame->enable[vpos] ?
  1567.                   current_frame->used[vpos] : 0));
  1568.       current_frame->enable[vpos] = 0;
  1569.     }
  1570.   else
  1571.     reassert_line_highlight (desired_frame->highlight[vpos], vpos);
  1572.  
  1573. #ifdef FAST_DISPLAY
  1574.   if (current_frame->enable[vpos])
  1575.   {
  1576.       obody = current_frame->contents[vpos];
  1577.       olen = current_frame->used[vpos];
  1578.   }
  1579.   else olen = 0;
  1580.  
  1581.   nbody = desired_frame->contents[vpos];
  1582.   nlen = desired_frame->used[vpos];
  1583.  
  1584.   /* Pretend trailing spaces are not there at all,
  1585.      unless for one reason or another we must write all spaces.  */
  1586.   /* We know that the previous character byte contains 0.  */
  1587.   if (! new_screen->highlight[vpos])
  1588.   {
  1589.       if (!must_write_spaces)
  1590.       while (nbody[nlen - 1] == ' ')
  1591.           nlen--;
  1592.   }
  1593.   else
  1594.   {
  1595.       /* For an inverse-video line, give it extra trailing spaces
  1596.      all the way to the screen edge
  1597.      so that the reverse video extends all the way across.  */
  1598.       while (nlen < FRAME_WIDTH(current_frame) - 1)
  1599.       nbody[nlen++] = ' ';
  1600.   }
  1601.   
  1602.   while (olen > 0 && nlen > 0 && *obody == *nbody)
  1603.   {
  1604.       olen--; nlen--; obody++; nbody++;
  1605.   }
  1606.   if (olen > 0 || nlen > 0)
  1607.       move_cursor (vpos, nbody - new_screen->contents[vpos]);
  1608.   if (nlen > 0) output_chars (nbody, nlen);
  1609.   
  1610.   if (olen > nlen && new_screen->used[vpos] != screen_width)
  1611.       clear_end_of_line(current_screen->used[vpos]);
  1612.   /* Exchange contents between current_screen and new_screen.  */
  1613.   temp = new_screen->contents[vpos];
  1614.   new_screen->contents[vpos] = current_screen->contents[vpos];
  1615.   current_screen->contents[vpos] = temp;
  1616.  
  1617.   /* One way or another, this will enable the line being updated.  */
  1618.   current_screen->enable[vpos] = 1;
  1619.   current_screen->used[vpos] = new_screen->used[vpos];
  1620.   current_screen->highlight[vpos] = new_screen->highlight[vpos];
  1621.  
  1622. #else /* not FAST_DISPLAY */
  1623.  
  1624.   if (! current_frame->enable[vpos])
  1625.     {
  1626.       olen = 0;
  1627.     }
  1628.   else
  1629.     {
  1630.       obody = current_frame->glyphs[vpos];
  1631.       olen = current_frame->used[vpos];
  1632.       if (! current_frame->highlight[vpos])
  1633.     {
  1634.       if (!must_write_spaces)
  1635.         while (obody[olen - 1] == SPACEGLYPH && olen > 0)
  1636.           olen--;
  1637.     }
  1638.       else
  1639.     {
  1640.       /* For an inverse-video line, remember we gave it
  1641.          spaces all the way to the frame edge
  1642.          so that the reverse video extends all the way across.  */
  1643.  
  1644.       while (olen < FRAME_WIDTH (frame) - 1)
  1645.         obody[olen++] = SPACEGLYPH;
  1646.     }
  1647.     }
  1648.  
  1649.   /* One way or another, this will enable the line being updated.  */
  1650.   current_frame->enable[vpos] = 1;
  1651.   current_frame->used[vpos] = desired_frame->used[vpos];
  1652.   current_frame->highlight[vpos] = desired_frame->highlight[vpos];
  1653.   current_frame->bufp[vpos] = desired_frame->bufp[vpos];
  1654.  
  1655. #ifdef HAVE_X_WINDOWS
  1656.   if (FRAME_X_P (frame))
  1657.     {
  1658.       current_frame->pix_width[vpos]
  1659.     = current_frame->used[vpos]
  1660.       * FONT_WIDTH (frame->display.x->font);
  1661.       current_frame->pix_height[vpos]
  1662.     = frame->display.x->line_height;
  1663.     }
  1664. #endif /* HAVE_X_WINDOWS */
  1665.  
  1666.   if (!desired_frame->enable[vpos])
  1667.     {
  1668.       nlen = 0;
  1669.       goto just_erase;
  1670.     }
  1671.  
  1672.   nbody = desired_frame->glyphs[vpos];
  1673.   nlen = desired_frame->used[vpos];
  1674.  
  1675.   /* Pretend trailing spaces are not there at all,
  1676.      unless for one reason or another we must write all spaces.  */
  1677.   if (! desired_frame->highlight[vpos])
  1678.     {
  1679.       if (!must_write_spaces)
  1680.     /* We know that the previous character byte contains 0.  */
  1681.     while (nbody[nlen - 1] == SPACEGLYPH)
  1682.       nlen--;
  1683.     }
  1684.   else
  1685.     {
  1686.       /* For an inverse-video line, give it extra trailing spaces
  1687.      all the way to the frame edge
  1688.      so that the reverse video extends all the way across.  */
  1689.  
  1690.       while (nlen < FRAME_WIDTH (frame) - 1)
  1691.     nbody[nlen++] = SPACEGLYPH;
  1692.     }
  1693.  
  1694.   /* If there's no i/d char, quickly do the best we can without it.  */
  1695.   if (!char_ins_del_ok)
  1696.     {
  1697.       int i,j;
  1698.  
  1699. #if 0
  1700.       if (FRAME_X_P (frame))
  1701.     {
  1702.       /* Under X, erase everything we are going to rewrite,
  1703.          and rewrite everything from the first char that's changed.
  1704.          This is part of supporting fonts like Courier
  1705.          whose chars can overlap outside the char width.  */
  1706.       for (i = 0; i < nlen; i++)
  1707.         if (i >= olen || nbody[i] != obody[i])
  1708.           break;
  1709.  
  1710.       cursor_to (vpos, i);
  1711.       if (i != olen)
  1712.         clear_end_of_line (olen);
  1713.       write_glyphs (nbody + i, nlen - i);
  1714.     }
  1715.       else
  1716.     {}
  1717. #endif /* 0 */
  1718.       for (i = 0; i < nlen; i++)
  1719.     {
  1720.       if (i >= olen || nbody[i] != obody[i])    /* A non-matching char. */
  1721.         {
  1722.           cursor_to (vpos, i);
  1723.           for (j = 1; (i + j < nlen &&
  1724.                (i + j >= olen || nbody[i+j] != obody[i+j]));
  1725.            j++);
  1726.  
  1727.           /* Output this run of non-matching chars.  */ 
  1728.           write_glyphs (nbody + i, j);
  1729.           i += j - 1;
  1730.  
  1731.           /* Now find the next non-match.  */
  1732.         }
  1733.     }
  1734.  
  1735.       /* Clear the rest of the line, or the non-clear part of it.  */
  1736.       if (olen > nlen)
  1737.     {
  1738.       cursor_to (vpos, nlen);
  1739.       clear_end_of_line (olen);
  1740.     }
  1741.  
  1742.       /* Exchange contents between current_frame and new_frame.  */
  1743.       temp = desired_frame->glyphs[vpos];
  1744.       desired_frame->glyphs[vpos] = current_frame->glyphs[vpos];
  1745.       current_frame->glyphs[vpos] = temp;
  1746.  
  1747.       /* Exchange charstarts between current_frame and new_frame.  */
  1748.       temp1 = desired_frame->charstarts[vpos];
  1749.       desired_frame->charstarts[vpos] = current_frame->charstarts[vpos];
  1750.       current_frame->charstarts[vpos] = temp1;
  1751.  
  1752.       return;
  1753.     }
  1754.  
  1755.   if (!olen)
  1756.     {
  1757.       nsp = (must_write_spaces || desired_frame->highlight[vpos])
  1758.           ? 0 : count_blanks (nbody);
  1759.       if (nlen > nsp)
  1760.     {
  1761.       cursor_to (vpos, nsp);
  1762.       write_glyphs (nbody + nsp, nlen - nsp);
  1763.     }
  1764.  
  1765.       /* Exchange contents between current_frame and new_frame.  */
  1766.       temp = desired_frame->glyphs[vpos];
  1767.       desired_frame->glyphs[vpos] = current_frame->glyphs[vpos];
  1768.       current_frame->glyphs[vpos] = temp;
  1769.  
  1770.       /* Exchange charstarts between current_frame and new_frame.  */
  1771.       temp1 = desired_frame->charstarts[vpos];
  1772.       desired_frame->charstarts[vpos] = current_frame->charstarts[vpos];
  1773.       current_frame->charstarts[vpos] = temp1;
  1774.  
  1775.       return;
  1776.     }
  1777.  
  1778.   obody[olen] = 1;
  1779.   save = nbody[nlen];
  1780.   nbody[nlen] = 0;
  1781.  
  1782.   /* Compute number of leading blanks in old and new contents.  */
  1783.   osp = count_blanks (obody);
  1784.   if (!desired_frame->highlight[vpos])
  1785.     nsp = count_blanks (nbody);
  1786.   else
  1787.     nsp = 0;
  1788.  
  1789.   /* Compute number of matching chars starting with first nonblank.  */
  1790.   begmatch = count_match (obody + osp, nbody + nsp);
  1791.  
  1792.   /* Spaces in new match implicit space past the end of old.  */
  1793.   /* A bug causing this to be a no-op was fixed in 18.29.  */
  1794.   if (!must_write_spaces && osp + begmatch == olen)
  1795.     {
  1796.       np1 = nbody + nsp;
  1797.       while (np1[begmatch] == SPACEGLYPH)
  1798.     begmatch++;
  1799.     }
  1800.  
  1801.   /* Avoid doing insert/delete char
  1802.      just cause number of leading spaces differs
  1803.      when the following text does not match. */
  1804.   if (begmatch == 0 && osp != nsp)
  1805.     osp = nsp = min (osp, nsp);
  1806.  
  1807.   /* Find matching characters at end of line */
  1808.   op1 = obody + olen;
  1809.   np1 = nbody + nlen;
  1810.   op2 = op1 + begmatch - min (olen - osp, nlen - nsp);
  1811.   while (op1 > op2 && op1[-1] == np1[-1])
  1812.     {
  1813.       op1--;
  1814.       np1--;
  1815.     }
  1816.   endmatch = obody + olen - op1;
  1817.  
  1818.   /* Put correct value back in nbody[nlen].
  1819.      This is important because direct_output_for_insert
  1820.      can write into the line at a later point.
  1821.      If this screws up the zero at the end of the line, re-establish it.  */
  1822.   nbody[nlen] = save;
  1823.   obody[olen] = 0;
  1824.  
  1825.   /* tem gets the distance to insert or delete.
  1826.      endmatch is how many characters we save by doing so.
  1827.      Is it worth it?  */
  1828.  
  1829.   tem = (nlen - nsp) - (olen - osp);
  1830.   if (endmatch && tem
  1831.       && (!char_ins_del_ok || endmatch <= char_ins_del_cost (frame)[tem]))
  1832.     endmatch = 0;
  1833.  
  1834.   /* nsp - osp is the distance to insert or delete.
  1835.      If that is nonzero, begmatch is known to be nonzero also.
  1836.      begmatch + endmatch is how much we save by doing the ins/del.
  1837.      Is it worth it?  */
  1838.  
  1839.   if (nsp != osp
  1840.       && (!char_ins_del_ok
  1841.       || begmatch + endmatch <= char_ins_del_cost (frame)[nsp - osp]))
  1842.     {
  1843.       begmatch = 0;
  1844.       endmatch = 0;
  1845.       osp = nsp = min (osp, nsp);
  1846.     }
  1847.  
  1848.   /* Now go through the line, inserting, writing and
  1849.      deleting as appropriate.  */
  1850.  
  1851.   if (osp > nsp)
  1852.     {
  1853.       cursor_to (vpos, nsp);
  1854.       delete_glyphs (osp - nsp);
  1855.     }
  1856.   else if (nsp > osp)
  1857.     {
  1858.       /* If going to delete chars later in line
  1859.      and insert earlier in the line,
  1860.      must delete first to avoid losing data in the insert */
  1861.       if (endmatch && nlen < olen + nsp - osp)
  1862.     {
  1863.       cursor_to (vpos, nlen - endmatch + osp - nsp);
  1864.       delete_glyphs (olen + nsp - osp - nlen);
  1865.       olen = nlen - (nsp - osp);
  1866.     }
  1867.       cursor_to (vpos, osp);
  1868.       insert_glyphs ((GLYPH *) 0, nsp - osp);
  1869.     }
  1870.   olen += nsp - osp;
  1871.  
  1872.   tem = nsp + begmatch + endmatch;
  1873.   if (nlen != tem || olen != tem)
  1874.     {
  1875.       cursor_to (vpos, nsp + begmatch);
  1876.       if (!endmatch || nlen == olen)
  1877.     {
  1878.       /* If new text being written reaches right margin,
  1879.          there is no need to do clear-to-eol at the end.
  1880.          (and it would not be safe, since cursor is not
  1881.          going to be "at the margin" after the text is done) */
  1882.       if (nlen == FRAME_WIDTH (frame))
  1883.         olen = 0;
  1884.       write_glyphs (nbody + nsp + begmatch, nlen - tem);
  1885.  
  1886. #ifdef obsolete
  1887.  
  1888. /* the following code loses disastrously if tem == nlen.
  1889.    Rather than trying to fix that case, I am trying the simpler
  1890.    solution found above.  */
  1891.  
  1892.       /* If the text reaches to the right margin,
  1893.          it will lose one way or another (depending on AutoWrap)
  1894.          to clear to end of line after outputting all the text.
  1895.          So pause with one character to go and clear the line then.  */
  1896.       if (nlen == FRAME_WIDTH (frame) && fast_clear_end_of_line && olen > nlen)
  1897.         {
  1898.           /* endmatch must be zero, and tem must equal nsp + begmatch */
  1899.           write_glyphs (nbody + tem, nlen - tem - 1);
  1900.           clear_end_of_line (olen);
  1901.           olen = 0;        /* Don't let it be cleared again later */
  1902.           write_glyphs (nbody + nlen - 1, 1);
  1903.         }
  1904.       else
  1905.         write_glyphs (nbody + nsp + begmatch, nlen - tem);
  1906. #endif    /* OBSOLETE */
  1907.  
  1908.     }
  1909.       else if (nlen > olen)
  1910.     {
  1911.       write_glyphs (nbody + nsp + begmatch, olen - tem);
  1912.       insert_glyphs (nbody + nsp + begmatch + olen - tem, nlen - olen);
  1913.       olen = nlen;
  1914.     }
  1915.       else if (olen > nlen)
  1916.     {
  1917.       write_glyphs (nbody + nsp + begmatch, nlen - tem);
  1918.       delete_glyphs (olen - nlen);
  1919.       olen = nlen;
  1920.     }
  1921.     }
  1922.  
  1923.  just_erase:
  1924.   /* If any unerased characters remain after the new line, erase them.  */
  1925.   if (olen > nlen)
  1926.     {
  1927.       cursor_to (vpos, nlen);
  1928.       clear_end_of_line (olen);
  1929.     }
  1930.  
  1931.   /* Exchange contents between current_frame and new_frame.  */
  1932.   temp = desired_frame->glyphs[vpos];
  1933.   desired_frame->glyphs[vpos] = current_frame->glyphs[vpos];
  1934.   current_frame->glyphs[vpos] = temp;
  1935.  
  1936.   /* Exchange charstarts between current_frame and new_frame.  */
  1937.   temp1 = desired_frame->charstarts[vpos];
  1938.   desired_frame->charstarts[vpos] = current_frame->charstarts[vpos];
  1939.   current_frame->charstarts[vpos] = temp1;
  1940. #endif /* not FAST_DISPLAY */
  1941. }
  1942.  
  1943. DEFUN ("open-termscript", Fopen_termscript, Sopen_termscript,
  1944.   1, 1, "FOpen termscript file: ",
  1945.   "Start writing all terminal output to FILE as well as the terminal.\n\
  1946. FILE = nil means just close any termscript file currently open.")
  1947.   (file)
  1948.      Lisp_Object file;
  1949. {
  1950.   if (termscript != 0) fclose (termscript);
  1951.   termscript = 0;
  1952.  
  1953.   if (! NILP (file))
  1954.     {
  1955.       file = Fexpand_file_name (file, Qnil);
  1956.       termscript = fopen (XSTRING (file)->data, "w");
  1957.       if (termscript == 0)
  1958.     report_file_error ("Opening termscript", Fcons (file, Qnil));
  1959.     }
  1960.   return Qnil;
  1961. }
  1962.  
  1963.  
  1964. #ifdef SIGWINCH
  1965. SIGTYPE
  1966. window_change_signal ()
  1967. {
  1968.   int width, height;
  1969.   extern int errno;
  1970.   int old_errno = errno;
  1971.  
  1972.   get_frame_size (&width, &height);
  1973.  
  1974.   /* The frame size change obviously applies to a termcap-controlled
  1975.      frame.  Find such a frame in the list, and assume it's the only
  1976.      one (since the redisplay code always writes to stdout, not a
  1977.      FILE * specified in the frame structure).  Record the new size,
  1978.      but don't reallocate the data structures now.  Let that be done
  1979.      later outside of the signal handler.  */
  1980.  
  1981.   {
  1982.     Lisp_Object tail, frame;
  1983.  
  1984.     FOR_EACH_FRAME (tail, frame)
  1985.       {
  1986.     if (FRAME_TERMCAP_P (XFRAME (frame)))
  1987.       {
  1988.         change_frame_size (XFRAME (frame), height, width, 0, 1);
  1989.         break;
  1990.       }
  1991.       }
  1992.   }
  1993.  
  1994.   signal (SIGWINCH, window_change_signal);
  1995.   errno = old_errno;
  1996. }
  1997. #endif /* SIGWINCH */
  1998.  
  1999.  
  2000. /* Do any change in frame size that was requested by a signal.  */
  2001.  
  2002. do_pending_window_change ()
  2003. {
  2004.   /* If window_change_signal should have run before, run it now.  */
  2005.   while (delayed_size_change)
  2006.     {
  2007.       Lisp_Object tail, frame;
  2008.  
  2009.       delayed_size_change = 0;
  2010.  
  2011.       FOR_EACH_FRAME (tail, frame)
  2012.     {
  2013.       FRAME_PTR f = XFRAME (frame);
  2014.  
  2015.       int height = FRAME_NEW_HEIGHT (f);
  2016.       int width = FRAME_NEW_WIDTH (f);
  2017.  
  2018.       if (height != 0 || width != 0)
  2019.         change_frame_size (f, height, width, 0, 0);
  2020.     }
  2021.     }
  2022. }
  2023.  
  2024.  
  2025. /* Change the frame height and/or width.  Values may be given as zero to
  2026.    indicate no change is to take place. 
  2027.  
  2028.    If DELAY is non-zero, then assume we're being called from a signal
  2029.    handler, and queue the change for later - perhaps the next
  2030.    redisplay.  Since this tries to resize windows, we can't call it
  2031.    from a signal handler.  */
  2032.  
  2033. change_frame_size (frame, newheight, newwidth, pretend, delay)
  2034.      register FRAME_PTR frame;
  2035.      int newheight, newwidth, pretend;
  2036. {
  2037.   /* If we can't deal with the change now, queue it for later.  */
  2038.   if (delay)
  2039.     {
  2040.       FRAME_NEW_HEIGHT (frame) = newheight;
  2041.       FRAME_NEW_WIDTH (frame) = newwidth;
  2042.       delayed_size_change = 1;
  2043.       return;
  2044.     }
  2045.  
  2046.   /* This size-change overrides any pending one for this frame.  */
  2047.   FRAME_NEW_HEIGHT (frame) = 0;
  2048.   FRAME_NEW_WIDTH  (frame) = 0;
  2049.  
  2050.   /* If an argument is zero, set it to the current value.  */
  2051.   newheight || (newheight = FRAME_HEIGHT (frame));
  2052.   newwidth  || (newwidth  = FRAME_WIDTH  (frame));
  2053.  
  2054.   /* Round up to the smallest acceptable size.  */
  2055.   check_frame_size (frame, &newheight, &newwidth);
  2056.  
  2057.   /* If we're not changing the frame size, quit now.  */
  2058.   if (newheight == FRAME_HEIGHT (frame)
  2059.       && newwidth == FRAME_WIDTH (frame))
  2060.     return;
  2061.  
  2062.   if (newheight != FRAME_HEIGHT (frame))
  2063.     {
  2064.       if (FRAME_HAS_MINIBUF_P (frame)
  2065.       && ! FRAME_MINIBUF_ONLY_P (frame))
  2066.     {
  2067.       /* Frame has both root and minibuffer.  */
  2068.       set_window_height (FRAME_ROOT_WINDOW (frame),
  2069.                  newheight - 1 - FRAME_MENU_BAR_LINES (frame), 0);
  2070.       XFASTINT (XWINDOW (FRAME_MINIBUF_WINDOW (frame))->top)
  2071.         = newheight - 1;
  2072.       set_window_height (FRAME_MINIBUF_WINDOW (frame), 1, 0);
  2073.     }
  2074.       else
  2075.     /* Frame has just one top-level window.  */
  2076.     set_window_height (FRAME_ROOT_WINDOW (frame),
  2077.                newheight - FRAME_MENU_BAR_LINES (frame), 0);
  2078.  
  2079.       if (FRAME_TERMCAP_P (frame) && !pretend)
  2080.     FrameRows = newheight;
  2081.  
  2082. #if 0
  2083.       if (frame->output_method == output_termcap)
  2084.     {
  2085.       frame_height = newheight;
  2086.       if (!pretend)
  2087.         FrameRows = newheight;
  2088.     }
  2089. #endif
  2090.     }
  2091.  
  2092.   if (newwidth != FRAME_WIDTH (frame))
  2093.     {
  2094.       set_window_width (FRAME_ROOT_WINDOW (frame), newwidth, 0);
  2095.       if (FRAME_HAS_MINIBUF_P (frame))
  2096.     set_window_width (FRAME_MINIBUF_WINDOW (frame), newwidth, 0);
  2097.  
  2098.       if (FRAME_TERMCAP_P (frame) && !pretend)
  2099.     FrameCols = newwidth;
  2100. #if 0
  2101.       if (frame->output_method == output_termcap)
  2102.     {
  2103.       frame_width = newwidth;
  2104.       if (!pretend)
  2105.         FrameCols = newwidth;
  2106.     }
  2107. #endif
  2108.     }
  2109.  
  2110.   FRAME_HEIGHT (frame) = newheight;
  2111.   FRAME_WIDTH (frame)  = newwidth;
  2112.  
  2113.   if (FRAME_CURSOR_X (frame) >= FRAME_WIDTH (frame))
  2114.     FRAME_CURSOR_X (frame) = FRAME_WIDTH (frame) - 1;
  2115.   if (FRAME_CURSOR_Y (frame) >= FRAME_HEIGHT (frame))
  2116.     FRAME_CURSOR_Y (frame) = FRAME_HEIGHT (frame) - 1;
  2117.  
  2118.   remake_frame_glyphs (frame);
  2119.   calculate_costs (frame);
  2120. }
  2121.  
  2122. DEFUN ("send-string-to-terminal", Fsend_string_to_terminal,
  2123.   Ssend_string_to_terminal, 1, 1, 0,
  2124.   "Send STRING to the terminal without alteration.\n\
  2125. Control characters in STRING will have terminal-dependent effects.")
  2126.   (str)
  2127.      Lisp_Object str;
  2128. {
  2129.   CHECK_STRING (str, 0);
  2130.   fwrite (XSTRING (str)->data, 1, XSTRING (str)->size, stdout);
  2131.   fflush (stdout);
  2132.   if (termscript)
  2133.     {
  2134.       fwrite (XSTRING (str)->data, 1, XSTRING (str)->size, termscript);
  2135.       fflush (termscript);
  2136.     }
  2137.   return Qnil;
  2138. }
  2139.  
  2140. DEFUN ("ding", Fding, Sding, 0, 1, 0,
  2141.   "Beep, or flash the screen.\n\
  2142. Also, unless an argument is given,\n\
  2143. terminate any keyboard macro currently executing.")
  2144.   (arg)
  2145.   Lisp_Object arg;
  2146. {
  2147.   if (!NILP (arg))
  2148.     {
  2149.       if (noninteractive)
  2150.     putchar (07);
  2151.       else
  2152.     ring_bell ();
  2153.       fflush (stdout);
  2154.     }
  2155.   else
  2156.     bitch_at_user ();
  2157.  
  2158.   return Qnil;
  2159. }
  2160.  
  2161. bitch_at_user ()
  2162. {
  2163.   if (noninteractive)
  2164.     putchar (07);
  2165.   else if (!INTERACTIVE)  /* Stop executing a keyboard macro. */
  2166.     error ("Keyboard macro terminated by a command ringing the bell");
  2167.   else
  2168.     ring_bell ();
  2169.   fflush (stdout);
  2170. }
  2171.  
  2172. DEFUN ("sleep-for", Fsleep_for, Ssleep_for, 1, 2, 0,
  2173.   "Pause, without updating display, for SECONDS seconds.\n\
  2174. SECONDS may be a floating-point value, meaning that you can wait for a\n\
  2175. fraction of a second.  Optional second arg MILLISECONDS specifies an\n\
  2176. additional wait period, in milliseconds; this may be useful if your\n\
  2177. Emacs was built without floating point support.\n\
  2178. \(Not all operating systems support waiting for a fraction of a second.)")
  2179.   (seconds, milliseconds)
  2180.      Lisp_Object seconds, milliseconds;
  2181. {
  2182.   int sec, usec;
  2183.  
  2184.   if (NILP (milliseconds))
  2185.     XSET (milliseconds, Lisp_Int, 0);
  2186.   else
  2187.     CHECK_NUMBER (milliseconds, 1);
  2188.   usec = XINT (milliseconds) * 1000;
  2189.  
  2190. #ifdef LISP_FLOAT_TYPE
  2191.   {
  2192.     double duration = extract_float (seconds);
  2193.     sec = (int) duration;
  2194.     usec += (duration - sec) * 1000000;
  2195.   }
  2196. #else
  2197.   CHECK_NUMBER (seconds, 0);
  2198.   sec = XINT (seconds);
  2199. #endif
  2200.  
  2201. #ifndef EMACS_HAS_USECS
  2202.   if (sec == 0 && usec != 0)
  2203.     error ("millisecond `sleep-for' not supported on %s", SYSTEM_TYPE);
  2204. #endif
  2205.  
  2206.   /* Assure that 0 <= usec < 1000000.  */
  2207.   if (usec < 0)
  2208.     {
  2209.       /* We can't rely on the rounding being correct if user is negative.  */
  2210.       if (-1000000 < usec)
  2211.     sec--, usec += 1000000;
  2212.       else
  2213.     sec -= -usec / 1000000, usec = 1000000 - (-usec % 1000000);
  2214.     }
  2215.   else
  2216.     sec += usec / 1000000, usec %= 1000000;
  2217.  
  2218.   if (sec <= 0)
  2219.     return Qnil;
  2220.  
  2221.   {
  2222.     Lisp_Object zero;
  2223.  
  2224.     XFASTINT (zero) = 0;
  2225.     wait_reading_process_input (sec, usec, zero, 0);
  2226.   }
  2227.  
  2228.   /* We should always have wait_reading_process_input; we have a dummy
  2229.      implementation for systems which don't support subprocesses.  */
  2230. #if 0
  2231.   /* No wait_reading_process_input */
  2232.   immediate_quit = 1;
  2233.   QUIT;
  2234.  
  2235. #ifdef VMS
  2236.   sys_sleep (sec);
  2237. #else /* not VMS */
  2238. /* The reason this is done this way 
  2239.     (rather than defined (H_S) && defined (H_T))
  2240.    is because the VMS preprocessor doesn't grok `defined' */
  2241. #ifdef HAVE_SELECT
  2242.   EMACS_GET_TIME (end_time);
  2243.   EMACS_SET_SECS_USECS (timeout, sec, usec);
  2244.   EMACS_ADD_TIME (end_time, end_time, timeout);
  2245.  
  2246.   while (1)
  2247.     {
  2248.       EMACS_GET_TIME (timeout);
  2249.       EMACS_SUB_TIME (timeout, end_time, timeout);
  2250.       if (EMACS_TIME_NEG_P (timeout)
  2251.       || !select (1, 0, 0, 0, &timeout))
  2252.     break;
  2253.     }
  2254. #else /* not HAVE_SELECT */
  2255.   sleep (sec);
  2256. #endif /* HAVE_SELECT */
  2257. #endif /* not VMS */
  2258.   
  2259.   immediate_quit = 0;
  2260. #endif /* no subprocesses */
  2261.  
  2262.   return Qnil;
  2263. }
  2264.  
  2265. /* This is just like wait_reading_process_input, except that
  2266.    it does the redisplay.
  2267.  
  2268.    It's also much like Fsit_for, except that it can be used for
  2269.    waiting for input as well.  One differnce is that sit_for
  2270.    does not call prepare_menu_bars; Fsit_for does call that.  */
  2271.  
  2272. Lisp_Object
  2273. sit_for (sec, usec, reading, display)
  2274.      int sec, usec, reading, display;
  2275. {
  2276.   Lisp_Object read_kbd;
  2277.  
  2278.   if (detect_input_pending ())
  2279.     return Qnil;
  2280.  
  2281.   if (display)
  2282.     redisplay_preserve_echo_area ();
  2283.  
  2284.   if (sec == 0 && usec == 0)
  2285.     return Qt;
  2286.  
  2287. #ifdef SIGIO
  2288.   gobble_input (0);
  2289. #endif
  2290.  
  2291.   XSET (read_kbd, Lisp_Int, reading ? -1 : 1);
  2292.   wait_reading_process_input (sec, usec, read_kbd, display);
  2293.  
  2294.  
  2295.   /* wait_reading_process_input should always be available now; it is
  2296.      simulated in a simple way on systems that don't support
  2297.      subprocesses.  */
  2298. #if 0
  2299.   /* No wait_reading_process_input available.  */
  2300.   immediate_quit = 1;
  2301.   QUIT;
  2302.  
  2303.   waitchannels = 1;
  2304. #ifdef VMS
  2305.   input_wait_timeout (XINT (arg));
  2306. #else                /* not VMS */
  2307. #ifndef HAVE_TIMEVAL
  2308.   timeout_sec = sec;
  2309.   select (1, &waitchannels, 0, 0, &timeout_sec);
  2310. #else /* HAVE_TIMEVAL */
  2311.   timeout.tv_sec = sec;  
  2312.   timeout.tv_usec = usec;
  2313.   select (1, &waitchannels, 0, 0, &timeout);
  2314. #endif /* HAVE_TIMEVAL */
  2315. #endif /* not VMS */
  2316.  
  2317.   immediate_quit = 0;
  2318. #endif 
  2319.  
  2320.   return detect_input_pending () ? Qnil : Qt;
  2321. }
  2322.  
  2323. DEFUN ("sit-for", Fsit_for, Ssit_for, 1, 3, 0,
  2324.   "Perform redisplay, then wait for SECONDS seconds or until input is available.\n\
  2325. SECONDS may be a floating-point value, meaning that you can wait for a\n\
  2326. fraction of a second.  Optional second arg MILLISECONDS specifies an\n\
  2327. additional wait period, in milliseconds; this may be useful if your\n\
  2328. Emacs was built without floating point support.\n\
  2329. \(Not all operating systems support waiting for a fraction of a second.)\n\
  2330. Optional third arg non-nil means don't redisplay, just wait for input.\n\
  2331. Redisplay is preempted as always if input arrives, and does not happen\n\
  2332. if input is available before it starts.\n\
  2333. Value is t if waited the full time with no input arriving.")
  2334.   (seconds, milliseconds, nodisp)
  2335.      Lisp_Object seconds, milliseconds, nodisp;
  2336. {
  2337.   int sec, usec;
  2338.  
  2339.   if (NILP (milliseconds))
  2340.     XSET (milliseconds, Lisp_Int, 0);
  2341.   else
  2342.     CHECK_NUMBER (milliseconds, 1);
  2343.   usec = XINT (milliseconds) * 1000;
  2344.  
  2345. #ifdef LISP_FLOAT_TYPE
  2346.   {
  2347.     double duration = extract_float (seconds);
  2348.     sec = (int) duration;
  2349.     usec += (duration - sec) * 1000000;
  2350.   }
  2351. #else
  2352.   CHECK_NUMBER (seconds, 0);
  2353.   sec = XINT (seconds);
  2354. #endif
  2355.  
  2356. #ifndef EMACS_HAS_USECS
  2357.   if (usec != 0 && sec == 0)
  2358.     error ("millisecond `sit-for' not supported on %s", SYSTEM_TYPE);
  2359. #endif
  2360.  
  2361.   if (NILP (nodisp))
  2362.     prepare_menu_bars ();
  2363.   return sit_for (sec, usec, 0, NILP (nodisp));
  2364. }
  2365.  
  2366. char *terminal_type;
  2367.  
  2368. /* Initialization done when Emacs fork is started, before doing stty. */
  2369. /* Determine terminal type and set terminal_driver */
  2370. /* Then invoke its decoding routine to set up variables
  2371.   in the terminal package */
  2372.  
  2373. init_display ()
  2374. {
  2375. #ifdef HAVE_X_WINDOWS
  2376.   extern int display_arg;
  2377. #endif
  2378.  
  2379.   meta_key = 0;
  2380.   inverse_video = 0;
  2381.   cursor_in_echo_area = 0;
  2382.   terminal_type = (char *) 0;
  2383.  
  2384.   /* Now is the time to initialize this; it's used by init_sys_modes
  2385.      during startup.  */
  2386.   Vwindow_system = Qnil;
  2387.  
  2388.   /* If the user wants to use a window system, we shouldn't bother
  2389.      initializing the terminal.  This is especially important when the
  2390.      terminal is so dumb that emacs gives up before and doesn't bother
  2391.      using the window system.
  2392.  
  2393.      If the DISPLAY environment variable is set, try to use X, and die
  2394.      with an error message if that doesn't work.  */
  2395.  
  2396. #ifdef AMIGA
  2397.   amiga_term_open();
  2398.   if (!inhibit_window_system) 
  2399.     {
  2400.       amiga_term_init();
  2401.       /* Using Intuition V2.04 */
  2402.       Vwindow_system = intern ("intuition");
  2403.       Vwindow_system_version = make_number (2);
  2404.  
  2405.       /* CHFIXME: following ok?, move to amiga_term_init? */
  2406.       
  2407.       remake_frame_glyphs (selected_frame);
  2408.       calculate_costs (selected_frame);
  2409.  
  2410.       /* X and Y coordinates of the cursor between updates. */
  2411.       FRAME_CURSOR_X (selected_frame) = 0;
  2412.       FRAME_CURSOR_Y (selected_frame) = 0;
  2413.  
  2414.       return;
  2415.     }
  2416. #endif /* AMIGA */
  2417.  
  2418. #ifdef HAVE_X_WINDOWS
  2419.   if (! display_arg)
  2420.     {
  2421. #ifdef VMS
  2422.       display_arg = (getenv ("DECW$DISPLAY") != 0);
  2423. #else
  2424.       display_arg = (getenv ("DISPLAY") != 0);
  2425. #endif
  2426.     }
  2427.  
  2428.   if (!inhibit_window_system && display_arg)
  2429.     {
  2430.       Vwindow_system = intern ("x");
  2431. #ifdef HAVE_X11
  2432.       Vwindow_system_version = make_number (11);
  2433. #else
  2434.       Vwindow_system_version = make_number (10);
  2435. #endif
  2436.       return;
  2437.     }
  2438. #endif /* HAVE_X_WINDOWS */
  2439.  
  2440.   /* If no window system has been specified, try to use the terminal.  */
  2441.   if (! isatty (0))
  2442.     {
  2443.       fprintf (stderr, "emacs: standard input is not a tty\n");
  2444.       exit (1);
  2445.     }
  2446.  
  2447.   /* Look at the TERM variable */
  2448.   terminal_type = (char *) getenv ("TERM");
  2449.   if (!terminal_type)
  2450.     {
  2451. #ifdef VMS
  2452.       fprintf (stderr, "Please specify your terminal type.\n\
  2453. For types defined in VMS, use  set term /device=TYPE.\n\
  2454. For types not defined in VMS, use  define emacs_term \"TYPE\".\n\
  2455. \(The quotation marks are necessary since terminal types are lower case.)\n");
  2456. #else
  2457.       fprintf (stderr, "Please set the environment variable TERM; see tset(1).\n");
  2458. #endif
  2459.       exit (1);
  2460.     }
  2461.  
  2462. #ifdef VMS
  2463.   /* VMS DCL tends to upcase things, so downcase term type.
  2464.      Hardly any uppercase letters in terminal types; should be none.  */
  2465.   {
  2466.     char *new = (char *) xmalloc (strlen (terminal_type) + 1);
  2467.     char *p;
  2468.  
  2469.     strcpy (new, terminal_type);
  2470.  
  2471.     for (p = new; *p; p++)
  2472.       if (isupper (*p))
  2473.     *p = tolower (*p);
  2474.  
  2475.     terminal_type = new;
  2476.   }    
  2477. #endif
  2478.  
  2479.   term_init (terminal_type);
  2480.  
  2481.   remake_frame_glyphs (selected_frame);
  2482.   calculate_costs (selected_frame);
  2483.  
  2484.   /* X and Y coordinates of the cursor between updates. */
  2485.   FRAME_CURSOR_X (selected_frame) = 0;
  2486.   FRAME_CURSOR_Y (selected_frame) = 0;
  2487.  
  2488. #ifdef SIGWINCH
  2489. #ifndef CANNOT_DUMP
  2490.   if (initialized)
  2491. #endif /* CANNOT_DUMP */
  2492.     signal (SIGWINCH, window_change_signal);
  2493. #endif /* SIGWINCH */
  2494. }
  2495.  
  2496. syms_of_display ()
  2497. {
  2498. #if defined(MULTI_FRAME) || defined(AMIGA) /* CHFIXME: why disabled if non MULTI_FRAME? */
  2499.   defsubr (&Sredraw_frame);
  2500. #endif
  2501.   defsubr (&Sredraw_display);
  2502.   defsubr (&Sopen_termscript);
  2503.   defsubr (&Sding);
  2504.   defsubr (&Ssit_for);
  2505.   defsubr (&Ssleep_for);
  2506.   defsubr (&Ssend_string_to_terminal);
  2507.  
  2508.   DEFVAR_INT ("baud-rate", &baud_rate,
  2509.     "The output baud rate of the terminal.\n\
  2510. On most systems, changing this value will affect the amount of padding\n\
  2511. and the other strategic decisions made during redisplay.");
  2512.   DEFVAR_BOOL ("inverse-video", &inverse_video,
  2513.     "*Non-nil means invert the entire frame display.\n\
  2514. This means everything is in inverse video which otherwise would not be.");
  2515.   DEFVAR_BOOL ("visible-bell", &visible_bell,
  2516.     "*Non-nil means try to flash the frame to represent a bell.");
  2517.   DEFVAR_BOOL ("no-redraw-on-reenter", &no_redraw_on_reenter,
  2518.     "*Non-nil means no need to redraw entire frame after suspending.\n\
  2519. A non-nil value is useful if the terminal can automatically preserve\n\
  2520. Emacs's frame display when you reenter Emacs.\n\
  2521. It is up to you to set this variable if your terminal can do that.");
  2522.   DEFVAR_LISP ("window-system", &Vwindow_system,
  2523.     "A symbol naming the window-system under which Emacs is running\n\
  2524. \(such as `x'), or nil if emacs is running on an ordinary terminal.");
  2525.   DEFVAR_LISP ("window-system-version", &Vwindow_system_version,
  2526.     "The version number of the window system in use.\n\
  2527. For X windows, this is 10 or 11.");
  2528.   DEFVAR_BOOL ("cursor-in-echo-area", &cursor_in_echo_area,
  2529.     "Non-nil means put cursor in minibuffer, at end of any message there.");
  2530.   DEFVAR_LISP ("glyph-table", &Vglyph_table,
  2531.     "Table defining how to output a glyph code to the frame.\n\
  2532. If not nil, this is a vector indexed by glyph code to define the glyph.\n\
  2533. Each element can be:\n\
  2534.  integer: a glyph code which this glyph is an alias for.\n\
  2535.  string: output this glyph using that string (not impl. in X windows).\n\
  2536.  nil: this glyph mod 256 is char code to output,\n\
  2537.     and this glyph / 256 is face code for X windows (see `face-id').");
  2538.   Vglyph_table = Qnil;
  2539.  
  2540.   DEFVAR_LISP ("standard-display-table", &Vstandard_display_table,
  2541.     "Display table to use for buffers that specify none.\n\
  2542. See `buffer-display-table' for more information.");
  2543.   Vstandard_display_table = Qnil;
  2544.  
  2545.   /* Initialize `window-system', unless init_display already decided it.  */
  2546. #ifdef CANNOT_DUMP
  2547.   if (noninteractive)
  2548. #endif
  2549.     {
  2550.       Vwindow_system = Qnil;
  2551.       Vwindow_system_version = Qnil;
  2552.     }
  2553. }
  2554.