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 / xdisp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-27  |  98.1 KB  |  3,433 lines

  1. /* Display generation from window structure and buffer text.
  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 <config.h>
  22. #include <stdio.h>
  23. /*#include <ctype.h>*/
  24. #undef NULL
  25. #include "lisp.h"
  26. #include "frame.h"
  27. #include "window.h"
  28. #include "termchar.h"
  29. #include "dispextern.h"
  30. #include "buffer.h"
  31. #include "indent.h"
  32. #include "commands.h"
  33. #include "macros.h"
  34. #include "disptab.h"
  35. #include "termhooks.h"
  36. #include "intervals.h"
  37.  
  38. #ifdef USE_PROTOS
  39. #define _XDISP_C
  40. #include "protos.h"
  41. #endif
  42.  
  43. #ifdef AMIGA
  44. #define MAX_WIN_LINE 64
  45. static int win_line_bufpos[MAX_WIN_LINE];
  46. static int win_line_modified;
  47. static struct buffer *win_line_buffer;
  48. #define CLEAR_WIN_LINE() (win_line_buffer = 0)
  49. #include <assert.h> /* CHFIXME */
  50. #define FAR far
  51. #else
  52. #define FAR
  53. #endif /* AMIGA */
  54.  
  55. #ifdef USE_X_TOOLKIT /* CHFIXME: move to config somehow */
  56. #define USE_EXTERNAL_MENU_BAR  
  57. #endif
  58.  
  59. #ifdef USE_EXTERNAL_MENU_BAR 
  60. extern void set_frame_menubar ();
  61. #endif
  62.  
  63. extern int interrupt_input;
  64. extern int command_loop_level;
  65.  
  66. /* Nonzero means print newline before next minibuffer message.  */
  67.  
  68. int noninteractive_need_newline;
  69.  
  70. #define min(a, b) ((a) < (b) ? (a) : (b))
  71. #define max(a, b) ((a) > (b) ? (a) : (b))
  72.  
  73. /* The buffer position of the first character appearing
  74.  entirely or partially on the current frame line.
  75.  Or zero, which disables the optimization for the current frame line. */
  76. static int this_line_bufpos;
  77.  
  78. /* Number of characters past the end of this line,
  79.    including the terminating newline */
  80. static int this_line_endpos;
  81.  
  82. /* The vertical position of this frame line. */
  83. static int this_line_vpos;
  84.  
  85. /* Hpos value for start of display on this frame line.
  86.    Usually zero, but negative if first character really began
  87.    on previous line */
  88. static int this_line_start_hpos;
  89.  
  90. /* Buffer that this_line variables are describing. */
  91. static struct buffer *this_line_buffer;
  92.  
  93. /* Set by try_window_id to the vpos of first of any lines
  94.    scrolled on to the bottom of the frame.  These lines should
  95.    not be included in any general scroll computation.  */
  96. static int scroll_bottom_vpos;
  97.  
  98. /* Value of echo_area_glyphs when it was last acted on.
  99.   If this is nonzero, there is a message on the frame
  100.   in the minibuffer and it should be erased as soon
  101.   as it is no longer requested to appear. */
  102. char *previous_echo_glyphs;
  103.  
  104. /* Nonzero means truncate lines in all windows less wide than the frame */
  105. int truncate_partial_width_windows;
  106.  
  107. Lisp_Object Vglobal_mode_string;
  108.  
  109. /* Marker for where to display an arrow on top of the buffer text.  */
  110. Lisp_Object Voverlay_arrow_position;
  111.  
  112. /* String to display for the arrow.  */
  113. Lisp_Object Voverlay_arrow_string;
  114.  
  115. /* Values of those variables at last redisplay.  */
  116. static Lisp_Object last_arrow_position, last_arrow_string;
  117.  
  118. Lisp_Object Qmenu_bar_update_hook;
  119.  
  120. /* Nonzero if overlay arrow has been displayed once in this window.  */
  121. static int overlay_arrow_seen;
  122.  
  123. /* Nonzero means highlight the region even in nonselected windows.  */
  124. static int highlight_nonselected_windows;
  125.  
  126. /* If cursor motion alone moves point off frame,
  127.    Try scrolling this many lines up or down if that will bring it back.  */
  128. int scroll_step;
  129.  
  130. /* Nonzero if try_window_id has made blank lines at window bottom
  131.  since the last redisplay that paused */
  132. static int blank_end_of_window;
  133.  
  134. /* Number of windows showing the buffer of the selected window.
  135.    keyboard.c refers to this.  */
  136. int buffer_shared;
  137.  
  138. /* display_text_line sets these to the frame position (origin 0) of point,
  139.    whether the window is selected or not.
  140.    Set one to -1 first to determine whether point was found afterwards.  */
  141.  
  142. static int cursor_vpos;
  143. static int cursor_hpos;
  144.  
  145. int debug_end_pos;
  146.  
  147. /* Nonzero means display mode line highlighted */
  148. int mode_line_inverse_video;
  149.  
  150. static void echo_area_display ();
  151. void mark_window_display_accurate ();
  152. static void redisplay_windows ();
  153. static void redisplay_window ();
  154. static void update_menu_bars ();
  155. static void update_menu_bar ();
  156. static void try_window ();
  157. static int try_window_id ();
  158. static struct position *display_text_line ();
  159. static void display_mode_line ();
  160. static int display_mode_element ();
  161. static char *fmodetrunc ();
  162. static char *decode_mode_spec ();
  163. static int display_string ();
  164. static void display_menu_bar ();
  165. static int display_count_lines ();
  166.  
  167. /* Prompt to display in front of the minibuffer contents */
  168. char *minibuf_prompt;
  169.  
  170. /* Width in columns of current minibuffer prompt.  */
  171. int minibuf_prompt_width;
  172.  
  173. /* Message to display instead of minibuffer contents
  174.    This is what the functions error and message make,
  175.    and command echoing uses it as well.
  176.    It overrides the minibuf_prompt as well as the buffer.  */
  177. char *echo_area_glyphs;
  178.  
  179. /* This is the length of the message in echo_area_glyphs.  */
  180. int echo_area_glyphs_length;
  181.  
  182. /* true iff we should redraw the mode lines on the next redisplay */
  183. int update_mode_lines;
  184.  
  185. /* Smallest number of characters before the gap
  186.    at any time since last redisplay that finished.
  187.    Valid for current buffer when try_window_id can be called.  */
  188. int beg_unchanged;
  189.  
  190. /* Smallest number of characters after the gap
  191.    at any time since last redisplay that finished.
  192.    Valid for current buffer when try_window_id can be called.  */
  193. int end_unchanged;
  194.  
  195. /* MODIFF as of last redisplay that finished;
  196.    if it matches MODIFF, beg_unchanged and end_unchanged
  197.    contain no useful information */
  198. int unchanged_modified;
  199.  
  200. /* Nonzero if head_clip or tail_clip of current buffer has changed
  201.    since last redisplay that finished */
  202. int clip_changed;
  203.  
  204. /* Nonzero if window sizes or contents have changed
  205.    since last redisplay that finished */
  206. int windows_or_buffers_changed;
  207.  
  208. /* Nonzero after display_mode_line if %l was used
  209.    and it displayed a line number.  */
  210. int line_number_displayed;
  211.  
  212. /* Maximum buffer size for which to display line numbers.  */
  213. int line_number_display_limit;
  214.  
  215. /* Display an echo area message M with a specified length of LEN chars.
  216.    The string may include null characters.  If m is 0, clear out any
  217.    existing message, and let the minibuffer text show through.
  218.    Do not pass text that is stored in a Lisp string.  */
  219.  
  220. void
  221. message2 (m, len)
  222.      char *m;
  223.      int len;
  224. {
  225.   if (noninteractive)
  226.     {
  227.       if (noninteractive_need_newline)
  228.     putc ('\n', stderr);
  229.       noninteractive_need_newline = 0;
  230.       fwrite (m, len, 1, stderr);
  231.       if (cursor_in_echo_area == 0)
  232.     fprintf (stderr, "\n");
  233.       fflush (stderr);
  234.     }
  235.   /* A null message buffer means that the frame hasn't really been
  236.      initialized yet.  Error messages get reported properly by
  237.      cmd_error, so this must be just an informative message; toss it.  */
  238.   else if (INTERACTIVE && FRAME_MESSAGE_BUF (selected_frame))
  239.     {
  240. #ifdef MULTI_FRAME
  241.       Lisp_Object minibuf_frame;
  242.  
  243.       choose_minibuf_frame ();
  244.       minibuf_frame = WINDOW_FRAME (XWINDOW (minibuf_window));
  245.       FRAME_SAMPLE_VISIBILITY (XFRAME (minibuf_frame));
  246.       if (FRAME_VISIBLE_P (selected_frame)
  247.       && ! FRAME_VISIBLE_P (XFRAME (minibuf_frame)))
  248.     Fmake_frame_visible (WINDOW_FRAME (XWINDOW (minibuf_window)));
  249. #endif
  250.  
  251.       if (m)
  252.     {
  253.       echo_area_glyphs = m;
  254.       echo_area_glyphs_length = len;
  255.     }
  256.       else
  257.     echo_area_glyphs = previous_echo_glyphs = 0;
  258.  
  259.       do_pending_window_change ();
  260.       echo_area_display ();
  261.       update_frame (XFRAME (XWINDOW (minibuf_window)->frame), 1, 1);
  262.       do_pending_window_change ();
  263.       if (frame_up_to_date_hook != 0 && ! gc_in_progress)
  264.     (*frame_up_to_date_hook) (XFRAME (XWINDOW (minibuf_window)->frame));
  265.     }
  266. }
  267.  
  268. void
  269. message1 (m)
  270.      char *m;
  271. {
  272.   message2 (m, (m ? strlen (m) : 0));
  273. }
  274.  
  275. /* Truncate what will be displayed in the echo area
  276.    the next time we display it--but don't redisplay it now.  */
  277.  
  278. void
  279. truncate_echo_area (len)
  280.      int len;
  281. {
  282.   /* A null message buffer means that the frame hasn't really been
  283.      initialized yet.  Error messages get reported properly by
  284.      cmd_error, so this must be just an informative message; toss it.  */
  285.   if (!noninteractive && INTERACTIVE && FRAME_MESSAGE_BUF (selected_frame))
  286.     echo_area_glyphs_length = len;
  287. }
  288.  
  289. /* Nonzero if FRAME_MESSAGE_BUF (selected_frame) is being used by print;
  290.    zero if being used by message.  */
  291. int message_buf_print;
  292.  
  293. /* Dump an informative message to the minibuf.  If m is 0, clear out
  294.    any existing message, and let the minibuffer text show through.  */
  295. /* VARARGS 1 */
  296. void
  297. message (m, a1, a2, a3)
  298.      char *m;
  299. {
  300.   if (noninteractive)
  301.     {
  302.       if (m)
  303.     {
  304.       if (noninteractive_need_newline)
  305.         putc ('\n', stderr);
  306.       noninteractive_need_newline = 0;
  307.       fprintf (stderr, m, a1, a2, a3);
  308.       if (cursor_in_echo_area == 0)
  309.         fprintf (stderr, "\n");
  310.       fflush (stderr);
  311.     }
  312.     }
  313.   else if (INTERACTIVE)
  314.     {
  315.       /* The frame whose minibuffer we're going to display the message on.
  316.      It may be larger than the selected frame, so we need
  317.      to use its buffer, not the selected frame's buffer.  */
  318.       FRAME_PTR echo_frame;
  319. #ifdef MULTI_FRAME
  320.       choose_minibuf_frame ();
  321.       echo_frame = XFRAME (WINDOW_FRAME (XWINDOW (minibuf_window)));
  322. #else
  323.       echo_frame = selected_frame;
  324. #endif
  325.  
  326.       /* A null message buffer means that the frame hasn't really been
  327.      initialized yet.  Error messages get reported properly by
  328.      cmd_error, so this must be just an informative message; toss it.  */
  329.       if (FRAME_MESSAGE_BUF (echo_frame))
  330.     {
  331.       if (m)
  332.         {
  333.           int len;
  334. #ifdef NO_ARG_ARRAY
  335.           int a[3];
  336.           a[0] = a1;
  337.           a[1] = a2;
  338.           a[2] = a3;
  339.  
  340.           len = doprnt (FRAME_MESSAGE_BUF (echo_frame),
  341.                 FRAME_WIDTH (echo_frame), m, 0, 3, a);
  342. #else
  343.           len = doprnt (FRAME_MESSAGE_BUF (echo_frame),
  344.                 FRAME_WIDTH (echo_frame), m, 0, 3, (char **) &a1);
  345. #endif /* NO_ARG_ARRAY */
  346.  
  347.           message2 (FRAME_MESSAGE_BUF (echo_frame), len);
  348.         }
  349.       else
  350.         message1 (0);
  351.  
  352.       /* Print should start at the beginning of the message
  353.          buffer next time.  */
  354.       message_buf_print = 0;
  355.     }
  356.     }
  357. }
  358.  
  359. static void
  360. echo_area_display ()
  361. {
  362.   register int vpos;
  363.   FRAME_PTR f;
  364.  
  365. #ifdef MULTI_FRAME
  366.   choose_minibuf_frame ();
  367. #endif
  368.  
  369.   f = XFRAME (WINDOW_FRAME (XWINDOW (minibuf_window)));
  370.  
  371.   if (! FRAME_VISIBLE_P (f))
  372.     return;
  373.  
  374.   if (frame_garbaged)
  375.     {
  376.       redraw_garbaged_frames ();
  377.       frame_garbaged = 0;
  378.     }
  379.  
  380.   if (echo_area_glyphs || minibuf_level == 0)
  381.     {
  382.       vpos = XFASTINT (XWINDOW (minibuf_window)->top);
  383.       get_display_line (f, vpos, 0);
  384.       display_string (XWINDOW (minibuf_window), vpos,
  385.               echo_area_glyphs ? echo_area_glyphs : "",
  386.               echo_area_glyphs ? echo_area_glyphs_length : -1,
  387.               0, 0, 0, 0, FRAME_WIDTH (f));
  388.  
  389.       /* If desired cursor location is on this line, put it at end of text */
  390.       if (FRAME_CURSOR_Y (f) == vpos)
  391.     FRAME_CURSOR_X (f) = FRAME_DESIRED_GLYPHS (f)->used[vpos];
  392.  
  393.       /* Fill the rest of the minibuffer window with blank lines.  */
  394.       {
  395.     int i;
  396.  
  397.     for (i = vpos + 1;
  398.          i < vpos + XFASTINT (XWINDOW (minibuf_window)->height); i++)
  399.       {
  400.         get_display_line (f, i, 0);
  401.         display_string (XWINDOW (minibuf_window), vpos,
  402.                 "", 0, 0, 0, 0, 0, FRAME_WIDTH (f));
  403.       }
  404.       }
  405.     }
  406.   else if (!EQ (minibuf_window, selected_window))
  407.     windows_or_buffers_changed++;
  408.  
  409.   if (EQ (minibuf_window, selected_window))
  410.     {
  411.       this_line_bufpos = 0;
  412. #ifdef AMIGA
  413.       CLEAR_WIN_LINE();
  414.     }
  415. #endif /* AMIGA */
  416.   
  417.   previous_echo_glyphs = echo_area_glyphs;
  418. }
  419.  
  420. #ifdef HAVE_X_WINDOWS
  421. /* I'm trying this out because I saw Unimpress use it, but it's
  422.    possible that this may mess adversely with some window managers.  -jla
  423.  
  424.    Wouldn't it be nice to use something like mode-line-format to
  425.    describe frame titles?  -JimB  */
  426.  
  427. /* Change the title of the frame to the name of the buffer displayed
  428.    in the currently selected window.  Don't do this for minibuffer frames,
  429.    and don't do it when there's only one non-minibuffer frame.  */
  430. static void
  431. x_consider_frame_title (frame)
  432.      Lisp_Object frame;
  433. {
  434.   FRAME_PTR f = XFRAME (frame);
  435.  
  436.   if (FRAME_X_P (f) && ! FRAME_MINIBUF_ONLY_P (f))
  437.     {
  438.       Lisp_Object title;
  439.  
  440.       title = Qnil;
  441.       if (! EQ (Fnext_frame (frame, Qnil), frame))
  442.     title = XBUFFER (XWINDOW (f->selected_window)->buffer)->name;
  443.  
  444.       x_implicitly_set_name (f, title, Qnil);
  445.     }
  446. }
  447. #endif
  448.  
  449. /* Prepare for redisplay by updating menu-bar item lists when appropriate.
  450.    This can't be done in `redisplay' itself because it can call eval.  */
  451.  
  452. void
  453. prepare_menu_bars ()
  454. {
  455.   register struct window *w = XWINDOW (selected_window);
  456.   int all_windows;
  457.  
  458.   if (noninteractive)
  459.     return;
  460.  
  461.   /* Set the visible flags for all frames.
  462.      Do this before checking for resized or garbaged frames; they want
  463.      to know if their frames are visible.
  464.      See the comment in frame.h for FRAME_SAMPLE_VISIBILITY.  */
  465.   {
  466.     Lisp_Object tail, frame;
  467.  
  468.     FOR_EACH_FRAME (tail, frame)
  469.       FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
  470.   }
  471.  
  472.   /* Notice any pending interrupt request to change frame size.  */
  473.   do_pending_window_change ();
  474.  
  475.   if (frame_garbaged)
  476.     {
  477.       redraw_garbaged_frames ();
  478.       frame_garbaged = 0;
  479.     }
  480.  
  481.   all_windows = (update_mode_lines || buffer_shared > 1
  482.          || clip_changed || windows_or_buffers_changed);
  483.  
  484.   /* Update the menu bar item lists, if appropriate.
  485.      This has to be done before any actual redisplay
  486.      or generation of display lines.  */
  487.   if (all_windows)
  488.     {
  489.       Lisp_Object tail, frame;
  490.  
  491.       FOR_EACH_FRAME (tail, frame)
  492.     update_menu_bar (XFRAME (frame));
  493.     }
  494.   else
  495.     update_menu_bar (selected_frame);
  496. }
  497.  
  498. /* Do a frame update, taking possible shortcuts into account.
  499.    This is the main external entry point for redisplay.
  500.  
  501.    If the last redisplay displayed an echo area message and that
  502.    message is no longer requested, we clear the echo area
  503.    or bring back the minibuffer if that is in use.
  504.  
  505.    Do not call eval from within this function.
  506.    Calls to eval after the call to echo_area_display would confuse
  507.    the display_line mechanism and would cause a crash.
  508.    Calls to eval before that point will work most of the time,
  509.    but can still lose, because  this function
  510.    can be called from signal handlers; with alarms set up;
  511.    or with synchronous processes running.
  512.  
  513.    See Fcall_process; if you called it from here, it could be
  514.    entered recursively.  */
  515.  
  516. static int do_verify_charstarts;
  517.  
  518. void
  519. redisplay ()
  520. {
  521.   register struct window *w = XWINDOW (selected_window);
  522.   register int pause;
  523.   int must_finish = 0;
  524.   int all_windows;
  525.   register int tlbufpos, tlendpos;
  526.   struct position pos;
  527.   extern int input_pending;
  528.  
  529.   if (noninteractive)
  530.     return;
  531.  
  532.   /* Set the visible flags for all frames.
  533.      Do this before checking for resized or garbaged frames; they want
  534.      to know if their frames are visible.
  535.      See the comment in frame.h for FRAME_SAMPLE_VISIBILITY.  */
  536.   {
  537.     Lisp_Object tail, frame;
  538.  
  539.     FOR_EACH_FRAME (tail, frame)
  540.       FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
  541.   }
  542.  
  543.   /* Notice any pending interrupt request to change frame size.  */
  544.   do_pending_window_change ();
  545.  
  546.   if (frame_garbaged)
  547.     {
  548.       redraw_garbaged_frames ();
  549.       frame_garbaged = 0;
  550.     }
  551.  
  552.   if (clip_changed || windows_or_buffers_changed)
  553.     update_mode_lines++;
  554.  
  555.   /* Detect case that we need to write a star in the mode line.  */
  556.   if (XFASTINT (w->last_modified) < MODIFF
  557.       && XFASTINT (w->last_modified) <= current_buffer->save_modified)
  558.     {
  559.       w->update_mode_line = Qt;
  560.       if (buffer_shared > 1)
  561.     update_mode_lines++;
  562.     }
  563.  
  564.   FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
  565.  
  566.   all_windows = update_mode_lines || buffer_shared > 1;
  567.  
  568.   /* If specs for an arrow have changed, do thorough redisplay
  569.      to ensure we remove any arrow that should no longer exist.  */
  570.   if (! EQ (Voverlay_arrow_position, last_arrow_position)
  571.       || ! EQ (Voverlay_arrow_string, last_arrow_string))
  572.     all_windows = 1, clip_changed = 1;
  573.  
  574.   /* Normally the message* functions will have already displayed and
  575.      updated the echo area, but the frame may have been trashed, or
  576.      the update may have been preempted, so display the echo area
  577.      again here.  */
  578.   if (echo_area_glyphs || previous_echo_glyphs)
  579.     {
  580.       echo_area_display ();
  581.       must_finish = 1;
  582.     }
  583. #ifdef AMIGA
  584.     if (windows_or_buffers_changed) CLEAR_WIN_LINE();
  585. #endif
  586.   
  587.   /* If showing region, and mark has changed, must redisplay whole window.  */
  588.   if (((!NILP (Vtransient_mark_mode)
  589.     && !NILP (XBUFFER (w->buffer)->mark_active))
  590.        != !NILP (w->region_showing))
  591.       || (!NILP (w->region_showing)
  592.       && !EQ (w->region_showing,
  593.           Fmarker_position (XBUFFER (w->buffer)->mark))))
  594.     this_line_bufpos = -1;
  595.  
  596.   tlbufpos = this_line_bufpos;
  597.   tlendpos = this_line_endpos;
  598.   if (!all_windows && tlbufpos > 0 && NILP (w->update_mode_line)
  599.       && FRAME_VISIBLE_P (XFRAME (w->frame))
  600.       /* Make sure recorded data applies to current buffer, etc */
  601.       && this_line_buffer == current_buffer
  602.       && current_buffer == XBUFFER (w->buffer)
  603.       && NILP (w->force_start)
  604.       /* Point must be on the line that we have info recorded about */
  605.       && PT >= tlbufpos
  606.       && PT <= Z - tlendpos
  607.       /* All text outside that line, including its final newline,
  608.      must be unchanged */
  609.       && (XFASTINT (w->last_modified) >= MODIFF
  610.       || (beg_unchanged >= tlbufpos - 1
  611.           && GPT >= tlbufpos
  612.           /* If selective display, can't optimize
  613.          if the changes start at the beginning of the line.  */
  614.           && ((XTYPE (current_buffer->selective_display) == Lisp_Int
  615.            && XINT (current_buffer->selective_display) > 0
  616.            ? (beg_unchanged >= tlbufpos
  617.               && GPT > tlbufpos)
  618.            : 1))
  619.           && end_unchanged >= tlendpos
  620.           && Z - GPT >= tlendpos)))
  621.     {
  622.       if (tlbufpos > BEGV && FETCH_CHAR (tlbufpos - 1) != '\n'
  623.       && (tlbufpos == ZV
  624.           || FETCH_CHAR (tlbufpos) == '\n'))
  625.     /* Former continuation line has disappeared by becoming empty */
  626.     goto cancel;
  627.       else if (XFASTINT (w->last_modified) < MODIFF
  628.            || MINI_WINDOW_P (w))
  629.     {
  630.       cursor_vpos = -1;
  631.       overlay_arrow_seen = 0;
  632.       display_text_line (w, tlbufpos, this_line_vpos, this_line_start_hpos,
  633.                  pos_tab_offset (w, tlbufpos));
  634.       /* If line contains point, is not continued,
  635.          and ends at same distance from eob as before, we win */
  636.       if (cursor_vpos >= 0 && this_line_bufpos
  637.           && this_line_endpos == tlendpos)
  638.         {
  639.           /* If this is not the window's last line,
  640.          we must adjust the charstarts of the lines below.  */
  641.           if (this_line_vpos + 1
  642.           < XFASTINT (w->top) + window_internal_height (w))
  643.         {
  644.           int left = XFASTINT (w->left);
  645.           int *charstart_next_line
  646.             = FRAME_CURRENT_GLYPHS (XFRAME (WINDOW_FRAME (w)))->charstarts[this_line_vpos + 1];
  647.           int i;
  648.           int adjust;
  649.  
  650.           if (Z - tlendpos == ZV)
  651.             /* This line ends at end of (accessible part of) buffer.
  652.                There is no newline to count.  */
  653.             adjust = Z - tlendpos - charstart_next_line[left];
  654.           else
  655.             /* This line ends in a newline.
  656.                Must take account of the newline and the rest of the
  657.                text that follows.  */
  658.             adjust = Z - tlendpos + 1 - charstart_next_line[left];
  659.  
  660.           adjust_window_charstarts (w, this_line_vpos, adjust);
  661.         }
  662.  
  663.           if (XFASTINT (w->width) != FRAME_WIDTH (XFRAME (WINDOW_FRAME (w))))
  664.         preserve_other_columns (w);
  665.           goto update;
  666.         }
  667.       else
  668.         goto cancel;
  669.     }
  670.       else if (PT == XFASTINT (w->last_point))
  671.     {
  672.       if (!must_finish)
  673.         {
  674.           do_pending_window_change ();
  675.           return;
  676.         }
  677.       goto update;
  678.     }
  679.       /* If highlighting the region, we can't just move the cursor.  */
  680.       else if (! (!NILP (Vtransient_mark_mode)
  681.           && !NILP (current_buffer->mark_active))
  682.            && NILP (w->region_showing))
  683.     {
  684.       pos = *compute_motion (tlbufpos, 0,
  685.                  XINT (w->hscroll) ? 1 - XINT (w->hscroll) : 0,
  686.                  PT, 2, - (1 << (SHORTBITS - 1)),
  687.                  window_internal_width (w) - 1,
  688.                  XINT (w->hscroll),
  689.                  pos_tab_offset (w, tlbufpos), w);
  690.       if (pos.vpos < 1)
  691.         {
  692.           FRAME_CURSOR_X (selected_frame)
  693.         = XFASTINT (w->left) + max (pos.hpos, 0);
  694.           FRAME_CURSOR_Y (selected_frame) = this_line_vpos;
  695.           goto update;
  696.         }
  697.       else
  698.         goto cancel;
  699.     }
  700.     cancel:
  701.       /* Text changed drastically or point moved off of line */
  702.       cancel_line (this_line_vpos, selected_frame);
  703.     }
  704.  
  705.   this_line_bufpos = 0;
  706.   all_windows |= buffer_shared > 1;
  707.  
  708.   if (all_windows)
  709.     {
  710.       Lisp_Object tail, frame;
  711.  
  712. #ifdef HAVE_X_WINDOWS
  713.       /* Since we're doing a thorough redisplay, we might as well
  714.      recompute all our display faces.  */
  715.       clear_face_vector ();
  716. #endif
  717.  
  718.       /* Recompute # windows showing selected buffer.
  719.      This will be incremented each time such a window is displayed.  */
  720.       buffer_shared = 0;
  721.  
  722.       FOR_EACH_FRAME (tail, frame)
  723.     {
  724.       FRAME_PTR f = XFRAME (frame);
  725.  
  726.       /* Mark all the scroll bars to be removed; we'll redeem the ones
  727.          we want when we redisplay their windows.  */
  728.       if (condemn_scroll_bars_hook)
  729.         (*condemn_scroll_bars_hook) (f);
  730.  
  731.       if (FRAME_VISIBLE_P (f))
  732.         redisplay_windows (FRAME_ROOT_WINDOW (f));
  733. #ifdef HAVE_X_WINDOWS
  734.       else if (FRAME_ICONIFIED_P (f)
  735.            && ! MINI_WINDOW_P (XWINDOW (f->selected_window)))
  736.         x_consider_frame_title (frame);
  737. #endif
  738.  
  739.       /* Any scroll bars which redisplay_windows should have nuked
  740.          should now go away.  */
  741.       if (judge_scroll_bars_hook)
  742.         (*judge_scroll_bars_hook) (f);
  743.     }
  744.     }
  745.   else if (FRAME_VISIBLE_P (selected_frame))
  746.     {
  747.       redisplay_window (selected_window, 1);
  748.       if (XFASTINT (w->width) != FRAME_WIDTH (selected_frame))
  749.     preserve_other_columns (w);
  750.     }
  751.  
  752. update: 
  753.   /* Prevent various kinds of signals during display update.
  754.      stdio is not robust about handling signals,
  755.      which can cause an apparent I/O error.  */
  756.   if (interrupt_input)
  757.     unrequest_sigio ();
  758.   stop_polling ();
  759.  
  760. #ifdef MULTI_FRAME
  761.   if (all_windows)
  762.     {
  763.       Lisp_Object tail;
  764.  
  765.       pause = 0;
  766.  
  767.       for (tail = Vframe_list; CONSP (tail); tail = XCONS (tail)->cdr)
  768.     {
  769.       FRAME_PTR f;
  770.  
  771.       if (XTYPE (XCONS (tail)->car) != Lisp_Frame)
  772.         continue;
  773.  
  774.       f = XFRAME (XCONS (tail)->car);
  775.       if (FRAME_VISIBLE_P (f))
  776.         {
  777.           pause |= update_frame (f, 0, 0);
  778.           if (!pause)
  779.         {
  780.           mark_window_display_accurate (f->root_window, 1);
  781.           if (frame_up_to_date_hook != 0)
  782.             (*frame_up_to_date_hook) (f);
  783.         }
  784.         }
  785.     }
  786.     }
  787.   else
  788. #endif /* MULTI_FRAME */
  789.     {
  790.       if (FRAME_VISIBLE_P (selected_frame))
  791.     pause = update_frame (selected_frame, 0, 0);
  792.  
  793.       /* We may have called echo_area_display at the top of this
  794.      function.  If the echo area is on another frame, that may
  795.      have put text on a frame other than the selected one, so the
  796.      above call to update_frame would not have caught it.  Catch
  797.      it here.  */
  798.       {
  799.     FRAME_PTR mini_frame
  800.       = XFRAME (WINDOW_FRAME (XWINDOW (minibuf_window)));
  801.     
  802.     if (mini_frame != selected_frame)
  803.       pause |= update_frame (mini_frame, 0, 0);
  804.       }
  805.     }
  806.  
  807.   /* If frame does not match, prevent doing single-line-update next time.
  808.      Also, don't forget to check every line to update the arrow.  */
  809.   if (pause)
  810.     {
  811. #ifdef AMIGA
  812.       CLEAR_WIN_LINE();
  813. #endif
  814.       this_line_bufpos = 0;
  815.       if (!NILP (last_arrow_position))
  816.     {
  817.       last_arrow_position = Qt;
  818.       last_arrow_string = Qt;
  819.     }
  820.       /* If we pause after scrolling, some lines in current_frame
  821.      may be null, so preserve_other_columns won't be able to
  822.      preserve all the vertical-bar separators.  So, avoid using it
  823.      in that case.  */
  824.       if (XFASTINT (w->width) != FRAME_WIDTH (selected_frame))
  825.     update_mode_lines = 1;
  826.     }
  827.  
  828.   /* Now text on frame agrees with windows, so
  829.      put info into the windows for partial redisplay to follow */
  830.  
  831.   if (!pause)
  832.     {
  833.       register struct buffer *b = XBUFFER (w->buffer);
  834.  
  835.       blank_end_of_window = 0;
  836.       clip_changed = 0;
  837.       unchanged_modified = BUF_MODIFF (b);
  838.       beg_unchanged = BUF_GPT (b) - BUF_BEG (b);
  839.       end_unchanged = BUF_Z (b) - BUF_GPT (b);
  840.  
  841.       XFASTINT (w->last_point) = BUF_PT (b);
  842.       XFASTINT (w->last_point_x) = FRAME_CURSOR_X (selected_frame);
  843.       XFASTINT (w->last_point_y) = FRAME_CURSOR_Y (selected_frame);
  844.  
  845.       if (all_windows)
  846.     mark_window_display_accurate (FRAME_ROOT_WINDOW (selected_frame), 1);
  847.       else
  848.     {
  849.       w->update_mode_line = Qnil;
  850.       XFASTINT (w->last_modified) = BUF_MODIFF (b);
  851.       w->window_end_valid = w->buffer;
  852.       last_arrow_position = Voverlay_arrow_position;
  853.       last_arrow_string = Voverlay_arrow_string;
  854.       if (do_verify_charstarts)
  855.         verify_charstarts (w);
  856.       if (frame_up_to_date_hook != 0)
  857.         (*frame_up_to_date_hook) (selected_frame);
  858.     }
  859.       update_mode_lines = 0;
  860.       windows_or_buffers_changed = 0;
  861.     }
  862.  
  863.   /* Start SIGIO interrupts coming again.
  864.      Having them off during the code above
  865.      makes it less likely one will discard output,
  866.      but not impossible, since there might be stuff
  867.      in the system buffer here.
  868.      But it is much hairier to try to do anything about that.  */
  869.  
  870.   if (interrupt_input)
  871.     request_sigio ();
  872.   start_polling ();
  873.  
  874.   /* Change frame size now if a change is pending.  */
  875.   do_pending_window_change ();
  876.  
  877.   /* If we just did a pending size change, redisplay again
  878.      for the new size.  */
  879.   if (windows_or_buffers_changed && !pause)
  880.     redisplay ();
  881. }
  882.  
  883. /* Redisplay, but leave alone any recent echo area message
  884.    unless another message has been requested in its place.
  885.  
  886.    This is useful in situations where you need to redisplay but no
  887.    user action has occurred, making it inappropriate for the message
  888.    area to be cleared.  See tracking_off and
  889.    wait_reading_process_input for examples of these situations.  */
  890.  
  891. redisplay_preserve_echo_area ()
  892. {
  893.   if (echo_area_glyphs == 0 && previous_echo_glyphs != 0)
  894.     {
  895.       echo_area_glyphs = previous_echo_glyphs;
  896.       redisplay ();
  897.       echo_area_glyphs = 0;
  898.     }
  899.   else
  900.     redisplay ();
  901. }
  902.  
  903. void
  904. mark_window_display_accurate (window, flag)
  905.      Lisp_Object window;
  906.      int flag;
  907. {
  908.   register struct window *w;
  909.  
  910.   for (;!NILP (window); window = w->next)
  911.     {
  912.       if (XTYPE (window) != Lisp_Window) abort ();
  913.       w = XWINDOW (window);
  914.  
  915.       if (!NILP (w->buffer))
  916.     {
  917.       XFASTINT (w->last_modified)
  918.         = !flag ? 0
  919.           : XBUFFER (w->buffer) == current_buffer
  920.         ? MODIFF : BUF_MODIFF (XBUFFER (w->buffer));
  921.  
  922.       /* Record if we are showing a region, so can make sure to
  923.          update it fully at next redisplay.  */
  924.       w->region_showing = (!NILP (Vtransient_mark_mode)
  925.                    && !NILP (XBUFFER (w->buffer)->mark_active)
  926.                    ? Fmarker_position (XBUFFER (w->buffer)->mark)
  927.                    : Qnil);
  928.     }
  929.  
  930.       w->window_end_valid = w->buffer;
  931.       w->update_mode_line = Qnil;
  932.  
  933.       if (!NILP (w->vchild))
  934.     mark_window_display_accurate (w->vchild, flag);
  935.       if (!NILP (w->hchild))
  936.     mark_window_display_accurate (w->hchild, flag);
  937.     }
  938.  
  939.   if (flag)
  940.     {
  941.       last_arrow_position = Voverlay_arrow_position;
  942.       last_arrow_string = Voverlay_arrow_string;
  943.     }
  944.   else
  945.     {
  946.       /* t is unequal to any useful value of Voverlay_arrow_... */
  947.       last_arrow_position = Qt;
  948.       last_arrow_string = Qt;
  949.     }
  950. }
  951.  
  952. /* Update the menu bar item list for frame F.
  953.    This has to be done before we start to fill in any display lines,
  954.    because it can call eval.  */
  955.  
  956. static void
  957. update_menu_bar (f)
  958.      FRAME_PTR f;
  959. {
  960.   struct buffer *old = current_buffer;
  961.   Lisp_Object window;
  962.   register struct window *w;
  963.   window = FRAME_SELECTED_WINDOW (f);
  964.   w = XWINDOW (window);
  965.   
  966.   if (update_mode_lines)
  967.     w->update_mode_line = Qt;
  968.  
  969.   if (
  970. #ifdef USE_EXTERNAL_MENU_BAR
  971.       FRAME_EXTERNAL_MENU_BAR (f) 
  972. #else
  973.       FRAME_MENU_BAR_LINES (f) > 0
  974. #endif
  975.       )
  976.     {
  977.       /* If the user has switched buffers or windows, we need to
  978.      recompute to reflect the new bindings.  But we'll
  979.      recompute when update_mode_lines is set too; that means
  980.      that people can use force-mode-line-update to request
  981.      that the menu bar be recomputed.  The adverse effect on
  982.      the rest of the redisplay algorithm is about the same as
  983.      windows_or_buffers_changed anyway.  */
  984.       if (windows_or_buffers_changed
  985.       || !NILP (w->update_mode_line)
  986.       || (XFASTINT (w->last_modified) < MODIFF
  987.           && (XFASTINT (w->last_modified)
  988.           <= XBUFFER (w->buffer)->save_modified)))
  989.     {
  990.       struct buffer *prev = current_buffer;
  991.       call1 (Vrun_hooks, Qmenu_bar_update_hook);
  992.       current_buffer = XBUFFER (w->buffer);
  993.       FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
  994.       current_buffer = prev;
  995. #ifdef USE_EXTERNAL_MENU_BAR
  996.       set_frame_menubar (f, 0);
  997. #endif /* USE_EXTERNAL_MENU_BAR */
  998.     }
  999.     }
  1000. }
  1001.  
  1002. int do_id = 1;
  1003.  
  1004. /* Redisplay WINDOW and its subwindows and siblings.  */
  1005.  
  1006. static void
  1007. redisplay_windows (window)
  1008.      Lisp_Object window;
  1009. {
  1010.   for (; !NILP (window); window = XWINDOW (window)->next)
  1011.     redisplay_window (window, 0);
  1012. }
  1013.  
  1014. /* Redisplay window WINDOW and its subwindows.  */
  1015.  
  1016. static void
  1017. redisplay_window (window, just_this_one)
  1018.      Lisp_Object window;
  1019.      int just_this_one;
  1020. {
  1021.   register struct window *w = XWINDOW (window);
  1022.   FRAME_PTR f = XFRAME (WINDOW_FRAME (w));
  1023.   int height;
  1024.   register int lpoint = PT;
  1025.   struct buffer *old = current_buffer;
  1026.   register int width = window_internal_width (w) - 1;
  1027.   register int startp;
  1028.   register int hscroll = XINT (w->hscroll);
  1029.   struct position pos;
  1030.   int opoint = PT;
  1031.   int tem;
  1032.   int window_needs_modeline;
  1033.  
  1034.   if (FRAME_HEIGHT (f) == 0) abort (); /* Some bug zeros some core */
  1035.  
  1036.   /* If this is a combination window, do its children; that's all.  */
  1037.  
  1038.   if (!NILP (w->vchild))
  1039.     {
  1040.       redisplay_windows (w->vchild);
  1041.       return;
  1042.     }
  1043.   if (!NILP (w->hchild))
  1044.     {
  1045.       redisplay_windows (w->hchild);
  1046.       return;
  1047.     }
  1048.   if (NILP (w->buffer))
  1049.     abort ();
  1050.   
  1051.   height = window_internal_height (w);
  1052.  
  1053.   if (MINI_WINDOW_P (w))
  1054.     {
  1055.       if (w == XWINDOW (minibuf_window))
  1056.     {
  1057.       if (echo_area_glyphs)
  1058.         /* We've already displayed the echo area glyphs, if any.  */
  1059.         goto finish_scroll_bars;
  1060.     }
  1061.       else
  1062.     {
  1063.       /* This is a minibuffer, but it's not the currently active one, so
  1064.          clear it.  */
  1065.       int vpos = XFASTINT (XWINDOW (FRAME_MINIBUF_WINDOW (f))->top);
  1066.       int i;
  1067.  
  1068.       for (i = 0; i < height; i++)
  1069.         {
  1070.           get_display_line (f, vpos + i, 0);
  1071.           display_string (w, vpos + i, "", 0, 0, 0, 1, 0, width);
  1072.         }
  1073.       
  1074.       goto finish_scroll_bars;
  1075.     }
  1076.     }
  1077.  
  1078.   if (update_mode_lines)
  1079.     w->update_mode_line = Qt;
  1080.  
  1081.   /* Otherwise set up data on this window; select its buffer and point value */
  1082.  
  1083.   current_buffer = XBUFFER (w->buffer);
  1084.   opoint = PT;
  1085.  
  1086.   /* Count number of windows showing the selected buffer.  */
  1087.  
  1088.   if (!just_this_one
  1089.       && current_buffer == XBUFFER (XWINDOW (selected_window)->buffer))
  1090.     buffer_shared++;
  1091.  
  1092.   /* POINT refers normally to the selected window.
  1093.      For any other window, set up appropriate value.  */
  1094.  
  1095.   if (!EQ (window, selected_window))
  1096.     {
  1097.       SET_PT (marker_position (w->pointm));
  1098.       if (PT < BEGV)
  1099.     {
  1100.       SET_PT (BEGV);
  1101.       Fset_marker (w->pointm, make_number (PT), Qnil);
  1102.     }
  1103.       else if (PT > (ZV - 1))
  1104.     {
  1105.       SET_PT (ZV);
  1106.       Fset_marker (w->pointm, make_number (PT), Qnil);
  1107.     }
  1108.     }
  1109.  
  1110.   /* If window-start is screwed up, choose a new one.  */
  1111.   if (XMARKER (w->start)->buffer != current_buffer)
  1112.     goto recenter;
  1113.  
  1114.   startp = marker_position (w->start);
  1115.  
  1116.   /* Handle case where place to start displaying has been specified,
  1117.      unless the specified location is outside the accessible range.  */
  1118.   if (!NILP (w->force_start))
  1119.     {
  1120.       /* Forget any recorded base line for line number display.  */
  1121.       w->base_line_number = Qnil;
  1122.       w->update_mode_line = Qt;
  1123.       w->force_start = Qnil;
  1124.       XFASTINT (w->last_modified) = 0;
  1125.       if (startp < BEGV) startp = BEGV;
  1126.       if (startp > ZV)   startp = ZV;
  1127.       try_window (window, startp);
  1128.       if (cursor_vpos < 0)
  1129.     {
  1130.       /* ??? What should happen here if highlighting a region?  */
  1131.       /* If point does not appear, move point so it does appear */
  1132.       pos = *compute_motion (startp, 0,
  1133.                 ((EQ (window, minibuf_window) && startp == 1)
  1134.                  ? minibuf_prompt_width : 0)
  1135.                 +
  1136.                 (hscroll ? 1 - hscroll : 0),
  1137.                 ZV, height / 2,
  1138.                 - (1 << (SHORTBITS - 1)),
  1139.                 width, hscroll, pos_tab_offset (w, startp), w);
  1140.       SET_PT (pos.bufpos);
  1141.       if (w != XWINDOW (selected_window))
  1142.         Fset_marker (w->pointm, make_number (PT), Qnil);
  1143.       else
  1144.         {
  1145.           if (current_buffer == old)
  1146.         lpoint = PT;
  1147.           FRAME_CURSOR_X (f) = max (0, pos.hpos) + XFASTINT (w->left);
  1148.           FRAME_CURSOR_Y (f) = pos.vpos + XFASTINT (w->top);
  1149.         }
  1150.     }
  1151.       goto done;
  1152.     }
  1153.  
  1154.   /* Handle case where text has not changed, only point,
  1155.      and it has not moved off the frame */
  1156.  
  1157.   /* This code is not used for minibuffer for the sake of
  1158.      the case of redisplaying to replace an echo area message;
  1159.      since in that case the minibuffer contents per se are usually unchanged.
  1160.      This code is of no real use in the minibuffer since
  1161.      the handling of this_line_bufpos, etc.,
  1162.      in redisplay handles the same cases.  */
  1163.  
  1164.   if (XFASTINT (w->last_modified) >= MODIFF
  1165.       && PT >= startp && !clip_changed
  1166.       && (just_this_one || XFASTINT (w->width) == FRAME_WIDTH (f))
  1167.       /* Can't use this case if highlighting a region.  */
  1168.       && !(!NILP (Vtransient_mark_mode) && !NILP (current_buffer->mark_active))
  1169.       && NILP (w->region_showing)
  1170.       && !EQ (window, minibuf_window))
  1171.     {
  1172. #ifdef XXXXAMIGA /* CHFIXME: check, mind. highlight case */
  1173.        int *wlb;
  1174.       int line, best_startp, best_vpos;
  1175.  
  1176.       best_startp = startp; best_vpos = 0;
  1177.       if (win_line_buffer && BUF_MODIFF(win_line_buffer) > win_line_modified)
  1178.       CLEAR_WIN_LINE();
  1179.       if (current_buffer == win_line_buffer && current_buffer == XBUFFER (w->buffer))
  1180.         {
  1181.       /* Try & find line to start from */
  1182.       wlb = win_line_bufpos; line = 0;
  1183.       while (line < MAX_WIN_LINE)
  1184.         {
  1185.           if (*wlb && point > *wlb && *wlb > best_startp)
  1186.             {
  1187.           best_startp = *wlb;
  1188.           best_vpos = line;
  1189.             }
  1190.           line++; wlb++;
  1191.         }
  1192.         }
  1193.       pos = *compute_motion (best_startp, best_vpos, (hscroll ? 1 - hscroll : 0),
  1194.                   point, height + 1, 10000, width, hscroll,
  1195.                 pos_tab_offset (w, best_startp), w);
  1196. #else /* not AMIGA */
  1197.       pos = *compute_motion (startp, 0, (hscroll ? 1 - hscroll : 0),
  1198.                 PT, height + 1, 10000, width, hscroll,
  1199.                 pos_tab_offset (w, startp), w);
  1200. #endif /* not AMIGA */
  1201.       if (pos.vpos < height)
  1202.     {
  1203.       /* Ok, point is still on frame */
  1204.       if (w == XWINDOW (FRAME_SELECTED_WINDOW (f)))
  1205.         {
  1206.           /* These variables are supposed to be origin 1 */
  1207.           FRAME_CURSOR_X (f) = max (0, pos.hpos) + XFASTINT (w->left);
  1208.           FRAME_CURSOR_Y (f) = pos.vpos + XFASTINT (w->top);
  1209.         }
  1210.       /* This doesn't do the trick, because if a window to the right of
  1211.          this one must be redisplayed, this does nothing because there
  1212.          is nothing in DesiredFrame yet, and then the other window is
  1213.          redisplayed, making likes that are empty in this window's columns.
  1214.          if (XFASTINT (w->width) != FRAME_WIDTH (f))
  1215.          preserve_my_columns (w);
  1216.          */
  1217.       goto done;
  1218.     }
  1219.       /* Don't bother trying redisplay with same start;
  1220.     we already know it will lose */
  1221.     }
  1222.   /* If current starting point was originally the beginning of a line
  1223.      but no longer is, find a new starting point.  */
  1224.   else if (!NILP (w->start_at_line_beg)
  1225.        && !(startp == BEGV
  1226.         || FETCH_CHAR (startp - 1) == '\n'))
  1227.     {
  1228.       goto recenter;
  1229.     }
  1230.   else if (just_this_one && !MINI_WINDOW_P (w)
  1231.        && PT >= startp
  1232.        && XFASTINT (w->last_modified)
  1233.        /* or else vmotion on first line won't work.  */
  1234.        && ! NILP (w->start_at_line_beg)
  1235.        && ! EQ (w->window_end_valid, Qnil)
  1236.        && do_id && !clip_changed
  1237.        && !blank_end_of_window
  1238.        && XFASTINT (w->width) == FRAME_WIDTH (f)
  1239.        /* Can't use this case if highlighting a region.  */
  1240.        && !(!NILP (Vtransient_mark_mode)
  1241.         && !NILP (current_buffer->mark_active))
  1242.        && NILP (w->region_showing)
  1243.        && EQ (last_arrow_position, Voverlay_arrow_position)
  1244.        && EQ (last_arrow_string, Voverlay_arrow_string)
  1245.        && (tem = try_window_id (FRAME_SELECTED_WINDOW (f)))
  1246.        && tem != -2)
  1247.     {
  1248.       /* tem > 0 means success.  tem == -1 means choose new start.
  1249.      tem == -2 means try again with same start,
  1250.       and nothing but whitespace follows the changed stuff.
  1251.      tem == 0 means try again with same start.  */
  1252.       if (tem > 0)
  1253.     goto done;
  1254.     }
  1255.   else if (startp >= BEGV && startp <= ZV
  1256.        /* Avoid starting display at end of buffer! */
  1257.        && (startp < ZV || startp == BEGV
  1258.            || (XFASTINT (w->last_modified) >= MODIFF)))
  1259.     {
  1260.       /* Try to redisplay starting at same place as before */
  1261.       /* If point has not moved off frame, accept the results */
  1262.       try_window (window, startp);
  1263.       if (cursor_vpos >= 0)
  1264.     {
  1265.       if (!just_this_one || clip_changed || beg_unchanged < startp)
  1266.         /* Forget any recorded base line for line number display.  */
  1267.         w->base_line_number = Qnil;
  1268.       goto done;
  1269.     }
  1270.       else
  1271.     cancel_my_columns (w);
  1272.     }
  1273.  
  1274.   XFASTINT (w->last_modified) = 0;
  1275.   w->update_mode_line = Qt;
  1276.  
  1277.   /* Try to scroll by specified few lines */
  1278.  
  1279.   if (scroll_step && !clip_changed)
  1280.     {
  1281.       if (PT > startp)
  1282.     {
  1283.       pos = *vmotion (Z - XFASTINT (w->window_end_pos),
  1284.               scroll_step, width, hscroll, window);
  1285.       if (pos.vpos >= height)
  1286.         goto scroll_fail;
  1287.     }
  1288.  
  1289.       pos = *vmotion (startp, PT < startp ? - scroll_step : scroll_step,
  1290.               width, hscroll, window);
  1291.  
  1292.       if (PT >= pos.bufpos)
  1293.     {
  1294.       try_window (window, pos.bufpos);
  1295.       if (cursor_vpos >= 0)
  1296.         {
  1297.           if (!just_this_one || clip_changed || beg_unchanged < startp)
  1298.         /* Forget any recorded base line for line number display.  */
  1299.         w->base_line_number = Qnil;
  1300.           goto done;
  1301.         }
  1302.       else
  1303.         cancel_my_columns (w);
  1304.     }
  1305.     scroll_fail: ;
  1306.     }
  1307.  
  1308.   /* Finally, just choose place to start which centers point */
  1309.  
  1310. recenter:
  1311.   /* Forget any previously recorded base line for line number display.  */
  1312.   w->base_line_number = Qnil;
  1313.  
  1314.   pos = *vmotion (PT, - (height / 2), width, hscroll, window);
  1315.   try_window (window, pos.bufpos);
  1316.  
  1317.   startp = marker_position (w->start);
  1318.   w->start_at_line_beg = 
  1319.     (startp == BEGV || FETCH_CHAR (startp - 1) == '\n') ? Qt : Qnil;
  1320.  
  1321. done:
  1322.   if ((!NILP (w->update_mode_line)
  1323.        /* If window not full width, must redo its mode line
  1324.       if the window to its side is being redone */
  1325.        || (!just_this_one && width < FRAME_WIDTH (f) - 1)
  1326.        || INTEGERP (w->base_line_pos))
  1327.       && height != XFASTINT (w->height))
  1328.     display_mode_line (w);
  1329.   if (! line_number_displayed
  1330.       && ! BUFFERP (w->base_line_pos))
  1331.     {
  1332.       w->base_line_pos = Qnil;
  1333.       w->base_line_number = Qnil;
  1334.     }
  1335.  
  1336.   /* When we reach a frame's selected window, redo the frame's menu bar.  */
  1337.   if (!NILP (w->update_mode_line)
  1338. #ifdef USE_EXTERNAL_MENU_BAR
  1339.       && FRAME_EXTERNAL_MENU_BAR (f) 
  1340. #else
  1341.       && FRAME_MENU_BAR_LINES (f) > 0
  1342. #endif
  1343.       && EQ (FRAME_SELECTED_WINDOW (f), window))
  1344.     display_menu_bar (w);
  1345.  
  1346.  finish_scroll_bars:
  1347.   if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
  1348.     {
  1349.       int start, end, whole;
  1350.  
  1351.       /* Calculate the start and end positions for the current window.
  1352.      At some point, it would be nice to choose between scrollbars
  1353.      which reflect the whole buffer size, with special markers
  1354.      indicating narrowing, and scrollbars which reflect only the
  1355.      visible region.
  1356.  
  1357.      Note that minibuffers sometimes aren't displaying any text.  */
  1358.       if (! MINI_WINDOW_P (w)
  1359.       || (w == XWINDOW (minibuf_window) && ! echo_area_glyphs))
  1360.     {
  1361.       whole = ZV - BEGV;
  1362.       start = startp - BEGV;
  1363.       /* I don't think this is guaranteed to be right.  For the
  1364.          moment, we'll pretend it is.  */
  1365.       end = (Z - XINT (w->window_end_pos)) - BEGV;
  1366.  
  1367.       if (end < start) end = start;
  1368.       if (whole < (end - start)) whole = end - start;
  1369.     }
  1370.       else
  1371.     start = end = whole = 0;
  1372.  
  1373.       /* Indicate what this scroll bar ought to be displaying now.  */
  1374.       (*set_vertical_scroll_bar_hook) (w, end - start, whole, start);
  1375.  
  1376.       /* Note that we actually used the scroll bar attached to this window,
  1377.      so it shouldn't be deleted at the end of redisplay.  */
  1378.       (*redeem_scroll_bar_hook) (w);
  1379.     }
  1380.  
  1381.   SET_PT (opoint);
  1382.   current_buffer = old;
  1383.   SET_PT (lpoint);
  1384. }
  1385.  
  1386. /* Do full redisplay on one window, starting at position `pos'. */
  1387.  
  1388. static void
  1389. try_window (window, pos)
  1390.      Lisp_Object window;
  1391.      register int pos;
  1392. {
  1393.   register struct window *w = XWINDOW (window);
  1394.   register int height = window_internal_height (w);
  1395.   register int vpos = XFASTINT (w->top);
  1396.   register int last_text_vpos = vpos;
  1397.   int tab_offset = pos_tab_offset (w, pos);
  1398.   FRAME_PTR f = XFRAME (w->frame);
  1399.   int width = window_internal_width (w) - 1;
  1400.   struct position val;
  1401.  
  1402. #ifdef AMIGA
  1403.   if (XBUFFER(w->buffer) == win_line_buffer) CLEAR_WIN_LINE();
  1404. #endif 
  1405.  
  1406.   Fset_marker (w->start, make_number (pos), Qnil);
  1407.   cursor_vpos = -1;
  1408.   overlay_arrow_seen = 0;
  1409.   val.hpos = XINT (w->hscroll) ? 1 - XINT (w->hscroll) : 0;
  1410.  
  1411.   while (--height >= 0)
  1412.     {
  1413.       val = *display_text_line (w, pos, vpos, val.hpos, tab_offset);
  1414.       tab_offset += width;
  1415.       if (val.vpos) tab_offset = 0;
  1416.       vpos++;
  1417.       if (pos != val.bufpos)
  1418.     last_text_vpos
  1419.       /* Next line, unless prev line ended in end of buffer with no cr */
  1420.       = vpos - (val.vpos && (FETCH_CHAR (val.bufpos - 1) != '\n'
  1421. #ifdef USE_TEXT_PROPERTIES
  1422.                  || ! NILP (Fget_char_property (val.bufpos-1,
  1423.                                 Qinvisible,
  1424.                                 window))
  1425. #endif
  1426.                  ));
  1427.       pos = val.bufpos;
  1428.     }
  1429.  
  1430.   /* If last line is continued in middle of character,
  1431.      include the split character in the text considered on the frame */
  1432.   if (val.hpos < (XINT (w->hscroll) ? 1 - XINT (w->hscroll) : 0))
  1433.     pos++;
  1434.  
  1435.   /* If bottom just moved off end of frame, change mode line percentage.  */
  1436.   if (XFASTINT (w->window_end_pos) == 0
  1437.       && Z != pos)
  1438.     w->update_mode_line = Qt;
  1439.  
  1440.   /* Say where last char on frame will be, once redisplay is finished.  */
  1441.   XFASTINT (w->window_end_pos) = Z - pos;
  1442.   XFASTINT (w->window_end_vpos) = last_text_vpos - XFASTINT (w->top);
  1443.   /* But that is not valid info until redisplay finishes.  */
  1444.   w->window_end_valid = Qnil;
  1445. }
  1446.  
  1447. /* Try to redisplay when buffer is modified locally,
  1448.  computing insert/delete line to preserve text outside
  1449.  the bounds of the changes.
  1450.  Return 1 if successful, 0 if if cannot tell what to do,
  1451.  or -1 to tell caller to find a new window start,
  1452.  or -2 to tell caller to do normal redisplay with same window start.  */
  1453.  
  1454. static int
  1455. try_window_id (window)
  1456.      Lisp_Object window;
  1457. {
  1458.   int pos;
  1459.   register struct window *w = XWINDOW (window);
  1460.   register int height = window_internal_height (w);
  1461.   FRAME_PTR f = XFRAME (w->frame);
  1462.   int top = XFASTINT (w->top);
  1463.   int start = marker_position (w->start);
  1464.   int width = window_internal_width (w) - 1;
  1465.   int hscroll = XINT (w->hscroll);
  1466.   int lmargin = hscroll > 0 ? 1 - hscroll : 0;
  1467.   register int vpos;
  1468.   register int i, tem;
  1469.   int last_text_vpos = 0;
  1470.   int stop_vpos;
  1471.   int selective
  1472.     = XTYPE (current_buffer->selective_display) == Lisp_Int
  1473.       ? XINT (current_buffer->selective_display)
  1474.     : !NILP (current_buffer->selective_display) ? -1 : 0;
  1475.  
  1476.   struct position val, bp, ep, xp, pp;
  1477.   int scroll_amount = 0;
  1478.   int delta;
  1479.   int tab_offset, epto;
  1480.  
  1481. #ifdef AMIGA
  1482.     if (XBUFFER(w->buffer) == win_line_buffer) CLEAR_WIN_LINE();
  1483. #endif
  1484.   
  1485.   if (GPT - BEG < beg_unchanged)
  1486.     beg_unchanged = GPT - BEG;
  1487.   if (Z - GPT < end_unchanged)
  1488.     end_unchanged = Z - GPT;
  1489.  
  1490.   if (beg_unchanged + BEG < start)
  1491.     return 0;            /* Give up if changes go above top of window */
  1492.  
  1493.   /* Find position before which nothing is changed.  */
  1494.   bp = *compute_motion (start, 0, lmargin,
  1495.             min (ZV, beg_unchanged + BEG), height + 1, 0,
  1496.             width, hscroll, pos_tab_offset (w, start), w);
  1497.   if (bp.vpos >= height)
  1498.     {
  1499.       if (PT < bp.bufpos && !bp.contin)
  1500.     {
  1501.       /* All changes are below the frame, and point is on the frame.
  1502.          We don't need to change the frame at all.
  1503.          But we need to update window_end_pos to account for
  1504.          any change in buffer size.  */
  1505.       bp = *compute_motion (start, 0, lmargin,
  1506.                 Z, height, 0,
  1507.                 width, hscroll, pos_tab_offset (w, start), w);
  1508.       XFASTINT (w->window_end_vpos) = height;
  1509.       XFASTINT (w->window_end_pos) = Z - bp.bufpos;
  1510.       return 1;
  1511.     }
  1512.       return 0;
  1513.     }
  1514.  
  1515.   vpos = bp.vpos;
  1516.  
  1517.   /* Find beginning of that frame line.  Must display from there.  */
  1518.   bp = *vmotion (bp.bufpos, 0, width, hscroll, window);
  1519.  
  1520.   pos = bp.bufpos;
  1521.   val.hpos = lmargin;
  1522.   if (pos < start)
  1523.     return -1;
  1524.  
  1525.   /* If about to start displaying at the beginning of a continuation line,
  1526.      really start with previous frame line, in case it was not
  1527.      continued when last redisplayed */
  1528.   if ((bp.contin && bp.bufpos - 1 == beg_unchanged && vpos > 0)
  1529.       ||
  1530.       /* Likewise if we have to worry about selective display.  */
  1531.       (selective > 0 && bp.bufpos - 1 == beg_unchanged && vpos > 0))
  1532.     {
  1533.       bp = *vmotion (bp.bufpos, -1, width, hscroll, window);
  1534.       --vpos;
  1535.       pos = bp.bufpos;
  1536.     }
  1537.  
  1538.   if (bp.contin && bp.hpos != lmargin)
  1539.     {
  1540.       val.hpos = bp.prevhpos - width + lmargin;
  1541.       pos--;
  1542.     }
  1543.  
  1544.   bp.vpos = vpos;
  1545.  
  1546.   /* Find first visible newline after which no more is changed.  */
  1547.   tem = find_next_newline (Z - max (end_unchanged, Z - ZV), 1);
  1548.   if (selective > 0)
  1549.     while (tem < ZV - 1 && (indented_beyond_p (tem, selective)))
  1550.       tem = find_next_newline (tem, 1);
  1551.  
  1552.   /* Compute the cursor position after that newline.  */
  1553.   ep = *compute_motion (pos, vpos, val.hpos, tem,
  1554.             height, - (1 << (SHORTBITS - 1)),
  1555.             width, hscroll, pos_tab_offset (w, bp.bufpos), w);
  1556.  
  1557.   /* If changes reach past the text available on the frame,
  1558.      just display rest of frame.  */
  1559.   if (ep.bufpos > Z - XFASTINT (w->window_end_pos))
  1560.     stop_vpos = height;
  1561.   else
  1562.     stop_vpos = ep.vpos;
  1563.  
  1564.   /* If no newline before ep, the line ep is on includes some changes
  1565.      that must be displayed.  Make sure we don't stop before it.  */
  1566.   /* Also, if changes reach all the way until ep.bufpos,
  1567.      it is possible that something was deleted after the
  1568.      newline before it, so the following line must be redrawn. */
  1569.   if (stop_vpos == ep.vpos
  1570.       && (ep.bufpos == BEGV
  1571.       || FETCH_CHAR (ep.bufpos - 1) != '\n'
  1572.       || ep.bufpos == Z - end_unchanged))
  1573.     stop_vpos = ep.vpos + 1;
  1574.  
  1575.   cursor_vpos = -1;
  1576.   overlay_arrow_seen = 0;
  1577.  
  1578.   /* If changes do not reach to bottom of window,
  1579.      figure out how much to scroll the rest of the window */
  1580.   if (stop_vpos < height)
  1581.     {
  1582.       /* Now determine how far up or down the rest of the window has moved */
  1583.       epto = pos_tab_offset (w, ep.bufpos);
  1584.       xp = *compute_motion (ep.bufpos, ep.vpos, ep.hpos,
  1585.                 Z - XFASTINT (w->window_end_pos),
  1586.                 10000, 0, width, hscroll, epto, w);
  1587.       scroll_amount = xp.vpos - XFASTINT (w->window_end_vpos);
  1588.  
  1589.       /* Is everything on frame below the changes whitespace?
  1590.      If so, no scrolling is really necessary.  */
  1591.       for (i = ep.bufpos; i < xp.bufpos; i++)
  1592.     {
  1593.       tem = FETCH_CHAR (i);
  1594.       if (tem != ' ' && tem != '\n' && tem != '\t')
  1595.         break;
  1596.     }
  1597.       if (i == xp.bufpos)
  1598.     return -2;
  1599.  
  1600.       XFASTINT (w->window_end_vpos) += scroll_amount;
  1601.  
  1602.       /* Before doing any scrolling, verify that point will be on frame. */
  1603.       if (PT > ep.bufpos && !(PT <= xp.bufpos && xp.bufpos < height))
  1604.     {
  1605.       if (PT <= xp.bufpos)
  1606.         {
  1607.           pp = *compute_motion (ep.bufpos, ep.vpos, ep.hpos,
  1608.                     PT, height, - (1 << (SHORTBITS - 1)),
  1609.                     width, hscroll, epto, w);
  1610.         }
  1611.       else
  1612.         {
  1613.           pp = *compute_motion (xp.bufpos, xp.vpos, xp.hpos,
  1614.                     PT, height, - (1 << (SHORTBITS - 1)),
  1615.                     width, hscroll,
  1616.                     pos_tab_offset (w, xp.bufpos), w);
  1617.         }
  1618.       if (pp.bufpos < PT || pp.vpos == height)
  1619.         return 0;
  1620.       cursor_vpos = pp.vpos + top;
  1621.       cursor_hpos = pp.hpos + XFASTINT (w->left);
  1622.     }
  1623.  
  1624.       if (stop_vpos - scroll_amount >= height
  1625.       || ep.bufpos == xp.bufpos)
  1626.     {
  1627.       if (scroll_amount < 0)
  1628.         stop_vpos -= scroll_amount;
  1629.       scroll_amount = 0;
  1630.       /* In this path, we have altered window_end_vpos
  1631.          and not left it negative.
  1632.          We must make sure that, in case display is preempted
  1633.          before the frame changes to reflect what we do here,
  1634.          further updates will not come to try_window_id
  1635.          and assume the frame and window_end_vpos match.  */
  1636.       blank_end_of_window = 1;
  1637.     }
  1638.       else if (!scroll_amount)
  1639.     {
  1640.       /* Even if we don't need to scroll, we must adjust the
  1641.          charstarts of subsequent lines (that we won't redisplay)
  1642.          according to the amount of text inserted or deleted.  */
  1643.       int oldpos = FRAME_CURRENT_GLYPHS (f)->charstarts[ep.vpos + top][0];
  1644.       int adjust = ep.bufpos - oldpos;
  1645.       adjust_window_charstarts (w, ep.vpos + top - 1, adjust);
  1646.     }
  1647.       else if (bp.bufpos == Z - end_unchanged)
  1648.     {
  1649.       /* If reprinting everything is nearly as fast as scrolling,
  1650.          don't bother scrolling.  Can happen if lines are short.  */
  1651.       if (scroll_cost (f, bp.vpos + top - scroll_amount,
  1652.                top + height - max (0, scroll_amount),
  1653.                scroll_amount)
  1654.           > xp.bufpos - bp.bufpos - 20)
  1655.         /* Return "try normal display with same window-start."
  1656.            Too bad we can't prevent further scroll-thinking.  */
  1657.         return -2;
  1658.       /* If pure deletion, scroll up as many lines as possible.
  1659.          In common case of killing a line, this can save the
  1660.          following line from being overwritten by scrolling
  1661.          and therefore having to be redrawn.  */
  1662.       tem = scroll_frame_lines (f, bp.vpos + top - scroll_amount,
  1663.                     top + height - max (0, scroll_amount),
  1664.                     scroll_amount, bp.bufpos);
  1665.       if (!tem)
  1666.         stop_vpos = height;
  1667.       else
  1668.         {
  1669.           /* scroll_frame_lines did not properly adjust subsequent
  1670.          lines' charstarts in the case where the text of the
  1671.          screen line at bp.vpos has changed.
  1672.          (This can happen in a deletion that ends in mid-line.)
  1673.          To adjust properly, we need to make things constent at
  1674.          the position ep.
  1675.          So do a second adjust to make that happen.
  1676.          Note that stop_vpos >= ep.vpos, so it is sufficient
  1677.          to update the charstarts for lines at ep.vpos and below.  */
  1678.           int oldstart
  1679.         = FRAME_CURRENT_GLYPHS (f)->charstarts[ep.vpos + top][0];
  1680.           adjust_window_charstarts (w, ep.vpos + top - 1,
  1681.                     ep.bufpos - oldstart);
  1682.         }
  1683.     }
  1684.       else if (scroll_amount)
  1685.     {
  1686.       /* If reprinting everything is nearly as fast as scrolling,
  1687.          don't bother scrolling.  Can happen if lines are short.  */
  1688.       /* Note that if scroll_amount > 0, xp.bufpos - bp.bufpos is an
  1689.          overestimate of cost of reprinting, since xp.bufpos
  1690.          would end up below the bottom of the window.  */
  1691.       if (scroll_cost (f, ep.vpos + top - scroll_amount,
  1692.                top + height - max (0, scroll_amount),
  1693.                scroll_amount)
  1694.           > xp.bufpos - ep.bufpos - 20)
  1695.         /* Return "try normal display with same window-start."
  1696.            Too bad we can't prevent further scroll-thinking.  */
  1697.         return -2;
  1698.       tem = scroll_frame_lines (f, ep.vpos + top - scroll_amount,
  1699.                      top + height - max (0, scroll_amount),
  1700.                      scroll_amount, ep.bufpos);
  1701.       if (!tem) stop_vpos = height;
  1702.     }
  1703.     }
  1704.  
  1705.   /* In any case, do not display past bottom of window */
  1706.   if (stop_vpos >= height)
  1707.     {
  1708.       stop_vpos = height;
  1709.       scroll_amount = 0;
  1710.     }
  1711.  
  1712.   /* Handle case where pos is before w->start --
  1713.      can happen if part of line had been clipped and is not clipped now */
  1714.   if (vpos == 0 && pos < marker_position (w->start))
  1715.     Fset_marker (w->start, make_number (pos), Qnil);
  1716.  
  1717.   /* Redisplay the lines where the text was changed */
  1718.   last_text_vpos = vpos;
  1719.   tab_offset = pos_tab_offset (w, pos);
  1720.   /* If we are starting display in mid-character, correct tab_offset
  1721.      to account for passing the line that that character really starts in.  */
  1722.   if (val.hpos < lmargin)
  1723.     tab_offset += width;
  1724.   while (vpos < stop_vpos)
  1725.     {
  1726.       val = *display_text_line (w, pos, top + vpos++, val.hpos, tab_offset);
  1727.       tab_offset += width;
  1728.       if (val.vpos) tab_offset = 0;
  1729.       if (pos != val.bufpos)
  1730.     last_text_vpos
  1731.       /* Next line, unless prev line ended in end of buffer with no cr */
  1732.         = vpos - (val.vpos && FETCH_CHAR (val.bufpos - 1) != '\n');
  1733.       pos = val.bufpos;
  1734.     }
  1735.  
  1736.   /* There are two cases:
  1737.      1) we have displayed down to the bottom of the window
  1738.      2) we have scrolled lines below stop_vpos by scroll_amount  */
  1739.  
  1740.   if (vpos == height)
  1741.     {
  1742.       /* If last line is continued in middle of character,
  1743.      include the split character in the text considered on the frame */
  1744.       if (val.hpos < lmargin)
  1745.     val.bufpos++;
  1746.       XFASTINT (w->window_end_vpos) = last_text_vpos;
  1747.       XFASTINT (w->window_end_pos) = Z - val.bufpos;
  1748.     }
  1749.  
  1750.   /* If scrolling made blank lines at window bottom,
  1751.      redisplay to fill those lines */
  1752.   if (scroll_amount < 0)
  1753.     {
  1754.       /* Don't consider these lines for general-purpose scrolling.
  1755.      That will save time in the scrolling computation.  */
  1756.       FRAME_SCROLL_BOTTOM_VPOS (f) = xp.vpos;
  1757.       vpos = xp.vpos;
  1758.       pos = xp.bufpos;
  1759.       val.hpos = lmargin;
  1760.       if (pos == ZV)
  1761.     vpos = height + scroll_amount;
  1762.       else if (xp.contin && xp.hpos != lmargin)
  1763.     {
  1764.       val.hpos = xp.prevhpos - width + lmargin;
  1765.       pos--;
  1766.     }
  1767.  
  1768.       blank_end_of_window = 1;
  1769.       tab_offset = pos_tab_offset (w, pos);
  1770.       /* If we are starting display in mid-character, correct tab_offset
  1771.      to account for passing the line that that character starts in.  */
  1772.       if (val.hpos < lmargin)
  1773.     tab_offset += width;
  1774.  
  1775.       while (vpos < height)
  1776.     {
  1777.       val = *display_text_line (w, pos, top + vpos++, val.hpos, tab_offset);
  1778.       tab_offset += width;
  1779.       if (val.vpos) tab_offset = 0;
  1780.       pos = val.bufpos;
  1781.     }
  1782.  
  1783.       /* Here is a case where display_text_line sets cursor_vpos wrong.
  1784.      Make it be fixed up, below.  */
  1785.       if (xp.bufpos == ZV
  1786.       && xp.bufpos == PT)
  1787.     cursor_vpos = -1;
  1788.     }
  1789.  
  1790.   /* If bottom just moved off end of frame, change mode line percentage.  */
  1791.   if (XFASTINT (w->window_end_pos) == 0
  1792.       && Z != val.bufpos)
  1793.     w->update_mode_line = Qt;
  1794.  
  1795.   /* Attempt to adjust end-of-text positions to new bottom line */
  1796.   if (scroll_amount)
  1797.     {
  1798.       delta = height - xp.vpos;
  1799.       if (delta < 0
  1800.       || (delta > 0 && xp.bufpos <= ZV)
  1801.       || (delta == 0 && xp.hpos))
  1802.     {
  1803.       val = *vmotion (Z - XFASTINT (w->window_end_pos),
  1804.               delta, width, hscroll, window);
  1805.       XFASTINT (w->window_end_pos) = Z - val.bufpos;
  1806.       XFASTINT (w->window_end_vpos) += val.vpos;
  1807.     }
  1808.     }
  1809.  
  1810.   w->window_end_valid = Qnil;
  1811.  
  1812.   /* If point was not in a line that was displayed, find it */
  1813.   if (cursor_vpos < 0)
  1814.     {
  1815.       val = *compute_motion (start, 0, lmargin, PT, 10000, 10000,
  1816.                  width, hscroll, pos_tab_offset (w, start), w);
  1817.       /* Admit failure if point is off frame now */
  1818.       if (val.vpos >= height)
  1819.     {
  1820.       for (vpos = 0; vpos < height; vpos++)
  1821.         cancel_line (vpos + top, f);
  1822.       return 0;
  1823.     }
  1824.       cursor_vpos = val.vpos + top;
  1825.       cursor_hpos = val.hpos + XFASTINT (w->left);
  1826.     }
  1827.  
  1828.   FRAME_CURSOR_X (f) = max (0, cursor_hpos);
  1829.   FRAME_CURSOR_Y (f) = cursor_vpos;
  1830.  
  1831.   if (debug_end_pos)
  1832.     {
  1833.       val = *compute_motion (start, 0, lmargin, ZV,
  1834.                  height, - (1 << (SHORTBITS - 1)),
  1835.                  width, hscroll, pos_tab_offset (w, start), w);
  1836.       if (val.vpos != XFASTINT (w->window_end_vpos))
  1837.     abort ();
  1838.       if (XFASTINT (w->window_end_pos)
  1839.       != Z - val.bufpos)
  1840.     abort ();
  1841.     }
  1842.  
  1843.   return 1;
  1844. }
  1845.  
  1846. /* Mark a section of BUF as modified, but only for the sake of redisplay.
  1847.    This is useful for recording changes to overlays.
  1848.  
  1849.    We increment the buffer's modification timestamp and set the
  1850.    redisplay caches (windows_or_buffers_changed, beg_unchanged, etc)
  1851.    as if the region of text between START and END had been modified;
  1852.    the redisplay code will check this against the windows' timestamps,
  1853.    and redraw the appropriate area of the buffer.
  1854.  
  1855.    However, if the buffer is unmodified, we bump the last-save
  1856.    timestamp as well, so that incrementing the timestamp doesn't fool
  1857.    Emacs into thinking that the buffer's text has been modified. 
  1858.  
  1859.    Tweaking the timestamps shouldn't hurt the first-modification
  1860.    timestamps recorded in the undo records; those values aren't
  1861.    written until just before a real text modification is made, so they
  1862.    will never catch the timestamp value just before this function gets
  1863.    called.  */
  1864.  
  1865. void
  1866. redisplay_region (buf, start, end)
  1867.      struct buffer *buf;
  1868.      int start, end;
  1869. {
  1870.   if (start == end)
  1871.     return;
  1872.  
  1873.   if (start > end)
  1874.     {
  1875.       int temp = start;
  1876.       start = end; end = temp;
  1877.     }
  1878.  
  1879.   if (buf != current_buffer)
  1880.     windows_or_buffers_changed = 1;
  1881.   else
  1882.     {
  1883.       if (unchanged_modified == MODIFF)
  1884.     {
  1885.       beg_unchanged = start - BEG;
  1886.       end_unchanged = Z - end;
  1887.     }
  1888.       else
  1889.     {
  1890.       if (Z - end < end_unchanged)
  1891.         end_unchanged = Z - end;
  1892.       if (start - BEG < beg_unchanged)
  1893.         beg_unchanged = start - BEG;
  1894.     }
  1895.     }
  1896.  
  1897.   /* Increment the buffer's time stamp, but also increment the save
  1898.      and autosave timestamps, so as not to screw up that timekeeping. */
  1899.   if (BUF_MODIFF (buf) == buf->save_modified)
  1900.     buf->save_modified++;
  1901.   if (BUF_MODIFF (buf) == buf->auto_save_modified)
  1902.     buf->auto_save_modified++;
  1903.  
  1904.   BUF_MODIFF (buf) ++;
  1905. }
  1906.  
  1907.  
  1908. /* Copy LEN glyphs starting address FROM to the rope TO.
  1909.    But don't actually copy the parts that would come in before S.
  1910.    Value is TO, advanced past the copied data.
  1911.    F is the frame we are displaying in.  */
  1912.  
  1913. static GLYPH *
  1914. copy_part_of_rope (f, to, s, from, len, face)
  1915.      FRAME_PTR f;
  1916.      register GLYPH *to; /* Copy to here. */
  1917.      register GLYPH *s; /* Starting point. */
  1918.      Lisp_Object *from;  /* Data to copy. */
  1919.      int len;
  1920.      int face;        /* Face to apply to glyphs which don't specify one. */
  1921. {
  1922.   int n = len;
  1923.   register Lisp_Object *fp = from;
  1924.   /* These cache the results of the last call to compute_glyph_face.  */
  1925.   int last_code = -1;
  1926.   int last_merged = 0;
  1927.  
  1928. #ifdef HAVE_X_WINDOWS
  1929.   if (! FRAME_TERMCAP_P (f))
  1930.     while (n--)
  1931.       {
  1932.     int glyph = (INTEGERP (*fp) ? XFASTINT (*fp) : 0);
  1933.     int facecode;
  1934.  
  1935.     if (FAST_GLYPH_FACE (glyph) == 0)
  1936.       /* If GLYPH has no face code, use FACE.  */
  1937.       facecode = face;
  1938.     else if (FAST_GLYPH_FACE (glyph) == last_code)
  1939.       /* If it's same as previous glyph, use same result.  */
  1940.       facecode = last_merged;
  1941.     else
  1942.       {
  1943.         /* Merge this glyph's face and remember the result.  */
  1944.         last_code = FAST_GLYPH_FACE (glyph);
  1945.         last_merged = facecode = compute_glyph_face (f, last_code, face);
  1946.       }
  1947.  
  1948.     if (to >= s)
  1949.       *to = FAST_MAKE_GLYPH (FAST_GLYPH_CHAR (glyph), facecode);
  1950.     ++to;
  1951.     ++fp;
  1952.       }
  1953.   else
  1954. #endif
  1955.     while (n--)
  1956.       {
  1957.     if (to >= s) *to = (INTEGERP (*fp) ? XFASTINT (*fp) : 0);
  1958.     ++to;
  1959.     ++fp;
  1960.       }
  1961.   return to;
  1962. }
  1963.  
  1964. /* Correct a glyph by replacing its specified user-level face code
  1965.    with a displayable computed face code.  */
  1966.  
  1967. static GLYPH
  1968. fix_glyph (f, glyph, cface)
  1969.      FRAME_PTR f;
  1970.      GLYPH glyph;
  1971.      int cface;
  1972. {
  1973. #ifdef HAVE_X_WINDOWS
  1974.   if (! FRAME_TERMCAP_P (f))
  1975.     {
  1976.       if (FAST_GLYPH_FACE (glyph) != 0)
  1977.     cface = compute_glyph_face (f, FAST_GLYPH_FACE (glyph), cface);
  1978.       glyph = FAST_MAKE_GLYPH (FAST_GLYPH_CHAR (glyph), cface);
  1979.     }
  1980. #endif
  1981.   return glyph;
  1982. }
  1983.  
  1984. /* Display one line of window w, starting at position START in W's buffer.
  1985.    Display starting at horizontal position HPOS, which is normally zero
  1986.    or negative.  A negative value causes output up to hpos = 0 to be discarded.
  1987.    This is done for negative hscroll, or when this is a continuation line
  1988.    and the continuation occurred in the middle of a multi-column character.
  1989.  
  1990.    TABOFFSET is an offset for ostensible hpos, used in tab stop calculations.
  1991.  
  1992.    Display on position VPOS on the frame.  (origin 0).
  1993.  
  1994.    Returns a STRUCT POSITION giving character to start next line with
  1995.    and where to display it, including a zero or negative hpos.
  1996.    The vpos field is not really a vpos; it is 1 unless the line is continued */
  1997.  
  1998. struct position val_display_text_line;
  1999.  
  2000. static struct position *
  2001. display_text_line (w, start, vpos, hpos, taboffset)
  2002.      struct window *w;
  2003.      int start;
  2004.      int vpos;
  2005.      int hpos;
  2006.      int taboffset;
  2007. {
  2008.   register int pos = start;
  2009.   register int c;
  2010.   register GLYPH *p1;
  2011.   int end;
  2012.   register int pause;
  2013.   register unsigned char *p;
  2014.   GLYPH *endp;
  2015.   register GLYPH *leftmargin;
  2016.   register GLYPH *p1prev = 0;
  2017.   register GLYPH *p1start;
  2018.   int *charstart;
  2019.   FRAME_PTR f = XFRAME (w->frame);
  2020.   int tab_width = XINT (current_buffer->tab_width);
  2021.   int ctl_arrow = !NILP (current_buffer->ctl_arrow);
  2022.   int width = window_internal_width (w) - 1;
  2023.   struct position val;
  2024.   int lastpos;
  2025.   int invis;
  2026.   int hscroll = XINT (w->hscroll);
  2027.   int truncate = (hscroll
  2028.           || (truncate_partial_width_windows
  2029.               && XFASTINT (w->width) < FRAME_WIDTH (f))
  2030.           || !NILP (current_buffer->truncate_lines));
  2031.  
  2032.   /* 1 if we should highlight the region.  */
  2033.   int highlight_region
  2034.     = !NILP (Vtransient_mark_mode) && !NILP (current_buffer->mark_active);
  2035.   int region_beg, region_end;
  2036.  
  2037.   int selective
  2038.     = XTYPE (current_buffer->selective_display) == Lisp_Int
  2039.       ? XINT (current_buffer->selective_display)
  2040.     : !NILP (current_buffer->selective_display) ? -1 : 0;
  2041.   register struct frame_glyphs *desired_glyphs = FRAME_DESIRED_GLYPHS (f);
  2042. #ifdef AMIGA /* CHFIXME */
  2043.   register struct Lisp_Vector *dp = (assert(w != 0), window_display_table (w));
  2044. #else
  2045.   register struct Lisp_Vector *dp = window_display_table (w);
  2046. #endif
  2047.  
  2048.   Lisp_Object default_invis_vector[3];
  2049.   /* Nonzero means display something where there are invisible lines.
  2050.      The precise value is the number of glyphs to display.  */
  2051.   int selective_rlen
  2052.     = (selective && dp && XTYPE (DISP_INVIS_VECTOR (dp)) == Lisp_Vector
  2053.        ? XVECTOR (DISP_INVIS_VECTOR (dp))->size
  2054.        : selective && !NILP (current_buffer->selective_display_ellipses)
  2055.        ? 3 : 0);
  2056.   /* This is the sequence of Lisp objects to display
  2057.      when there are invisible lines.  */
  2058.   Lisp_Object *invis_vector_contents
  2059.     = (dp && XTYPE (DISP_INVIS_VECTOR (dp)) == Lisp_Vector
  2060.        ? XVECTOR (DISP_INVIS_VECTOR (dp))->contents
  2061.        : default_invis_vector);
  2062.  
  2063.   GLYPH truncator = (dp == 0 || XTYPE (DISP_TRUNC_GLYPH (dp)) != Lisp_Int
  2064.              ? '$' : XINT (DISP_TRUNC_GLYPH (dp)));
  2065.   GLYPH continuer = (dp == 0 || XTYPE (DISP_CONTINUE_GLYPH (dp)) != Lisp_Int
  2066.              ? '\\' : XINT (DISP_CONTINUE_GLYPH (dp)));
  2067.  
  2068.   /* The next buffer location at which the face should change, due
  2069.      to overlays or text property changes.  */
  2070.   int next_face_change;
  2071.  
  2072. #ifdef USE_TEXT_PROPERTIES
  2073.   /* The next location where the `invisible' property changes */
  2074.   int next_invisible;
  2075. #endif
  2076.   
  2077.   /* The face we're currently using.  */
  2078.   int current_face = 0;
  2079.   int i;
  2080.  
  2081.   XFASTINT (default_invis_vector[2]) = '.';
  2082.   default_invis_vector[0] = default_invis_vector[1] = default_invis_vector[2];
  2083.  
  2084.   hpos += XFASTINT (w->left);
  2085.   get_display_line (f, vpos, XFASTINT (w->left));
  2086.   if (tab_width <= 0 || tab_width > 1000) tab_width = 8;
  2087.  
  2088.   /* Show where to highlight the region.  */
  2089.   if (highlight_region && XMARKER (current_buffer->mark)->buffer != 0
  2090.       /* Maybe highlight only in selected window.  */
  2091.       && (highlight_nonselected_windows
  2092.       || w == XWINDOW (selected_window)))
  2093.     {
  2094.       region_beg = marker_position (current_buffer->mark);
  2095.       if (PT < region_beg)
  2096.     {
  2097.       region_end = region_beg;
  2098.       region_beg = PT;
  2099.     }
  2100.       else
  2101.     region_end = PT;
  2102.       w->region_showing = Qt;
  2103.     }
  2104.   else
  2105.     region_beg = region_end = -1;
  2106.  
  2107.   if (MINI_WINDOW_P (w) && start == 1
  2108.       && vpos == XFASTINT (w->top))
  2109.     {
  2110.       if (minibuf_prompt)
  2111.     {
  2112.       minibuf_prompt_width
  2113.         = (display_string (w, vpos, minibuf_prompt, -1, hpos,
  2114.                    (!truncate ? continuer : truncator),
  2115.                    1, -1, -1)
  2116.            - hpos);
  2117.       hpos += minibuf_prompt_width;
  2118.     }
  2119.       else
  2120.     minibuf_prompt_width = 0;
  2121.     }
  2122.  
  2123.   desired_glyphs->bufp[vpos] = pos;
  2124.   p1 = desired_glyphs->glyphs[vpos] + hpos;
  2125.   p1start = p1;
  2126.   charstart = desired_glyphs->charstarts[vpos] + hpos;
  2127.   /* In case we don't ever write anything into it...  */
  2128.   desired_glyphs->charstarts[vpos][XFASTINT (w->left)] = -1;
  2129.   end = ZV;
  2130.   leftmargin = desired_glyphs->glyphs[vpos] + XFASTINT (w->left);
  2131.   endp = leftmargin + width;
  2132.  
  2133.   /* Arrange the overlays nicely for our purposes.  Usually, we call
  2134.      display_text_line on only one line at a time, in which case this
  2135.      can't really hurt too much, or we call it on lines which appear
  2136.      one after another in the buffer, in which case all calls to
  2137.      recenter_overlay_lists but the first will be pretty cheap.  */
  2138.   recenter_overlay_lists (current_buffer, pos);
  2139.  
  2140.   /* Loop generating characters.
  2141.      Stop at end of buffer, before newline,
  2142.      if reach or pass continuation column,
  2143.      or at face change.  */
  2144.   pause = pos;
  2145.   next_face_change = pos;
  2146. #ifdef USE_TEXT_PROPERTIES
  2147.   next_invisible = pos;
  2148. #endif
  2149.   while (1)
  2150.     {
  2151.       /* Record which glyph starts a character,
  2152.      and the character position of that character.  */
  2153.       if (p1 >= leftmargin)
  2154.     charstart[p1 - p1start] = pos;
  2155.  
  2156.       if (p1 >= endp)
  2157.     break;
  2158.  
  2159.       p1prev = p1;
  2160.       if (pos >= pause)
  2161.     {
  2162.       /* Did we hit the end of the visible region of the buffer?
  2163.          Stop here.  */
  2164.       if (pos >= end)
  2165.         break;
  2166.  
  2167.       /* Did we reach point?  Record the cursor location.  */
  2168.       if (pos == PT && cursor_vpos < 0)
  2169.         {
  2170.           cursor_vpos = vpos;
  2171.           cursor_hpos = p1 - leftmargin;
  2172.         }
  2173.  
  2174. #ifdef USE_TEXT_PROPERTIES
  2175.       /* if the `invisible' property is set to t, we can skip to
  2176.          the next property change */
  2177.       while (pos == next_invisible && pos < end)
  2178.         {
  2179.           Lisp_Object position, limit, endpos, prop, ww;
  2180.           XFASTINT (position) = pos;
  2181.           XSET (ww, Lisp_Window, w);
  2182.           prop = Fget_char_property (position, Qinvisible, ww);
  2183.           /* This is just an estimate to give reasonable
  2184.          performance; nothing should go wrong if it is too small.  */
  2185.           limit = Fnext_overlay_change (position);
  2186.           if (XFASTINT (limit) > pos + 50)
  2187.         XFASTINT (limit) = pos + 50;
  2188.           endpos = Fnext_single_property_change (position, Qinvisible,
  2189.                         Fcurrent_buffer (), limit);
  2190.           if (INTEGERP (endpos))
  2191.         next_invisible = XINT (endpos);
  2192.           else
  2193.         next_invisible = end;
  2194.           if (! NILP (prop))
  2195.         {
  2196.           if (pos < PT && next_invisible >= PT)
  2197.             {
  2198.               cursor_vpos = vpos;
  2199.               cursor_hpos = p1 - leftmargin;
  2200.             }
  2201.           pos = next_invisible;
  2202.         }
  2203.         }
  2204.       if (pos >= end)
  2205.         break;
  2206. #endif
  2207.  
  2208. #ifdef HAVE_X_WINDOWS
  2209.       /* Did we hit a face change?  Figure out what face we should
  2210.          use now.  We also hit this the first time through the
  2211.          loop, to see what face we should start with.  */
  2212.       if (pos >= next_face_change && FRAME_X_P (f))
  2213.         current_face = compute_char_face (f, w, pos,
  2214.                           region_beg, region_end,
  2215.                           &next_face_change, pos + 50, 0);
  2216. #endif
  2217.  
  2218.       pause = end;
  2219.  
  2220. #ifdef USE_TEXT_PROPERTIES      
  2221.       if (pos < next_invisible && next_invisible < pause)
  2222.         pause = next_invisible;
  2223. #endif
  2224.       if (pos < next_face_change && next_face_change < pause)
  2225.         pause = next_face_change;
  2226.  
  2227.       /* Wouldn't you hate to read the next line to someone over
  2228.              the phone?  */
  2229.       if (pos < PT && PT < pause)
  2230.         pause = PT;
  2231.       if (pos < GPT && GPT < pause)
  2232.         pause = GPT;
  2233.  
  2234.       p = &FETCH_CHAR (pos);
  2235.     }
  2236.       c = *p++;
  2237.       if (c >= 040 && c < 0177
  2238.       && (dp == 0 || XTYPE (DISP_CHAR_VECTOR (dp, c)) != Lisp_Vector))
  2239.     {
  2240.       if (p1 >= leftmargin)
  2241.         *p1 = MAKE_GLYPH (f, c, current_face);
  2242.       p1++;
  2243.     }
  2244.       else if (c == '\n')
  2245.     {
  2246.       invis = 0;
  2247.       while (pos + 1 < end
  2248.          && selective > 0
  2249.          && indented_beyond_p (pos + 1, selective))
  2250.         {
  2251.           invis = 1;
  2252.           pos = find_next_newline (pos + 1, 1);
  2253.           if (FETCH_CHAR (pos - 1) == '\n')
  2254.         pos--;
  2255.         }
  2256.       if (invis && selective_rlen > 0 && p1 >= leftmargin)
  2257.         {
  2258.           p1 += selective_rlen;
  2259.           if (p1 - leftmargin > width)
  2260.         p1 = endp;
  2261.           copy_part_of_rope (f, p1prev, p1prev, invis_vector_contents,
  2262.                  (p1 - p1prev), current_face);
  2263.         }
  2264. #ifdef HAVE_X_WINDOWS
  2265.       /* Draw the face of the newline character as extending all the 
  2266.          way to the end of the frame line.  */
  2267.       if (current_face)
  2268.         while (p1 < endp)
  2269.           *p1++ = FAST_MAKE_GLYPH (' ', current_face);
  2270. #endif
  2271.       break;
  2272.     }
  2273.       else if (c == '\t')
  2274.     {
  2275.       do
  2276.         {
  2277.           if (p1 >= leftmargin && p1 < endp)
  2278.         *p1 = MAKE_GLYPH (f, ' ', current_face);
  2279.           p1++;
  2280.         }
  2281.       while ((p1 - leftmargin + taboffset + hscroll - (hscroll > 0))
  2282.          % tab_width);
  2283.     }
  2284.       else if (c == Ctl ('M') && selective == -1)
  2285.     {
  2286.       pos = find_next_newline (pos, 1);
  2287.       if (FETCH_CHAR (pos - 1) == '\n')
  2288.         pos--;
  2289.       if (selective_rlen > 0)
  2290.         {
  2291.           p1 += selective_rlen;
  2292.           if (p1 - leftmargin > width)
  2293.         p1 = endp;
  2294.           copy_part_of_rope (f, p1prev, p1prev, invis_vector_contents,
  2295.                  (p1 - p1prev), current_face);
  2296.         }
  2297. #ifdef HAVE_X_WINDOWS
  2298.       /* Draw the face of the newline character as extending all the 
  2299.          way to the end of the frame line.  */
  2300.       if (current_face)
  2301.         while (p1 < endp)
  2302.           *p1++ = FAST_MAKE_GLYPH (' ', current_face);
  2303. #endif
  2304.       break;
  2305.     }
  2306.       else if (dp != 0 && XTYPE (DISP_CHAR_VECTOR (dp, c)) == Lisp_Vector)
  2307.     {
  2308.       p1 = copy_part_of_rope (f, p1, leftmargin,
  2309.                   XVECTOR (DISP_CHAR_VECTOR (dp, c))->contents,
  2310.                   XVECTOR (DISP_CHAR_VECTOR (dp, c))->size,
  2311.                   current_face);
  2312.     }
  2313.       else if (c < 0200 && ctl_arrow)
  2314.     {
  2315.       if (p1 >= leftmargin)
  2316.         *p1 = fix_glyph (f, (dp && XTYPE (DISP_CTRL_GLYPH (dp)) == Lisp_Int
  2317.                  ? XINT (DISP_CTRL_GLYPH (dp)) : '^'),
  2318.                  current_face);
  2319.       p1++;
  2320.       if (p1 >= leftmargin && p1 < endp)
  2321.         *p1 = MAKE_GLYPH (f, c ^ 0100, current_face);
  2322.       p1++;
  2323.     }
  2324.       else
  2325.     {
  2326.       if (p1 >= leftmargin)
  2327.         *p1 = fix_glyph (f, (dp && XTYPE (DISP_ESCAPE_GLYPH (dp)) == Lisp_Int
  2328.                  ? XINT (DISP_ESCAPE_GLYPH (dp)) : '\\'),
  2329.                  current_face);
  2330.       p1++;
  2331.       if (p1 >= leftmargin && p1 < endp)
  2332.         *p1 = MAKE_GLYPH (f, (c >> 6) + '0', current_face);
  2333.       p1++;
  2334.       if (p1 >= leftmargin && p1 < endp)
  2335.         *p1 = MAKE_GLYPH (f, (7 & (c >> 3)) + '0', current_face);
  2336.       p1++;
  2337.       if (p1 >= leftmargin && p1 < endp)
  2338.         *p1 = MAKE_GLYPH (f, (7 & c) + '0', current_face);
  2339.       p1++;
  2340.     }
  2341.  
  2342.       /* Do nothing here for a char that's entirely off the left edge.  */
  2343.       if (p1 >= leftmargin)
  2344.     {
  2345.       /* For all the glyphs occupied by this character, except for the
  2346.          first, store -1 in charstarts.  */
  2347.       if (p1 != p1prev)
  2348.         {
  2349.           int *p2x = &charstart[p1prev - p1start];
  2350.           int *p2 = &charstart[p1 - p1start];
  2351.  
  2352.           /* The window's left column should always
  2353.          contain a character position.
  2354.          And don't clobber anything to the left of that.  */
  2355.           if (p1prev < leftmargin)
  2356.         {
  2357.           p2x = charstart + (leftmargin - p1start);
  2358.           *p2x = pos;
  2359.         }
  2360.  
  2361.           /* This loop skips over the char p2x initially points to.  */
  2362.           while (++p2x < p2)
  2363.         *p2x = -1;
  2364.         }
  2365.     }
  2366.  
  2367.       pos++;
  2368.     }
  2369.  
  2370.   val.hpos = - XINT (w->hscroll);
  2371.   if (val.hpos)
  2372.     val.hpos++;
  2373.  
  2374.   val.vpos = 1;
  2375.  
  2376.   lastpos = pos;
  2377.  
  2378.   /* Store 0 in this charstart line for the positions where
  2379.      there is no character.  But do leave what was recorded
  2380.      for the character that ended the line.  */
  2381.   /* Add 1 in the endtest to compensate for the fact that ENDP was
  2382.      made from WIDTH, which is 1 less than the window's actual
  2383.      internal width.  */
  2384.   i = p1 - p1start + 1;
  2385.   if (p1 < leftmargin)
  2386.     i += leftmargin - p1;
  2387.   for (; i < endp - p1start + 1; i++)
  2388.     charstart[i] = 0;
  2389.  
  2390.   /* Handle continuation in middle of a character */
  2391.   /* by backing up over it */
  2392.   if (p1 > endp)
  2393.     {
  2394.       /* Don't back up if we never actually displayed any text.
  2395.      This occurs when the minibuffer prompt takes up the whole line.  */
  2396.       if (p1prev)
  2397.     {
  2398.       /* Start the next line with that same character */
  2399.       pos--;
  2400.       /* but at negative hpos, to skip the columns output on this line.  */
  2401.       val.hpos += p1prev - endp;
  2402.     }
  2403.  
  2404.       /* Keep in this line everything up to the continuation column.  */
  2405.       p1 = endp;
  2406.     }
  2407.  
  2408.   /* Finish deciding which character to start the next line on,
  2409.      and what hpos to start it at.
  2410.      Also set `lastpos' to the last position which counts as "on this line"
  2411.      for cursor-positioning.  */
  2412.  
  2413.   if (pos < ZV)
  2414.     {
  2415.       if (FETCH_CHAR (pos) == '\n')
  2416.     {
  2417.       /* If stopped due to a newline, start next line after it */
  2418.       pos++;
  2419.       /* Check again for hidden lines, in case the newline occurred exactly
  2420.          at the right margin.  */
  2421.       while (pos < ZV && selective > 0
  2422.          && indented_beyond_p (pos, selective))
  2423.         pos = find_next_newline (pos, 1);
  2424.     }
  2425.       else
  2426.     /* Stopped due to right margin of window */
  2427.     {
  2428.       if (truncate)
  2429.         {
  2430.           *p1++ = fix_glyph (f, truncator, 0);
  2431.           /* Truncating => start next line after next newline,
  2432.          and point is on this line if it is before the newline,
  2433.          and skip none of first char of next line */
  2434.           do
  2435.         pos = find_next_newline (pos, 1);
  2436.           while (pos < ZV && selective > 0
  2437.              && indented_beyond_p (pos, selective));
  2438.           val.hpos = XINT (w->hscroll) ? 1 - XINT (w->hscroll) : 0;
  2439.  
  2440.           lastpos = pos - (FETCH_CHAR (pos - 1) == '\n');
  2441.         }
  2442.       else
  2443.         {
  2444.           *p1++ = fix_glyph (f, continuer, 0);
  2445.           val.vpos = 0;
  2446.           lastpos--;
  2447.         }
  2448.     }
  2449.     }
  2450.  
  2451.   /* If point is at eol or in invisible text at eol,
  2452.      record its frame location now.  */
  2453.  
  2454.   if (start <= PT && PT <= lastpos && cursor_vpos < 0)
  2455.     {
  2456.       cursor_vpos = vpos;
  2457.       cursor_hpos = p1 - leftmargin;
  2458.     }
  2459.  
  2460.   if (cursor_vpos == vpos)
  2461.     {
  2462.       if (cursor_hpos < 0) cursor_hpos = 0;
  2463.       if (cursor_hpos > width) cursor_hpos = width;
  2464.       cursor_hpos += XFASTINT (w->left);
  2465.       if (w == XWINDOW (FRAME_SELECTED_WINDOW (f)))
  2466.     {
  2467.       FRAME_CURSOR_Y (f) = cursor_vpos;
  2468.       FRAME_CURSOR_X (f) = cursor_hpos;
  2469.  
  2470.       if (w == XWINDOW (selected_window))
  2471.         {
  2472.           /* Line is not continued and did not start
  2473.          in middle of character */
  2474.           if ((hpos - XFASTINT (w->left)
  2475.            == (XINT (w->hscroll) ? 1 - XINT (w->hscroll) : 0))
  2476.           && val.vpos)
  2477.         {
  2478.           this_line_bufpos = start;
  2479.           this_line_buffer = current_buffer;
  2480.           this_line_vpos = cursor_vpos;
  2481.           this_line_start_hpos = hpos;
  2482.           this_line_endpos = Z - lastpos;
  2483.         }
  2484.           else
  2485.         this_line_bufpos = 0;
  2486.         }
  2487.     }
  2488.     }
  2489.  
  2490.   /* If hscroll and line not empty, insert truncation-at-left marker */
  2491.   if (hscroll && lastpos != start)
  2492.     {
  2493.       *leftmargin = fix_glyph (f, truncator, 0);
  2494.       if (p1 <= leftmargin)
  2495.     p1 = leftmargin + 1;
  2496.     }
  2497.  
  2498.   if (XFASTINT (w->width) + XFASTINT (w->left) != FRAME_WIDTH (f))
  2499.     {
  2500.       endp++;
  2501.       if (p1 < leftmargin) p1 = leftmargin;
  2502.       while (p1 < endp) *p1++ = SPACEGLYPH;
  2503.  
  2504.       /* Don't draw vertical bars if we're using scroll bars.  They're
  2505.          covered up by the scroll bars, and it's distracting to see
  2506.          them when the scroll bar windows are flickering around to be
  2507.          reconfigured.  */
  2508.       *p1++ = (FRAME_HAS_VERTICAL_SCROLL_BARS (f)
  2509.            ? ' ' : '|');
  2510.     }
  2511.   desired_glyphs->used[vpos] = max (desired_glyphs->used[vpos],
  2512.                    p1 - desired_glyphs->glyphs[vpos]);
  2513.   desired_glyphs->glyphs[vpos][desired_glyphs->used[vpos]] = 0;
  2514.  
  2515.   /* If the start of this line is the overlay arrow-position,
  2516.      then put the arrow string into the display-line.  */
  2517.  
  2518.   if (XTYPE (Voverlay_arrow_position) == Lisp_Marker
  2519.       && current_buffer == XMARKER (Voverlay_arrow_position)->buffer
  2520.       && start == marker_position (Voverlay_arrow_position)
  2521.       && XTYPE (Voverlay_arrow_string) == Lisp_String
  2522.       && ! overlay_arrow_seen)
  2523.     {
  2524.       unsigned char *p = XSTRING (Voverlay_arrow_string)->data;
  2525.       int i;
  2526.       int len = XSTRING (Voverlay_arrow_string)->size;
  2527.       int arrow_end;
  2528.  
  2529.       if (len > width)
  2530.     len = width;
  2531.       for (i = 0; i < len; i++)
  2532.     leftmargin[i] = p[i];
  2533.  
  2534.       /* Bug in SunOS 4.1.1 compiler requires this intermediate variable.  */
  2535.       arrow_end = (leftmargin - desired_glyphs->glyphs[vpos]) + len;
  2536.       if (desired_glyphs->used[vpos] < arrow_end)
  2537.     desired_glyphs->used[vpos] = arrow_end;
  2538.  
  2539.       overlay_arrow_seen = 1;
  2540.     }
  2541.  
  2542.   val.bufpos = pos;
  2543.   val_display_text_line = val;
  2544.   return &val_display_text_line;
  2545. }
  2546.  
  2547. /* Redisplay the menu bar in the frame for window W.  */
  2548.  
  2549. static void
  2550. display_menu_bar (w)
  2551.      struct window *w;
  2552. {
  2553.   Lisp_Object items, tail;
  2554.   register int vpos = 0;
  2555.   register FRAME_PTR f = XFRAME (WINDOW_FRAME (w));
  2556.   int maxendcol = FRAME_WIDTH (f);
  2557.   int hpos = 0;
  2558.   int i;
  2559.  
  2560. #ifndef USE_EXTERNAL_MENU_BAR
  2561.   if (FRAME_MENU_BAR_LINES (f) <= 0)
  2562.     return;
  2563.  
  2564.   get_display_line (f, vpos, 0);
  2565.  
  2566.   items = FRAME_MENU_BAR_ITEMS (f);
  2567.   for (i = 0; i < XVECTOR (items)->size; i += 3)
  2568.     {
  2569.       Lisp_Object pos, string;
  2570.       string = XVECTOR (items)->contents[i + 1];
  2571.       if (NILP (string))
  2572.     break;
  2573.  
  2574.       XFASTINT (XVECTOR (items)->contents[i + 2]) = hpos;
  2575.  
  2576.       if (hpos < maxendcol)
  2577.     hpos = display_string (XWINDOW (FRAME_ROOT_WINDOW (f)), vpos,
  2578.                    XSTRING (string)->data,
  2579.                    XSTRING (string)->size,
  2580.                    hpos, 0, 0, hpos, maxendcol);
  2581.       /* Put a gap of 3 spaces between items.  */
  2582.       if (hpos < maxendcol)
  2583.     {
  2584.       int hpos1 = hpos + 3;
  2585.       hpos = display_string (w, vpos, "", 0, hpos, 0, 0,
  2586.                  min (hpos1, maxendcol), maxendcol);
  2587.     }
  2588.     }
  2589.  
  2590.   FRAME_DESIRED_GLYPHS (f)->bufp[vpos] = 0;
  2591.   FRAME_DESIRED_GLYPHS (f)->highlight[vpos] = mode_line_inverse_video;
  2592.  
  2593.   /* Fill out the line with spaces.  */
  2594.   if (maxendcol > hpos)
  2595.     hpos = display_string (w, vpos, "", 0, hpos, 0, 0, maxendcol, maxendcol);
  2596.  
  2597.   /* Clear the rest of the lines allocated to the menu bar.  */
  2598.   vpos++;
  2599.   while (vpos < FRAME_MENU_BAR_LINES (f))
  2600.     get_display_line (f, vpos++, 0);
  2601. #endif /* not USE_EXTERNAL_MENU_BAR */
  2602. }
  2603.  
  2604. /* Display the mode line for window w */
  2605.  
  2606. static void
  2607. display_mode_line (w)
  2608.      struct window *w;
  2609. {
  2610.   register int vpos = XFASTINT (w->height) + XFASTINT (w->top) - 1;
  2611.   register int left = XFASTINT (w->left);
  2612.   register int right = XFASTINT (w->width) + left;
  2613.   register FRAME_PTR f = XFRAME (WINDOW_FRAME (w));
  2614.  
  2615.   line_number_displayed = 0;
  2616.  
  2617.   get_display_line (f, vpos, left);
  2618.   display_mode_element (w, vpos, left, 0, right, right,
  2619.             current_buffer->mode_line_format);
  2620.   FRAME_DESIRED_GLYPHS (f)->bufp[vpos] = 0;
  2621.  
  2622.   /* Make the mode line inverse video if the entire line
  2623.      is made of mode lines.
  2624.      I.e. if this window is full width,
  2625.      or if it is the child of a full width window
  2626.      (which implies that that window is split side-by-side
  2627.      and the rest of this line is mode lines of the sibling windows).  */
  2628.   if (XFASTINT (w->width) == FRAME_WIDTH (f)
  2629.       || XFASTINT (XWINDOW (w->parent)->width) == FRAME_WIDTH (f))
  2630.     FRAME_DESIRED_GLYPHS (f)->highlight[vpos] = mode_line_inverse_video;
  2631. #ifdef HAVE_X_WINDOWS
  2632.   else if (! FRAME_TERMCAP_P (f))
  2633.     {
  2634.       /* For a partial width window, explicitly set face of each glyph. */
  2635.       int i;
  2636.       GLYPH *ptr = FRAME_DESIRED_GLYPHS (f)->glyphs[vpos];
  2637.       for (i = left; i < right; ++i)
  2638.     ptr[i] = FAST_MAKE_GLYPH (FAST_GLYPH_CHAR (ptr[i]), 1);
  2639.     }
  2640. #endif
  2641.  
  2642. #ifdef HAVE_X_WINDOWS
  2643.   if (w == XWINDOW (f->selected_window))
  2644.     x_consider_frame_title (WINDOW_FRAME (w));
  2645. #endif
  2646. }
  2647.  
  2648. /* Contribute ELT to the mode line for window W.
  2649.    How it translates into text depends on its data type.
  2650.  
  2651.    VPOS is the position of the mode line being displayed.
  2652.  
  2653.    HPOS is the position (absolute on frame) where this element's text
  2654.    should start.  The output is truncated automatically at the right
  2655.    edge of window W.
  2656.  
  2657.    DEPTH is the depth in recursion.  It is used to prevent
  2658.    infinite recursion here.
  2659.  
  2660.    MINENDCOL is the hpos before which the element may not end.
  2661.    The element is padded at the right with spaces if nec
  2662.    to reach this column.
  2663.  
  2664.    MAXENDCOL is the hpos past which this element may not extend.
  2665.    If MINENDCOL is > MAXENDCOL, MINENDCOL takes priority.
  2666.    (This is necessary to make nested padding and truncation work.)
  2667.  
  2668.    Returns the hpos of the end of the text generated by ELT.
  2669.    The next element will receive that value as its HPOS arg,
  2670.    so as to concatenate the elements.  */
  2671.  
  2672. static int
  2673. display_mode_element (w, vpos, hpos, depth, minendcol, maxendcol, elt)
  2674.      struct window *w;
  2675.      register int vpos, hpos;
  2676.      int depth;
  2677.      int minendcol;
  2678.      register int maxendcol;
  2679.      register Lisp_Object elt;
  2680. {
  2681.  tail_recurse:
  2682.   if (depth > 10)
  2683.     goto invalid;
  2684.  
  2685.   depth++;
  2686.  
  2687. #ifdef SWITCH_ENUM_BUG
  2688.   switch ((int) XTYPE (elt))
  2689. #else
  2690.   switch (XTYPE (elt))
  2691. #endif
  2692.     {
  2693.     case Lisp_String:
  2694.       {
  2695.     /* A string: output it and check for %-constructs within it.  */
  2696.     register unsigned char c;
  2697.     register unsigned char *this = XSTRING (elt)->data;
  2698.  
  2699.     while (hpos < maxendcol && *this)
  2700.       {
  2701.         unsigned char *last = this;
  2702.         while ((c = *this++) != '\0' && c != '%')
  2703.           ;
  2704.         if (this - 1 != last)
  2705.           {
  2706.         register int lim = --this - last + hpos;
  2707.         hpos = display_string (w, vpos, last, -1, hpos, 0, 1,
  2708.                        hpos, min (lim, maxendcol));
  2709.           }
  2710.         else /* c == '%' */
  2711.           {
  2712.         register int spec_width = 0;
  2713.  
  2714.         /* We can't allow -ve args due to the "%-" construct */
  2715.         /* Argument specifies minwidth but not maxwidth
  2716.            (maxwidth can be specified by
  2717.              (<negative-number> . <stuff>) mode-line elements) */
  2718.  
  2719.         while ((c = *this++) >= '0' && c <= '9')
  2720.           {
  2721.             spec_width = spec_width * 10 + (c - '0');
  2722.           }
  2723.  
  2724.         spec_width += hpos;
  2725.         if (spec_width > maxendcol)
  2726.           spec_width = maxendcol;
  2727.  
  2728.         if (c == 'M')
  2729.           hpos = display_mode_element (w, vpos, hpos, depth,
  2730.                            spec_width, maxendcol,
  2731.                            Vglobal_mode_string);
  2732.         else if (c != 0)
  2733.           hpos = display_string (w, vpos,
  2734.                      decode_mode_spec (w, c,
  2735.                                maxendcol - hpos),
  2736.                      -1,
  2737.                      hpos, 0, 1, spec_width, maxendcol);
  2738.           }
  2739.       }
  2740.       }
  2741.       break;
  2742.  
  2743.     case Lisp_Symbol:
  2744.       /* A symbol: process the value of the symbol recursively
  2745.      as if it appeared here directly.  Avoid error if symbol void.
  2746.      Special case: if value of symbol is a string, output the string
  2747.      literally.  */
  2748.       {
  2749.     register Lisp_Object tem;
  2750.     tem = Fboundp (elt);
  2751.     if (!NILP (tem))
  2752.       {
  2753.         tem = Fsymbol_value (elt);
  2754.         /* If value is a string, output that string literally:
  2755.            don't check for % within it.  */
  2756.         if (XTYPE (tem) == Lisp_String)
  2757.           hpos = display_string (w, vpos, XSTRING (tem)->data,
  2758.                      XSTRING (tem)->size,
  2759.                      hpos, 0, 1, minendcol, maxendcol);
  2760.         /* Give up right away for nil or t.  */
  2761.         else if (!EQ (tem, elt))
  2762.           { elt = tem; goto tail_recurse; }
  2763.       }
  2764.       }
  2765.       break;
  2766.  
  2767.     case Lisp_Cons:
  2768.       {
  2769.     register Lisp_Object car, tem;
  2770.  
  2771.     /* A cons cell: three distinct cases.
  2772.        If first element is a string or a cons, process all the elements
  2773.        and effectively concatenate them.
  2774.        If first element is a negative number, truncate displaying cdr to
  2775.        at most that many characters.  If positive, pad (with spaces)
  2776.        to at least that many characters.
  2777.        If first element is a symbol, process the cadr or caddr recursively
  2778.        according to whether the symbol's value is non-nil or nil.  */
  2779.     car = XCONS (elt)->car;
  2780.     if (XTYPE (car) == Lisp_Symbol)
  2781.       {
  2782.         tem = Fboundp (car);
  2783.         elt = XCONS (elt)->cdr;
  2784.         if (XTYPE (elt) != Lisp_Cons)
  2785.           goto invalid;
  2786.         /* elt is now the cdr, and we know it is a cons cell.
  2787.            Use its car if CAR has a non-nil value.  */
  2788.         if (!NILP (tem))
  2789.           {
  2790.         tem = Fsymbol_value (car);
  2791.         if (!NILP (tem))
  2792.           { elt = XCONS (elt)->car; goto tail_recurse; }
  2793.           }
  2794.         /* Symbol's value is nil (or symbol is unbound)
  2795.            Get the cddr of the original list
  2796.            and if possible find the caddr and use that.  */
  2797.         elt = XCONS (elt)->cdr;
  2798.         if (NILP (elt))
  2799.           break;
  2800.         else if (XTYPE (elt) != Lisp_Cons)
  2801.           goto invalid;
  2802.         elt = XCONS (elt)->car;
  2803.         goto tail_recurse;
  2804.       }
  2805.     else if (XTYPE (car) == Lisp_Int)
  2806.       {
  2807.         register int lim = XINT (car);
  2808.         elt = XCONS (elt)->cdr;
  2809.         if (lim < 0)
  2810.           /* Negative int means reduce maximum width.
  2811.          DO NOT change MINENDCOL here!
  2812.          (20 -10 . foo) should truncate foo to 10 col
  2813.          and then pad to 20.  */
  2814.           maxendcol = min (maxendcol, hpos - lim);
  2815.         else if (lim > 0)
  2816.           {
  2817.         /* Padding specified.  Don't let it be more than
  2818.            current maximum.  */
  2819.         lim += hpos;
  2820.         if (lim > maxendcol)
  2821.           lim = maxendcol;
  2822.         /* If that's more padding than already wanted, queue it.
  2823.            But don't reduce padding already specified even if
  2824.            that is beyond the current truncation point.  */
  2825.         if (lim > minendcol)
  2826.           minendcol = lim;
  2827.           }
  2828.         goto tail_recurse;
  2829.       }
  2830.     else if (XTYPE (car) == Lisp_String || XTYPE (car) == Lisp_Cons)
  2831.       {
  2832.         register int limit = 50;
  2833.         /* LIMIT is to protect against circular lists.  */
  2834.         while (XTYPE (elt) == Lisp_Cons && --limit > 0
  2835.            && hpos < maxendcol)
  2836.           {
  2837.         hpos = display_mode_element (w, vpos, hpos, depth,
  2838.                          hpos, maxendcol,
  2839.                          XCONS (elt)->car);
  2840.         elt = XCONS (elt)->cdr;
  2841.           }
  2842.       }
  2843.       }
  2844.       break;
  2845.  
  2846.     default:
  2847.     invalid:
  2848.       return (display_string (w, vpos, "*invalid*", -1, hpos, 0, 1,
  2849.                   minendcol, maxendcol));
  2850.     }
  2851.  
  2852.  end:
  2853.   if (minendcol > hpos)
  2854.     hpos = display_string (w, vpos, "", 0, hpos, 0, 1, minendcol, maxendcol);
  2855.   return hpos;
  2856. }
  2857.  
  2858. /* Return a string for the output of a mode line %-spec for window W,
  2859.    generated by character C and width MAXWIDTH.  */
  2860.  
  2861. #ifdef AMIGA
  2862. const
  2863. #endif
  2864. static char FAR lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
  2865.  
  2866. static char *
  2867. decode_mode_spec (w, c, maxwidth)
  2868.      struct window *w;
  2869.      register char c;
  2870.      register int maxwidth;
  2871. {
  2872.   Lisp_Object obj;
  2873.   FRAME_PTR f = XFRAME (WINDOW_FRAME (w));
  2874.   char *decode_mode_spec_buf = (char *) FRAME_TEMP_GLYPHS (f)->total_contents;
  2875.  
  2876.   obj = Qnil;
  2877.   if (maxwidth > FRAME_WIDTH (f))
  2878.     maxwidth = FRAME_WIDTH (f);
  2879.  
  2880.   switch (c)
  2881.     {
  2882.     case 'b': 
  2883.       obj = current_buffer->name;
  2884. #if 0
  2885.       if (maxwidth >= 3 && XSTRING (obj)->size > maxwidth)
  2886.     {
  2887.       bcopy (XSTRING (obj)->data, decode_mode_spec_buf, maxwidth - 1);
  2888.       decode_mode_spec_buf[maxwidth - 1] = '\\';
  2889.       decode_mode_spec_buf[maxwidth] = '\0';
  2890.       return decode_mode_spec_buf;
  2891.     }
  2892. #endif
  2893.       break;
  2894.  
  2895.     case 'f': 
  2896.       obj = current_buffer->filename;
  2897. #if 0
  2898.       if (NILP (obj))
  2899.     return "[none]";
  2900.       else if (XTYPE (obj) == Lisp_String && XSTRING (obj)->size > maxwidth)
  2901.     {
  2902.       bcopy ("...", decode_mode_spec_buf, 3);
  2903.       bcopy (XSTRING (obj)->data + XSTRING (obj)->size - maxwidth + 3,
  2904.          decode_mode_spec_buf + 3, maxwidth - 3);
  2905.       return decode_mode_spec_buf;
  2906.     }
  2907. #endif
  2908.       break;
  2909.  
  2910.     case 'l':
  2911.       {
  2912.     int startpos = marker_position (w->start);
  2913.     int line, linepos, topline;
  2914.     int nlines, junk;
  2915.     Lisp_Object tem;
  2916.     int height = XFASTINT (w->height);
  2917.  
  2918.     /* If we decided that this buffer isn't suitable for line numbers, 
  2919.        don't forget that too fast.  */
  2920.     if (EQ (w->base_line_pos, w->buffer))
  2921.       return "??";
  2922.  
  2923.     /* If the buffer is very big, don't waste time.  */
  2924.     if (ZV - BEGV > line_number_display_limit)
  2925.       {
  2926.         w->base_line_pos = Qnil;
  2927.         w->base_line_number = Qnil;
  2928.         return "??";
  2929.       }
  2930.  
  2931.     if (!NILP (w->base_line_number)
  2932.         && !NILP (w->base_line_pos)
  2933.         && XFASTINT (w->base_line_pos) <= marker_position (w->start))
  2934.       {
  2935.         line = XFASTINT (w->base_line_number);
  2936.         linepos = XFASTINT (w->base_line_pos);
  2937.       }
  2938.     else
  2939.       {
  2940.         line = 1;
  2941.         linepos = BEGV;
  2942.       }
  2943.  
  2944.     /* Count lines from base line to window start position.  */
  2945.     nlines = display_count_lines (linepos, startpos, startpos, &junk);
  2946.  
  2947.     topline = nlines + line;
  2948.  
  2949.     /* Determine a new base line, if the old one is too close
  2950.        or too far away, or if we did not have one.
  2951.        "Too close" means it's plausible a scroll-down would
  2952.        go back past it.  */
  2953.     if (startpos == BEGV)
  2954.       {
  2955.         XFASTINT (w->base_line_number) = topline;
  2956.         XFASTINT (w->base_line_pos) = BEGV;
  2957.       }
  2958.     else if (nlines < height + 25 || nlines > height * 3 + 50
  2959.          || linepos == BEGV)
  2960.       {
  2961.         int limit = BEGV;
  2962.         int position;
  2963.         int distance = (height * 2 + 30) * 200;
  2964.  
  2965.         if (startpos - distance > limit)
  2966.           limit = startpos - distance;
  2967.  
  2968.         nlines = display_count_lines (startpos, limit,
  2969.                       -(height * 2 + 30),
  2970.                       &position);
  2971.         /* If we couldn't find the lines we wanted within 
  2972.            200 chars per line,
  2973.            give up on line numbers for this window.  */
  2974.         if (position == startpos - distance)
  2975.           {
  2976.         w->base_line_pos = w->buffer;
  2977.         w->base_line_number = Qnil;
  2978.         return "??";
  2979.           }
  2980.  
  2981.         XFASTINT (w->base_line_number) = topline - nlines;
  2982.         XFASTINT (w->base_line_pos) = position;
  2983.       }
  2984.  
  2985.     /* Now count lines from the start pos to point.  */
  2986.     nlines = display_count_lines (startpos, PT, PT, &junk);
  2987.  
  2988.     /* Record that we did display the line number.  */
  2989.     line_number_displayed = 1;
  2990.  
  2991.     /* Make the string to show.  */
  2992.     sprintf (decode_mode_spec_buf, "%d", topline + nlines);
  2993.     return decode_mode_spec_buf;
  2994.       }
  2995.       break;
  2996.  
  2997.     case 'm': 
  2998.       obj = current_buffer->mode_name;
  2999.       break;
  3000.  
  3001.     case 'n':
  3002.       if (BEGV > BEG || ZV < Z)
  3003.     return " Narrow";
  3004.       break;
  3005.  
  3006.     case '*':
  3007.       if (!NILP (current_buffer->read_only))
  3008.     return "%";
  3009.       if (MODIFF > current_buffer->save_modified)
  3010.     return "*";
  3011.       return "-";
  3012.  
  3013.     case '+':
  3014.       if (MODIFF > current_buffer->save_modified)
  3015.     return "*";
  3016.       return "-";
  3017.  
  3018.     case 's':
  3019.       /* status of process */
  3020.       obj = Fget_buffer_process (Fcurrent_buffer ());
  3021.       if (NILP (obj))
  3022.     return "no process";
  3023. #ifdef subprocesses
  3024.       obj = Fsymbol_name (Fprocess_status (obj));
  3025. #endif
  3026.       break;
  3027.  
  3028.     case 't':            /* indicate TEXT or BINARY */
  3029. #ifdef MSDOS
  3030.       return NILP (current_buffer->buffer_file_type) ? "T" : "B";
  3031. #else /* not MSDOS */
  3032.       return "T";
  3033. #endif /* not MSDOS */
  3034.  
  3035.     case 'p':
  3036.       {
  3037.     int pos = marker_position (w->start);
  3038.     int total = ZV - BEGV;
  3039.  
  3040.     if (XFASTINT (w->window_end_pos) <= Z - ZV)
  3041.       {
  3042.         if (pos <= BEGV)
  3043.           return "All";
  3044.         else
  3045.           return "Bottom";
  3046.       }
  3047.     else if (pos <= BEGV)
  3048.       return "Top";
  3049.     else
  3050.       {
  3051.         total = ((pos - BEGV) * 100 + total - 1) / total;
  3052.         /* We can't normally display a 3-digit number,
  3053.            so get us a 2-digit number that is close.  */
  3054.         if (total == 100)
  3055.           total = 99;
  3056.         sprintf (decode_mode_spec_buf, "%2d%%", total);
  3057.         return decode_mode_spec_buf;
  3058.       }
  3059.       }
  3060.  
  3061.       /* Display percentage of size above the bottom of the screen.  */
  3062.     case 'P':
  3063.       {
  3064.     int toppos = marker_position (w->start);
  3065.     int botpos = Z - XFASTINT (w->window_end_pos);
  3066.     int total = ZV - BEGV;
  3067.  
  3068.     if (botpos >= ZV)
  3069.       {
  3070.         if (toppos <= BEGV)
  3071.           return "All";
  3072.         else
  3073.           return "Bottom";
  3074.       }
  3075.     else
  3076.       {
  3077.         total = ((botpos - BEGV) * 100 + total - 1) / total;
  3078.         /* We can't normally display a 3-digit number,
  3079.            so get us a 2-digit number that is close.  */
  3080.         if (total == 100)
  3081.           total = 99;
  3082.         if (toppos <= BEGV)
  3083.           sprintf (decode_mode_spec_buf, "Top%2d%%", total);
  3084.         else
  3085.           sprintf (decode_mode_spec_buf, "%2d%%", total);
  3086.         return decode_mode_spec_buf;
  3087.       }
  3088.       }
  3089.  
  3090.     case '%':
  3091.       return "%";
  3092.  
  3093.     case '[': 
  3094.       {
  3095.     int i;
  3096.     char *p;
  3097.  
  3098.     if (command_loop_level > 5)
  3099.       return "[[[... ";
  3100.     p = decode_mode_spec_buf;
  3101.     for (i = 0; i < command_loop_level; i++)
  3102.       *p++ = '[';
  3103.     *p = 0;
  3104.     return decode_mode_spec_buf;
  3105.       }
  3106.  
  3107.     case ']': 
  3108.       {
  3109.     int i;
  3110.     char *p;
  3111.  
  3112.     if (command_loop_level > 5)
  3113.       return " ...]]]";
  3114.     p = decode_mode_spec_buf;
  3115.     for (i = 0; i < command_loop_level; i++)
  3116.       *p++ = ']';
  3117.     *p = 0;
  3118.     return decode_mode_spec_buf;
  3119.       }
  3120.       
  3121.     case '-':
  3122.       {
  3123.     register char *p;
  3124.     register int i;
  3125.     
  3126.     if (maxwidth < sizeof (lots_of_dashes))
  3127.       return lots_of_dashes;
  3128.     else
  3129.       {
  3130.         for (p = decode_mode_spec_buf, i = maxwidth; i > 0; i--)
  3131.           *p++ = '-';
  3132.         *p = '\0';
  3133.       }
  3134.     return decode_mode_spec_buf;
  3135.       }
  3136.     }
  3137.   
  3138.   if (XTYPE (obj) == Lisp_String)
  3139.     return (char *) XSTRING (obj)->data;
  3140.   else
  3141.     return "";
  3142. }
  3143.  
  3144. /* Count up to N lines starting from FROM.
  3145.    But don't go beyond LIMIT.
  3146.    Return the number of lines thus found (always positive).
  3147.    Store the position after what was found into *POS_PTR.  */
  3148.  
  3149. static int
  3150. display_count_lines (from, limit, n, pos_ptr)
  3151.      int from, limit, n;
  3152.      int *pos_ptr;
  3153. {
  3154.   int oldbegv = BEGV;
  3155.   int oldzv = ZV;
  3156.   int shortage = 0;
  3157.   
  3158.   if (limit < from)
  3159.     BEGV = limit;
  3160.   else
  3161.     ZV = limit;
  3162.  
  3163.   *pos_ptr = scan_buffer ('\n', from, n, &shortage, 0);
  3164.  
  3165.   ZV = oldzv;
  3166.   BEGV = oldbegv;
  3167.  
  3168.   if (n < 0)
  3169.     /* When scanning backwards, scan_buffer stops *after* the last newline
  3170.        it finds, but does count it.  Compensate for that.  */
  3171.     return - n - shortage - (*pos_ptr != limit);
  3172.   return n - shortage;
  3173. }  
  3174.  
  3175. /* Display STRING on one line of window W, starting at HPOS.
  3176.    Display at position VPOS.  Caller should have done get_display_line.
  3177.    If VPOS == -1, display it as the current frame's title.
  3178.    LENGTH is the length of STRING, or -1 meaning STRING is null-terminated.
  3179.  
  3180.   TRUNCATE is GLYPH to display at end if truncated.  Zero for none.
  3181.  
  3182.   MINCOL is the first column ok to end at.  (Pad with spaces to this col.)
  3183.   MAXCOL is the last column ok to end at.  Truncate here.
  3184.     -1 for MINCOL or MAXCOL means no explicit minimum or maximum.
  3185.   Both count from the left edge of the frame, as does HPOS.
  3186.   The right edge of W is an implicit maximum.
  3187.   If TRUNCATE is nonzero, the implicit maximum is one column before the edge.
  3188.  
  3189.   OBEY_WINDOW_WIDTH says to put spaces or vertical bars
  3190.   at the place where the current window ends in this line
  3191.   and not display anything beyond there.  Otherwise, only MAXCOL
  3192.   controls where to stop output.
  3193.  
  3194.   Returns ending hpos.  */
  3195.  
  3196. static int
  3197. display_string (w, vpos, string, length, hpos, truncate,
  3198.         obey_window_width, mincol, maxcol)
  3199.      struct window *w;
  3200.      unsigned char *string;
  3201.      int length;
  3202.      int vpos, hpos;
  3203.      GLYPH truncate;
  3204.      int obey_window_width;
  3205.      int mincol, maxcol;
  3206. {
  3207.   register int c;
  3208.   register GLYPH *p1;
  3209.   int hscroll = XINT (w->hscroll);
  3210.   int tab_width = XINT (XBUFFER (w->buffer)->tab_width);
  3211.   register GLYPH *start;
  3212.   register GLYPH *end;
  3213.   FRAME_PTR f = XFRAME (WINDOW_FRAME (w));
  3214.   struct frame_glyphs *desired_glyphs = FRAME_DESIRED_GLYPHS (f);
  3215.   GLYPH *p1start = desired_glyphs->glyphs[vpos] + hpos;
  3216.   int window_width = XFASTINT (w->width);
  3217.  
  3218.   /* Use the standard display table, not the window's display table.
  3219.      We don't want the mode line in rot13.  */
  3220.   register struct Lisp_Vector *dp = 0;
  3221.   int i;
  3222.  
  3223.   if (XTYPE (Vstandard_display_table) == Lisp_Vector
  3224.       && XVECTOR (Vstandard_display_table)->size == DISP_TABLE_SIZE)
  3225.     dp = XVECTOR (Vstandard_display_table);
  3226.  
  3227.   if (tab_width <= 0 || tab_width > 1000) tab_width = 8;
  3228.  
  3229.   p1 = p1start;
  3230.   start = desired_glyphs->glyphs[vpos] + XFASTINT (w->left);
  3231.  
  3232.   if (obey_window_width)
  3233.     {
  3234.       end = start + window_width - (truncate != 0);
  3235.  
  3236.       if ((window_width + XFASTINT (w->left)) != FRAME_WIDTH (f))
  3237.     {
  3238.       if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
  3239.         {
  3240.           int i;
  3241.  
  3242.           for (i = 0; i < VERTICAL_SCROLL_BAR_WIDTH; i++)
  3243.         *end-- = ' ';
  3244.         }
  3245.       else
  3246.         *end-- = '|';
  3247.     }
  3248.     }
  3249.  
  3250.   if (! obey_window_width
  3251.       || (maxcol >= 0 && end - desired_glyphs->glyphs[vpos] > maxcol))
  3252.     end = desired_glyphs->glyphs[vpos] + maxcol;
  3253.  
  3254.   /* Store 0 in charstart for these columns.  */
  3255.   for (i = (hpos >= 0 ? hpos : 0); i < end - p1start + hpos; i++)
  3256.     desired_glyphs->charstarts[vpos][i] = 0;
  3257.  
  3258.   if (maxcol >= 0 && mincol > maxcol)
  3259.     mincol = maxcol;
  3260.  
  3261.   while (p1 < end)
  3262.     {
  3263.       if (length == 0)
  3264.     break;
  3265.       c = *string++;
  3266.       /* Specified length.  */
  3267.       if (length >= 0)
  3268.     length--;
  3269.       /* Unspecified length (null-terminated string).  */
  3270.       else if (c == 0)
  3271.     break;
  3272.  
  3273.       if (c >= 040 && c < 0177
  3274.       && (dp == 0 || XTYPE (DISP_CHAR_VECTOR (dp, c)) != Lisp_Vector))
  3275.     {
  3276.       if (p1 >= start)
  3277.         *p1 = c;
  3278.       p1++;
  3279.     }
  3280.       else if (c == '\t')
  3281.     {
  3282.       do
  3283.         {
  3284.           if (p1 >= start && p1 < end)
  3285.         *p1 = SPACEGLYPH;
  3286.           p1++;
  3287.         }
  3288.       while ((p1 - start + hscroll - (hscroll > 0)) % tab_width);
  3289.     }
  3290.       else if (dp != 0 && XTYPE (DISP_CHAR_VECTOR (dp, c)) == Lisp_Vector)
  3291.     {
  3292.       p1 = copy_part_of_rope (f, p1, start,
  3293.                   XVECTOR (DISP_CHAR_VECTOR (dp, c))->contents,
  3294.                   XVECTOR (DISP_CHAR_VECTOR (dp, c))->size,
  3295.                   0);
  3296.     }
  3297.       else if (c < 0200 && ! NILP (buffer_defaults.ctl_arrow))
  3298.     {
  3299.       if (p1 >= start)
  3300.         *p1 = fix_glyph (f, (dp && XTYPE (DISP_CTRL_GLYPH (dp)) == Lisp_Int
  3301.                  ? XINT (DISP_CTRL_GLYPH (dp)) : '^'),
  3302.                  0);
  3303.       p1++;
  3304.       if (p1 >= start && p1 < end)
  3305.         *p1 = c ^ 0100;
  3306.       p1++;
  3307.     }
  3308.       else
  3309.     {
  3310.       if (p1 >= start)
  3311.         *p1 = fix_glyph (f, (dp && XTYPE (DISP_ESCAPE_GLYPH (dp)) == Lisp_Int
  3312.                  ? XINT (DISP_ESCAPE_GLYPH (dp)) : '\\'),
  3313.                  0);
  3314.       p1++;
  3315.       if (p1 >= start && p1 < end)
  3316.         *p1 = (c >> 6) + '0';
  3317.       p1++;
  3318.       if (p1 >= start && p1 < end)
  3319.         *p1 = (7 & (c >> 3)) + '0';
  3320.       p1++;
  3321.       if (p1 >= start && p1 < end)
  3322.         *p1 = (7 & c) + '0';
  3323.       p1++;
  3324.     }
  3325.     }
  3326.  
  3327.   if (c && length > 0)
  3328.     {
  3329.       p1 = end;
  3330.       if (truncate) *p1++ = fix_glyph (f, truncate, 0);
  3331.     }
  3332.   else if (mincol >= 0)
  3333.     {
  3334.       end = desired_glyphs->glyphs[vpos] + mincol;
  3335.       while (p1 < end)
  3336.     *p1++ = SPACEGLYPH;
  3337.     }
  3338.  
  3339.   {
  3340.     register int len = p1 - desired_glyphs->glyphs[vpos];
  3341.  
  3342.     if (len > desired_glyphs->used[vpos])
  3343.       desired_glyphs->used[vpos] = len;
  3344.     desired_glyphs->glyphs[vpos][desired_glyphs->used[vpos]] = 0;
  3345.  
  3346.     return len;
  3347.   }
  3348. }
  3349.  
  3350. void
  3351. syms_of_xdisp ()
  3352. {
  3353.   staticpro (&Qmenu_bar_update_hook);
  3354.   Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
  3355.  
  3356.   staticpro (&last_arrow_position);
  3357.   staticpro (&last_arrow_string);
  3358.   last_arrow_position = Qnil;
  3359.   last_arrow_string = Qnil;
  3360.  
  3361.   DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
  3362.     "String (or mode line construct) included (normally) in `mode-line-format'.");
  3363.   Vglobal_mode_string = Qnil;
  3364.  
  3365.   DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
  3366.     "Marker for where to display an arrow on top of the buffer text.\n\
  3367. This must be the beginning of a line in order to work.\n\
  3368. See also `overlay-arrow-string'.");
  3369.   Voverlay_arrow_position = Qnil;
  3370.  
  3371.   DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
  3372.     "String to display as an arrow.  See also `overlay-arrow-position'.");
  3373.   Voverlay_arrow_string = Qnil;
  3374.  
  3375.   DEFVAR_INT ("scroll-step", &scroll_step,
  3376.     "*The number of lines to try scrolling a window by when point moves out.\n\
  3377. If that fails to bring point back on frame, point is centered instead.\n\
  3378. If this is zero, point is always centered after it moves off frame.");
  3379.  
  3380.   DEFVAR_INT ("debug-end-pos", &debug_end_pos, "Don't ask");
  3381.  
  3382.   DEFVAR_BOOL ("truncate-partial-width-windows",
  3383.            &truncate_partial_width_windows,
  3384.     "*Non-nil means truncate lines in all windows less than full frame wide.");
  3385.   truncate_partial_width_windows = 1;
  3386.  
  3387.   DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
  3388.     "*Non-nil means use inverse video for the mode line.");
  3389.   mode_line_inverse_video = 1;
  3390.  
  3391.   DEFVAR_INT ("line-number-display-limit", &line_number_display_limit,
  3392.     "*Maximum buffer size for which line number should be displayed.");
  3393.   line_number_display_limit = 1000000;
  3394.  
  3395.   DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
  3396.     "*Non-nil means highlight region even in nonselected windows.");
  3397.   highlight_nonselected_windows = 1;
  3398. }
  3399.  
  3400. /* initialize the window system */
  3401. init_xdisp ()
  3402. {
  3403.   Lisp_Object root_window;
  3404. #ifndef COMPILER_REGISTER_BUG
  3405.   register
  3406. #endif /* COMPILER_REGISTER_BUG */
  3407.    struct window *mini_w;
  3408.  
  3409. #ifdef AMIGA
  3410.   CLEAR_WIN_LINE();
  3411. #endif
  3412.  
  3413.   this_line_bufpos = 0;
  3414.  
  3415.   mini_w = XWINDOW (minibuf_window);
  3416.   root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
  3417.  
  3418.   echo_area_glyphs = 0;
  3419.   previous_echo_glyphs = 0;
  3420.  
  3421.   if (!noninteractive)
  3422.     {
  3423.       FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
  3424.       XFASTINT (XWINDOW (root_window)->top) = 0;
  3425.       set_window_height (root_window, FRAME_HEIGHT (f) - 1, 0);
  3426.       XFASTINT (mini_w->top) = FRAME_HEIGHT (f) - 1;
  3427.       set_window_height (minibuf_window, 1, 0);
  3428.  
  3429.       XFASTINT (XWINDOW (root_window)->width) = FRAME_WIDTH (f);
  3430.       XFASTINT (mini_w->width) = FRAME_WIDTH (f);
  3431.     }
  3432. }
  3433.