home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / e20313sr.zip / emacs / 20.3.1 / src / xdisp.c < prev    next >
C/C++ Source or Header  |  1999-07-31  |  174KB  |  5,584 lines

  1. /* Display generation from window structure and buffer text.
  2.    Copyright (C) 1985, 86, 87, 88, 93, 94, 95, 97, 1998
  3.      Free Software Foundation, Inc.
  4.  
  5. This file is part of GNU Emacs.
  6.  
  7. GNU Emacs is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2, or (at your option)
  10. any later version.
  11.  
  12. GNU Emacs is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with GNU Emacs; see the file COPYING.  If not, write to
  19. the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  20. Boston, MA 02111-1307, USA.  */
  21.  
  22. /* Modified for emx by Jeremy Bowen, May 1999 */
  23.  
  24. #include <config.h>
  25. #include <stdio.h>
  26. /*#include <ctype.h>*/
  27. #undef NULL
  28. #include "lisp.h"
  29. #include "frame.h"
  30. #include "window.h"
  31. #include "termchar.h"
  32. #include "dispextern.h"
  33. #include "buffer.h"
  34. #include "charset.h"
  35. #include "indent.h"
  36. #include "commands.h"
  37. #include "macros.h"
  38. #include "disptab.h"
  39. #include "termhooks.h"
  40. #include "intervals.h"
  41. #include "keyboard.h"
  42. #include "coding.h"
  43. #include "process.h"
  44. #include "region-cache.h"
  45.  
  46. #ifdef HAVE_X_WINDOWS
  47. #ifndef HAVE_PM
  48. #include "xterm.h"
  49. #else /* HAVE_PM */
  50. #include "pmterm.h"
  51. #endif /* HAVE_PM */
  52. #endif    /* HAVE_X_WINDOWS */
  53.  
  54. #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI)
  55. extern void set_frame_menubar ();
  56. extern int pending_menu_activation;
  57. #endif
  58.  
  59. extern int interrupt_input;
  60. extern int command_loop_level;
  61.  
  62. extern int minibuffer_auto_raise;
  63.  
  64. extern Lisp_Object Qface;
  65.  
  66. extern Lisp_Object Voverriding_local_map;
  67. extern Lisp_Object Voverriding_local_map_menu_flag;
  68.  
  69. Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
  70. Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
  71. Lisp_Object Qredisplay_end_trigger_functions;
  72. Lisp_Object Qinhibit_point_motion_hooks;
  73.  
  74. /* Non-nil means don't actually do any redisplay.  */
  75.  
  76. Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
  77.  
  78. /* Nonzero means print newline to stdout before next minibuffer message.  */
  79.  
  80. int noninteractive_need_newline;
  81.  
  82. /* Nonzero means print newline to message log before next message.  */
  83.  
  84. static int message_log_need_newline;
  85.  
  86. #define min(a, b) ((a) < (b) ? (a) : (b))
  87. #define max(a, b) ((a) > (b) ? (a) : (b))
  88. #define minmax(floor, val, ceil) \
  89.     ((val) < (floor) ? (floor) : (val) > (ceil) ? (ceil) : (val))
  90.  
  91. /* The buffer position of the first character appearing
  92.  entirely or partially on the current frame line.
  93.  Or zero, which disables the optimization for the current frame line. */
  94. static int this_line_bufpos;
  95.  
  96. /* Number of characters past the end of this line,
  97.    including the terminating newline */
  98. static int this_line_endpos;
  99.  
  100. /* The vertical position of this frame line. */
  101. static int this_line_vpos;
  102.  
  103. /* Hpos value for start of display on this frame line.
  104.    Usually zero, but negative if first character really began
  105.    on previous line */
  106. static int this_line_start_hpos;
  107.  
  108. /* Buffer that this_line variables are describing. */
  109. static struct buffer *this_line_buffer;
  110.  
  111. /* Value of echo_area_glyphs when it was last acted on.
  112.   If this is nonzero, there is a message on the frame
  113.   in the minibuffer and it should be erased as soon
  114.   as it is no longer requested to appear. */
  115. char *previous_echo_glyphs;
  116.  
  117. /* Nonzero means truncate lines in all windows less wide than the frame */
  118. int truncate_partial_width_windows;
  119.  
  120. /* Nonzero means we have more than one non-minibuffer-only frame.
  121.    Not guaranteed to be accurate except while parsing frame-title-format.  */
  122. int multiple_frames;
  123.  
  124. Lisp_Object Vglobal_mode_string;
  125.  
  126. /* Marker for where to display an arrow on top of the buffer text.  */
  127. Lisp_Object Voverlay_arrow_position;
  128.  
  129. /* String to display for the arrow.  */
  130. Lisp_Object Voverlay_arrow_string;
  131.  
  132. /* Values of those variables at last redisplay.
  133.    However, if Voverlay_arrow_position is a marker,
  134.    last_arrow_position is its numerical position.  */
  135. static Lisp_Object last_arrow_position, last_arrow_string;
  136.  
  137. /* Like mode-line-format, but for the titlebar on a visible frame.  */
  138. Lisp_Object Vframe_title_format;
  139.  
  140. /* Like mode-line-format, but for the titlebar on an iconified frame.  */
  141. Lisp_Object Vicon_title_format;
  142.  
  143. /* List of functions to call when a window's size changes.  These
  144.    functions get one arg, a frame on which one or more windows' sizes
  145.    have changed.  */
  146. static Lisp_Object Vwindow_size_change_functions;
  147.  
  148. Lisp_Object Qmenu_bar_update_hook;
  149.  
  150. /* Nonzero if overlay arrow has been displayed once in this window.  */
  151. static int overlay_arrow_seen;
  152.  
  153. /* Nonzero if visible end of buffer has already been displayed once
  154.    in this window.  (We need this variable in case there are overlay
  155.    strings that get displayed there.)  */
  156. static int zv_strings_seen;
  157.  
  158. /* Nonzero means highlight the region even in nonselected windows.  */
  159. static int highlight_nonselected_windows;
  160.  
  161. /* If cursor motion alone moves point off frame,
  162.    Try scrolling this many lines up or down if that will bring it back.  */
  163. static int scroll_step;
  164.  
  165. /* Non-0 means scroll just far enough to bring point back on the screen,
  166.    when appropriate.  */
  167. static int scroll_conservatively;
  168.  
  169. /* Recenter the window whenever point gets within this many lines
  170.    of the top or bottom of the window.  */
  171. int scroll_margin;
  172.  
  173. /* Number of characters of overlap to show,
  174.    when scrolling a one-line window such as a minibuffer.  */
  175. static int minibuffer_scroll_overlap;
  176.  
  177. /* Nonzero if try_window_id has made blank lines at window bottom
  178.  since the last redisplay that paused */
  179. static int blank_end_of_window;
  180.  
  181. /* Number of windows showing the buffer of the selected window
  182.    (or another buffer with the same base buffer).
  183.    keyboard.c refers to this.  */
  184. int buffer_shared;
  185.  
  186. /* display_text_line sets these to the frame position (origin 0) of point,
  187.    whether the window is selected or not.
  188.    Set one to -1 first to determine whether point was found afterwards.  */
  189.  
  190. static int cursor_vpos;
  191. static int cursor_hpos;
  192.  
  193. static int debug_end_pos;
  194.  
  195. /* Nonzero means display mode line highlighted */
  196. int mode_line_inverse_video;
  197.  
  198. static void redisplay_internal ();
  199. static int message_log_check_duplicate ();
  200. static void echo_area_display ();
  201. void mark_window_display_accurate ();
  202. static void redisplay_windows ();
  203. static void redisplay_window ();
  204. static void update_menu_bar ();
  205. static void try_window ();
  206. static int try_window_id ();
  207. static struct position *display_text_line ();
  208. static void display_mode_line ();
  209. static int display_mode_element ();
  210. static char *decode_mode_spec ();
  211. static int display_string ();
  212. static void display_menu_bar ();
  213. static int display_count_lines ();
  214.  
  215. /* Prompt to display in front of the minibuffer contents */
  216. Lisp_Object minibuf_prompt;
  217.  
  218. /* Width in columns of current minibuffer prompt.  */
  219. int minibuf_prompt_width;
  220.  
  221. /* Message to display instead of minibuffer contents
  222.    This is what the functions error and message make,
  223.    and command echoing uses it as well.
  224.    It overrides the minibuf_prompt as well as the buffer.  */
  225. char *echo_area_glyphs;
  226.  
  227. /* This is the length of the message in echo_area_glyphs.  */
  228. int echo_area_glyphs_length;
  229.  
  230. /* This is the window where the echo area message was displayed.
  231.    It is always a minibuffer window, but it may not be the
  232.    same window currently active as a minibuffer.  */
  233. Lisp_Object echo_area_window;
  234.  
  235. /* Nonzero means multibyte characters were enabled when the echo area
  236.    message was specified.  */
  237. int message_enable_multibyte;
  238.  
  239. /* true iff we should redraw the mode lines on the next redisplay */
  240. int update_mode_lines;
  241.  
  242. /* Smallest number of characters before the gap
  243.    at any time since last redisplay that finished.
  244.    Valid for current buffer when try_window_id can be called.  */
  245. int beg_unchanged;
  246.  
  247. /* Smallest number of characters after the gap
  248.    at any time since last redisplay that finished.
  249.    Valid for current buffer when try_window_id can be called.  */
  250. int end_unchanged;
  251.  
  252. /* MODIFF as of last redisplay that finished;
  253.    if it matches MODIFF, and overlay_unchanged_modified
  254.    matches OVERLAY_MODIFF, that means beg_unchanged and end_unchanged
  255.    contain no useful information */
  256. int unchanged_modified;
  257.  
  258. /* OVERLAY_MODIFF as of last redisplay that finished.  */
  259. int overlay_unchanged_modified;
  260.  
  261. /* Nonzero if window sizes or contents have changed
  262.    since last redisplay that finished */
  263. int windows_or_buffers_changed;
  264.  
  265. /* Nonzero after display_mode_line if %l was used
  266.    and it displayed a line number.  */
  267. int line_number_displayed;
  268.  
  269. /* Maximum buffer size for which to display line numbers.  */
  270. static int line_number_display_limit;
  271.  
  272. /* Number of lines to keep in the message log buffer.
  273.    t means infinite.  nil means don't log at all.  */
  274. Lisp_Object Vmessage_log_max;
  275.  
  276. #define COERCE_MARKER(X)    \
  277.   (MARKERP ((X)) ? Fmarker_position (X) : (X))
  278.  
  279. static int pos_tab_offset P_ ((struct window *, int, int));
  280.  
  281. /* Output a newline in the *Messages* buffer if "needs" one.  */
  282.  
  283. void
  284. message_log_maybe_newline ()
  285. {
  286.   if (message_log_need_newline)
  287.     message_dolog ("", 0, 1, 0);
  288. }
  289.  
  290.  
  291. /* Add a string to the message log, optionally terminated with a newline.
  292.    This function calls low-level routines in order to bypass text property
  293.    hooks, etc. which might not be safe to run.
  294.    MULTIBYTE, if nonzero, means interpret the contents of M as multibyte.  */
  295.  
  296. void
  297. message_dolog (m, len, nlflag, multibyte)
  298.      char *m;
  299.      int len, nlflag, multibyte;
  300. {
  301.   if (!NILP (Vmessage_log_max))
  302.     {
  303.       struct buffer *oldbuf;
  304.       Lisp_Object oldpoint, oldbegv, oldzv;
  305.       int old_windows_or_buffers_changed = windows_or_buffers_changed;
  306.       int point_at_end = 0;
  307.       int zv_at_end = 0;
  308.       Lisp_Object old_deactivate_mark, tem;
  309.       struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
  310.  
  311.       old_deactivate_mark = Vdeactivate_mark;
  312.       oldbuf = current_buffer;
  313.       Fset_buffer (Fget_buffer_create (build_string ("*Messages*")));
  314.       current_buffer->undo_list = Qt;
  315.  
  316.       oldpoint = Fpoint_marker ();
  317.       oldbegv = Fpoint_min_marker ();
  318.       oldzv = Fpoint_max_marker ();
  319.       GCPRO4 (oldpoint, oldbegv, oldzv, old_deactivate_mark);
  320.  
  321.       if (PT == Z)
  322.     point_at_end = 1;
  323.       if (ZV == Z)
  324.     zv_at_end = 1;
  325.  
  326.       BEGV = BEG;
  327.       BEGV_BYTE = BEG_BYTE;
  328.       ZV = Z;
  329.       ZV_BYTE = Z_BYTE;
  330.       TEMP_SET_PT_BOTH (Z, Z_BYTE);
  331.  
  332.       /* Insert the string--maybe converting multibyte to single byte
  333.      or vice versa, so that all the text fits the buffer.  */
  334.       if (multibyte
  335.       && NILP (current_buffer->enable_multibyte_characters))
  336.     {
  337.       int c, i = 0, nbytes;
  338.       /* Convert a multibyte string to single-byte
  339.          for the *Message* buffer.  */
  340.       while (i < len)
  341.         {
  342.           c = STRING_CHAR (m + i, len - i);
  343.           i += XFASTINT (Fchar_bytes (make_number (c)));
  344.           /* Truncate the character to its last byte--we can only hope
  345.          the user is happy with the character he gets,
  346.          since if it isn't right, there is no way to do it right.  */
  347.           c &= 0xff;
  348.           insert_char (c);
  349.         }
  350.     }
  351.       else if (! multibyte
  352.            && ! NILP (current_buffer->enable_multibyte_characters))
  353.     {
  354.       int i = 0;
  355.       unsigned char *msg = (unsigned char *) m;
  356.       /* Convert a single-byte string to multibyte
  357.          for the *Message* buffer.  */
  358.       while (i < len)
  359.         {
  360.           int c = unibyte_char_to_multibyte (msg[i++]);
  361.           insert_char (c);
  362.         }
  363.     }
  364.       else if (len)
  365.     insert_1 (m, len, 1, 0, 0);
  366.  
  367.       if (nlflag)
  368.     {
  369.       int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
  370.       insert_1 ("\n", 1, 1, 0, 0);
  371.  
  372.       scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
  373.       this_bol = PT;
  374.       this_bol_byte = PT_BYTE;
  375.  
  376.       if (this_bol > BEG)
  377.         {
  378.           scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
  379.           prev_bol = PT;
  380.           prev_bol_byte = PT_BYTE;
  381.  
  382.           dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
  383.                          this_bol, this_bol_byte);
  384.           if (dup)
  385.         {
  386.           del_range_both (prev_bol, prev_bol_byte,
  387.                   this_bol, this_bol_byte, 0);
  388.           if (dup > 1)
  389.             {
  390.               char dupstr[40];
  391.               int duplen;
  392.  
  393.               /* If you change this format, don't forget to also
  394.              change message_log_check_duplicate.  */
  395.               sprintf (dupstr, " [%d times]", dup);
  396.               duplen = strlen (dupstr);
  397.               TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
  398.               insert_1 (dupstr, duplen, 1, 0, 1);
  399.             }
  400.         }
  401.         }
  402.  
  403.       if (NATNUMP (Vmessage_log_max))
  404.         {
  405.           scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
  406.                 -XFASTINT (Vmessage_log_max) - 1, 0);
  407.           del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
  408.         }
  409.     }
  410.       BEGV = XMARKER (oldbegv)->charpos;
  411.       BEGV_BYTE = marker_byte_position (oldbegv);
  412.  
  413.       if (zv_at_end)
  414.     {
  415.       ZV = Z;
  416.       ZV_BYTE = Z_BYTE;
  417.     }
  418.       else
  419.     {
  420.       ZV = XMARKER (oldzv)->charpos;
  421.       ZV_BYTE = marker_byte_position (oldzv);
  422.     }
  423.  
  424.       if (point_at_end)
  425.     TEMP_SET_PT_BOTH (Z, Z_BYTE);
  426.       else
  427.     Fgoto_char (oldpoint);
  428.  
  429.       UNGCPRO;
  430.       free_marker (oldpoint);
  431.       free_marker (oldbegv);
  432.       free_marker (oldzv);
  433.  
  434.       tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
  435.       set_buffer_internal (oldbuf);
  436.       if (NILP (tem))
  437.     windows_or_buffers_changed = old_windows_or_buffers_changed;
  438.       message_log_need_newline = !nlflag;
  439.       Vdeactivate_mark = old_deactivate_mark;
  440.     }
  441. }
  442.  
  443. /* We are at the end of the buffer after just having inserted a newline.
  444.    (Note: We depend on the fact we won't be crossing the gap.)
  445.    Check to see if the most recent message looks a lot like the previous one.
  446.    Return 0 if different, 1 if the new one should just replace it, or a
  447.    value N > 1 if we should also append " [N times]".  */
  448.  
  449. static int
  450. message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
  451.      int prev_bol, this_bol;
  452.      int prev_bol_byte, this_bol_byte;
  453. {
  454.   int i;
  455.   int len = Z - 1 - this_bol;
  456.   int seen_dots = 0;
  457.   unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
  458.   unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
  459.  
  460.   for (i = 0; i < len; i++)
  461.     {
  462.       if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.'
  463.       && p1[i] != '\n')
  464.     seen_dots = 1;
  465.       if (p1[i] != p2[i])
  466.     return seen_dots;
  467.     }
  468.   p1 += len;
  469.   if (*p1 == '\n')
  470.     return 2;
  471.   if (*p1++ == ' ' && *p1++ == '[')
  472.     {
  473.       int n = 0;
  474.       while (*p1 >= '0' && *p1 <= '9')
  475.     n = n * 10 + *p1++ - '0';
  476.       if (strncmp (p1, " times]\n", 8) == 0)
  477.     return n+1;
  478.     }
  479.   return 0;
  480. }
  481.  
  482. /* Display an echo area message M with a specified length of LEN chars.
  483.    The string may include null characters.  If M is 0, clear out any
  484.    existing message, and let the minibuffer text show through.
  485.  
  486.    The buffer M must continue to exist until after the echo area
  487.    gets cleared or some other message gets displayed there.
  488.  
  489.    Do not pass text that is stored in a Lisp string.
  490.    Do not pass text in a buffer that was alloca'd.  */
  491.  
  492. void
  493. message2 (m, len, multibyte)
  494.      char *m;
  495.      int len;
  496.      int multibyte;
  497. {
  498.   /* First flush out any partial line written with print.  */
  499.   message_log_maybe_newline ();
  500.   if (m)
  501.     message_dolog (m, len, 1, multibyte);
  502.   message2_nolog (m, len, multibyte);
  503. }
  504.  
  505.  
  506. /* The non-logging counterpart of message2.  */
  507.  
  508. void
  509. message2_nolog (m, len, multibyte)
  510.      char *m;
  511.      int len;
  512. {
  513.   message_enable_multibyte = multibyte;
  514.  
  515.   if (noninteractive)
  516.     {
  517.       if (noninteractive_need_newline)
  518.     putc ('\n', stderr);
  519.       noninteractive_need_newline = 0;
  520.       if (m)
  521.     fwrite (m, len, 1, stderr);
  522.       if (cursor_in_echo_area == 0)
  523.     fprintf (stderr, "\n");
  524.       fflush (stderr);
  525.     }
  526.   /* A null message buffer means that the frame hasn't really been
  527.      initialized yet.  Error messages get reported properly by
  528.      cmd_error, so this must be just an informative message; toss it.  */
  529.   else if (INTERACTIVE && FRAME_MESSAGE_BUF (selected_frame))
  530.     {
  531.       Lisp_Object mini_window;
  532.       FRAME_PTR f;
  533.  
  534.       /* Get the frame containing the minibuffer
  535.      that the selected frame is using.  */
  536.       mini_window = FRAME_MINIBUF_WINDOW (selected_frame);
  537.       f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
  538.  
  539.       FRAME_SAMPLE_VISIBILITY (f);
  540.       if (FRAME_VISIBLE_P (selected_frame)
  541.       && ! FRAME_VISIBLE_P (f))
  542.     Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
  543.  
  544.       if (m)
  545.     {
  546.       echo_area_glyphs = m;
  547.       echo_area_glyphs_length = len;
  548.  
  549.       if (minibuffer_auto_raise)
  550.         Fraise_frame  (WINDOW_FRAME (XWINDOW (mini_window)));
  551.     }
  552.       else
  553.     echo_area_glyphs = previous_echo_glyphs = 0;
  554.  
  555.       do_pending_window_change ();
  556.       echo_area_display ();
  557.       update_frame (f, 1, 1);
  558.       do_pending_window_change ();
  559.       if (frame_up_to_date_hook != 0 && ! gc_in_progress)
  560.     (*frame_up_to_date_hook) (f);
  561.     }
  562. }
  563.  
  564. /* Display in echo area the null-terminated ASCII-only string M.
  565.    If M is 0, clear out any existing message,
  566.    and let the minibuffer text show through.
  567.  
  568.    The string M must continue to exist until after the echo area
  569.    gets cleared or some other message gets displayed there.
  570.  
  571.    Do not pass text that is stored in a Lisp string.
  572.    Do not pass text in a buffer that was alloca'd.  */
  573.  
  574. void
  575. message1 (m)
  576.      char *m;
  577. {
  578.   message2 (m, (m ? strlen (m) : 0), 0);
  579. }
  580.  
  581. void
  582. message1_nolog (m)
  583.      char *m;
  584. {
  585.   message2_nolog (m, (m ? strlen (m) : 0), 0);
  586. }
  587.  
  588. /* Display a message M which contains a single %s
  589.    which gets replaced with STRING.  */
  590.  
  591. void
  592. message_with_string (m, string, log)
  593.      char *m;
  594.      Lisp_Object string;
  595.      int log;
  596. {
  597.   if (noninteractive)
  598.     {
  599.       if (m)
  600.     {
  601.       if (noninteractive_need_newline)
  602.         putc ('\n', stderr);
  603.       noninteractive_need_newline = 0;
  604.       fprintf (stderr, m, XSTRING (string)->data);
  605.       if (cursor_in_echo_area == 0)
  606.         fprintf (stderr, "\n");
  607.       fflush (stderr);
  608.     }
  609.     }
  610.   else if (INTERACTIVE)
  611.     {
  612.       /* The frame whose minibuffer we're going to display the message on.
  613.      It may be larger than the selected frame, so we need
  614.      to use its buffer, not the selected frame's buffer.  */
  615.       Lisp_Object mini_window;
  616.       FRAME_PTR f;
  617.  
  618.       /* Get the frame containing the minibuffer
  619.      that the selected frame is using.  */
  620.       mini_window = FRAME_MINIBUF_WINDOW (selected_frame);
  621.       f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
  622.  
  623.       /* A null message buffer means that the frame hasn't really been
  624.      initialized yet.  Error messages get reported properly by
  625.      cmd_error, so this must be just an informative message; toss it.  */
  626.       if (FRAME_MESSAGE_BUF (f))
  627.     {
  628.       int len;
  629.       char *a[1];
  630.       a[0] = (char *) XSTRING (string)->data;
  631.  
  632.       len = doprnt (FRAME_MESSAGE_BUF (f),
  633.             FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
  634.  
  635.       if (log)
  636.         message2 (FRAME_MESSAGE_BUF (f), len,
  637.               STRING_MULTIBYTE (string));
  638.       else
  639.         message2_nolog (FRAME_MESSAGE_BUF (f), len,
  640.                 STRING_MULTIBYTE (string));
  641.  
  642.       /* Print should start at the beginning of the message
  643.          buffer next time.  */
  644.       message_buf_print = 0;
  645.     }
  646.     }
  647. }
  648.  
  649. /* Truncate what will be displayed in the echo area
  650.    the next time we display it--but don't redisplay it now.  */
  651.  
  652. void
  653. truncate_echo_area (len)
  654.      int len;
  655. {
  656.   /* A null message buffer means that the frame hasn't really been
  657.      initialized yet.  Error messages get reported properly by
  658.      cmd_error, so this must be just an informative message; toss it.  */
  659.   if (!noninteractive && INTERACTIVE && FRAME_MESSAGE_BUF (selected_frame))
  660.     echo_area_glyphs_length = len;
  661. }
  662.  
  663. /* Nonzero if FRAME_MESSAGE_BUF (selected_frame) is being used by print;
  664.    zero if being used by message.  */
  665. int message_buf_print;
  666.  
  667. /* Dump an informative message to the minibuf.  If M is 0, clear out
  668.    any existing message, and let the minibuffer text show through.  */
  669.  
  670. /* VARARGS 1 */
  671. void
  672. message (m, a1, a2, a3)
  673.      char *m;
  674.      EMACS_INT a1, a2, a3;
  675. {
  676.   if (noninteractive)
  677.     {
  678.       if (m)
  679.     {
  680.       if (noninteractive_need_newline)
  681.         putc ('\n', stderr);
  682.       noninteractive_need_newline = 0;
  683.       fprintf (stderr, m, a1, a2, a3);
  684.       if (cursor_in_echo_area == 0)
  685.         fprintf (stderr, "\n");
  686.       fflush (stderr);
  687.     }
  688.     }
  689.   else if (INTERACTIVE)
  690.     {
  691.       /* The frame whose minibuffer we're going to display the message on.
  692.      It may be larger than the selected frame, so we need
  693.      to use its buffer, not the selected frame's buffer.  */
  694.       Lisp_Object mini_window;
  695.       FRAME_PTR f;
  696.  
  697.       /* Get the frame containing the minibuffer
  698.      that the selected frame is using.  */
  699.       mini_window = FRAME_MINIBUF_WINDOW (selected_frame);
  700.       f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
  701.  
  702.       /* A null message buffer means that the frame hasn't really been
  703.      initialized yet.  Error messages get reported properly by
  704.      cmd_error, so this must be just an informative message; toss it.  */
  705.       if (FRAME_MESSAGE_BUF (f))
  706.     {
  707.       if (m)
  708.         {
  709.           int len;
  710. #ifdef NO_ARG_ARRAY
  711.           char *a[3];
  712.           a[0] = (char *) a1;
  713.           a[1] = (char *) a2;
  714.           a[2] = (char *) a3;
  715.  
  716.           len = doprnt (FRAME_MESSAGE_BUF (f),
  717.                 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
  718. #else
  719.           len = doprnt (FRAME_MESSAGE_BUF (f),
  720.                 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
  721.                 (char **) &a1);
  722. #endif /* NO_ARG_ARRAY */
  723.  
  724.           message2 (FRAME_MESSAGE_BUF (f), len, 0);
  725.         }
  726.       else
  727.         message1 (0);
  728.  
  729.       /* Print should start at the beginning of the message
  730.          buffer next time.  */
  731.       message_buf_print = 0;
  732.     }
  733.     }
  734. }
  735.  
  736. /* The non-logging version of message.  */
  737. void
  738. message_nolog (m, a1, a2, a3)
  739.      char *m;
  740.      EMACS_INT a1, a2, a3;
  741. {
  742.   Lisp_Object old_log_max;
  743.   old_log_max = Vmessage_log_max;
  744.   Vmessage_log_max = Qnil;
  745.   message (m, a1, a2, a3);
  746.   Vmessage_log_max = old_log_max;
  747. }
  748.  
  749. void
  750. update_echo_area ()
  751. {
  752.   message2 (echo_area_glyphs, echo_area_glyphs_length,
  753.         ! NILP (current_buffer->enable_multibyte_characters));
  754. }
  755.  
  756. static void
  757. echo_area_display ()
  758. {
  759.   register int vpos;
  760.   FRAME_PTR f;
  761.   Lisp_Object mini_window;
  762.  
  763.   /* Choose the minibuffer window for this display.
  764.      It is the minibuffer window used by the selected frame.  */
  765.   mini_window = FRAME_MINIBUF_WINDOW (selected_frame);
  766.   /* This is the frame that window is in.  */
  767.   f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
  768.  
  769.   if (! FRAME_VISIBLE_P (f))
  770.     return;
  771.  
  772.   if (frame_garbaged)
  773.     {
  774.       redraw_garbaged_frames ();
  775.       frame_garbaged = 0;
  776.     }
  777.  
  778.   if (echo_area_glyphs || minibuf_level == 0)
  779.     {
  780.       int i;
  781.  
  782.       echo_area_window = mini_window;
  783.  
  784.       vpos = XFASTINT (XWINDOW (mini_window)->top);
  785.       get_display_line (f, vpos, 0);
  786.  
  787.       /* Make sure the columns that overlap a left-hand scroll bar
  788.      are always clear.  */
  789.       for (i = 0; i < FRAME_LEFT_SCROLL_BAR_WIDTH (f); i++)
  790.     f->desired_glyphs->glyphs[vpos][i] = SPACEGLYPH;
  791.  
  792.       display_string (XWINDOW (mini_window), vpos,
  793.               echo_area_glyphs ? echo_area_glyphs : "",
  794.               echo_area_glyphs ? echo_area_glyphs_length : -1,
  795.               FRAME_LEFT_SCROLL_BAR_WIDTH (f),
  796.               0, 0, 0,
  797.               FRAME_WIDTH (f) + FRAME_LEFT_SCROLL_BAR_WIDTH (f),
  798.               message_enable_multibyte);
  799.  
  800. #if 0 /* This just gets in the way.  update_frame does the job.  */
  801.       /* If desired cursor location is on this line, put it at end of text */
  802.       if (cursor_in_echo_area)
  803.     FRAME_CURSOR_Y (f) = vpos;
  804.       if (FRAME_CURSOR_Y (f) == vpos)
  805.     FRAME_CURSOR_X (f) = FRAME_DESIRED_GLYPHS (f)->used[vpos];
  806. #endif
  807.  
  808.       /* Fill the rest of the minibuffer window with blank lines.  */
  809.       {
  810.     int i;
  811.  
  812.     for (i = vpos + 1;
  813.          i < vpos + XFASTINT (XWINDOW (mini_window)->height); i++)
  814.       {
  815.         get_display_line (f, i, 0);
  816.         /* We don't use FRAME_SCROLL_BAR_WIDTH (f) as the starting
  817.            hpos, because it is good to clear whatever is behind the
  818.            scroll bar.  This does not affect the scroll bar itself.  */
  819.         display_string (XWINDOW (mini_window), i,
  820.                 "", 0, 
  821.                             0, 0, 0,
  822.                             0, FRAME_WIDTH (f) + FRAME_SCROLL_BAR_WIDTH (f),
  823.                 0);
  824.       }
  825.       }
  826.     }
  827.   else if (!EQ (mini_window, selected_window))
  828.     windows_or_buffers_changed++;
  829.  
  830.   if (EQ (mini_window, selected_window))
  831.     this_line_bufpos = 0;
  832.  
  833.   previous_echo_glyphs = echo_area_glyphs;
  834. }
  835.  
  836. /* Update frame titles.  */
  837.  
  838. #ifdef HAVE_WINDOW_SYSTEM
  839. static char frame_title_buf[512];
  840. static char *frame_title_ptr;
  841.  
  842. static int
  843. store_frame_title (str, mincol, maxcol)
  844.      char *str;
  845.      int mincol, maxcol;
  846. {
  847.   char *limit;
  848.   if (maxcol < 0 || maxcol >= sizeof(frame_title_buf))
  849.     maxcol = sizeof (frame_title_buf);
  850.   limit = &frame_title_buf[maxcol];
  851.   while (*str != '\0' && frame_title_ptr < limit)
  852.     *frame_title_ptr++ = *str++;
  853.   while (frame_title_ptr < &frame_title_buf[mincol])
  854.     *frame_title_ptr++ = ' ';
  855.   return frame_title_ptr - frame_title_buf;
  856. }
  857.  
  858. static void
  859. x_consider_frame_title (frame)
  860.      Lisp_Object frame;
  861. {
  862.   Lisp_Object fmt;
  863.   struct buffer *obuf;
  864.   int len;
  865.   FRAME_PTR f = XFRAME (frame);
  866.  
  867.   if (!(FRAME_WINDOW_P (f) || FRAME_MINIBUF_ONLY_P (f) || f->explicit_name))
  868.     return;
  869.  
  870.   /* Do we have more than one visible frame on this X display?  */
  871.   {
  872.     Lisp_Object tail;
  873.  
  874.     for (tail = Vframe_list; CONSP (tail); tail = XCONS (tail)->cdr)
  875.       {
  876.     FRAME_PTR tf = XFRAME (XCONS (tail)->car);
  877.  
  878.     if (tf != f && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
  879.         && !FRAME_MINIBUF_ONLY_P (tf)
  880.         && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
  881.       break;
  882.       }
  883.  
  884.     multiple_frames = CONSP (tail);
  885.   }
  886.  
  887.   obuf = current_buffer;
  888.   Fset_buffer (XWINDOW (f->selected_window)->buffer);
  889.   fmt = (FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format);
  890.   frame_title_ptr = frame_title_buf;
  891.   len = display_mode_element (XWINDOW (f->selected_window), 0, 0, 0,
  892.                   0, sizeof (frame_title_buf), fmt);
  893.   frame_title_ptr = 0;
  894.   set_buffer_internal (obuf);
  895.   /* Set the name only if it's changed.  This avoids consing
  896.      in the common case where it hasn't.  (If it turns out that we've
  897.      already wasted too much time by walking through the list with
  898.      display_mode_element, then we might need to optimize at a higher
  899.      level than this.)  */
  900.   if (! STRINGP (f->name) || STRING_BYTES (XSTRING (f->name)) != len
  901.       || bcmp (frame_title_buf, XSTRING (f->name)->data, len) != 0)
  902.     x_implicitly_set_name (f, make_string (frame_title_buf, len), Qnil);
  903. }
  904. #else
  905. #define frame_title_ptr ((char *)0)
  906. #define store_frame_title(str, mincol, maxcol) 0
  907. #endif
  908.  
  909. /* Prepare for redisplay by updating menu-bar item lists when appropriate.
  910.    This can call eval.  */
  911.  
  912. void
  913. prepare_menu_bars ()
  914. {
  915.   register struct window *w = XWINDOW (selected_window);
  916.   int all_windows;
  917.   struct gcpro gcpro1, gcpro2;
  918.  
  919.   all_windows = (update_mode_lines || buffer_shared > 1
  920.          || windows_or_buffers_changed);
  921.  
  922.   /* Update all frame titles based on their buffer names, etc.
  923.      We do this before the menu bars so that the buffer-menu
  924.      will show the up-to-date frame titles.
  925.  
  926.      This used to be done after the menu bars, for a reason that
  927.      was stated as follows but which I do not understand:
  928.      "We do this after the menu bars so that the frame will first
  929.      create its menu bar using the name `emacs' if no other name
  930.      has yet been specified."
  931.      I think that is no longer a concern.  */
  932. #ifdef HAVE_WINDOW_SYSTEM
  933.   if (windows_or_buffers_changed || update_mode_lines)
  934.     {
  935.       Lisp_Object tail, frame;
  936.  
  937.       FOR_EACH_FRAME (tail, frame)
  938.     if (FRAME_VISIBLE_P (XFRAME (frame))
  939.         || FRAME_ICONIFIED_P (XFRAME (frame)))
  940.       x_consider_frame_title (frame);
  941.     }
  942. #endif
  943.  
  944.   /* Update the menu bar item lists, if appropriate.
  945.      This has to be done before any actual redisplay
  946.      or generation of display lines.  */
  947.   if (all_windows)
  948.     {
  949.       Lisp_Object tail, frame;
  950.       int count = specpdl_ptr - specpdl;
  951.  
  952.       record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
  953.  
  954.       FOR_EACH_FRAME (tail, frame)
  955.     {
  956.       /* If a window on this frame changed size,
  957.          report that to the user and clear the size-change flag.  */
  958.       if (FRAME_WINDOW_SIZES_CHANGED (XFRAME (frame)))
  959.         {
  960.           Lisp_Object functions;
  961.           /* Clear flag first in case we get error below.  */
  962.           FRAME_WINDOW_SIZES_CHANGED (XFRAME (frame)) = 0;
  963.           functions = Vwindow_size_change_functions;
  964.           GCPRO2 (tail, functions);
  965.           while (CONSP (functions))
  966.         {
  967.           call1 (XCONS (functions)->car, frame);
  968.           functions = XCONS (functions)->cdr;
  969.         }
  970.           UNGCPRO;
  971.         }
  972.       GCPRO1 (tail);
  973.       update_menu_bar (XFRAME (frame), 0);
  974.       UNGCPRO;
  975.     }
  976.  
  977.       unbind_to (count, Qnil);
  978.     }
  979.   else
  980.     update_menu_bar (selected_frame, 1);
  981.  
  982.   /* Motif needs this.  See comment in xmenu.c.
  983.      Turn it off when pending_menu_activation is not defined.  */
  984. #ifdef USE_X_TOOLKIT
  985.   pending_menu_activation = 0;
  986. #endif
  987. }
  988.  
  989. /* Do a frame update, taking possible shortcuts into account.
  990.    This is the main external entry point for redisplay.
  991.  
  992.    If the last redisplay displayed an echo area message and that
  993.    message is no longer requested, we clear the echo area
  994.    or bring back the minibuffer if that is in use.
  995.  
  996.    Do not call eval from within this function.
  997.    Calls to eval after the call to echo_area_display would confuse
  998.    the display_line mechanism and would cause a crash.
  999.    Calls to eval before that point will work most of the time,
  1000.    but can still lose, because  this function
  1001.    can be called from signal handlers; with alarms set up;
  1002.    or with synchronous processes running.
  1003.  
  1004.    See Fcall_process; if you called it from here, it could be
  1005.    entered recursively.  */
  1006.  
  1007. static int do_verify_charstarts;
  1008.  
  1009. /* Counter is used to clear the face cache
  1010.    no more than once ever 1000 redisplays.  */
  1011. static int clear_face_cache_count;
  1012.  
  1013. /* Record the previous terminal frame we displayed.  */
  1014. static FRAME_PTR previous_terminal_frame;
  1015.  
  1016. void
  1017. redisplay ()
  1018. {
  1019.   redisplay_internal (0);
  1020. }
  1021.  
  1022. /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay
  1023.    is not in response to any user action; therefore, we should
  1024.    preserve the echo area.  (Actually, our caller does that job.)
  1025.    Perhaps in the future avoid recentering windows
  1026.    if it is not necessary; currently that causes some problems.  */
  1027.  
  1028. static void
  1029. redisplay_internal (preserve_echo_area)
  1030.      int preserve_echo_area;
  1031. {
  1032.   register struct window *w = XWINDOW (selected_window);
  1033.   register int pause;
  1034.   int must_finish = 0;
  1035.   int all_windows;
  1036.   register int tlbufpos, tlendpos;
  1037.   struct position pos;
  1038.   int number_of_visible_frames;
  1039.  
  1040.   if (noninteractive)
  1041.     return;
  1042.  
  1043. #ifdef USE_X_TOOLKIT
  1044.   if (popup_activated ())
  1045.     return;
  1046. #endif
  1047.  
  1048.   if (! NILP (Vinhibit_redisplay))
  1049.     return;
  1050.  
  1051.  retry:
  1052.  
  1053.   if (! FRAME_WINDOW_P (selected_frame)
  1054.       && previous_terminal_frame != selected_frame)
  1055.     {
  1056.       /* Since frames on an ASCII terminal share the same display area,
  1057.      displaying a different frame means redisplay the whole thing.  */
  1058.       windows_or_buffers_changed++;
  1059.       SET_FRAME_GARBAGED (selected_frame);
  1060.       XSETFRAME (Vterminal_frame, selected_frame);
  1061.     }
  1062.   previous_terminal_frame = selected_frame;
  1063.  
  1064.   /* Set the visible flags for all frames.
  1065.      Do this before checking for resized or garbaged frames; they want
  1066.      to know if their frames are visible.
  1067.      See the comment in frame.h for FRAME_SAMPLE_VISIBILITY.  */
  1068.   {
  1069.     Lisp_Object tail, frame;
  1070.  
  1071.     number_of_visible_frames = 0;
  1072.  
  1073.     FOR_EACH_FRAME (tail, frame)
  1074.       {
  1075.     FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
  1076.  
  1077.     if (FRAME_VISIBLE_P (XFRAME (frame)))
  1078.       number_of_visible_frames++;
  1079.  
  1080.     /* Clear out all the display lines in which we will generate the
  1081.        glyphs to display.  */
  1082.     init_desired_glyphs (XFRAME (frame));
  1083.       }
  1084.   }
  1085.  
  1086.   /* Notice any pending interrupt request to change frame size.  */
  1087.   do_pending_window_change ();
  1088.  
  1089.   if (frame_garbaged)
  1090.     {
  1091.       redraw_garbaged_frames ();
  1092.       frame_garbaged = 0;
  1093.     }
  1094.  
  1095.   prepare_menu_bars ();
  1096.  
  1097.   if (windows_or_buffers_changed)
  1098.     update_mode_lines++;
  1099.  
  1100.   /* Detect case that we need to write or remove a star in the mode line.  */
  1101.   if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
  1102.     {
  1103.       w->update_mode_line = Qt;
  1104.       if (buffer_shared > 1)
  1105.     update_mode_lines++;
  1106.     }
  1107.  
  1108.   /* If %c is in use, update it if needed.  */
  1109.   if (!NILP (w->column_number_displayed)
  1110.       /* This alternative quickly identifies a common case
  1111.      where no change is needed.  */
  1112.       && !(PT == XFASTINT (w->last_point)
  1113.        && XFASTINT (w->last_modified) >= MODIFF
  1114.        && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
  1115.       && XFASTINT (w->column_number_displayed) != current_column ())
  1116.     w->update_mode_line = Qt; 
  1117.  
  1118.   FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
  1119.  
  1120.   all_windows = update_mode_lines || buffer_shared > 1;
  1121.  
  1122.   /* If specs for an arrow have changed, do thorough redisplay
  1123.      to ensure we remove any arrow that should no longer exist.  */
  1124.   if (! EQ (COERCE_MARKER (Voverlay_arrow_position), last_arrow_position)
  1125.       || ! EQ (Voverlay_arrow_string, last_arrow_string))
  1126.     all_windows = 1;
  1127.  
  1128.   /* Normally the message* functions will have already displayed and
  1129.      updated the echo area, but the frame may have been trashed, or
  1130.      the update may have been preempted, so display the echo area
  1131.      again here.  */
  1132.   if (echo_area_glyphs || previous_echo_glyphs)
  1133.     {
  1134.       echo_area_display ();
  1135.       must_finish = 1;
  1136.     }
  1137.  
  1138.   /* If showing region, and mark has changed, must redisplay whole window.  */
  1139.   if (((!NILP (Vtransient_mark_mode)
  1140.     && !NILP (XBUFFER (w->buffer)->mark_active))
  1141.        != !NILP (w->region_showing))
  1142.       || (!NILP (w->region_showing)
  1143.       && !EQ (w->region_showing,
  1144.           Fmarker_position (XBUFFER (w->buffer)->mark))))
  1145.     this_line_bufpos = -1;
  1146.  
  1147.   tlbufpos = this_line_bufpos;
  1148.   tlendpos = this_line_endpos;
  1149.   if (!all_windows && tlbufpos > 0 && NILP (w->update_mode_line)
  1150.       && !current_buffer->clip_changed
  1151.       && FRAME_VISIBLE_P (XFRAME (w->frame))
  1152.       && !FRAME_OBSCURED_P (XFRAME (w->frame))
  1153.       /* Make sure recorded data applies to current buffer, etc */
  1154.       && this_line_buffer == current_buffer
  1155.       && current_buffer == XBUFFER (w->buffer)
  1156.       && NILP (w->force_start)
  1157.       /* Point must be on the line that we have info recorded about */
  1158.       && PT >= tlbufpos
  1159.       && PT <= Z - tlendpos
  1160.       /* All text outside that line, including its final newline,
  1161.      must be unchanged */
  1162.       && ((XFASTINT (w->last_modified) >= MODIFF
  1163.        && (XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF))
  1164.       || (beg_unchanged >= tlbufpos - 1
  1165.           && GPT >= tlbufpos
  1166.           /* If selective display, can't optimize
  1167.          if the changes start at the beginning of the line.  */
  1168.           && ((INTEGERP (current_buffer->selective_display)
  1169.            && XINT (current_buffer->selective_display) > 0
  1170.            ? (beg_unchanged >= tlbufpos
  1171.               && GPT > tlbufpos)
  1172.            : 1))
  1173.           && end_unchanged >= tlendpos
  1174.           && Z - GPT >= tlendpos)))
  1175.     {
  1176.       int tlbufpos_byte = CHAR_TO_BYTE (tlbufpos);
  1177.       if (tlbufpos > BEGV && FETCH_BYTE (tlbufpos_byte - 1) != '\n'
  1178.       && (tlbufpos == ZV
  1179.           || FETCH_BYTE (tlbufpos_byte) == '\n'))
  1180.     /* Former continuation line has disappeared by becoming empty */
  1181.     goto cancel;
  1182.       else if (XFASTINT (w->last_modified) < MODIFF
  1183.            || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
  1184.            || MINI_WINDOW_P (w))
  1185.     {
  1186.       /* We have to handle the case of continuation around a
  1187.          wide-column character (See the comment in indent.c around
  1188.          line 885).
  1189.  
  1190.          For instance, in the following case:
  1191.  
  1192.          --------  Insert  --------
  1193.          K_A_N_\\   `a'    K_A_N_a\        `X_' are wide-column chars.
  1194.          J_I_       ==>    J_I_        `^^' are cursors.
  1195.          ^^                ^^
  1196.          --------          --------
  1197.  
  1198.          As we have to redraw the line above, we should goto cancel.  */
  1199.  
  1200.       struct position val;
  1201.       int prevline;
  1202.       int opoint = PT, opoint_byte = PT_BYTE;
  1203.  
  1204.       scan_newline (tlbufpos, tlbufpos_byte, BEGV, BEGV_BYTE, -1, 1);
  1205.  
  1206.       val = *compute_motion (PT, 0,
  1207.                  XINT (w->hscroll) ? 1 - XINT (w->hscroll) : 0,
  1208.                  0,
  1209.                  tlbufpos,
  1210.                  1 << (BITS_PER_SHORT - 1),
  1211.                  1 << (BITS_PER_SHORT - 1),
  1212.                  window_internal_width (w) - 1,
  1213.                  XINT (w->hscroll), 0, w);
  1214.       SET_PT_BOTH (opoint, opoint_byte);
  1215.       if (val.hpos != this_line_start_hpos)
  1216.         goto cancel;
  1217.  
  1218.       cursor_vpos = -1;
  1219.       overlay_arrow_seen = 0;
  1220.       zv_strings_seen = 0;
  1221.       display_text_line (w, tlbufpos, tlbufpos_byte,
  1222.                  this_line_vpos, this_line_start_hpos,
  1223.                  pos_tab_offset (w, tlbufpos, tlbufpos_byte), 0);
  1224.       /* If line contains point, is not continued,
  1225.          and ends at same distance from eob as before, we win */
  1226.       if (cursor_vpos >= 0 && this_line_bufpos
  1227.           && this_line_endpos == tlendpos)
  1228.         {
  1229.           /* If this is not the window's last line,
  1230.          we must adjust the charstarts of the lines below.  */
  1231.           if (this_line_vpos + 1
  1232.           < XFASTINT (w->top) + window_internal_height (w))
  1233.         {
  1234.           int left = WINDOW_LEFT_MARGIN (w);
  1235.           int *charstart_next_line
  1236.             = FRAME_CURRENT_GLYPHS (XFRAME (WINDOW_FRAME (w)))->charstarts[this_line_vpos + 1];
  1237.           int adjust;
  1238.  
  1239.           if (Z - tlendpos == ZV)
  1240.             /* This line ends at end of (accessible part of) buffer.
  1241.                There is no newline to count.  */
  1242.             adjust = Z - tlendpos - charstart_next_line[left];
  1243.           else
  1244.             /* This line ends in a newline.
  1245.                Must take account of the newline and the rest of the
  1246.                text that follows.  */
  1247.             adjust = Z - tlendpos + 1 - charstart_next_line[left];
  1248.  
  1249.           adjust_window_charstarts (w, this_line_vpos, adjust);
  1250.         }
  1251.  
  1252.           if (!WINDOW_FULL_WIDTH_P (w))
  1253.         preserve_other_columns (w);
  1254.           goto update;
  1255.         }
  1256.       else
  1257.         goto cancel;
  1258.     }
  1259.       else if (PT == XFASTINT (w->last_point)
  1260.            /* Make sure the cursor was last displayed
  1261.           in this window.  Otherwise we have to reposition it.  */
  1262.            && XINT (w->top) <= FRAME_CURSOR_Y (selected_frame)
  1263.            && (XINT (w->top) + XINT (w->height)
  1264.            > FRAME_CURSOR_Y (selected_frame)))
  1265.     {
  1266.       if (!must_finish)
  1267.         {
  1268.           do_pending_window_change ();
  1269.           return;
  1270.         }
  1271.       goto update;
  1272.     }
  1273.       /* If highlighting the region, or if the cursor is in the echo area,
  1274.      then we can't just move the cursor.  */
  1275.       else if (! (!NILP (Vtransient_mark_mode)
  1276.           && !NILP (current_buffer->mark_active))
  1277.            && (w == XWINDOW (current_buffer->last_selected_window)
  1278.            || highlight_nonselected_windows)
  1279.            && NILP (w->region_showing)
  1280.            && !cursor_in_echo_area)
  1281.     {
  1282.       pos = *compute_motion (tlbufpos, 0,
  1283.                  XINT (w->hscroll) ? 1 - XINT (w->hscroll) : 0,
  1284.                  0,
  1285.                  PT, 2, - (1 << (BITS_PER_SHORT - 1)),
  1286.                  window_internal_width (w) - 1,
  1287.                  XINT (w->hscroll),
  1288.                  pos_tab_offset (w, tlbufpos, tlbufpos_byte),
  1289.                  w);
  1290.       if (pos.vpos < 1)
  1291.         {
  1292.           int width = window_internal_width (w) - 1;
  1293.           FRAME_CURSOR_X (selected_frame)
  1294.         = WINDOW_LEFT_MARGIN (w) + minmax (0, pos.hpos, width);
  1295.           FRAME_CURSOR_Y (selected_frame) = this_line_vpos;
  1296.           goto update;
  1297.         }
  1298.       else
  1299.         goto cancel;
  1300.     }
  1301.     cancel:
  1302.       /* Text changed drastically or point moved off of line */
  1303.       cancel_line (this_line_vpos, selected_frame);
  1304.     }
  1305.  
  1306.   this_line_bufpos = 0;
  1307.   all_windows |= buffer_shared > 1;
  1308.  
  1309.   clear_face_cache_count++;
  1310.  
  1311.   if (all_windows)
  1312.     {
  1313.       Lisp_Object tail, frame;
  1314.  
  1315. #ifdef HAVE_FACES
  1316.       /* Clear the face cache, only when we do a full redisplay
  1317.      and not too often either.  */
  1318.       if (clear_face_cache_count > 1000)
  1319.     {
  1320.       clear_face_cache ();
  1321.       clear_face_cache_count = 0;
  1322.     }
  1323. #endif
  1324.  
  1325.       /* Recompute # windows showing selected buffer.
  1326.      This will be incremented each time such a window is displayed.  */
  1327.       buffer_shared = 0;
  1328.  
  1329.       FOR_EACH_FRAME (tail, frame)
  1330.     {
  1331.       FRAME_PTR f = XFRAME (frame);
  1332.       if (FRAME_WINDOW_P (f) || f == selected_frame)
  1333.         {
  1334.  
  1335.           /* Mark all the scroll bars to be removed; we'll redeem the ones
  1336.          we want when we redisplay their windows.  */
  1337.           if (condemn_scroll_bars_hook)
  1338.         (*condemn_scroll_bars_hook) (f);
  1339.  
  1340.           if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
  1341.         redisplay_windows (FRAME_ROOT_WINDOW (f), preserve_echo_area);
  1342.  
  1343.           /* Any scroll bars which redisplay_windows should have nuked
  1344.          should now go away.  */
  1345.           if (judge_scroll_bars_hook)
  1346.         (*judge_scroll_bars_hook) (f);
  1347.         }
  1348.     }
  1349.     }
  1350.   else if (FRAME_VISIBLE_P (selected_frame) && !FRAME_OBSCURED_P (selected_frame))
  1351.     {
  1352.       redisplay_window (selected_window, 1, preserve_echo_area);
  1353.       if (!WINDOW_FULL_WIDTH_P (w))
  1354.     preserve_other_columns (w);
  1355.     }
  1356.  
  1357. update: 
  1358.   /* Prevent various kinds of signals during display update.
  1359.      stdio is not robust about handling signals,
  1360.      which can cause an apparent I/O error.  */
  1361.   if (interrupt_input)
  1362.     unrequest_sigio ();
  1363.   stop_polling ();
  1364.  
  1365.   if (all_windows)
  1366.     {
  1367.       Lisp_Object tail;
  1368.  
  1369.       pause = 0;
  1370.  
  1371.       for (tail = Vframe_list; CONSP (tail); tail = XCONS (tail)->cdr)
  1372.     {
  1373.       FRAME_PTR f;
  1374.  
  1375.       if (!FRAMEP (XCONS (tail)->car))
  1376.         continue;
  1377.  
  1378.       f = XFRAME (XCONS (tail)->car);
  1379.  
  1380.       if ((FRAME_WINDOW_P (f) || f == selected_frame)
  1381.           && FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
  1382.         {
  1383.           pause |= update_frame (f, 0, 0);
  1384.           if (!pause)
  1385.         {
  1386.           mark_window_display_accurate (f->root_window, 1);
  1387.           if (frame_up_to_date_hook != 0)
  1388.             (*frame_up_to_date_hook) (f);
  1389.         }
  1390.         }
  1391.     }
  1392.     }
  1393.   else
  1394.     {
  1395.       if (FRAME_VISIBLE_P (selected_frame) && !FRAME_OBSCURED_P (selected_frame))
  1396.     pause = update_frame (selected_frame, 0, 0);
  1397.       else
  1398.     pause = 0;
  1399.  
  1400.       /* We may have called echo_area_display at the top of this
  1401.      function.  If the echo area is on another frame, that may
  1402.      have put text on a frame other than the selected one, so the
  1403.      above call to update_frame would not have caught it.  Catch
  1404.      it here.  */
  1405.       {
  1406.     Lisp_Object mini_window;
  1407.     FRAME_PTR mini_frame;
  1408.  
  1409.     mini_window = FRAME_MINIBUF_WINDOW (selected_frame);
  1410.     mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
  1411.     
  1412.     if (mini_frame != selected_frame && FRAME_WINDOW_P (mini_frame))
  1413.       pause |= update_frame (mini_frame, 0, 0);
  1414.       }
  1415.     }
  1416.  
  1417.   /* If frame does not match, prevent doing single-line-update next time.
  1418.      Also, don't forget to check every line to update the arrow.  */
  1419.   if (pause)
  1420.     {
  1421.       this_line_bufpos = 0;
  1422.       if (!NILP (last_arrow_position))
  1423.     {
  1424.       last_arrow_position = Qt;
  1425.       last_arrow_string = Qt;
  1426.     }
  1427.       /* If we pause after scrolling, some lines in current_frame
  1428.      may be null, so preserve_other_columns won't be able to
  1429.      preserve all the vertical-bar separators.  So, avoid using it
  1430.      in that case.  */
  1431.       if (!WINDOW_FULL_WIDTH_P (w))
  1432.     update_mode_lines = 1;
  1433.     }
  1434.  
  1435.   /* Now text on frame agrees with windows, so
  1436.      put info into the windows for partial redisplay to follow */
  1437.  
  1438.   if (!pause)
  1439.     {
  1440.       register struct buffer *b = XBUFFER (w->buffer);
  1441.  
  1442.       blank_end_of_window = 0;
  1443.       unchanged_modified = BUF_MODIFF (b);
  1444.       overlay_unchanged_modified = BUF_OVERLAY_MODIFF (b);
  1445.       beg_unchanged = BUF_GPT (b) - BUF_BEG (b);
  1446.       end_unchanged = BUF_Z (b) - BUF_GPT (b);
  1447.  
  1448.       /* Record the last place cursor was displayed in this window.
  1449.      But not if cursor is in the echo area, because in that case
  1450.      FRAME_CURSOR_X and FRAME_CURSOR_Y are in the echo area.  */
  1451.       if (!(cursor_in_echo_area && FRAME_HAS_MINIBUF_P (selected_frame)
  1452.         && EQ (FRAME_MINIBUF_WINDOW (selected_frame), minibuf_window)))
  1453.     {
  1454.       XSETFASTINT (w->last_point, BUF_PT (b));
  1455.       XSETFASTINT (w->last_point_x, FRAME_CURSOR_X (selected_frame));
  1456.       XSETFASTINT (w->last_point_y, FRAME_CURSOR_Y (selected_frame));
  1457.     }
  1458.       else
  1459.     /* Make last_point invalid, since we don't really know
  1460.        where the cursor would be if it were not in the echo area.  */
  1461.     XSETINT (w->last_point, -1);
  1462.  
  1463.       if (all_windows)
  1464.     mark_window_display_accurate (FRAME_ROOT_WINDOW (selected_frame), 1);
  1465.       else
  1466.     {
  1467.       b->clip_changed = 0;
  1468.       w->update_mode_line = Qnil;
  1469.       XSETFASTINT (w->last_modified, BUF_MODIFF (b));
  1470.       XSETFASTINT (w->last_overlay_modified, BUF_OVERLAY_MODIFF (b));
  1471.       w->last_had_star
  1472.         = (BUF_MODIFF (XBUFFER (w->buffer)) > BUF_SAVE_MODIFF (XBUFFER (w->buffer))
  1473.            ? Qt : Qnil);
  1474.  
  1475.       /* Record if we are showing a region, so can make sure to
  1476.          update it fully at next redisplay.  */
  1477.       w->region_showing = (!NILP (Vtransient_mark_mode)
  1478.                    && (w == XWINDOW (current_buffer->last_selected_window)
  1479.                    || highlight_nonselected_windows)
  1480.                    && !NILP (XBUFFER (w->buffer)->mark_active)
  1481.                    ? Fmarker_position (XBUFFER (w->buffer)->mark)
  1482.                    : Qnil);
  1483.  
  1484.       w->window_end_valid = w->buffer;
  1485.       last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
  1486.       last_arrow_string = Voverlay_arrow_string;
  1487.       if (do_verify_charstarts)
  1488.         verify_charstarts (w);
  1489.       if (frame_up_to_date_hook != 0)
  1490.         (*frame_up_to_date_hook) (selected_frame);
  1491.     }
  1492.       update_mode_lines = 0;
  1493.       windows_or_buffers_changed = 0;
  1494.     }
  1495.  
  1496.   /* Start SIGIO interrupts coming again.
  1497.      Having them off during the code above
  1498.      makes it less likely one will discard output,
  1499.      but not impossible, since there might be stuff
  1500.      in the system buffer here.
  1501.      But it is much hairier to try to do anything about that.  */
  1502.  
  1503.   if (interrupt_input)
  1504.     request_sigio ();
  1505.   start_polling ();
  1506.  
  1507.   /* If something has become visible now which was not before,
  1508.      redisplay again, so that we get them.  */
  1509.   if (!pause)
  1510.     {
  1511.       Lisp_Object tail, frame;
  1512.       int new_count = 0;
  1513.  
  1514.       FOR_EACH_FRAME (tail, frame)
  1515.     {
  1516.       int this_is_visible = 0;
  1517.  
  1518.       if (XFRAME (frame)->visible)
  1519.         this_is_visible = 1;
  1520.       FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
  1521.       if (XFRAME (frame)->visible)
  1522.         this_is_visible = 1;
  1523.  
  1524.       if (this_is_visible)
  1525.         new_count++;
  1526.     }
  1527.  
  1528.       if (new_count != number_of_visible_frames)
  1529.     windows_or_buffers_changed++;
  1530.     }
  1531.  
  1532.   /* Change frame size now if a change is pending.  */
  1533.   do_pending_window_change ();
  1534.  
  1535.   /* If we just did a pending size change, or have additional
  1536.      visible frames, redisplay again.  */
  1537.   if (windows_or_buffers_changed && !pause)
  1538.     goto retry;
  1539. }
  1540.  
  1541. /* Redisplay, but leave alone any recent echo area message
  1542.    unless another message has been requested in its place.
  1543.  
  1544.    This is useful in situations where you need to redisplay but no
  1545.    user action has occurred, making it inappropriate for the message
  1546.    area to be cleared.  See tracking_off and
  1547.    wait_reading_process_input for examples of these situations.  */
  1548.  
  1549. void
  1550. redisplay_preserve_echo_area ()
  1551. {
  1552.   if (echo_area_glyphs == 0 && previous_echo_glyphs != 0)
  1553.     {
  1554.       echo_area_glyphs = previous_echo_glyphs;
  1555.       redisplay_internal (1);
  1556.       echo_area_glyphs = 0;
  1557.     }
  1558.   else
  1559.     redisplay_internal (1);
  1560. }
  1561.  
  1562. void
  1563. mark_window_display_accurate (window, flag)
  1564.      Lisp_Object window;
  1565.      int flag;
  1566. {
  1567.   register struct window *w;
  1568.  
  1569.   for (;!NILP (window); window = w->next)
  1570.     {
  1571.       if (!WINDOWP (window)) abort ();
  1572.       w = XWINDOW (window);
  1573.  
  1574.       if (!NILP (w->buffer))
  1575.     {
  1576.       XSETFASTINT (w->last_modified,
  1577.                !flag ? 0 : BUF_MODIFF (XBUFFER (w->buffer)));
  1578.       XSETFASTINT (w->last_overlay_modified,
  1579.                !flag ? 0 : BUF_OVERLAY_MODIFF (XBUFFER (w->buffer)));
  1580.       w->last_had_star
  1581.         = (BUF_MODIFF (XBUFFER (w->buffer)) > BUF_SAVE_MODIFF (XBUFFER (w->buffer))
  1582.            ? Qt : Qnil);
  1583.  
  1584.       /* Record if we are showing a region, so can make sure to
  1585.          update it fully at next redisplay.  */
  1586.       w->region_showing = (!NILP (Vtransient_mark_mode)
  1587.                    && (w == XWINDOW (current_buffer->last_selected_window)
  1588.                    || highlight_nonselected_windows)
  1589.                    && !NILP (XBUFFER (w->buffer)->mark_active)
  1590.                    ? Fmarker_position (XBUFFER (w->buffer)->mark)
  1591.                    : Qnil);
  1592.     }
  1593.  
  1594.       w->window_end_valid = w->buffer;
  1595.       w->update_mode_line = Qnil;
  1596.       if (!NILP (w->buffer) && flag)
  1597.     XBUFFER (w->buffer)->clip_changed = 0;
  1598.  
  1599.       if (!NILP (w->vchild))
  1600.     mark_window_display_accurate (w->vchild, flag);
  1601.       if (!NILP (w->hchild))
  1602.     mark_window_display_accurate (w->hchild, flag);
  1603.     }
  1604.  
  1605.   if (flag)
  1606.     {
  1607.       last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
  1608.       last_arrow_string = Voverlay_arrow_string;
  1609.     }
  1610.   else
  1611.     {
  1612.       /* t is unequal to any useful value of Voverlay_arrow_... */
  1613.       last_arrow_position = Qt;
  1614.       last_arrow_string = Qt;
  1615.     }
  1616. }
  1617.  
  1618. /* Update the menu bar item list for frame F.
  1619.    This has to be done before we start to fill in any display lines,
  1620.    because it can call eval.
  1621.  
  1622.    If SAVE_MATCH_DATA is 1, we must save and restore it here.  */
  1623.  
  1624. static void
  1625. update_menu_bar (f, save_match_data)
  1626.      FRAME_PTR f;
  1627.      int save_match_data;
  1628. {
  1629.   struct buffer *old = current_buffer;
  1630.   Lisp_Object window;
  1631.   register struct window *w;
  1632.  
  1633.   window = FRAME_SELECTED_WINDOW (f);
  1634.   w = XWINDOW (window);
  1635.   
  1636.   if (update_mode_lines)
  1637.     w->update_mode_line = Qt;
  1638.  
  1639.   if (FRAME_WINDOW_P (f)
  1640.       ?
  1641. #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI)
  1642.       FRAME_EXTERNAL_MENU_BAR (f) 
  1643. #else
  1644.       FRAME_MENU_BAR_LINES (f) > 0
  1645. #endif
  1646.       : FRAME_MENU_BAR_LINES (f) > 0)
  1647.     {
  1648.       /* If the user has switched buffers or windows, we need to
  1649.      recompute to reflect the new bindings.  But we'll
  1650.      recompute when update_mode_lines is set too; that means
  1651.      that people can use force-mode-line-update to request
  1652.      that the menu bar be recomputed.  The adverse effect on
  1653.      the rest of the redisplay algorithm is about the same as
  1654.      windows_or_buffers_changed anyway.  */
  1655.       if (windows_or_buffers_changed
  1656.       || !NILP (w->update_mode_line)
  1657.       || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
  1658.            < BUF_MODIFF (XBUFFER (w->buffer)))
  1659.           != !NILP (w->last_had_star))
  1660.       || ((!NILP (Vtransient_mark_mode)
  1661.            && !NILP (XBUFFER (w->buffer)->mark_active))
  1662.           != !NILP (w->region_showing)))
  1663.     {
  1664.       struct buffer *prev = current_buffer;
  1665.       int count = specpdl_ptr - specpdl;
  1666.  
  1667.       set_buffer_internal_1 (XBUFFER (w->buffer));
  1668.       if (save_match_data)
  1669.         record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
  1670.       if (NILP (Voverriding_local_map_menu_flag))
  1671.         {
  1672.           specbind (Qoverriding_terminal_local_map, Qnil);
  1673.           specbind (Qoverriding_local_map, Qnil);
  1674.         }
  1675.  
  1676.       /* Run the Lucid hook.  */
  1677.       call1 (Vrun_hooks, Qactivate_menubar_hook);
  1678.       /* If it has changed current-menubar from previous value,
  1679.          really recompute the menubar from the value.  */
  1680.       if (! NILP (Vlucid_menu_bar_dirty_flag))
  1681.         call0 (Qrecompute_lucid_menubar);
  1682.       safe_run_hooks (Qmenu_bar_update_hook);
  1683.       FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
  1684.       /* Redisplay the menu bar in case we changed it.  */
  1685. #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI)
  1686.       if (FRAME_WINDOW_P (f))
  1687.         set_frame_menubar (f, 0, 0);
  1688.       else
  1689.         /* On a terminal screen, the menu bar is an ordinary screen
  1690.          line, and this makes it get updated.  */
  1691.         w->update_mode_line = Qt;
  1692. #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
  1693.       /* In the non-toolkit version, the menu bar is an ordinary screen
  1694.          line, and this makes it get updated.  */
  1695.       w->update_mode_line = Qt;
  1696. #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
  1697.  
  1698.       unbind_to (count, Qnil);
  1699.       set_buffer_internal_1 (prev);
  1700.     }
  1701.     }
  1702. }
  1703.  
  1704. int do_id = 1;
  1705.  
  1706. /* Redisplay WINDOW and its subwindows and siblings.  */
  1707.  
  1708. static void
  1709. redisplay_windows (window, preserve_echo_area)
  1710.      Lisp_Object window;
  1711.      int preserve_echo_area;
  1712. {
  1713.   for (; !NILP (window); window = XWINDOW (window)->next)
  1714.     redisplay_window (window, 0, preserve_echo_area);
  1715. }
  1716.  
  1717. /* Return value in display table DP (Lisp_Char_Table *) for character
  1718.    C.  Since a display table doesn't have any parent, we don't have to
  1719.    follow parent.  Do not call this function directly but use the
  1720.    macro DISP_CHAR_VECTOR.  */
  1721. Lisp_Object
  1722. disp_char_vector (dp, c)
  1723.      struct Lisp_Char_Table *dp;
  1724.      int c;
  1725. {
  1726.   int code[4], i;
  1727.   Lisp_Object val;
  1728.  
  1729.   if (SINGLE_BYTE_CHAR_P (c)) return (dp->contents[c]);
  1730.   
  1731.   SPLIT_NON_ASCII_CHAR (c, code[0], code[1], code[2]);
  1732.   if (code[0] != CHARSET_COMPOSITION)
  1733.     {
  1734.       if (code[1] < 32) code[1] = -1;
  1735.       else if (code[2] < 32) code[2] = -1;
  1736.     }
  1737.   /* Here, the possible range of CODE[0] (== charset ID) is
  1738.      128..MAX_CHARSET.  Since the top level char table contains data
  1739.      for multibyte characters after 256th element, we must increment
  1740.      CODE[0] by 128 to get a correct index.  */
  1741.   code[0] += 128;
  1742.   code[3] = -1;        /* anchor */
  1743.  
  1744.   for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
  1745.     {
  1746.       val = dp->contents[code[i]];
  1747.       if (!SUB_CHAR_TABLE_P (val))
  1748.     return (NILP (val) ? dp->defalt : val);
  1749.     }
  1750.   /* Here, VAL is a sub char table.  We return the default value of it.  */
  1751.   return (dp->defalt);
  1752. }
  1753.  
  1754. /* Redisplay window WINDOW and its subwindows.  */
  1755.  
  1756. static void
  1757. redisplay_window (window, just_this_one, preserve_echo_area)
  1758.      Lisp_Object window;
  1759.      int just_this_one, preserve_echo_area;
  1760. {
  1761.   register struct window *w = XWINDOW (window);
  1762.   FRAME_PTR f = XFRAME (WINDOW_FRAME (w));
  1763.   int height;
  1764.   int lpoint = PT;
  1765.   int lpoint_byte = PT_BYTE;
  1766.   struct buffer *old = current_buffer;
  1767.   register int width = window_internal_width (w) - 1;
  1768.   register int startp, startp_byte;
  1769.   register int hscroll = XINT (w->hscroll);
  1770.   struct position pos;
  1771.   int opoint = PT;
  1772.   int opoint_byte = PT_BYTE;
  1773.   int tem;
  1774.   int update_mode_line;
  1775.   struct Lisp_Char_Table *dp = window_display_table (w);
  1776.   int really_switched_buffer = 0;
  1777.   int count = specpdl_ptr - specpdl;
  1778.  
  1779.   if (Z == Z_BYTE && lpoint != lpoint_byte)
  1780.     abort ();
  1781.   if (lpoint_byte < lpoint)
  1782.     abort ();
  1783.  
  1784.   if (FRAME_HEIGHT (f) == 0) abort (); /* Some bug zeros some core */
  1785.  
  1786.   /* If this is a combination window, do its children; that's all.  */
  1787.  
  1788.   if (!NILP (w->vchild))
  1789.     {
  1790.       redisplay_windows (w->vchild, preserve_echo_area);
  1791.       return;
  1792.     }
  1793.   if (!NILP (w->hchild))
  1794.     {
  1795.       redisplay_windows (w->hchild, preserve_echo_area);
  1796.       return;
  1797.     }
  1798.   if (NILP (w->buffer))
  1799.     abort ();
  1800.  
  1801.   specbind (Qinhibit_point_motion_hooks, Qt);
  1802.  
  1803.   height = window_internal_height (w);
  1804.   update_mode_line = (!NILP (w->update_mode_line) || update_mode_lines);
  1805.   if (XBUFFER (w->buffer)->clip_changed)
  1806.     update_mode_line = 1;
  1807.  
  1808.   if (MINI_WINDOW_P (w))
  1809.     {
  1810.       if (w == XWINDOW (echo_area_window) && echo_area_glyphs)
  1811.     /* We've already displayed the echo area glyphs in this window.  */
  1812.     goto finish_scroll_bars;
  1813.       else if (w != XWINDOW (minibuf_window))
  1814.     {
  1815.       /* This is a minibuffer, but it's not the currently active one,
  1816.          so clear it.  */
  1817.       int vpos = XFASTINT (w->top);
  1818.       int i;
  1819.  
  1820.       for (i = 0; i < height; i++)
  1821.         {
  1822.           get_display_line (f, vpos + i, 0);
  1823.           display_string (w, vpos + i, "", 0, 
  1824.                   FRAME_LEFT_SCROLL_BAR_WIDTH (f),
  1825.                   0, 1, 0, width, 0);
  1826.         }
  1827.       
  1828.       goto finish_scroll_bars;
  1829.     }
  1830.     }
  1831.  
  1832.   /* Otherwise set up data on this window; select its buffer and point value */
  1833.  
  1834.   if (update_mode_line)
  1835.     /* Really select the buffer, for the sake of buffer-local variables.  */
  1836.     {
  1837.       set_buffer_internal_1 (XBUFFER (w->buffer));
  1838.       really_switched_buffer = 1;
  1839.     }
  1840.   else
  1841.     set_buffer_temp (XBUFFER (w->buffer));
  1842.  
  1843.   opoint = PT;
  1844.   opoint_byte = PT_BYTE;
  1845.  
  1846.   if (Z == Z_BYTE && opoint != opoint_byte)
  1847.     abort ();
  1848.   if (opoint_byte < opoint)
  1849.     abort ();
  1850.  
  1851.   /* If %c is in mode line, update it if needed.  */
  1852.   if (!NILP (w->column_number_displayed)
  1853.       /* This alternative quickly identifies a common case
  1854.      where no change is needed.  */
  1855.       && !(PT == XFASTINT (w->last_point)
  1856.        && XFASTINT (w->last_modified) >= MODIFF
  1857.        && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
  1858.       && XFASTINT (w->column_number_displayed) != current_column ())
  1859.     update_mode_line = 1; 
  1860.  
  1861.   /* Count number of windows showing the selected buffer.
  1862.      An indirect buffer counts as its base buffer.  */
  1863.  
  1864.   if (!just_this_one)
  1865.     {
  1866.       struct buffer *current_base, *window_base;
  1867.       current_base = current_buffer;
  1868.       window_base = XBUFFER (XWINDOW (selected_window)->buffer);
  1869.       if (current_base->base_buffer)
  1870.     current_base = current_base->base_buffer;
  1871.       if (window_base->base_buffer)
  1872.     window_base = window_base->base_buffer;
  1873.       if (current_base == window_base)
  1874.     buffer_shared++;
  1875.     }
  1876.  
  1877.   /* POINT refers normally to the selected window.
  1878.      For any other window, set up appropriate value.  */
  1879.  
  1880.   if (!EQ (window, selected_window))
  1881.     {
  1882.       int new_pt = XMARKER (w->pointm)->charpos;
  1883.       int new_pt_byte = marker_byte_position (w->pointm);
  1884.       if (new_pt < BEGV)
  1885.     {
  1886.       new_pt = BEGV;
  1887.       new_pt_byte = BEGV_BYTE;
  1888.       set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
  1889.     }
  1890.       else if (new_pt > (ZV - 1))
  1891.     {
  1892.       new_pt = ZV;
  1893.       new_pt_byte = ZV_BYTE;
  1894.       set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
  1895.     }
  1896.       /* We don't use SET_PT so that the point-motion hooks don't run.  */
  1897.       TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
  1898.     }
  1899.  
  1900.   /* If any of the character widths specified in the display table
  1901.      have changed, invalidate the width run cache.  It's true that this
  1902.      may be a bit late to catch such changes, but the rest of
  1903.      redisplay goes (non-fatally) haywire when the display table is
  1904.      changed, so why should we worry about doing any better?  */
  1905.   if (current_buffer->width_run_cache)
  1906.     {
  1907.       struct Lisp_Char_Table *disptab = buffer_display_table ();
  1908.  
  1909.       if (! disptab_matches_widthtab (disptab,
  1910.                                       XVECTOR (current_buffer->width_table)))
  1911.         {
  1912.           invalidate_region_cache (current_buffer,
  1913.                                    current_buffer->width_run_cache,
  1914.                                    BEG, Z);
  1915.           recompute_width_table (current_buffer, disptab);
  1916.         }
  1917.     }
  1918.  
  1919.   /* If window-start is screwed up, choose a new one.  */
  1920.   if (XMARKER (w->start)->buffer != current_buffer)
  1921.     goto recenter;
  1922.  
  1923.   startp = marker_position (w->start);
  1924.   startp_byte = marker_byte_position (w->start);
  1925.  
  1926.   /* If someone specified a new starting point but did not insist,
  1927.      check whether it can be used.  */
  1928.   if (!NILP (w->optional_new_start)
  1929.       && startp >= BEGV && startp <= ZV)
  1930.     {
  1931.       w->optional_new_start = Qnil;
  1932.       /* Check whether this start pos is usable given where point is.  */
  1933.  
  1934.       pos = *compute_motion (startp, 0,
  1935.                  (((EQ (window, minibuf_window)
  1936.                 && startp == BEG)
  1937.                    ? minibuf_prompt_width : 0)
  1938.                   + (hscroll ? 1 - hscroll : 0)),
  1939.                  0,
  1940.                  PT, height, 
  1941.                  /* BUG FIX: See the comment of
  1942.                                 Fpos_visible_in_window_p (window.c).  */
  1943.                  - (1 << (BITS_PER_SHORT - 1)),
  1944.                  width, hscroll,
  1945.                  pos_tab_offset (w, startp, startp_byte), w);
  1946.       /* If PT does fit on the screen, we will use this start pos,
  1947.      so do so by setting force_start.  */
  1948.       if (pos.bufpos == PT)
  1949.     w->force_start = Qt;
  1950.     }
  1951.  
  1952.   /* Handle case where place to start displaying has been specified,
  1953.      unless the specified location is outside the accessible range.  */
  1954.   if (!NILP (w->force_start))
  1955.     {
  1956.       w->force_start = Qnil;
  1957.       /* Forget any recorded base line for line number display.  */
  1958.       w->base_line_number = Qnil;
  1959.       /* Redisplay the mode line.  Select the buffer properly for that.
  1960.      Also, run the hook window-scroll-functions
  1961.      because we have scrolled.  */
  1962.       /* Note, we do this after clearing force_start because
  1963.      if there's an error, it is better to forget about force_start
  1964.      than to get into an infinite loop calling the hook functions
  1965.      and having them get more errors.  */
  1966.       if (!update_mode_line
  1967.       || ! NILP (Vwindow_scroll_functions))
  1968.     {
  1969.       Lisp_Object temp[3];
  1970.  
  1971.       if (!really_switched_buffer)
  1972.         {
  1973.           set_buffer_temp (old);
  1974.           set_buffer_internal_1 (XBUFFER (w->buffer));
  1975.         }
  1976.       really_switched_buffer = 1;
  1977.       update_mode_line = 1;
  1978.       w->update_mode_line = Qt;
  1979.       if (! NILP (Vwindow_scroll_functions))
  1980.         {
  1981.           run_hook_with_args_2 (Qwindow_scroll_functions, window,
  1982.                     make_number (startp));
  1983.           startp = marker_position (w->start);
  1984.           startp_byte = marker_byte_position (w->start);
  1985.         }
  1986.     }
  1987.       XSETFASTINT (w->last_modified, 0);
  1988.       XSETFASTINT (w->last_overlay_modified, 0);
  1989.       if (startp < BEGV) startp = BEGV, startp_byte = BEGV_BYTE;
  1990.       if (startp > ZV)   startp = ZV, startp = ZV_BYTE;
  1991.       try_window (window, startp);
  1992.       if (cursor_vpos < 0)
  1993.     {
  1994.       /* If point does not appear, move point so it does appear */
  1995.       pos = *compute_motion (startp, 0,
  1996.                  (((EQ (window, minibuf_window)
  1997.                     && startp == BEG)
  1998.                    ? minibuf_prompt_width : 0)
  1999.                   + (hscroll ? 1 - hscroll : 0)),
  2000.                  0,
  2001.                  ZV, height / 2,
  2002.                  - (1 << (BITS_PER_SHORT - 1)),
  2003.                  width, hscroll,
  2004.                  pos_tab_offset (w, startp, startp_byte),
  2005.                  w);
  2006.       TEMP_SET_PT_BOTH (pos.bufpos, pos.bytepos);
  2007.       if (w != XWINDOW (selected_window))
  2008.         set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
  2009.       else
  2010.         {
  2011.           if (current_buffer == old)
  2012.         {
  2013.           lpoint = PT;
  2014.           lpoint_byte = PT_BYTE;
  2015.         }
  2016.           FRAME_CURSOR_X (f) = (WINDOW_LEFT_MARGIN (w)
  2017.                     + minmax (0, pos.hpos, width));
  2018.           FRAME_CURSOR_Y (f) = pos.vpos + XFASTINT (w->top);
  2019.         }
  2020.       /* If we are highlighting the region,
  2021.          then we just changed the region, so redisplay to show it.  */
  2022.       if (!NILP (Vtransient_mark_mode)
  2023.           && !NILP (current_buffer->mark_active))
  2024.         {
  2025.           cancel_my_columns (XWINDOW (window));
  2026.           try_window (window, startp);
  2027.         }
  2028.     }
  2029.       goto done;
  2030.     }
  2031.  
  2032.   /* Handle case where text has not changed, only point,
  2033.      and it has not moved off the frame.  */
  2034.  
  2035.   /* This code is not used for minibuffer for the sake of
  2036.      the case of redisplaying to replace an echo area message;
  2037.      since in that case the minibuffer contents per se are usually unchanged.
  2038.      This code is of no real use in the minibuffer since
  2039.      the handling of this_line_bufpos, etc.,
  2040.      in redisplay handles the same cases.  */
  2041.  
  2042.   if (XFASTINT (w->last_modified) >= MODIFF
  2043.       && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF
  2044.       && PT >= startp && !current_buffer->clip_changed
  2045.       && (just_this_one || WINDOW_FULL_WIDTH_P (w))
  2046.       /* If force-mode-line-update was called, really redisplay;
  2047.      that's how redisplay is forced after e.g. changing
  2048.      buffer-invisibility-spec.  */
  2049.       && NILP (w->update_mode_line)
  2050.       /* Can't use this case if highlighting a region.  */
  2051.       && !(!NILP (Vtransient_mark_mode) && !NILP (current_buffer->mark_active))
  2052.       && NILP (w->region_showing)
  2053.       /* If end pos is out of date, scroll bar and percentage will be wrong */
  2054.       && INTEGERP (w->window_end_vpos)
  2055.       && XFASTINT (w->window_end_vpos) < XFASTINT (w->height)
  2056.       && !EQ (window, minibuf_window)
  2057.       && (!MARKERP (Voverlay_arrow_position)
  2058.       || current_buffer != XMARKER (Voverlay_arrow_position)->buffer))
  2059.     {
  2060.       /* All positions in this clause are relative to the window edge.  */
  2061.  
  2062.       int this_scroll_margin = scroll_margin;
  2063.       int last_point_y = XFASTINT (w->last_point_y) - XINT (w->top);
  2064.       int last_point_x = (XFASTINT (w->last_point_x) - WINDOW_LEFT_MARGIN (w));
  2065.  
  2066.       /* Find where PT is located now on the frame.  */
  2067.       /* Check just_this_one as a way of verifying that the 
  2068.      window edges have not changed.  */
  2069.       if (PT == XFASTINT (w->last_point) && just_this_one
  2070.       /* If CURSOR_IN_ECHO_AREA, last_point_x and last_point_y
  2071.          refer to the echo area and are not related to this window.  */
  2072.       && ! cursor_in_echo_area)
  2073.     {
  2074.       pos.hpos = last_point_x;
  2075.       pos.vpos = last_point_y;
  2076.       pos.bufpos = PT;
  2077.     }
  2078.       else if (PT > XFASTINT (w->last_point)
  2079.            && ! cursor_in_echo_area
  2080.            && XFASTINT (w->last_point) > startp && just_this_one
  2081.            /* We can't use this if point is in the left margin of a
  2082.           hscrolled window, because w->last_point_x has been
  2083.           clipped to the window edges.  */
  2084.            && !(last_point_x <= 0 && hscroll))
  2085.     {
  2086.       int last_point = XFASTINT (w->last_point);
  2087.       int last_point_byte = CHAR_TO_BYTE (last_point);
  2088.       int tab_offset = (pos_tab_offset (w, last_point, last_point_byte)
  2089.                 - (last_point_x + hscroll - !! hscroll));
  2090.  
  2091.       pos = *compute_motion (last_point, last_point_y, last_point_x, 0,
  2092.                  PT, height,
  2093.                  /* BUG FIX: See the comment of    
  2094.                     Fpos_visible_in_window_p (window.c).  */
  2095.                  - (1 << (BITS_PER_SHORT - 1)),
  2096.                  width, hscroll,
  2097.                  tab_offset,
  2098.                  w);
  2099.     }
  2100.       else
  2101.     {
  2102.       pos = *compute_motion (startp, 0, (hscroll ? 1 - hscroll : 0), 0,
  2103.                  PT, height,
  2104.                  /* BUG FIX: See the comment of
  2105.                     Fpos_visible_in_window_p (window.c).  */
  2106.                  - (1 << (BITS_PER_SHORT - 1)),
  2107.                  width, hscroll,
  2108.                  pos_tab_offset (w, startp, startp_byte),
  2109.                  w);
  2110.     }
  2111.  
  2112.       /* Don't use a scroll margin that is negative or too large.  */
  2113.       if (this_scroll_margin < 0)
  2114.     this_scroll_margin = 0;
  2115.  
  2116.       if (XINT (w->height) < 4 * scroll_margin)
  2117.     this_scroll_margin = XINT (w->height) / 4;
  2118.  
  2119.       /* If point fits on the screen, and not within the scroll margin,
  2120.      we are ok.  */
  2121.       if (pos.vpos < height - this_scroll_margin
  2122.       && (pos.vpos >= this_scroll_margin || startp == BEGV))
  2123.     {
  2124.       /* Ok, point is still on frame */
  2125.       if (w == XWINDOW (FRAME_SELECTED_WINDOW (f)))
  2126.         {
  2127.           /* These variables are supposed to be origin 1 */
  2128.           FRAME_CURSOR_X (f) = (WINDOW_LEFT_MARGIN (w)
  2129.                     + minmax (0, pos.hpos, width));
  2130.           FRAME_CURSOR_Y (f) = pos.vpos + XFASTINT (w->top);
  2131.         }
  2132.       /* This doesn't do the trick, because if a window to the right of
  2133.          this one must be redisplayed, this does nothing because there
  2134.          is nothing in DesiredFrame yet, and then the other window is
  2135.          redisplayed, making likes that are empty in this window's columns.
  2136.          if (WINDOW_FULL_WIDTH_P (w))
  2137.          preserve_my_columns (w);
  2138.          */
  2139.       if (current_buffer->clip_changed
  2140.           && ! NILP (Vwindow_scroll_functions))
  2141.         run_hook_with_args_2 (Qwindow_scroll_functions, window,
  2142.                   make_number (marker_position (w->start)));
  2143.  
  2144.       goto done;
  2145.     }
  2146.       /* Don't bother trying redisplay with same start;
  2147.      we already know it will lose.  */
  2148.     }
  2149.   /* If current starting point was originally the beginning of a line
  2150.      but no longer is, find a new starting point.  */
  2151.   else if (!NILP (w->start_at_line_beg)
  2152.        && !(startp <= BEGV
  2153.         || FETCH_BYTE (startp_byte - 1) == '\n'))
  2154.     {
  2155.       goto recenter;
  2156.     }
  2157.   else if (just_this_one && !MINI_WINDOW_P (w)
  2158.        && PT >= startp
  2159.        && XFASTINT (w->last_modified)
  2160.        /* or else vmotion on first line won't work.  */
  2161.        && ! NILP (w->start_at_line_beg)
  2162.        && ! EQ (w->window_end_valid, Qnil)
  2163.        && do_id && !current_buffer->clip_changed
  2164.        && !blank_end_of_window
  2165.        && WINDOW_FULL_WIDTH_P (w)
  2166.        /* Can't use this case if highlighting a region.  */
  2167.        && !(!NILP (Vtransient_mark_mode)
  2168.         && !NILP (current_buffer->mark_active))
  2169.        /* Don't use try_window_id if newline
  2170.           doesn't display as the end of a line.  */
  2171.        && !(dp != 0 && VECTORP (DISP_CHAR_VECTOR (dp, '\n')))
  2172.        && NILP (w->region_showing)
  2173.        && EQ (last_arrow_position, COERCE_MARKER (Voverlay_arrow_position))
  2174.        && EQ (last_arrow_string, Voverlay_arrow_string)
  2175.        && (tem = try_window_id (FRAME_SELECTED_WINDOW (f)))
  2176.        && tem != -2)
  2177.     {
  2178.       /* tem > 0 means success.  tem == -1 means choose new start.
  2179.      tem == -2 means try again with same start,
  2180.       and nothing but whitespace follows the changed stuff.
  2181.      tem == 0 means try again with same start.  */
  2182.       if (tem > 0)
  2183.     goto done;
  2184.     }
  2185.   else if (startp >= BEGV && startp <= ZV
  2186.        && (startp < ZV
  2187.            /* Avoid starting at end of buffer.  */
  2188. #if 0 /* This change causes trouble for M-! finger & RET.
  2189.      It will have to be considered later.  */
  2190.            || ! EQ (window, selected_window)
  2191.            /* Don't do the recentering if redisplay
  2192.           is not for no user action.  */
  2193.            || preserve_echo_area
  2194. #endif
  2195.            || startp == BEGV
  2196.            || (XFASTINT (w->last_modified) >= MODIFF
  2197.            && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
  2198.     {
  2199.       /* Try to redisplay starting at same place as before */
  2200.       /* If point has not moved off frame, accept the results */
  2201.       try_window (window, startp);
  2202.       if (cursor_vpos >= 0)
  2203.     {
  2204.       if (!just_this_one || current_buffer->clip_changed
  2205.           || beg_unchanged < startp)
  2206.         /* Forget any recorded base line for line number display.  */
  2207.         w->base_line_number = Qnil;
  2208.  
  2209.       if (current_buffer->clip_changed
  2210.           && ! NILP (Vwindow_scroll_functions))
  2211.         run_hook_with_args_2 (Qwindow_scroll_functions, window,
  2212.                   make_number (marker_position (w->start)));
  2213.  
  2214.       goto done;
  2215.     }
  2216.       else
  2217.     cancel_my_columns (w);
  2218.     }
  2219.  
  2220.   XSETFASTINT (w->last_modified, 0);
  2221.   XSETFASTINT (w->last_overlay_modified, 0);
  2222.   /* Redisplay the mode line.  Select the buffer properly for that.  */
  2223.   if (!update_mode_line)
  2224.     {
  2225.       if (!really_switched_buffer)
  2226.     {
  2227.       set_buffer_temp (old);
  2228.       set_buffer_internal_1 (XBUFFER (w->buffer));
  2229.     }
  2230.       update_mode_line = 1;
  2231.       w->update_mode_line = Qt;
  2232.     }
  2233.  
  2234.   /* Try to scroll by specified few lines */
  2235.  
  2236.   if ((scroll_conservatively || scroll_step)
  2237.       && !current_buffer->clip_changed
  2238.       && startp >= BEGV && startp <= ZV)
  2239.     {
  2240.       int this_scroll_margin = scroll_margin;
  2241.       int scroll_margin_pos, scroll_margin_bytepos;
  2242.       int scroll_max = scroll_step;
  2243.       Lisp_Object ltemp;
  2244.  
  2245.       if (scroll_conservatively)
  2246.     scroll_max = scroll_conservatively;
  2247.  
  2248.       /* Don't use a scroll margin that is negative or too large.  */
  2249.       if (this_scroll_margin < 0)
  2250.     this_scroll_margin = 0;
  2251.  
  2252.       if (XINT (w->height) < 4 * this_scroll_margin)
  2253.     this_scroll_margin = XINT (w->height) / 4;
  2254.  
  2255.       ltemp = Fwindow_end (window, Qt);
  2256.       scroll_margin_pos = XINT (ltemp);
  2257.  
  2258.       if (this_scroll_margin)
  2259.     {
  2260.       pos = *vmotion (scroll_margin_pos, -this_scroll_margin, w);
  2261.       scroll_margin_pos = pos.bufpos;
  2262.       scroll_margin_bytepos = pos.bytepos;
  2263.     }
  2264.       else
  2265.     scroll_margin_bytepos = CHAR_TO_BYTE (scroll_margin_pos);
  2266.  
  2267.       if (PT >= scroll_margin_pos)
  2268.     {
  2269.       struct position pos;
  2270.       pos = *compute_motion (scroll_margin_pos, 0, 0, 0,
  2271.                  PT, XFASTINT (w->height), 0,
  2272.                  XFASTINT (w->width), XFASTINT (w->hscroll),
  2273.                  pos_tab_offset (w, scroll_margin_pos,
  2274.                          scroll_margin_bytepos),
  2275.                  w);
  2276.       if (pos.vpos >= scroll_max)
  2277.         goto scroll_fail_1;
  2278.  
  2279.       pos = *vmotion (startp,
  2280.               scroll_conservatively ? pos.vpos + 1 : scroll_step,
  2281.               w);
  2282.  
  2283.       if (! NILP (Vwindow_scroll_functions))
  2284.         {
  2285.           set_marker_both (w->start, Qnil, pos.bufpos, pos.bytepos);
  2286.           run_hook_with_args_2 (Qwindow_scroll_functions, window,
  2287.                     make_number (pos.bufpos));
  2288.           pos.bufpos = marker_position (w->start);
  2289.         }
  2290.       try_window (window, pos.bufpos);
  2291.       if (cursor_vpos >= 0)
  2292.         {
  2293.           if (!just_this_one || current_buffer->clip_changed
  2294.           || beg_unchanged < startp)
  2295.         /* Forget any recorded base line for line number display.  */
  2296.         w->base_line_number = Qnil;
  2297.           goto done;
  2298.         }
  2299.       else
  2300.         cancel_my_columns (w);
  2301.     }
  2302.  
  2303.       scroll_margin_pos = startp;
  2304.       if (this_scroll_margin)
  2305.     {
  2306.       pos = *vmotion (scroll_margin_pos, this_scroll_margin, w);
  2307.       scroll_margin_pos = pos.bufpos;
  2308.     }
  2309.       if (PT < scroll_margin_pos)
  2310.     {
  2311.       struct position pos;
  2312.       pos = *compute_motion (PT, 0, 0, 0,
  2313.                  scroll_margin_pos, XFASTINT (w->height), 0,
  2314.                  XFASTINT (w->width), XFASTINT (w->hscroll),
  2315.                  pos_tab_offset (w, PT, PT_BYTE),
  2316.                  w);
  2317.       if (pos.vpos > scroll_max)
  2318.         goto scroll_fail_1;
  2319.  
  2320.       pos = *vmotion (startp,
  2321.               scroll_conservatively ? -pos.vpos : - scroll_step,
  2322.               w);
  2323.  
  2324.       if (! NILP (Vwindow_scroll_functions))
  2325.         {
  2326.           set_marker_both (w->start, Qnil, pos.bufpos, pos.bytepos);
  2327.           run_hook_with_args_2 (Qwindow_scroll_functions, window,
  2328.                     make_number (pos.bufpos));
  2329.           pos.bufpos = marker_position (w->start);
  2330.         }
  2331.       try_window (window, pos.bufpos);
  2332.       if (cursor_vpos >= 0)
  2333.         {
  2334.           if (!just_this_one || current_buffer->clip_changed
  2335.           || beg_unchanged < startp)
  2336.         /* Forget any recorded base line for line number display.  */
  2337.         w->base_line_number = Qnil;
  2338.           goto done;
  2339.         }
  2340.       else
  2341.         cancel_my_columns (w);
  2342.     }
  2343.     scroll_fail_1: ;
  2344.     }
  2345.  
  2346. #if 0
  2347.   if (scroll_step && ! scroll_margin && !current_buffer->clip_changed
  2348.       && startp >= BEGV && startp <= ZV)
  2349.     {
  2350.       if (margin_call == 0)
  2351.     margin_call = (PT > startp ? 1 : -1);
  2352.       if (margin_call > 0)
  2353.     {
  2354.       pos = *vmotion (Z - XFASTINT (w->window_end_pos), scroll_step, w);
  2355.       if (pos.vpos >= height)
  2356.         goto scroll_fail;
  2357.     }
  2358.  
  2359.       pos = *vmotion (startp, (margin_call < 0 ? - scroll_step : scroll_step),
  2360.               w);
  2361.  
  2362.       if (PT >= pos.bufpos)
  2363.     {
  2364.       if (! NILP (Vwindow_scroll_functions))
  2365.         {
  2366.           set_marker_both (w->start, Qnil, pos.bufpos, pos.bytepos);
  2367.           run_hook_with_args_2 (Qwindow_scroll_functions, window,
  2368.                     make_number (pos.bufpos));
  2369.           pos.bufpos = marker_position (w->start);
  2370.         }
  2371.       try_window (window, pos.bufpos);
  2372.       if (cursor_vpos >= 0)
  2373.         {
  2374.           if (!just_this_one || current_buffer->clip_changed
  2375.           || beg_unchanged < startp)
  2376.         /* Forget any recorded base line for line number display.  */
  2377.         w->base_line_number = Qnil;
  2378.           goto done;
  2379.         }
  2380.       else
  2381.         cancel_my_columns (w);
  2382.     }
  2383.     scroll_fail: ;
  2384.     }
  2385. #endif
  2386.  
  2387.   /* Finally, just choose place to start which centers point */
  2388.  
  2389. recenter:
  2390.   /* Forget any previously recorded base line for line number display.  */
  2391.   w->base_line_number = Qnil;
  2392.  
  2393.   pos = *vmotion (PT, - (height / 2), w);
  2394.  
  2395.   /* The minibuffer is often just one line.  Ordinary scrolling
  2396.      gives little overlap and looks bad.  So show 20 chars before point.  */
  2397.   if (height == 1
  2398.       && (pos.bufpos >= PT - minibuffer_scroll_overlap
  2399.       /* If we scrolled less than 1/2 line forward, we will
  2400.          get too much overlap, so change to the usual amount.  */
  2401.       || pos.bufpos < startp + width / 2)
  2402.       && PT > BEGV + minibuffer_scroll_overlap
  2403.       /* If we scrolled to an actual line boundary,
  2404.      that's different; don't ignore line boundaries.  */
  2405.       && FETCH_BYTE (pos.bytepos - 1) != '\n')
  2406.     {
  2407.       pos.bufpos = PT - minibuffer_scroll_overlap;
  2408.       pos.bytepos = CHAR_TO_BYTE (pos.bufpos);
  2409.     }
  2410.     
  2411.   /* Set startp here explicitly in case that helps avoid an infinite loop
  2412.      in case the window-scroll-functions functions get errors.  */
  2413.   set_marker_both (w->start, Qnil, pos.bufpos, pos.bytepos);
  2414.   if (! NILP (Vwindow_scroll_functions))
  2415.     {
  2416.       run_hook_with_args_2 (Qwindow_scroll_functions, window,
  2417.                 make_number (pos.bufpos));
  2418.       pos.bufpos = marker_position (w->start);
  2419.       pos.bytepos = marker_byte_position (w->start);
  2420.     }
  2421.   try_window (window, pos.bufpos);
  2422.  
  2423.   startp = marker_position (w->start);
  2424.   startp_byte = marker_byte_position (w->start);
  2425.   w->start_at_line_beg
  2426.     = (startp == BEGV || FETCH_BYTE (startp_byte - 1) == '\n') ? Qt : Qnil;
  2427.  
  2428. done:
  2429.   if ((update_mode_line
  2430.        /* If window not full width, must redo its mode line
  2431.       if the window to its side is being redone */
  2432.        || (!just_this_one && !WINDOW_FULL_WIDTH_P (w))
  2433.        || INTEGERP (w->base_line_pos)
  2434.        || (!NILP (w->column_number_displayed)
  2435.        && XFASTINT (w->column_number_displayed) != current_column ()))
  2436.       && height != XFASTINT (w->height))
  2437.     {
  2438.       FRAME_PTR oframe = selected_frame;
  2439.       if (!really_switched_buffer)
  2440.     {
  2441.       set_buffer_temp (old);
  2442.       set_buffer_internal_1 (XBUFFER (w->buffer));
  2443.       really_switched_buffer = 1;
  2444.     }
  2445.       selected_frame = f;
  2446.       display_mode_line (w);
  2447.       selected_frame = oframe;
  2448.     }
  2449.   if (! line_number_displayed
  2450.       && ! BUFFERP (w->base_line_pos))
  2451.     {
  2452.       w->base_line_pos = Qnil;
  2453.       w->base_line_number = Qnil;
  2454.     }
  2455.  
  2456.   /* When we reach a frame's selected window, redo the frame's menu bar.  */
  2457.   if (update_mode_line
  2458.       && (FRAME_WINDOW_P (f)
  2459.       ?
  2460. #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI)
  2461.       FRAME_EXTERNAL_MENU_BAR (f) 
  2462. #else
  2463.       FRAME_MENU_BAR_LINES (f) > 0
  2464. #endif
  2465.       : FRAME_MENU_BAR_LINES (f) > 0)
  2466.       && EQ (FRAME_SELECTED_WINDOW (f), window))
  2467.     display_menu_bar (w);
  2468.  
  2469.  finish_scroll_bars:
  2470.   if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
  2471.     {
  2472.       int start, end, whole;
  2473.  
  2474.       /* Calculate the start and end positions for the current window.
  2475.      At some point, it would be nice to choose between scrollbars
  2476.      which reflect the whole buffer size, with special markers
  2477.      indicating narrowing, and scrollbars which reflect only the
  2478.      visible region.
  2479.  
  2480.      Note that minibuffers sometimes aren't displaying any text.  */
  2481.       if (! MINI_WINDOW_P (w)
  2482.       || (w == XWINDOW (minibuf_window) && ! echo_area_glyphs))
  2483.     {
  2484.       whole = ZV - BEGV;
  2485.       start = marker_position (w->start) - BEGV;
  2486.       /* I don't think this is guaranteed to be right.  For the
  2487.          moment, we'll pretend it is.  */
  2488.       end = (Z - XINT (w->window_end_pos)) - BEGV;
  2489.  
  2490.       if (end < start) end = start;
  2491.       if (whole < (end - start)) whole = end - start;
  2492.     }
  2493.       else
  2494.     start = end = whole = 0;
  2495.  
  2496.       /* Indicate what this scroll bar ought to be displaying now.  */
  2497.       (*set_vertical_scroll_bar_hook) (w, end - start, whole, start);
  2498.  
  2499.       /* Note that we actually used the scroll bar attached to this window,
  2500.      so it shouldn't be deleted at the end of redisplay.  */
  2501.       (*redeem_scroll_bar_hook) (w);
  2502.     }
  2503.  
  2504.   TEMP_SET_PT_BOTH (opoint, opoint_byte);
  2505.   if (really_switched_buffer)
  2506.     set_buffer_internal_1 (old);
  2507.   else
  2508.     set_buffer_temp (old);
  2509.   TEMP_SET_PT_BOTH (lpoint, lpoint_byte);
  2510.  
  2511.   unbind_to (count, Qnil);
  2512. }
  2513.  
  2514. /* Do full redisplay on one window, starting at position `pos'. */
  2515.  
  2516. static void
  2517. try_window (window, pos)
  2518.      Lisp_Object window;
  2519.      register int pos;
  2520. {
  2521.   register struct window *w = XWINDOW (window);
  2522.   register int height = window_internal_height (w);
  2523.   register int vpos = XFASTINT (w->top);
  2524.   register int last_text_vpos = vpos;
  2525.   FRAME_PTR f = XFRAME (w->frame);
  2526.   int width = window_internal_width (w) - 1;
  2527.   struct position val;
  2528.  
  2529.   /* POS should never be out of range!  */
  2530.   if (pos < XBUFFER (w->buffer)->begv
  2531.       || pos > XBUFFER (w->buffer)->zv)
  2532.     abort ();
  2533.  
  2534.   if (XMARKER (w->start)->charpos != pos)
  2535.     Fset_marker (w->start, make_number (pos), Qnil);
  2536.  
  2537.   cursor_vpos = -1;
  2538.   overlay_arrow_seen = 0;
  2539.   zv_strings_seen = 0;
  2540.   val.hpos = XINT (w->hscroll) ? 1 - XINT (w->hscroll) : 0;
  2541.   val.ovstring_chars_done = 0;
  2542.   val.bytepos = marker_byte_position (w->start);
  2543.   val.tab_offset = pos_tab_offset (w, pos, val.bytepos);
  2544.  
  2545.   while (--height >= 0)
  2546.     {
  2547.       val = *display_text_line (w, pos, val.bytepos, vpos,
  2548.                 val.hpos, val.tab_offset,
  2549.                 val.ovstring_chars_done);
  2550.       /* The following code is omitted because we maintain tab_offset
  2551.          in VAL.  */
  2552. #if 0
  2553.       tab_offset += width;
  2554.       if (val.vpos) tab_offset = 0;
  2555. #endif  /* 0 */
  2556.       vpos++;
  2557.       if (pos != val.bufpos)
  2558.     {
  2559.       int invis = 0;
  2560. #ifdef USE_TEXT_PROPERTIES
  2561.       Lisp_Object invis_prop;
  2562.       invis_prop = Fget_char_property (make_number (val.bufpos - 1),
  2563.                        Qinvisible, window);
  2564.       invis = TEXT_PROP_MEANS_INVISIBLE (invis_prop);
  2565. #endif
  2566.  
  2567.       last_text_vpos
  2568.         /* Next line, unless prev line ended in end of buffer with no cr */
  2569.         = vpos - (val.vpos
  2570.               && (FETCH_BYTE (val.bytepos - 1) != '\n' || invis));
  2571.     }
  2572.       pos = val.bufpos;
  2573.     }
  2574.  
  2575.   /* If last line is continued in middle of character,
  2576.      include the split character in the text considered on the frame */
  2577.   if (val.hpos < (XINT (w->hscroll) ? 1 - XINT (w->hscroll) : 0))
  2578.     pos++;
  2579.  
  2580.   /* If bottom just moved off end of frame, change mode line percentage.  */
  2581.   if (XFASTINT (w->window_end_pos) == 0
  2582.       && Z != pos)
  2583.     w->update_mode_line = Qt;
  2584.  
  2585.   /* Say where last char on frame will be, once redisplay is finished.  */
  2586.   XSETFASTINT (w->window_end_pos, Z - pos);
  2587.   XSETFASTINT (w->window_end_vpos, last_text_vpos - XFASTINT (w->top));
  2588.   /* But that is not valid info until redisplay finishes.  */
  2589.   w->window_end_valid = Qnil;
  2590. }
  2591.  
  2592. /* Try to redisplay when buffer is modified locally,
  2593.  computing insert/delete line to preserve text outside
  2594.  the bounds of the changes.
  2595.  Return 1 if successful, 0 if if cannot tell what to do,
  2596.  or -1 to tell caller to find a new window start,
  2597.  or -2 to tell caller to do normal redisplay with same window start.  */
  2598.  
  2599. static int
  2600. try_window_id (window)
  2601.      Lisp_Object window;
  2602. {
  2603.   int pos, pos_byte;
  2604.   int opoint, opoint_byte;
  2605.   register struct window *w = XWINDOW (window);
  2606.   register int height = window_internal_height (w);
  2607.   FRAME_PTR f = XFRAME (w->frame);
  2608.   int top = XFASTINT (w->top);
  2609.   int start = marker_position (w->start);
  2610.   int width = window_internal_width (w) - 1;
  2611.   int hscroll = XINT (w->hscroll);
  2612.   int lmargin = hscroll > 0 ? 1 - hscroll : 0;
  2613.   int did_motion;
  2614.   register int vpos;
  2615.   register int i, tem;
  2616.   int last_text_vpos = 0;
  2617.   int stop_vpos;
  2618.   int selective = (INTEGERP (current_buffer->selective_display)
  2619.            ? XINT (current_buffer->selective_display)
  2620.            : !NILP (current_buffer->selective_display) ? -1 : 0);
  2621.   struct position val, bp, ep, xp, pp;
  2622.   int scroll_amount = 0;
  2623.   int delta;
  2624.   int epto, old_tick;
  2625.  
  2626.   int start_byte = marker_byte_position (w->start);
  2627.  
  2628.   if (GPT - BEG < beg_unchanged)
  2629.     beg_unchanged = GPT - BEG;
  2630.   if (Z - GPT < end_unchanged)
  2631.     end_unchanged = Z - GPT;
  2632.  
  2633.   if (beg_unchanged + BEG < start)
  2634.     return 0;            /* Give up if changes go above top of window */
  2635.  
  2636.   /* Find position before which nothing is changed.  */
  2637.   bp = *compute_motion (start, 0, lmargin, 0,
  2638.             min (ZV, beg_unchanged + BEG), height,
  2639.             /* BUG FIX: See the comment of
  2640.                            Fpos_visible_in_window_p (window.c).  */
  2641.             - (1 << (BITS_PER_SHORT - 1)),
  2642.             width, hscroll,
  2643.             pos_tab_offset (w, start, start_byte),
  2644.             w);
  2645.   if (bp.vpos >= height)
  2646.     {
  2647.       if (PT < bp.bufpos)
  2648.     {
  2649.       /* All changes are beyond the window end, and point is on the screen.
  2650.          We don't need to change the text at all.
  2651.          But we need to update window_end_pos to account for
  2652.          any change in buffer size.  */
  2653.       bp = *compute_motion (start, 0, lmargin, 0,
  2654.                 ZV, height,
  2655.                 /* BUG FIX: See the comment of
  2656.                                    Fpos_visible_in_window_p() (window.c).  */
  2657.                 - (1 << (BITS_PER_SHORT - 1)),
  2658.                 width, hscroll,
  2659.                 pos_tab_offset (w, start, start_byte), w);
  2660.       XSETFASTINT (w->window_end_vpos, height);
  2661.       XSETFASTINT (w->window_end_pos, Z - bp.bufpos);
  2662.       goto findpoint;
  2663.     }
  2664.       return 0;
  2665.     }
  2666.  
  2667.   vpos = bp.vpos;
  2668.  
  2669.   /* Find beginning of that frame line.  Must display from there.  */
  2670.   bp = *vmotion (bp.bufpos, 0, w);
  2671.  
  2672.   pos = bp.bufpos;
  2673.   pos_byte = bp.bytepos;
  2674.   val.hpos = lmargin;
  2675.   if (pos < start)
  2676.     return -1;
  2677.  
  2678.   did_motion = 0;
  2679.   /* If about to start displaying at the beginning of a continuation line,
  2680.      really start with previous frame line, in case it was not
  2681.      continued when last redisplayed */
  2682.   if ((bp.contin && bp.bufpos - 1 == beg_unchanged && vpos > 0)
  2683.       ||
  2684.       /* Likewise if we have to worry about selective display.  */
  2685.       (selective > 0 && bp.bufpos - 1 == beg_unchanged && vpos > 0))
  2686.     {
  2687.       bp = *vmotion (bp.bufpos, -1, w);
  2688.       --vpos;
  2689.       pos = bp.bufpos;
  2690.       pos_byte = bp.bytepos;
  2691.     }
  2692.   val.tab_offset = bp.tab_offset; /* Update tab offset.  */
  2693.  
  2694.   if (bp.contin && bp.hpos != lmargin)
  2695.     {
  2696.       val.hpos = bp.prevhpos - width + lmargin;
  2697.       val.tab_offset = bp.tab_offset + bp.prevhpos - width;
  2698.       did_motion = 1;
  2699.       DEC_BOTH (pos, pos_byte);
  2700.     }
  2701.  
  2702.   bp.vpos = vpos;
  2703.  
  2704.   /* Find first visible newline after which no more is changed.  */
  2705.   opoint = PT, opoint_byte = PT_BYTE;
  2706.   SET_PT (Z - max (end_unchanged, Z - ZV));
  2707.   scan_newline (PT, PT_BYTE, ZV, ZV_BYTE, 1, 1);
  2708.   if (selective > 0)
  2709.     while (PT < ZV - 1 && indented_beyond_p (PT, PT_BYTE, selective))
  2710.       scan_newline (PT, PT_BYTE, ZV, ZV_BYTE, 1, 1);
  2711.   tem = PT;
  2712.   SET_PT_BOTH (opoint, opoint_byte);
  2713.  
  2714.   /* Compute the cursor position after that newline.  */
  2715.   ep = *compute_motion (pos, vpos, val.hpos, did_motion, tem,
  2716.             height, - (1 << (BITS_PER_SHORT - 1)),
  2717.             width, hscroll,
  2718.             /* We have tab offset in VAL, use it.  */
  2719.             val.tab_offset, w); 
  2720.  
  2721.   /* If changes reach past the text available on the frame,
  2722.      just display rest of frame.  */
  2723.   if (ep.bufpos > Z - XFASTINT (w->window_end_pos))
  2724.     stop_vpos = height;
  2725.   else
  2726.     stop_vpos = ep.vpos;
  2727.  
  2728.   /* If no newline before ep, the line ep is on includes some changes
  2729.      that must be displayed.  Make sure we don't stop before it.  */
  2730.   /* Also, if changes reach all the way until ep.bufpos,
  2731.      it is possible that something was deleted after the
  2732.      newline before it, so the following line must be redrawn. */
  2733.   if (stop_vpos == ep.vpos
  2734.       && (ep.bufpos == BEGV
  2735.       || FETCH_BYTE (ep.bytepos - 1) != '\n'
  2736.       || ep.bufpos == Z - end_unchanged))
  2737.     stop_vpos = ep.vpos + 1;
  2738.  
  2739.   cursor_vpos = -1;
  2740.   overlay_arrow_seen = 0;
  2741.   zv_strings_seen = 0;
  2742.  
  2743.   /* If changes do not reach to bottom of window,
  2744.      figure out how much to scroll the rest of the window */
  2745.   if (stop_vpos < height)
  2746.     {
  2747.       /* Now determine how far up or down the rest of the window has moved */
  2748.       xp = *compute_motion (ep.bufpos, ep.vpos, ep.hpos, 1,
  2749.                 Z - XFASTINT (w->window_end_pos),
  2750.                 /* Don't care for VPOS... */
  2751.                 1 << (BITS_PER_SHORT - 1),
  2752.                 /* ... nor HPOS.  */
  2753.                 1 << (BITS_PER_SHORT - 1),
  2754.                 width, hscroll, ep.tab_offset, w);
  2755.       scroll_amount = xp.vpos - XFASTINT (w->window_end_vpos);
  2756.  
  2757.       /* Is everything on frame below the changes whitespace?
  2758.      If so, no scrolling is really necessary.  */
  2759.       for (i = ep.bytepos; i < xp.bytepos; i++)
  2760.     {
  2761.       tem = FETCH_BYTE (i);
  2762.       if (tem != ' ' && tem != '\n' && tem != '\t')
  2763.         break;
  2764.     }
  2765.       if (i == xp.bytepos)
  2766.     return -2;
  2767.  
  2768.       XSETFASTINT (w->window_end_vpos,
  2769.            XFASTINT (w->window_end_vpos) + scroll_amount);
  2770.  
  2771.       /* Before doing any scrolling, verify that point will be on frame. */
  2772.       if (PT > ep.bufpos && !(PT <= xp.bufpos && xp.vpos < height))
  2773.     {
  2774.       if (PT <= xp.bufpos)
  2775.         {
  2776.           pp = *compute_motion (ep.bufpos, ep.vpos, ep.hpos, 1,
  2777.                     PT, height, - (1 << (BITS_PER_SHORT - 1)),
  2778.                     width, hscroll,
  2779.                     /* We have tab offset in EP, use it.  */
  2780.                     ep.tab_offset, w);
  2781.         }
  2782.       else
  2783.         {
  2784.           pp = *compute_motion (xp.bufpos, xp.vpos, xp.hpos, 1,
  2785.                     PT, height, - (1 << (BITS_PER_SHORT - 1)),
  2786.                     width, hscroll,
  2787.                     /* We have tab offset in XP, use it. */
  2788.                     xp.tab_offset, w);
  2789.         }
  2790.       if (pp.bufpos < PT || pp.vpos == height)
  2791.         return 0;
  2792.       cursor_vpos = pp.vpos + top;
  2793.       cursor_hpos = WINDOW_LEFT_MARGIN (w) + minmax (0, pp.hpos, width);
  2794.     }
  2795.  
  2796.       if (stop_vpos - scroll_amount >= height
  2797.       || ep.bufpos == xp.bufpos)
  2798.     {
  2799.       if (scroll_amount < 0)
  2800.         stop_vpos -= scroll_amount;
  2801.       scroll_amount = 0;
  2802.       /* In this path, we have altered window_end_vpos
  2803.          and not left it negative.
  2804.          We must make sure that, in case display is preempted
  2805.          before the frame changes to reflect what we do here,
  2806.          further updates will not come to try_window_id
  2807.          and assume the frame and window_end_vpos match.  */
  2808.       blank_end_of_window = 1;
  2809.     }
  2810.       else if (!scroll_amount)
  2811.     {
  2812.       /* Even if we don't need to scroll, we must adjust the
  2813.          charstarts of subsequent lines (that we won't redisplay)
  2814.          according to the amount of text inserted or deleted.  */
  2815.       int oldpos = FRAME_CURRENT_GLYPHS (f)->charstarts[ep.vpos + top][0];
  2816.       int adjust = ep.bufpos - oldpos;
  2817.       adjust_window_charstarts (w, ep.vpos + top - 1, adjust);
  2818.     }
  2819.       else if (bp.bufpos == Z - end_unchanged)
  2820.     {
  2821.       /* If reprinting everything is nearly as fast as scrolling,
  2822.          don't bother scrolling.  Can happen if lines are short.  */
  2823.       if (scroll_cost (f, bp.vpos + top - scroll_amount,
  2824.                top + height - max (0, scroll_amount),
  2825.                scroll_amount)
  2826.           > xp.bufpos - bp.bufpos - 20)
  2827.         /* Return "try normal display with same window-start."
  2828.            Too bad we can't prevent further scroll-thinking.  */
  2829.         return -2;
  2830.       /* If pure deletion, scroll up as many lines as possible.
  2831.          In common case of killing a line, this can save the
  2832.          following line from being overwritten by scrolling
  2833.          and therefore having to be redrawn.  */
  2834.       tem = scroll_frame_lines (f, bp.vpos + top - scroll_amount,
  2835.                     top + height - max (0, scroll_amount),
  2836.                     scroll_amount, bp.bufpos);
  2837.       if (!tem)
  2838.         stop_vpos = height;
  2839.       else
  2840.         {
  2841.           /* scroll_frame_lines did not properly adjust subsequent
  2842.          lines' charstarts in the case where the text of the
  2843.          screen line at bp.vpos has changed.
  2844.          (This can happen in a deletion that ends in mid-line.)
  2845.          To adjust properly, we need to make things consistent
  2846.          at the position ep.
  2847.          So do a second adjust to make that happen.
  2848.          Note that stop_vpos >= ep.vpos, so it is sufficient
  2849.          to update the charstarts for lines at ep.vpos and below.  */
  2850.           int oldstart
  2851.         = FRAME_CURRENT_GLYPHS (f)->charstarts[ep.vpos + top][0];
  2852.           adjust_window_charstarts (w, ep.vpos + top - 1,
  2853.                     ep.bufpos - oldstart);
  2854.         }
  2855.     }
  2856.       else if (scroll_amount)
  2857.     {
  2858.       /* If reprinting everything is nearly as fast as scrolling,
  2859.          don't bother scrolling.  Can happen if lines are short.  */
  2860.       /* Note that if scroll_amount > 0, xp.bufpos - bp.bufpos is an
  2861.          overestimate of cost of reprinting, since xp.bufpos
  2862.          would end up below the bottom of the window.  */
  2863.       if (scroll_cost (f, ep.vpos + top - scroll_amount,
  2864.                top + height - max (0, scroll_amount),
  2865.                scroll_amount)
  2866.           > xp.bufpos - ep.bufpos - 20)
  2867.         /* Return "try normal display with same window-start."
  2868.            Too bad we can't prevent further scroll-thinking.  */
  2869.         return -2;
  2870.       tem = scroll_frame_lines (f, ep.vpos + top - scroll_amount,
  2871.                      top + height - max (0, scroll_amount),
  2872.                      scroll_amount, ep.bufpos);
  2873.       if (!tem) stop_vpos = height;
  2874.     }
  2875.     }
  2876.  
  2877.   /* In any case, do not display past bottom of window */
  2878.   if (stop_vpos >= height)
  2879.     {
  2880.       stop_vpos = height;
  2881.       scroll_amount = 0;
  2882.     }
  2883.  
  2884.   /* Handle case where pos is before w->start --
  2885.      can happen if part of line had been clipped and is not clipped now */
  2886.   if (vpos == 0 && pos < marker_position (w->start))
  2887.     Fset_marker (w->start, make_number (pos), Qnil);
  2888.  
  2889.   val.bytepos = pos_byte;
  2890.  
  2891.   /* Redisplay the lines where the text was changed */
  2892.   last_text_vpos = vpos;
  2893.   /* The following code is omitted because we maintain tab offset in
  2894.      val.tab_offset.  */
  2895. #if 0
  2896.   tab_offset = pos_tab_offset (w, pos, pos_byte);
  2897.   /* If we are starting display in mid-character, correct tab_offset
  2898.      to account for passing the line that that character really starts in.  */
  2899.   if (val.hpos < lmargin)
  2900.     tab_offset += width;
  2901. #endif /* 0 */
  2902.   old_tick = MODIFF;
  2903.   while (vpos < stop_vpos)
  2904.     {
  2905.       val = *display_text_line (w, pos, val.bytepos, top + vpos++,
  2906.                 val.hpos, val.tab_offset,
  2907.                 val.ovstring_chars_done);
  2908.       /* If display_text_line ran a hook and changed some text,
  2909.      redisplay all the way to bottom of buffer
  2910.      So that we show the changes.  */
  2911.       if (old_tick != MODIFF)
  2912.     stop_vpos = height;
  2913.       /* The following code is omitted because we maintain tab offset
  2914.      in val.tab_offset.  */
  2915. #if 0
  2916.       tab_offset += width;
  2917.       if (val.vpos) tab_offset = 0;
  2918. #endif
  2919.       if (pos != val.bufpos)
  2920.     last_text_vpos
  2921.       /* Next line, unless prev line ended in end of buffer with no cr */
  2922.         = vpos - (val.vpos && FETCH_BYTE (val.bytepos - 1) != '\n');
  2923.       pos = val.bufpos;
  2924.     }
  2925.  
  2926.   /* There are two cases:
  2927.      1) we have displayed down to the bottom of the window
  2928.      2) we have scrolled lines below stop_vpos by scroll_amount  */
  2929.  
  2930.   if (vpos == height)
  2931.     {
  2932.       /* If last line is continued in middle of character,
  2933.      include the split character in the text considered on the frame */
  2934.       if (val.hpos < lmargin)
  2935.     val.bufpos++;
  2936.       XSETFASTINT (w->window_end_vpos, last_text_vpos);
  2937.       XSETFASTINT (w->window_end_pos, Z - val.bufpos);
  2938.     }
  2939.  
  2940.   /* If scrolling made blank lines at window bottom,
  2941.      redisplay to fill those lines */
  2942.   if (scroll_amount < 0)
  2943.     {
  2944.       /* Don't consider these lines for general-purpose scrolling.
  2945.      That will save time in the scrolling computation.  */
  2946.       FRAME_SCROLL_BOTTOM_VPOS (f) = xp.vpos;
  2947.       vpos = xp.vpos;
  2948.       pos = xp.bufpos;
  2949.       pos_byte = xp.bytepos;
  2950.       val.hpos = xp.hpos;
  2951.       val.tab_offset = xp.tab_offset;
  2952.       if (pos == ZV)
  2953.     { /* Display from next line */
  2954.       vpos = height + scroll_amount;
  2955.       val.hpos = lmargin;
  2956.       val.tab_offset = 0;
  2957.     }
  2958.       else if (xp.contin && xp.hpos != lmargin)
  2959.     {
  2960.       val.hpos = xp.prevhpos - width + lmargin;
  2961.       val.tab_offset = xp.tab_offset + bp.prevhpos - width;
  2962.       DEC_BOTH (pos, pos_byte);
  2963.     }
  2964.  
  2965.       blank_end_of_window = 1;
  2966.       /* The following code is omitted because we maintain tab offset
  2967.      in val.tab_offset.  */
  2968. #if 0
  2969.       tab_offset = pos_tab_offset (w, pos, pos_byte);
  2970.       /* If we are starting display in mid-character, correct tab_offset
  2971.      to account for passing the line that that character starts in.  */
  2972.       if (val.hpos < lmargin)
  2973.     tab_offset += width;
  2974. #endif
  2975.       val.bytepos = pos_byte;
  2976.       while (vpos < height)
  2977.     {
  2978.       val = *display_text_line (w, pos, val.bytepos,
  2979.                     top + vpos++, val.hpos,
  2980.                     val.tab_offset, val.ovstring_chars_done);
  2981.       /* The following code is omitted because we maintain tab
  2982.          offset in val.tab_offset.  */
  2983. #if 0
  2984.       tab_offset += width;
  2985.       if (val.vpos) tab_offset = 0;
  2986. #endif /* 0 */
  2987.       pos = val.bufpos;
  2988.     }
  2989.  
  2990.       /* Here is a case where display_text_line sets cursor_vpos wrong.
  2991.      Make it be fixed up, below.  */
  2992.       if (xp.bufpos == ZV
  2993.       && xp.bufpos == PT)
  2994.     cursor_vpos = -1;
  2995.     }
  2996.  
  2997.   /* If bottom just moved off end of frame, change mode line percentage.  */
  2998.   if (XFASTINT (w->window_end_pos) == 0
  2999.       && Z != val.bufpos)
  3000.     w->update_mode_line = Qt;
  3001.  
  3002.   /* Attempt to adjust end-of-text positions to new bottom line */
  3003.   if (scroll_amount)
  3004.     {
  3005.       delta = height - xp.vpos;
  3006.       if (delta < 0
  3007.       || (delta > 0 && xp.bufpos <= ZV)
  3008.       || (delta == 0 && xp.hpos))
  3009.     {
  3010.       val = *vmotion (Z - XFASTINT (w->window_end_pos), delta, w);
  3011.       XSETFASTINT (w->window_end_pos, Z - val.bufpos);
  3012.       XSETFASTINT (w->window_end_vpos,
  3013.                XFASTINT (w->window_end_vpos) + val.vpos);
  3014.     }
  3015.     }
  3016.  
  3017.   w->window_end_valid = Qnil;
  3018.  
  3019.   /* If point was not in a line that was displayed, find it */
  3020.   if (cursor_vpos < 0)
  3021.     {
  3022.     findpoint:
  3023.       val = *compute_motion (start, 0, lmargin, 0, PT, 
  3024.                  /* Don't care for VPOS...  */
  3025.                  1 << (BITS_PER_SHORT - 1),
  3026.                  /* ... nor HPOS.  */
  3027.                  1 << (BITS_PER_SHORT - 1),
  3028.                  width, hscroll,
  3029.                  pos_tab_offset (w, start, start_byte),
  3030.                  w);
  3031.       /* Admit failure if point is off frame now */
  3032.       if (val.vpos >= height)
  3033.     {
  3034.       for (vpos = 0; vpos < height; vpos++)
  3035.         cancel_line (vpos + top, f);
  3036.       return 0;
  3037.     }
  3038.       cursor_vpos = val.vpos + top;
  3039.       cursor_hpos = WINDOW_LEFT_MARGIN (w) + minmax (0, val.hpos, width);
  3040.     }
  3041.  
  3042.   FRAME_CURSOR_X (f) = cursor_hpos;
  3043.   FRAME_CURSOR_Y (f) = cursor_vpos;
  3044.  
  3045.   if (debug_end_pos)
  3046.     {
  3047.       val = *compute_motion (start, 0, lmargin, 0, ZV,
  3048.                  height, - (1 << (BITS_PER_SHORT - 1)),
  3049.                  width, hscroll,
  3050.                  pos_tab_offset (w, start, start_byte),
  3051.                  w);
  3052.       if (val.vpos != XFASTINT (w->window_end_vpos))
  3053.     abort ();
  3054.       if (XFASTINT (w->window_end_pos)
  3055.       != Z - val.bufpos)
  3056.     abort ();
  3057.     }
  3058.  
  3059.   return 1;
  3060. }
  3061.  
  3062. /* Copy LEN glyphs starting address FROM to the rope TO.
  3063.    But don't actually copy the parts that would come in before S.
  3064.    Value is TO, advanced past the copied data.
  3065.    F is the frame we are displaying in.  */
  3066.  
  3067. static GLYPH *
  3068. copy_part_of_rope (f, to, s, from, len, face)
  3069.      FRAME_PTR f;
  3070.      register GLYPH *to; /* Copy to here. */
  3071.      register GLYPH *s; /* Starting point. */
  3072.      Lisp_Object *from;  /* Data to copy. */
  3073.      int len;
  3074.      int face;        /* Face to apply to glyphs which don't specify one. */
  3075. {
  3076.   int n = len;
  3077.   register Lisp_Object *fp = from;
  3078.   /* These cache the results of the last call to compute_glyph_face.  */
  3079.   int last_code = -1;
  3080.   int last_merged = 0;
  3081.  
  3082. #ifdef HAVE_FACES
  3083.   if (! FRAME_TERMCAP_P (f))
  3084.     while (n--)
  3085.       {
  3086.     GLYPH glyph = (INTEGERP (*fp) ? XFASTINT (*fp) : 0);
  3087.     int facecode;
  3088.     unsigned int c = FAST_GLYPH_CHAR (glyph);
  3089.  
  3090.     if (c > MAX_CHAR)
  3091.       /* For an invalid character code, use space.  */
  3092.       c = ' ';
  3093.  
  3094.     if (FAST_GLYPH_FACE (glyph) == 0)
  3095.       /* If GLYPH has no face code, use FACE.  */
  3096.       facecode = face;
  3097.     else if (FAST_GLYPH_FACE (glyph) == last_code)
  3098.       /* If it's same as previous glyph, use same result.  */
  3099.       facecode = last_merged;
  3100.     else
  3101.       {
  3102.         /* Merge this glyph's face and remember the result.  */
  3103.         last_code = FAST_GLYPH_FACE (glyph);
  3104.         last_merged = facecode = compute_glyph_face (f, last_code, face);
  3105.       }
  3106.  
  3107.     if (to >= s)
  3108.       *to = FAST_MAKE_GLYPH (c, facecode);
  3109.     ++to;
  3110.     ++fp;
  3111.       }
  3112.   else
  3113. #endif
  3114.     while (n--)
  3115.       {
  3116.     if (to >= s) *to = (INTEGERP (*fp) ? XFASTINT (*fp) : 0);
  3117.     ++to;
  3118.     ++fp;
  3119.       }
  3120.   return to;
  3121. }
  3122.  
  3123. /* Correct a glyph by replacing its specified user-level face code
  3124.    with a displayable computed face code.  */
  3125.  
  3126. static GLYPH
  3127. fix_glyph (f, glyph, cface)
  3128.      FRAME_PTR f;
  3129.      GLYPH glyph;
  3130.      int cface;
  3131. {
  3132. #ifdef HAVE_FACES
  3133.   if (! FRAME_TERMCAP_P (f))
  3134.     {
  3135.       if (FAST_GLYPH_FACE (glyph) != 0)
  3136.     cface = compute_glyph_face (f, FAST_GLYPH_FACE (glyph), cface);
  3137.       glyph = FAST_MAKE_GLYPH (FAST_GLYPH_CHAR (glyph), cface);
  3138.     }
  3139. #endif
  3140.   return glyph;
  3141. }
  3142.  
  3143. /* Return the column of position POS / POS_BYTE in window W's buffer.
  3144.    When used on the character at the beginning of a line,
  3145.    starting at column 0, this says how much to subtract from
  3146.    the column position of any character in the line
  3147.    to get its horizontal position on the screen.  */
  3148.  
  3149. static int
  3150. pos_tab_offset (w, pos, pos_byte)
  3151.      struct window *w;
  3152.      register int pos, pos_byte;
  3153. {
  3154.   int opoint = PT;
  3155.   int opoint_byte = PT_BYTE;
  3156.   int col;
  3157.   int width = window_internal_width (w) - 1;
  3158.  
  3159.   if (pos == BEGV)
  3160.     return MINI_WINDOW_P (w) ? -minibuf_prompt_width : 0;
  3161.  
  3162.   if (FETCH_BYTE (pos_byte - 1) == '\n')
  3163.     return 0;
  3164.  
  3165.   TEMP_SET_PT_BOTH (pos, pos_byte);
  3166.   col = current_column ();
  3167.   TEMP_SET_PT_BOTH (opoint, opoint_byte);
  3168.  
  3169.   return col;
  3170. }
  3171.  
  3172. /* Display one line of window W, starting at char position START in W's buffer.
  3173.    START_BYTE is the corresponding byte position.
  3174.  
  3175.    Display starting at horizontal position HPOS, expressed relative to
  3176.    W's left edge.  In situations where the text at START shouldn't
  3177.    start at the left margin (i.e. when the window is hscrolled, or
  3178.    we're continuing a line which left off in the midst of a
  3179.    multi-column character), HPOS should be negative; we throw away
  3180.    characters up 'til hpos = 0.  So, HPOS must take hscrolling into
  3181.    account.
  3182.  
  3183.    TABOFFSET is an offset for ostensible hpos, used in tab stop calculations.
  3184.  
  3185.    OVSTR_DONE is the number of chars of overlay before/after strings
  3186.    at this position which have already been processed.
  3187.  
  3188.    Display on position VPOS on the frame.  It is origin 0, relative to
  3189.    the top of the frame, not W.
  3190.  
  3191.    Returns a STRUCT POSITION giving character to start next line with
  3192.    and where to display it, including a zero or negative hpos.
  3193.    The vpos field is not really a vpos; it is 1 unless the line is continued */
  3194.  
  3195. struct position val_display_text_line;
  3196.  
  3197. static struct position *
  3198. display_text_line (w, start, start_byte, vpos, hpos, taboffset, ovstr_done)
  3199.      struct window *w;
  3200.      int start;
  3201.      int vpos;
  3202.      int hpos;
  3203.      int taboffset;
  3204.      int ovstr_done;
  3205. {
  3206.   register int pos = start;
  3207.   int pos_byte = start_byte;
  3208.   register int c;
  3209.   register GLYPH *p1;
  3210.   int pause, limit_byte;
  3211.   register unsigned char *p;
  3212.   GLYPH *endp;
  3213.   register GLYPH *leftmargin;
  3214.   register GLYPH *p1prev;
  3215.   register GLYPH *p1start;
  3216.   GLYPH *p1_wide_column_end = (GLYPH *) 0;
  3217.   int prevpos, prevpos_byte;
  3218.   int *charstart;
  3219.   FRAME_PTR f = XFRAME (w->frame);
  3220.   int tab_width = XINT (current_buffer->tab_width);
  3221.   int ctl_arrow = !NILP (current_buffer->ctl_arrow);
  3222.   int width = window_internal_width (w) - 1;
  3223.   struct position val;
  3224.   int lastpos, lastpos_byte;
  3225.   int invis;
  3226.   int last_invis_skip = 0;
  3227.   Lisp_Object last_invis_prop;
  3228.   int hscroll = XINT (w->hscroll);
  3229.   int truncate = (hscroll
  3230.           || (truncate_partial_width_windows
  3231.               && !WINDOW_FULL_WIDTH_P (w))
  3232.           || !NILP (current_buffer->truncate_lines));
  3233.  
  3234.   /* 1 if this buffer has a region to highlight.  */
  3235.   int highlight_region
  3236.     = (!NILP (Vtransient_mark_mode) && !NILP (current_buffer->mark_active)
  3237.        && XMARKER (current_buffer->mark)->buffer != 0);
  3238.   int region_beg, region_end;
  3239.  
  3240.   int selective = (INTEGERP (current_buffer->selective_display)
  3241.            ? XINT (current_buffer->selective_display)
  3242.            : !NILP (current_buffer->selective_display) ? -1 : 0);
  3243.   register struct frame_glyphs *desired_glyphs = FRAME_DESIRED_GLYPHS (f);
  3244.   register struct Lisp_Char_Table *dp = window_display_table (w);
  3245.  
  3246.   Lisp_Object default_invis_vector[3];
  3247.   /* Number of characters of ellipsis to display after an invisible line
  3248.      if it calls for an ellipsis.
  3249.      Note that this value can be nonzero regardless of whether
  3250.      selective display is enabled--you must check that separately.  */
  3251.   int selective_rlen
  3252.     = (dp && VECTORP (DISP_INVIS_VECTOR (dp))
  3253.        ? XVECTOR (DISP_INVIS_VECTOR (dp))->size
  3254.        : !NILP (current_buffer->selective_display_ellipses) ? 3 : 0);
  3255.   /* This is the sequence of Lisp objects to display
  3256.      when there are invisible lines.  */
  3257.   Lisp_Object *invis_vector_contents
  3258.     = (dp && VECTORP (DISP_INVIS_VECTOR (dp))
  3259.        ? XVECTOR (DISP_INVIS_VECTOR (dp))->contents
  3260.        : default_invis_vector);
  3261.  
  3262.   GLYPH truncator = (dp == 0 || !INTEGERP (DISP_TRUNC_GLYPH (dp))
  3263.              || !GLYPH_CHAR_VALID_P (XINT (DISP_TRUNC_GLYPH (dp)))
  3264.              ? '$' : XINT (DISP_TRUNC_GLYPH (dp)));
  3265.   GLYPH continuer = (dp == 0 || !INTEGERP (DISP_CONTINUE_GLYPH (dp))
  3266.              || !GLYPH_CHAR_VALID_P (XINT (DISP_CONTINUE_GLYPH (dp)))
  3267.              ? '\\' : XINT (DISP_CONTINUE_GLYPH (dp)));
  3268.  
  3269.   /* If 1, we must handle multibyte characters.  */
  3270.   int multibyte = !NILP (current_buffer->enable_multibyte_characters);
  3271.   /* Length of multibyte form of each character.  */
  3272.   int len;
  3273.   /* Glyphs generated should be set this bit mask if text must be
  3274.      displayed from right to left.  */
  3275.   GLYPH rev_dir_bit = (NILP (current_buffer->direction_reversed)
  3276.                ? 0 : GLYPH_MASK_REV_DIR);
  3277.  
  3278.   /* The next buffer location at which the face should change, due
  3279.      to overlays or text property changes.  */
  3280.   int next_face_change;
  3281.  
  3282.   /* The next location where the `invisible' property changes, or an
  3283.      overlay starts or ends.  */
  3284.   int next_boundary;
  3285.  
  3286.   /* The face we're currently using.  */
  3287.   int current_face = 0;
  3288.   int i;
  3289.  
  3290.   XSETFASTINT (default_invis_vector[2], '.');
  3291.   default_invis_vector[0] = default_invis_vector[1] = default_invis_vector[2];
  3292.  
  3293.   get_display_line (f, vpos, WINDOW_LEFT_MARGIN (w));
  3294.   if (tab_width <= 0 || tab_width > 1000) tab_width = 8;
  3295.  
  3296.   /* Show where to highlight the region.  */
  3297.   if (highlight_region
  3298.       /* Maybe highlight only in selected window.  */
  3299.       && (highlight_nonselected_windows
  3300.       || w == XWINDOW (selected_window)
  3301.       || (MINI_WINDOW_P (XWINDOW (selected_window))
  3302.           && w == XWINDOW (Vminibuf_scroll_window))))
  3303.     {
  3304.       region_beg = marker_position (current_buffer->mark);
  3305.       if (PT < region_beg)
  3306.     {
  3307.       region_end = region_beg;
  3308.       region_beg = PT;
  3309.     }
  3310.       else
  3311.     region_end = PT;
  3312.       w->region_showing = Qt;
  3313.     }
  3314.   else
  3315.     {
  3316.       region_beg = region_end = -1;
  3317.       w->region_showing = Qnil;
  3318.     }
  3319.  
  3320.   if (MINI_WINDOW_P (w)
  3321.       && start == BEG
  3322.       && vpos == XFASTINT (w->top))
  3323.     {
  3324.       if (! NILP (minibuf_prompt))
  3325.     {
  3326.       int old_width = minibuf_prompt_width;
  3327.  
  3328.       minibuf_prompt_width
  3329.         = (display_string (w, vpos, XSTRING (minibuf_prompt)->data,
  3330.                    STRING_BYTES (XSTRING (minibuf_prompt)),
  3331.                    hpos + WINDOW_LEFT_MARGIN (w),
  3332.                    /* Display a space if we truncate.  */
  3333.                    ' ',
  3334.                    1, -1,
  3335.                    /* Truncate the prompt a little before the
  3336.                   margin, so user input can at least start
  3337.                   on the first line.  */
  3338.                    (XFASTINT (w->width) > 10
  3339.                 ? XFASTINT (w->width) - 4 : -1),
  3340.                    STRING_MULTIBYTE (minibuf_prompt))
  3341.            - hpos - WINDOW_LEFT_MARGIN (w));
  3342.       hpos += minibuf_prompt_width;
  3343.       taboffset -= minibuf_prompt_width - old_width;
  3344.     }
  3345.       else
  3346.     minibuf_prompt_width = 0;
  3347.     }
  3348.  
  3349.   /* If we're hscrolled at all, use compute_motion to skip over any
  3350.      text off the left edge of the window.  compute_motion may know
  3351.      tricks to do this faster than we can.  */
  3352.   if (hpos < 0)
  3353.     {
  3354.       struct position *left_edge
  3355.         = compute_motion (pos, vpos, hpos, 0,
  3356.                           ZV, vpos, 0,
  3357.                           width, hscroll, taboffset, w);
  3358.  
  3359.       /* Retrieve the buffer position and column provided by
  3360.          compute_motion.  We can't assume that the column will be
  3361.          zero, because you may have multi-column characters crossing
  3362.          the left margin.  
  3363.  
  3364.          compute_motion may have moved us past the screen position we
  3365.          requested, if we hit a multi-column character, or the end of
  3366.          the line.  If so, back up.  */
  3367.       if (left_edge->vpos > vpos
  3368.           || left_edge->hpos > 0)
  3369.         {
  3370.           pos = left_edge->bufpos;
  3371.       pos_byte = left_edge->bytepos;
  3372.       DEC_BOTH (pos, pos_byte);
  3373.           hpos = left_edge->prevhpos;
  3374.         }
  3375.       else
  3376.         {
  3377.           pos = left_edge->bufpos;
  3378.       pos_byte = left_edge->bytepos;
  3379.           hpos = left_edge->hpos;
  3380.         }
  3381.     }
  3382.  
  3383.   hpos += WINDOW_LEFT_MARGIN (w);
  3384.  
  3385.   desired_glyphs->bufp[vpos] = start;
  3386.   p1 = desired_glyphs->glyphs[vpos] + hpos;
  3387.   p1start = p1;
  3388.   charstart = desired_glyphs->charstarts[vpos] + hpos;
  3389.   /* In case we don't ever write anything into it...  */
  3390.   desired_glyphs->charstarts[vpos][WINDOW_LEFT_MARGIN (w)] = -1;
  3391.   leftmargin = desired_glyphs->glyphs[vpos] + WINDOW_LEFT_MARGIN (w);
  3392.   endp = leftmargin + width;
  3393.  
  3394.   /* Arrange the overlays nicely for our purposes.  Usually, we call
  3395.      display_text_line on only one line at a time, in which case this
  3396.      can't really hurt too much, or we call it on lines which appear
  3397.      one after another in the buffer, in which case all calls to
  3398.      recenter_overlay_lists but the first will be pretty cheap.  */
  3399.   recenter_overlay_lists (current_buffer, pos);
  3400.  
  3401.   /* Loop generating characters.
  3402.      Stop at end of buffer, before newline,
  3403.      if reach or pass continuation column,
  3404.      or at face change.  */
  3405.   pause = pos;
  3406.   limit_byte = pos_byte;
  3407.   next_face_change = pos;
  3408.   next_boundary = pos;
  3409.   p1prev = p1;
  3410.   prevpos = pos;
  3411.   prevpos_byte = pos_byte;
  3412.  
  3413.   /* If the window is hscrolled and point is in the invisible part of the
  3414.      current line beyond the left margin we can record the cursor location
  3415.      right away.  */
  3416.   if (hscroll && start <= PT && PT < pos && cursor_vpos < 0)
  3417.     {
  3418.       cursor_vpos = vpos;
  3419.       cursor_hpos = p1 - leftmargin;
  3420.     }
  3421.  
  3422.   while (p1 < endp)
  3423.     {
  3424.       if (pos >= pause)
  3425.     {
  3426.       int e_t_h;
  3427.  
  3428.       while (pos == next_boundary)
  3429.         {
  3430.           Lisp_Object position, limit, prop, ww;
  3431.  
  3432.           /* Display the overlay strings here, unless we're at ZV
  3433.          and have already displayed the appropriate strings
  3434.          on an earlier line.  */
  3435.           if (pos < ZV || !zv_strings_seen++)
  3436.         {
  3437.           int ovlen;
  3438.           unsigned char *ovstr;
  3439.           ovlen = overlay_strings (pos, w, &ovstr);
  3440.  
  3441.           if (ovlen > 0)
  3442.             {
  3443.               /* Skip the ones we did in a previous line.  */
  3444.               ovstr += ovstr_done;
  3445.               ovlen -= ovstr_done;
  3446.  
  3447.               while (ovlen > 0)
  3448.             {
  3449.               int charset, cols;
  3450.               GLYPH g;
  3451.  
  3452.               if (multibyte)
  3453.                 {
  3454.                   c = STRING_CHAR_AND_LENGTH (ovstr, ovlen, len);
  3455.                   ovstr += len, ovlen -= len, ovstr_done += len;
  3456.                   charset = CHAR_CHARSET (c);
  3457.                   cols = (charset == CHARSET_COMPOSITION
  3458.                       ? cmpchar_table[COMPOSITE_CHAR_ID (c)]->width
  3459.                       : CHARSET_WIDTH (charset));
  3460.                 }
  3461.               else
  3462.                 {
  3463.                   c = *ovstr++, ovlen--, ovstr_done++;
  3464.                   cols = 1;
  3465.                 }
  3466.               g = MAKE_GLYPH (f, c, current_face) | rev_dir_bit;
  3467.               while (cols-- > 0)
  3468.                 {
  3469.                   if (p1 >= leftmargin && p1 < endp)
  3470.                 *p1 = g, g |= GLYPH_MASK_PADDING;
  3471.                   p1++;
  3472.                 }
  3473.             }
  3474.               /* If we did all the overlay strings
  3475.              and we have room for text, clear ovstr_done
  3476.              just for neatness' sake.  */
  3477.               if (ovlen == 0 && p1 < endp)
  3478.             ovstr_done = 0;
  3479.             }
  3480.         }
  3481.  
  3482.           /* Did we reach point?  Record the cursor location.  */
  3483.           if (pos == PT && cursor_vpos < 0)
  3484.         {
  3485.           cursor_vpos = vpos;
  3486.           cursor_hpos = p1 - leftmargin;
  3487.         }
  3488.  
  3489.           if (pos >= ZV)
  3490.         break;
  3491.  
  3492.           XSETFASTINT (position, pos);
  3493.           limit = Fnext_overlay_change (position);
  3494. #ifdef USE_TEXT_PROPERTIES
  3495.           /* This is just an estimate to give reasonable
  3496.          performance; nothing should go wrong if it is too small.  */
  3497.           if (XFASTINT (limit) > pos + 50)
  3498.         {
  3499.           int limitpos = pos + 50;
  3500.           XSETFASTINT (limit, limitpos);
  3501.         }
  3502.           limit = Fnext_single_property_change (position, Qinvisible,
  3503.                             Fcurrent_buffer (), limit);
  3504. #endif
  3505.           next_boundary = XFASTINT (limit);
  3506.           /* if the `invisible' property is set, we can skip to
  3507.          the next property change.  */
  3508.           XSETWINDOW (ww, w);
  3509.           prop = Fget_char_property (position, Qinvisible, ww);
  3510.           if (TEXT_PROP_MEANS_INVISIBLE (prop))
  3511.         {
  3512.           if (pos < PT && next_boundary >= PT)
  3513.             {
  3514.               cursor_vpos = vpos;
  3515.               cursor_hpos = p1 - leftmargin;
  3516.             }
  3517.           pos = next_boundary;
  3518.           pos_byte = CHAR_TO_BYTE (pos);
  3519.           last_invis_skip = pos;
  3520.           last_invis_prop = prop;
  3521.         }
  3522.         }
  3523.  
  3524.       /* Did we reach point?  Record the cursor location.  */
  3525.       if (pos == PT && cursor_vpos < 0)
  3526.         {
  3527.           cursor_vpos = vpos;
  3528.           cursor_hpos = p1 - leftmargin;
  3529.         }
  3530.  
  3531.       /* Did we hit the end of the visible region of the buffer?
  3532.          Stop here.  */
  3533.       if (pos >= ZV)
  3534.         {
  3535.           /* Update charstarts for the end of this line.  */
  3536.           /* Do nothing if off the left edge or at the right edge.  */
  3537.           if (p1 >= leftmargin && p1 + 1 != endp)
  3538.         {
  3539.           int *p2x = &charstart[(p1 < leftmargin
  3540.                      ? leftmargin : p1)
  3541.                     - p1start];
  3542.           *p2x++ = pos;
  3543.         }
  3544.           break;
  3545.         }
  3546.  
  3547.       /* Figure out where (if at all) the
  3548.          redisplay_end_trigger-hook should run.  */
  3549.       if (MARKERP (w->redisplay_end_trigger)
  3550.           && XMARKER (w->redisplay_end_trigger)->buffer != 0)
  3551.         e_t_h = marker_position (w->redisplay_end_trigger);
  3552.       else if (INTEGERP (w->redisplay_end_trigger))
  3553.         e_t_h = XINT (w->redisplay_end_trigger);
  3554.       else
  3555.         e_t_h = ZV;
  3556.  
  3557.       /* If we've gone past the place to run a hook,
  3558.          run the hook.  */
  3559.       if (pos >= e_t_h && e_t_h != ZV)
  3560.         {
  3561.           Lisp_Object args[3];
  3562.  
  3563.           args[0] = Qredisplay_end_trigger_functions;
  3564.           XSETWINDOW (args[1], w);
  3565.           XSETINT (args[2], e_t_h);
  3566.  
  3567.           /* Since we are *trying* to run these functions,
  3568.          don't try to run them again, even if they get an error.  */
  3569.           w->redisplay_end_trigger = Qnil;
  3570.           Frun_hook_with_args (3, args);
  3571.  
  3572.           e_t_h = ZV;
  3573.           /* Notice if it changed the face of this character.  */
  3574.           next_face_change = pos;
  3575.         }
  3576.  
  3577. #ifdef HAVE_FACES
  3578.       /* Did we hit a face change?  Figure out what face we should
  3579.          use now.  We also hit this the first time through the
  3580.          loop, to see what face we should start with.  */
  3581.       if (pos >= next_face_change
  3582.           && (FRAME_WINDOW_P (f) || FRAME_MSDOS_P (f)))
  3583.         {
  3584.           int limit = pos + 50;
  3585.  
  3586.           current_face = compute_char_face (f, w, pos,
  3587.                         region_beg, region_end,
  3588.                         &next_face_change, limit, 0);
  3589.         }
  3590. #endif
  3591.  
  3592.       /* Compute the next place we need to stop
  3593.          and do something special; set PAUSE.  */
  3594.  
  3595.       pause = ZV;
  3596.  
  3597.       if (pos < next_boundary && next_boundary < pause)
  3598.         pause = next_boundary;
  3599.       if (pos < next_face_change && next_face_change < pause)
  3600.         pause = next_face_change;
  3601.  
  3602.       if (e_t_h < pause)
  3603.         pause = e_t_h;
  3604.  
  3605.       /* Wouldn't you hate to read the next line to someone over
  3606.              the phone?  */
  3607.       if (pos < PT && PT < pause)
  3608.         pause = PT;
  3609.       if (pos < GPT && GPT < pause)
  3610.         pause = GPT;
  3611.  
  3612.       /* LIMIT_BYTE is not the same place in the buffer as PAUSE.
  3613.          It is a limit on valid characters.
  3614.          We use it to bound STRING_CHAR_AND_LENGTH.  */
  3615.       limit_byte = ZV_BYTE;
  3616.       if (pos < GPT && GPT_BYTE < limit_byte)
  3617.         limit_byte = GPT_BYTE;
  3618.  
  3619.       {
  3620.         int temp = CHAR_TO_BYTE (pos);
  3621.         p = BYTE_POS_ADDR (temp);
  3622.       }
  3623.     }
  3624.  
  3625.       if (p1 >= endp)
  3626.     break;
  3627.  
  3628.       p1prev = p1;
  3629.       p1_wide_column_end = (GLYPH *) 0;
  3630.  
  3631.       if (multibyte)
  3632.     c = STRING_CHAR_AND_LENGTH (p, limit_byte - pos_byte, len), p += len;
  3633.       else
  3634.     c = *p++, len = 1;
  3635.       /* Let a display table override all standard display methods.  */
  3636.       if (dp != 0 && VECTORP (DISP_CHAR_VECTOR (dp, c)))
  3637.     {
  3638.       p1 = copy_part_of_rope (f, p1, leftmargin,
  3639.                   XVECTOR (DISP_CHAR_VECTOR (dp, c))->contents,
  3640.                   XVECTOR (DISP_CHAR_VECTOR (dp, c))->size,
  3641.                   current_face, rev_dir_bit);
  3642.     }
  3643.       else if (c >= 040 && c < 0177)
  3644.     {
  3645.       if (p1 >= leftmargin)
  3646.         *p1 = MAKE_GLYPH (f, c, current_face) | rev_dir_bit;
  3647.       p1++;
  3648.     }
  3649.       else if (c == '\n')
  3650.     {
  3651. #if 0
  3652.       /* Same as p1prev, but after the invis_vector_contents text
  3653.          (if we have that on this line).  */
  3654.       GLYPH *p1prev_modified;
  3655. #endif
  3656.  
  3657.       invis = 0;
  3658.       if (last_invis_skip == pos
  3659.           && TEXT_PROP_MEANS_INVISIBLE_WITH_ELLIPSIS (last_invis_prop))
  3660.         invis = 1;
  3661.       while (pos + 1 < ZV
  3662.          && selective > 0
  3663.          && indented_beyond_p (pos + 1, pos_byte + 1, selective))
  3664.         {
  3665.           int opoint = PT, opoint_byte = PT_BYTE;
  3666.  
  3667.           invis = 1;
  3668.           INC_BOTH (pos, pos_byte);
  3669.           scan_newline (pos, pos_byte, ZV, ZV_BYTE, 1, 1);
  3670.           pos = PT, pos_byte = PT_BYTE;
  3671.           if (FETCH_BYTE (pos_byte - 1) == '\n')
  3672.         {
  3673.           pos--;
  3674.           pos_byte--;
  3675.         }
  3676.           SET_PT_BOTH (opoint, opoint_byte);
  3677.         }
  3678.       if (invis && selective_rlen > 0 && p1 >= leftmargin)
  3679.         {
  3680. #if 0
  3681.           GLYPH *cs, *csend;
  3682.  
  3683.           cs = charstart + (p1 - p1start);
  3684. #endif
  3685.  
  3686.           p1 += selective_rlen;
  3687.           if (p1 - leftmargin > width)
  3688.         p1 = endp;
  3689.  
  3690. #if 0 /* This needs more work; charstarts needs to record
  3691.      both whether a position ho;ds an ellipsis character
  3692.      and what buffer position it corresponds to.  */
  3693.           csend = charstart + (p1 - p1start);
  3694.           while (cs != csend)
  3695.         *cs++ = -2;
  3696.           /* The idea is to use p1prev_modified instead of p1prev
  3697.          in the loop below over p2x.  */
  3698.           p1prev_modified = p1;
  3699. #endif
  3700.  
  3701.           copy_part_of_rope (f, p1prev, p1prev, invis_vector_contents,
  3702.                  (p1 - p1prev), current_face, rev_dir_bit);
  3703.         }
  3704.  
  3705.       /* Update charstarts for the newline that ended this line.  */
  3706.       /* Do nothing here for a char that's entirely off the left edge
  3707.          or if it starts at the right edge.  */
  3708.       if (p1 >= leftmargin && p1prev != endp)
  3709.         {
  3710.           /* Store the newline's position into charstarts
  3711.          for the column where the newline starts.
  3712.          Store -1 for the rest of the glyphs it occupies.  */
  3713.           int *p2x = &charstart[(p1prev < leftmargin
  3714.                      ? leftmargin : p1prev)
  3715.                     - p1start];
  3716.           int *p2 = &charstart[(p1 < endp ? p1 : endp) - p1start];
  3717.  
  3718.           *p2x++ = pos;
  3719.           while (p2x < p2)
  3720.         *p2x++ = -1;
  3721.         }
  3722. #ifdef HAVE_FACES
  3723.       /* Draw the face of the newline character as extending all the 
  3724.          way to the end of the frame line.  */
  3725.       if (current_face)
  3726.         {
  3727.           if (p1 < leftmargin)
  3728.         p1 = leftmargin;
  3729.           while (p1 < endp)
  3730.         *p1++ = FAST_MAKE_GLYPH (' ', current_face) | rev_dir_bit;
  3731.         }
  3732. #endif
  3733.  
  3734.       break;
  3735.     }
  3736.       else if (c == '\t')
  3737.     {
  3738.       do
  3739.         {
  3740.           if (p1 >= leftmargin && p1 < endp)
  3741.         *p1 = MAKE_GLYPH (f, ' ', current_face) | rev_dir_bit;
  3742.           p1++;
  3743.         }
  3744.       while ((p1 - leftmargin + taboffset + hscroll - (hscroll > 0))
  3745.          % tab_width);
  3746.     }
  3747.       else if (c == Ctl ('M') && selective == -1)
  3748.     {
  3749.       int opoint = PT, opoint_byte = PT_BYTE;
  3750.       scan_newline (pos, pos_byte, ZV, ZV_BYTE, 1, 1);
  3751.       pos = PT, pos_byte = PT_BYTE;
  3752.       SET_PT_BOTH (opoint, opoint_byte);
  3753.  
  3754.       if (FETCH_BYTE (pos_byte - 1) == '\n')
  3755.         pos--, pos_byte--;
  3756.       if (selective_rlen > 0)
  3757.         {
  3758.           p1 += selective_rlen;
  3759.           if (p1 - leftmargin > width)
  3760.         p1 = endp;
  3761.           copy_part_of_rope (f, p1prev, p1prev, invis_vector_contents,
  3762.                  (p1 - p1prev), current_face, rev_dir_bit);
  3763.         }
  3764. #ifdef HAVE_FACES
  3765.       /* Draw the face of the newline character as extending all the 
  3766.          way to the end of the frame line.  */
  3767.       if (current_face)
  3768.         {
  3769.           if (p1 < leftmargin)
  3770.         p1 = leftmargin;
  3771.           while (p1 < endp)
  3772.         *p1++ = FAST_MAKE_GLYPH (' ', current_face) | rev_dir_bit;
  3773.         }
  3774. #endif
  3775.  
  3776.       /* Update charstarts for the ^M that ended this line.  */
  3777.       /* Do nothing here for a char that's entirely off the left edge
  3778.          or if it starts at the right edge.  */
  3779.       if (p1 >= leftmargin && p1prev != endp)
  3780.         {
  3781.           /* Store the newline's position into charstarts
  3782.          for the column where the newline starts.
  3783.          Store -1 for the rest of the glyphs it occupies.  */
  3784.           int *p2x = &charstart[(p1prev < leftmargin
  3785.                      ? leftmargin : p1prev)
  3786.                     - p1start];
  3787.           int *p2 = &charstart[(p1 < endp ? p1 : endp) - p1start];
  3788.  
  3789.           *p2x++ = pos;
  3790.           while (p2x < p2)
  3791.         *p2x++ = -1;
  3792.         }
  3793.       break;
  3794.     }
  3795.       else if (c < 0200 && ctl_arrow)
  3796.     {
  3797.       if (p1 >= leftmargin)
  3798.         *p1 = (fix_glyph
  3799.            (f, (dp && INTEGERP (DISP_CTRL_GLYPH (dp))
  3800.             && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (dp)))
  3801.             ? XINT (DISP_CTRL_GLYPH (dp)) : '^'),
  3802.             current_face)
  3803.            | rev_dir_bit);
  3804.       p1++;
  3805.       if (p1 >= leftmargin && p1 < endp)
  3806.         *p1 = MAKE_GLYPH (f, c ^ 0100, current_face) | rev_dir_bit;
  3807.       p1++;
  3808.     }
  3809.       else
  3810.     {
  3811.       /* C is a multibyte character or a character to be displayed
  3812.              by octal form.  */
  3813.       int remaining_bytes = len;
  3814.  
  3815.       if (c >= 0400)
  3816.         {
  3817.           /* C is a multibyte character.  */
  3818.           int charset = CHAR_CHARSET (c);
  3819.           int columns = (charset == CHARSET_COMPOSITION
  3820.                  ? cmpchar_table[COMPOSITE_CHAR_ID (c)]->width
  3821.                  : CHARSET_WIDTH (charset));
  3822.           GLYPH g = MAKE_GLYPH (f, c, current_face) | rev_dir_bit;
  3823.  
  3824.           while (columns--)
  3825.         {
  3826.           if (p1 >= leftmargin && p1 < endp)
  3827.             *p1 = g, g |= GLYPH_MASK_PADDING;
  3828.           p1++;
  3829.         }
  3830.           p1_wide_column_end = p1;
  3831.           remaining_bytes -= CHARSET_BYTES (charset);
  3832.         }
  3833.  
  3834.       while (remaining_bytes > 0)
  3835.         {
  3836.           c = *(p - remaining_bytes--);
  3837.  
  3838.           if (p1 >= leftmargin && p1 < endp)
  3839.         *p1 = (fix_glyph
  3840.                (f,
  3841.             (dp && INTEGERP (DISP_ESCAPE_GLYPH (dp))
  3842.              && GLYPH_CHAR_VALID_P (XINT (DISP_ESCAPE_GLYPH (dp)))
  3843.              ? XINT (DISP_ESCAPE_GLYPH (dp)) : '\\'),
  3844.             current_face)
  3845.                | rev_dir_bit);
  3846.           p1++;
  3847.           if (p1 >= leftmargin && p1 < endp)
  3848.         *p1 = (MAKE_GLYPH (f, (c >> 6) + '0', current_face)
  3849.                | rev_dir_bit);
  3850.           p1++;
  3851.           if (p1 >= leftmargin && p1 < endp)
  3852.         *p1 = (MAKE_GLYPH (f, (7 & (c >> 3)) + '0', current_face)
  3853.                | rev_dir_bit);
  3854.           p1++;
  3855.           if (p1 >= leftmargin && p1 < endp)
  3856.         *p1 = (MAKE_GLYPH (f, (7 & c) + '0', current_face)
  3857.                | rev_dir_bit);
  3858.           p1++;
  3859.         }
  3860.     }
  3861.  
  3862.       prevpos = pos;
  3863.       prevpos_byte = pos_byte;
  3864.       pos++;
  3865.       pos_byte += len;
  3866.  
  3867.       /* Update charstarts for the character just output.  */
  3868.  
  3869.       /* Do nothing here for a char that's entirely off the left edge.  */
  3870.       if (p1 >= leftmargin)
  3871.     {
  3872.       /* Store the char's position into charstarts
  3873.          for the first glyph occupied by this char.
  3874.          Store -1 for the rest of the glyphs it occupies.  */
  3875.       if (p1 != p1prev)
  3876.         {
  3877.           int *p2x = &charstart[(p1prev < leftmargin
  3878.                      ? leftmargin : p1prev)
  3879.                     - p1start];
  3880.           int *p2 = &charstart[(p1 < endp ? p1 : endp) - p1start];
  3881.  
  3882.           if (p2x < p2)
  3883.         *p2x++ = prevpos;
  3884.           while (p2x < p2)
  3885.         *p2x++ = -1;
  3886.         }
  3887.     }
  3888.     }
  3889.  
  3890.   val.hpos = - XINT (w->hscroll);
  3891.   if (val.hpos)
  3892.     val.hpos++;
  3893.  
  3894.   val.vpos = 1;
  3895.  
  3896.   lastpos = pos;
  3897.   lastpos_byte = pos_byte;
  3898.  
  3899.   /* Store 0 in this charstart line for the positions where
  3900.      there is no character.  But do leave what was recorded
  3901.      for the character that ended the line.  */
  3902.   /* Add 1 in the endtest to compensate for the fact that ENDP was
  3903.      made from WIDTH, which is 1 less than the window's actual
  3904.      internal width.  */
  3905.   i = p1 - p1start + 1;
  3906.   if (p1 < leftmargin)
  3907.     i += leftmargin - p1;
  3908.   for (; i < endp - p1start + 1; i++)
  3909.     charstart[i] = 0;
  3910.  
  3911.   /* Handle continuation in middle of a character */
  3912.   /* by backing up over it */
  3913.   if (p1 > endp)
  3914.     {
  3915.       /* Don't back up if we never actually displayed any text.
  3916.      This occurs when the minibuffer prompt takes up the whole line.  */
  3917.       if (p1prev)
  3918.     {
  3919.       /* Start the next line with that same character whose
  3920.              character code is C and the length of multi-byte form is
  3921.              LEN.  */
  3922.       pos = prevpos;
  3923.       pos_byte = prevpos_byte;
  3924.  
  3925.       if (p1_wide_column_end < endp)
  3926.         /* As ENDP is not in the middle of wide-column character,
  3927.            we can break the line at ENDP and start from the middle
  3928.            column in the next line.  So, adjust VAL.HPOS to skip
  3929.            the columns output on this line.  */
  3930.         val.hpos += p1prev - endp;
  3931.       else
  3932.         {
  3933.           /* We displayed a wide-column character at around ENDP.
  3934.          Since we can't broke it in the middle, the whole
  3935.          character should be driven into the next line.  */
  3936.           /* As the result, the actual columns occupied by the
  3937.          text on this line is less than WIDTH.  VAL.TAB_OFFSET
  3938.          must be adjusted.  */
  3939.           taboffset = taboffset + (p1prev - endp);
  3940.           /* Let's fill unused columns with TRUNCATOR or CONTINUER.  */
  3941.           {
  3942.         GLYPH g = fix_glyph (f, truncate ? truncator : continuer, 0);
  3943.         while (p1prev < endp)
  3944.           *p1prev++ = g;
  3945.           }
  3946.           /* If POINT is at POS, cursor should not on this line.  */
  3947.           lastpos = pos;
  3948.           lastpos_byte = pos_byte;
  3949.           if (PT == pos)
  3950.         cursor_vpos = -1;
  3951.         }
  3952.     }
  3953.  
  3954.       /* Keep in this line everything up to the continuation column.  */
  3955.       p1 = endp;
  3956.     }
  3957.  
  3958.   /* Finish deciding which character to start the next line on,
  3959.      and what hpos to start it at.
  3960.      Also set `lastpos' to the last position which counts as "on this line"
  3961.      for cursor-positioning.  */
  3962.  
  3963.   if (pos < ZV)
  3964.     {
  3965.       if (FETCH_BYTE (pos_byte) == '\n')
  3966.     {
  3967.       int opoint = PT, opoint_byte = PT_BYTE;
  3968.  
  3969.       /* If stopped due to a newline, start next line after it */
  3970.       SET_PT_BOTH (pos + 1, pos_byte + 1);
  3971.  
  3972.       val.tab_offset = 0;
  3973.       /* Check again for hidden lines, in case the newline occurred exactly
  3974.          at the right margin.  */
  3975.       while (PT < ZV && selective > 0
  3976.          && indented_beyond_p (PT, PT_BYTE, selective))
  3977.         scan_newline (PT, PT_BYTE, ZV, ZV_BYTE, 1, 1);
  3978.  
  3979.       pos = PT, pos_byte = PT_BYTE;
  3980.       SET_PT_BOTH (opoint, opoint_byte);
  3981.     }
  3982.       else
  3983.     /* Stopped due to right margin of window */
  3984.     {
  3985.       if (truncate)
  3986.         {
  3987.           int opoint = PT, opoint_byte = PT_BYTE;
  3988.  
  3989.           SET_PT_BOTH (pos, pos_byte);
  3990.           *p1++ = fix_glyph (f, truncator, 0);
  3991.           /* Truncating => start next line after next newline,
  3992.          and point is on this line if it is before the newline,
  3993.          and skip none of first char of next line */
  3994.           do
  3995.         scan_newline (PT, PT_BYTE, ZV, ZV_BYTE, 1, 1);
  3996.           while (PT < ZV && selective > 0
  3997.              && indented_beyond_p (PT, PT_BYTE, selective));
  3998.           pos = PT, pos_byte = PT_BYTE;
  3999.           val.hpos = XINT (w->hscroll) ? 1 - XINT (w->hscroll) : 0;
  4000.           SET_PT_BOTH (opoint, opoint_byte);
  4001.  
  4002.           lastpos = pos - (FETCH_BYTE (pos_byte - 1) == '\n');
  4003.           lastpos_byte = CHAR_TO_BYTE (lastpos);
  4004.           val.tab_offset = 0;
  4005.         }
  4006.       else
  4007.         {
  4008.           *p1++ = fix_glyph (f, continuer, 0);
  4009.           val.vpos = 0;
  4010.           DEC_BOTH (lastpos, lastpos_byte);
  4011.           val.tab_offset = taboffset + width;
  4012.         }
  4013.     }
  4014.     }
  4015.   else
  4016.     val.tab_offset = 0;
  4017.  
  4018.   /* If point is at eol or in invisible text at eol,
  4019.      record its frame location now.  */
  4020.  
  4021.   if (start <= PT && PT <= lastpos && cursor_vpos < 0)
  4022.     {
  4023.       cursor_vpos = vpos;
  4024.       cursor_hpos = p1 - leftmargin;
  4025.     }
  4026.  
  4027.   if (cursor_vpos == vpos)
  4028.     {
  4029.       if (cursor_hpos < 0) cursor_hpos = 0;
  4030.       if (cursor_hpos > width) cursor_hpos = width;
  4031.       cursor_hpos += WINDOW_LEFT_MARGIN (w);
  4032.       if (w == XWINDOW (FRAME_SELECTED_WINDOW (f)))
  4033.     {
  4034.       this_line_bufpos = 0;
  4035.  
  4036.       /* If this frame's cursor will be in its echo area,
  4037.          don't record a cursor from the window text,
  4038.          and turn off the optimization for cursor-motion-only case.  */
  4039.       if (!(cursor_in_echo_area && FRAME_HAS_MINIBUF_P (f)
  4040.         && EQ (FRAME_MINIBUF_WINDOW (f), minibuf_window)))
  4041.         {
  4042.           FRAME_CURSOR_Y (f) = cursor_vpos;
  4043.           FRAME_CURSOR_X (f) = cursor_hpos;
  4044.  
  4045.           if (w == XWINDOW (selected_window))
  4046.         {
  4047.           /* Line is not continued and did not start
  4048.              in middle of character */
  4049.           if ((hpos - WINDOW_LEFT_MARGIN (w)
  4050.                == (XINT (w->hscroll) ? 1 - XINT (w->hscroll) : 0))
  4051.               && val.vpos)
  4052.             {
  4053.               this_line_bufpos = start;
  4054.               this_line_buffer = current_buffer;
  4055.               this_line_vpos = cursor_vpos;
  4056.               this_line_start_hpos = hpos - WINDOW_LEFT_MARGIN (w);
  4057.               this_line_endpos = Z - lastpos;
  4058.             }
  4059.         }
  4060.         }
  4061.     }
  4062.     }
  4063.  
  4064.   /* If hscroll and line not empty, insert truncation-at-left marker */
  4065.   if (hscroll && lastpos != start)
  4066.     {
  4067.       GLYPH g = fix_glyph (f, truncator, 0);
  4068.       *leftmargin = g;
  4069.       if (p1 <= leftmargin)
  4070.     p1 = leftmargin + 1;
  4071.       else            /* MULE: it may be a wide-column character */
  4072.     {
  4073.       p1prev = leftmargin + 1;
  4074.       while (p1prev < p1 && *p1prev & GLYPH_MASK_PADDING)
  4075.         *p1prev++ = g;
  4076.     }
  4077.     }
  4078.  
  4079.   if (!WINDOW_RIGHTMOST_P (w))
  4080.     {
  4081.       endp++;
  4082.       if (p1 < leftmargin) p1 = leftmargin;
  4083.       while (p1 < endp) *p1++ = SPACEGLYPH;
  4084.  
  4085.       /* Don't draw vertical bars if we're using scroll bars.  They're
  4086.          covered up by the scroll bars, and it's distracting to see
  4087.          them when the scroll bar windows are flickering around to be
  4088.          reconfigured.  */
  4089.       if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
  4090.     {
  4091.       int i;
  4092.       for (i = 0; i < FRAME_SCROLL_BAR_COLS (f); i++)
  4093.         *p1++ = SPACEGLYPH;
  4094.     }
  4095.       else if (!FRAME_HAS_VERTICAL_SCROLL_BARS (f))
  4096.     *p1++ = (dp && INTEGERP (DISP_BORDER_GLYPH (dp))
  4097.          ? XINT (DISP_BORDER_GLYPH (dp))
  4098.          : '|');
  4099.     }
  4100.   desired_glyphs->used[vpos] = max (desired_glyphs->used[vpos],
  4101.                    p1 - desired_glyphs->glyphs[vpos]);
  4102.   desired_glyphs->glyphs[vpos][desired_glyphs->used[vpos]] = 0;
  4103.  
  4104.   /* If the start of this line is the overlay arrow-position,
  4105.      then put the arrow string into the display-line.  */
  4106.  
  4107.   if (MARKERP (Voverlay_arrow_position)
  4108.       && current_buffer == XMARKER (Voverlay_arrow_position)->buffer
  4109.       && start == marker_position (Voverlay_arrow_position)
  4110.       && STRINGP (Voverlay_arrow_string)
  4111.       && ! overlay_arrow_seen)
  4112.     {
  4113.       int i, i_byte;
  4114.       int len = XSTRING (Voverlay_arrow_string)->size;
  4115.       int arrow_end;
  4116.  
  4117.       if (len > width)
  4118.     len = width;
  4119.  
  4120.       /* If the arrow string has text props, obey them when displaying.  */
  4121.       for (i = 0, i_byte = 0; i < len; )
  4122.     {
  4123.       int c;
  4124.       Lisp_Object face, ilisp;
  4125.       int newface;
  4126.       int idx = i;
  4127.  
  4128.       if (STRING_MULTIBYTE (Voverlay_arrow_string))
  4129.         FETCH_STRING_CHAR_ADVANCE (c, Voverlay_arrow_string, i, i_byte);
  4130.       else
  4131.         c = XSTRING (Voverlay_arrow_string)->data[i++];
  4132.  
  4133.       XSETFASTINT (ilisp, i);
  4134. #ifdef HAVE_FACES
  4135.       if (FRAME_WINDOW_P (f))
  4136.         {
  4137.           face = Fget_text_property (ilisp, Qface, Voverlay_arrow_string);
  4138.           newface = compute_glyph_face_1 (f, face, 0);
  4139.           c = FAST_MAKE_GLYPH (c, newface);
  4140.         }
  4141. #endif /* HAVE_FACES */
  4142.       leftmargin[idx] = c;
  4143.     }
  4144.  
  4145.       /* Bug in SunOS 4.1.1 compiler requires this intermediate variable.  */
  4146.       arrow_end = (leftmargin - desired_glyphs->glyphs[vpos]) + len;
  4147.       if (desired_glyphs->used[vpos] < arrow_end)
  4148.     desired_glyphs->used[vpos] = arrow_end;
  4149.  
  4150.       overlay_arrow_seen = 1;
  4151.     }
  4152.  
  4153.   val.bufpos = pos;
  4154.   val.bytepos = pos_byte;
  4155.   val.ovstring_chars_done = ovstr_done;
  4156.   val_display_text_line = val;
  4157.   return &val_display_text_line;
  4158. }
  4159.  
  4160. /* Redisplay the menu bar in the frame for window W.  */
  4161.  
  4162. static void
  4163. display_menu_bar (w)
  4164.      struct window *w;
  4165. {
  4166.   Lisp_Object items, tail;
  4167.   register int vpos = 0;
  4168.   register FRAME_PTR f = XFRAME (WINDOW_FRAME (w));
  4169.   int maxendcol = FRAME_WIDTH (f);
  4170.   int hpos = 0;
  4171.   int i;
  4172.  
  4173. #ifdef HAVE_NTGUI
  4174.   if (!NILP (Vwindow_system))
  4175.     return;
  4176. #endif
  4177.  
  4178. #ifdef USE_X_TOOLKIT
  4179.   if (FRAME_X_P (f))
  4180.     return;
  4181. #endif /* USE_X_TOOLKIT */
  4182.  
  4183.   get_display_line (f, vpos, 0);
  4184.  
  4185.   items = FRAME_MENU_BAR_ITEMS (f);
  4186.   for (i = 0; i < XVECTOR (items)->size; i += 4)
  4187.     {
  4188.       Lisp_Object pos, string;
  4189.       string = XVECTOR (items)->contents[i + 1];
  4190.       if (NILP (string))
  4191.     break;
  4192.  
  4193.       XSETFASTINT (XVECTOR (items)->contents[i + 3], hpos);
  4194.  
  4195.       if (hpos < maxendcol)
  4196.     hpos = display_string (w, vpos,
  4197.                    XSTRING (string)->data,
  4198.                    STRING_BYTES (XSTRING (string)),
  4199.                    hpos, 0, 0, hpos, maxendcol,
  4200.                    STRING_MULTIBYTE (string));
  4201.       /* Put a space between items.  */
  4202.       if (hpos < maxendcol)
  4203.     {
  4204.       int hpos1 = hpos + 1;
  4205.       hpos = display_string (w, vpos, "", 0, hpos, 0, 0,
  4206.                  min (hpos1, maxendcol), maxendcol, 0);
  4207.     }
  4208.     }
  4209.  
  4210.   FRAME_DESIRED_GLYPHS (f)->bufp[vpos] = 0;
  4211.   FRAME_DESIRED_GLYPHS (f)->highlight[vpos] = mode_line_inverse_video;
  4212.  
  4213.   /* Fill out the line with spaces.  */
  4214.   if (maxendcol > hpos)
  4215.     hpos = display_string (w, vpos, "", 0, hpos, 0, 0, maxendcol, maxendcol, 0);
  4216.  
  4217.   /* Clear the rest of the lines allocated to the menu bar.  */
  4218.   vpos++;
  4219.   while (vpos < FRAME_MENU_BAR_LINES (f))
  4220.     get_display_line (f, vpos++, 0);
  4221. }
  4222.  
  4223. /* Display the mode line for window w */
  4224.  
  4225. static void
  4226. display_mode_line (w)
  4227.      struct window *w;
  4228. {
  4229.   register int vpos = XFASTINT (w->height) + XFASTINT (w->top) - 1;
  4230.   register int left = WINDOW_LEFT_MARGIN (w);
  4231.   register int right = WINDOW_RIGHT_MARGIN (w);
  4232.   register FRAME_PTR f = XFRAME (WINDOW_FRAME (w));
  4233.  
  4234.   line_number_displayed = 0;
  4235.   w->column_number_displayed = Qnil;
  4236.  
  4237.   get_display_line (f, vpos, left);
  4238.  
  4239.   /* Temporarily make frame F's kboard the current kboard
  4240.      so that kboard-local variables in the mode_line_format
  4241.      will get the right values.  */
  4242.   push_frame_kboard (f);
  4243.  
  4244.   display_mode_element (w, vpos, left, 0, right, right,
  4245.             current_buffer->mode_line_format);
  4246.  
  4247.   pop_frame_kboard ();
  4248.  
  4249.   FRAME_DESIRED_GLYPHS (f)->bufp[vpos] = 0;
  4250.  
  4251.   /* Put the mode line in inverse video.
  4252.      Use faces if possible, since that lets us handle
  4253.      partial-width windows and avoid inverting the scroll bar columns.  */
  4254. #ifdef HAVE_FACES
  4255.   if (! FRAME_TERMCAP_P (f) && mode_line_inverse_video)
  4256.     {
  4257.       /* For a partial width window, explicitly set face of each glyph. */
  4258.       int i;
  4259.       unsigned int padding;
  4260.       GLYPH *ptr = FRAME_DESIRED_GLYPHS (f)->glyphs[vpos];
  4261.       for (i = left; i < right; ++i)
  4262.     {
  4263.       padding = ptr[i] & GLYPH_MASK_PADDING;
  4264.       ptr[i] = FAST_MAKE_GLYPH (FAST_GLYPH_CHAR (ptr[i]), 1) | padding;
  4265.     }
  4266.     }
  4267.   else
  4268. #endif
  4269.  
  4270.   /* Make the mode line inverse video if the entire line
  4271.      is made of mode lines.
  4272.      I.e. if this window is full width,
  4273.      or if it is the child of a full width window
  4274.      (which implies that that window is split side-by-side
  4275.      and the rest of this line is mode lines of the sibling windows).  */
  4276.   if (WINDOW_FULL_WIDTH_P (w)
  4277.       || WINDOW_FULL_WIDTH_P (XWINDOW (w->parent)))
  4278.     FRAME_DESIRED_GLYPHS (f)->highlight[vpos] = mode_line_inverse_video;
  4279. }
  4280.  
  4281. /* Contribute ELT to the mode line for window W.
  4282.    How it translates into text depends on its data type.
  4283.  
  4284.    VPOS is the position of the mode line being displayed.
  4285.  
  4286.    HPOS is the position (absolute on frame) where this element's text
  4287.    should start.  The output is truncated automatically at the right
  4288.    edge of window W.
  4289.  
  4290.    DEPTH is the depth in recursion.  It is used to prevent
  4291.    infinite recursion here.
  4292.  
  4293.    MINENDCOL is the hpos before which the element may not end.
  4294.    The element is padded at the right with spaces if nec
  4295.    to reach this column.
  4296.  
  4297.    MAXENDCOL is the hpos past which this element may not extend.
  4298.    If MINENDCOL is > MAXENDCOL, MINENDCOL takes priority.
  4299.    (This is necessary to make nested padding and truncation work.)
  4300.  
  4301.    Returns the hpos of the end of the text generated by ELT.
  4302.    The next element will receive that value as its HPOS arg,
  4303.    so as to concatenate the elements.  */
  4304.  
  4305. static int
  4306. display_mode_element (w, vpos, hpos, depth, minendcol, maxendcol, elt)
  4307.      struct window *w;
  4308.      register int vpos, hpos;
  4309.      int depth;
  4310.      int minendcol;
  4311.      register int maxendcol;
  4312.      register Lisp_Object elt;
  4313. {
  4314.  tail_recurse:
  4315.   if (depth > 10)
  4316.     goto invalid;
  4317.  
  4318.   depth++;
  4319.  
  4320.   switch (SWITCH_ENUM_CAST (XTYPE (elt)))
  4321.     {
  4322.     case Lisp_String:
  4323.       {
  4324.     /* A string: output it and check for %-constructs within it.  */
  4325.     register unsigned char c;
  4326.     register unsigned char *this = XSTRING (elt)->data;
  4327.  
  4328.     while (hpos < maxendcol && *this)
  4329.       {
  4330.         unsigned char *last = this;
  4331.         while ((c = *this++) != '\0' && c != '%')
  4332.           ;
  4333.         if (this - 1 != last)
  4334.           {
  4335.         register int lim = --this - last + hpos;
  4336.         if (frame_title_ptr)
  4337.           hpos = store_frame_title (last, hpos, min (lim, maxendcol));
  4338.         else
  4339.           hpos = display_string (w, vpos, last, -1, hpos, 0, 1,
  4340.                      hpos, min (lim, maxendcol),
  4341.                      STRING_MULTIBYTE (elt));
  4342.           }
  4343.         else /* c == '%' */
  4344.           {
  4345.         register int minendcol;
  4346.         register int spec_width = 0;
  4347.  
  4348.         /* We can't allow -ve args due to the "%-" construct */
  4349.         /* Argument specifies minwidth but not maxwidth
  4350.            (maxwidth can be specified by
  4351.              (<negative-number> . <stuff>) mode-line elements) */
  4352.  
  4353.         while ((c = *this++) >= '0' && c <= '9')
  4354.           {
  4355.             spec_width = spec_width * 10 + (c - '0');
  4356.           }
  4357.  
  4358.         minendcol = hpos + spec_width;
  4359.         if (minendcol > maxendcol)
  4360.           {
  4361.             spec_width = maxendcol - hpos;
  4362.             minendcol = maxendcol;
  4363.           }
  4364.  
  4365.         if (c == 'M')
  4366.           hpos = display_mode_element (w, vpos, hpos, depth,
  4367.                            spec_width, maxendcol,
  4368.                            Vglobal_mode_string);
  4369.         else if (c != 0)
  4370.           {
  4371.             char *spec = decode_mode_spec (w, c, spec_width,
  4372.                            maxendcol - hpos);
  4373.             if (frame_title_ptr)
  4374.               hpos = store_frame_title (spec, minendcol, maxendcol);
  4375.             else
  4376.               hpos = display_string (w, vpos, spec, -1,
  4377.                          hpos, 0, 1,
  4378.                          minendcol, maxendcol, -1);
  4379.           }
  4380.           }
  4381.       }
  4382.       }
  4383.       break;
  4384.  
  4385.     case Lisp_Symbol:
  4386.       /* A symbol: process the value of the symbol recursively
  4387.      as if it appeared here directly.  Avoid error if symbol void.
  4388.      Special case: if value of symbol is a string, output the string
  4389.      literally.  */
  4390.       {
  4391.     register Lisp_Object tem;
  4392.     tem = Fboundp (elt);
  4393.     if (!NILP (tem))
  4394.       {
  4395.         tem = Fsymbol_value (elt);
  4396.         /* If value is a string, output that string literally:
  4397.            don't check for % within it.  */
  4398.         if (STRINGP (tem))
  4399.           {
  4400.         if (frame_title_ptr)
  4401.           hpos = store_frame_title (XSTRING (tem)->data,
  4402.                         minendcol, maxendcol);
  4403.         else
  4404.           hpos = display_string (w, vpos, XSTRING (tem)->data,
  4405.                      STRING_BYTES (XSTRING (tem)),
  4406.                      hpos, 0, 1, minendcol, maxendcol,
  4407.                      STRING_MULTIBYTE (tem));
  4408.           }
  4409.         /* Give up right away for nil or t.  */
  4410.         else if (!EQ (tem, elt))
  4411.           { elt = tem; goto tail_recurse; }
  4412.       }
  4413.       }
  4414.       break;
  4415.  
  4416.     case Lisp_Cons:
  4417.       {
  4418.     register Lisp_Object car, tem;
  4419.  
  4420.     /* A cons cell: three distinct cases.
  4421.        If first element is a string or a cons, process all the elements
  4422.        and effectively concatenate them.
  4423.        If first element is a negative number, truncate displaying cdr to
  4424.        at most that many characters.  If positive, pad (with spaces)
  4425.        to at least that many characters.
  4426.        If first element is a symbol, process the cadr or caddr recursively
  4427.        according to whether the symbol's value is non-nil or nil.  */
  4428.     car = XCONS (elt)->car;
  4429.     if (SYMBOLP (car))
  4430.       {
  4431.         tem = Fboundp (car);
  4432.         elt = XCONS (elt)->cdr;
  4433.         if (!CONSP (elt))
  4434.           goto invalid;
  4435.         /* elt is now the cdr, and we know it is a cons cell.
  4436.            Use its car if CAR has a non-nil value.  */
  4437.         if (!NILP (tem))
  4438.           {
  4439.         tem = Fsymbol_value (car);
  4440.         if (!NILP (tem))
  4441.           { elt = XCONS (elt)->car; goto tail_recurse; }
  4442.           }
  4443.         /* Symbol's value is nil (or symbol is unbound)
  4444.            Get the cddr of the original list
  4445.            and if possible find the caddr and use that.  */
  4446.         elt = XCONS (elt)->cdr;
  4447.         if (NILP (elt))
  4448.           break;
  4449.         else if (!CONSP (elt))
  4450.           goto invalid;
  4451.         elt = XCONS (elt)->car;
  4452.         goto tail_recurse;
  4453.       }
  4454.     else if (INTEGERP (car))
  4455.       {
  4456.         register int lim = XINT (car);
  4457.         elt = XCONS (elt)->cdr;
  4458.         if (lim < 0)
  4459.           /* Negative int means reduce maximum width.
  4460.          DO NOT change MINENDCOL here!
  4461.          (20 -10 . foo) should truncate foo to 10 col
  4462.          and then pad to 20.  */
  4463.           maxendcol = min (maxendcol, hpos - lim);
  4464.         else if (lim > 0)
  4465.           {
  4466.         /* Padding specified.  Don't let it be more than
  4467.            current maximum.  */
  4468.         lim += hpos;
  4469.         if (lim > maxendcol)
  4470.           lim = maxendcol;
  4471.         /* If that's more padding than already wanted, queue it.
  4472.            But don't reduce padding already specified even if
  4473.            that is beyond the current truncation point.  */
  4474.         if (lim > minendcol)
  4475.           minendcol = lim;
  4476.           }
  4477.         goto tail_recurse;
  4478.       }
  4479.     else if (STRINGP (car) || CONSP (car))
  4480.       {
  4481.         register int limit = 50;
  4482.         /* LIMIT is to protect against circular lists.  */
  4483.         while (CONSP (elt) && --limit > 0
  4484.            && hpos < maxendcol)
  4485.           {
  4486.         hpos = display_mode_element (w, vpos, hpos, depth,
  4487.                          hpos, maxendcol,
  4488.                          XCONS (elt)->car);
  4489.         elt = XCONS (elt)->cdr;
  4490.           }
  4491.       }
  4492.       }
  4493.       break;
  4494.  
  4495.     default:
  4496.     invalid:
  4497.       if (frame_title_ptr)
  4498.     hpos = store_frame_title ("*invalid*", minendcol, maxendcol);
  4499.       else
  4500.     hpos = display_string (w, vpos, "*invalid*", -1, hpos, 0, 1,
  4501.                    minendcol, maxendcol, 0);
  4502.       return hpos;
  4503.     }
  4504.  
  4505.   if (minendcol > hpos)
  4506.     if (frame_title_ptr)
  4507.       hpos = store_frame_title ("", minendcol, maxendcol);
  4508.     else
  4509.       hpos = display_string (w, vpos, "", 0, hpos,
  4510.                  0, 1, minendcol, maxendcol, 0);
  4511.   return hpos;
  4512. }
  4513.  
  4514. /* Write a null-terminated, right justified decimal representation of
  4515.    the positive integer D to BUF using a minimal field width WIDTH.  */
  4516.  
  4517. static void
  4518. pint2str (buf, width, d)
  4519.      register char *buf;
  4520.      register int width;
  4521.      register int d;
  4522. {
  4523.   register char *p = buf;
  4524.   
  4525.   if (d <= 0)
  4526.       *p++ = '0';
  4527.   else
  4528.       while (d > 0)
  4529.       {
  4530.       *p++ = d % 10 + '0';
  4531.       d /= 10;
  4532.       }
  4533.   for (width -= (int) (p - buf); width > 0; --width) *p++ = ' ';
  4534.   *p-- = '\0';
  4535.   while (p > buf)
  4536.   {
  4537.       d = *buf;
  4538.       *buf++ = *p;
  4539.       *p-- = d;
  4540.   }
  4541. }
  4542.  
  4543. /* Set a mnemonic character for CODING_SYSTEM (Lisp symbol) in BUF.
  4544.    If EOL_FLAG is 1, set also a mnemonic character for end-of-line
  4545.    type of CODING_SYSTEM.  Return updated pointer into BUF.  */
  4546.  
  4547. static char *
  4548. decode_mode_spec_coding (coding_system, buf, eol_flag)
  4549.      Lisp_Object coding_system;
  4550.      register char *buf;
  4551.      int eol_flag;
  4552. {
  4553.   Lisp_Object val;
  4554.   int multibyte = !NILP (current_buffer->enable_multibyte_characters);
  4555.  
  4556.   val = coding_system;
  4557.  
  4558.   if (NILP (val))        /* Not yet decided.  */
  4559.     {
  4560.       if (multibyte)
  4561.     *buf++ = '-';
  4562.       if (eol_flag)
  4563.     *buf++ = eol_mnemonic_undecided;
  4564.       /* Don't mention EOL conversion if it isn't decided.  */
  4565.     }
  4566.   else
  4567.     {
  4568.       Lisp_Object eolvalue;
  4569.  
  4570.       eolvalue = Fget (coding_system, Qeol_type);
  4571.  
  4572.       while (!NILP (val) && SYMBOLP (val))
  4573.     {
  4574.       val = Fget (val, Qcoding_system);
  4575.       if (NILP (eolvalue))
  4576.         eolvalue = Fget (val, Qeol_type);
  4577.     }
  4578.  
  4579.       if (multibyte)
  4580.     *buf++ = XFASTINT (XVECTOR (val)->contents[1]);
  4581.  
  4582.       if (eol_flag)
  4583.     {
  4584.       /* The EOL conversion we are using.  */
  4585.       int eoltype;
  4586.       /* The EOL conversion that is normal on this system.  */
  4587.  
  4588.       if (NILP (eolvalue))    /* Not yet decided.  */
  4589.         eoltype = eol_mnemonic_undecided;
  4590.       else if (VECTORP (eolvalue)) /* Not yet decided.  */
  4591.         eoltype = eol_mnemonic_undecided;
  4592.       else            /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
  4593.         eoltype = (XFASTINT (eolvalue) == 0
  4594.                ? eol_mnemonic_unix
  4595.                : (XFASTINT (eolvalue) == 1
  4596.               ? eol_mnemonic_dos : eol_mnemonic_mac));
  4597.  
  4598.       /* Mention the EOL conversion if it is not the usual one.  */
  4599.       *buf++ = eoltype;
  4600.     }
  4601.     }
  4602.   return buf;
  4603. }
  4604.  
  4605. /* Return a string for the output of a mode line %-spec for window W,
  4606.    generated by character C.  SPEC_WIDTH is the field width when
  4607.    padding to the left (%c, %l).  The value returned from this
  4608.    function will later be truncated to width MAXWIDTH. */
  4609.  
  4610. static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
  4611.  
  4612. static char *
  4613. decode_mode_spec (w, c, spec_width, maxwidth)
  4614.      struct window *w;
  4615.      register char c;
  4616.      register int spec_width;
  4617.      register int maxwidth;
  4618. {
  4619.   Lisp_Object obj;
  4620.   FRAME_PTR f = XFRAME (WINDOW_FRAME (w));
  4621.   char *decode_mode_spec_buf = (char *) FRAME_TEMP_GLYPHS (f)->total_contents;
  4622.   struct buffer *b = XBUFFER (w->buffer);
  4623.  
  4624.   obj = Qnil;
  4625.   if (maxwidth > FRAME_WIDTH (f))
  4626.     maxwidth = FRAME_WIDTH (f);
  4627.  
  4628.   switch (c)
  4629.     {
  4630.     case '*':
  4631.       if (!NILP (b->read_only))
  4632.     return "%";
  4633.       if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
  4634.     return "*";
  4635.       return "-";
  4636.  
  4637.     case '+':
  4638.       /* This differs from %* only for a modified read-only buffer.  */
  4639.       if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
  4640.     return "*";
  4641.       if (!NILP (b->read_only))
  4642.     return "%";
  4643.       return "-";
  4644.  
  4645.     case '&':
  4646.       /* This differs from %* in ignoring read-only-ness.  */
  4647.       if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
  4648.     return "*";
  4649.       return "-";
  4650.  
  4651.     case '%':
  4652.       return "%";
  4653.  
  4654.     case '[': 
  4655.       {
  4656.     int i;
  4657.     char *p;
  4658.  
  4659.     if (command_loop_level > 5)
  4660.       return "[[[... ";
  4661.     p = decode_mode_spec_buf;
  4662.     for (i = 0; i < command_loop_level; i++)
  4663.       *p++ = '[';
  4664.     *p = 0;
  4665.     return decode_mode_spec_buf;
  4666.       }
  4667.  
  4668.     case ']': 
  4669.       {
  4670.     int i;
  4671.     char *p;
  4672.  
  4673.     if (command_loop_level > 5)
  4674.       return " ...]]]";
  4675.     p = decode_mode_spec_buf;
  4676.     for (i = 0; i < command_loop_level; i++)
  4677.       *p++ = ']';
  4678.     *p = 0;
  4679.     return decode_mode_spec_buf;
  4680.       }
  4681.  
  4682.     case '-':
  4683.       {
  4684.     register char *p;
  4685.     register int i;
  4686.     
  4687.     if (maxwidth < sizeof (lots_of_dashes))
  4688.       return lots_of_dashes;
  4689.     else
  4690.       {
  4691.         for (p = decode_mode_spec_buf, i = maxwidth; i > 0; i--)
  4692.           *p++ = '-';
  4693.         *p = '\0';
  4694.       }
  4695.     return decode_mode_spec_buf;
  4696.       }
  4697.  
  4698.     case 'b': 
  4699.       obj = b->name;
  4700. #if 0
  4701.       if (maxwidth >= 3 && STRING_BYTES (XSTRING (obj)) > maxwidth)
  4702.     {
  4703.       bcopy (XSTRING (obj)->data, decode_mode_spec_buf, maxwidth - 1);
  4704.       decode_mode_spec_buf[maxwidth - 1] = '\\';
  4705.       decode_mode_spec_buf[maxwidth] = '\0';
  4706.       return decode_mode_spec_buf;
  4707.     }
  4708. #endif
  4709.       break;
  4710.  
  4711.     case 'c':
  4712.       {
  4713.     int col = current_column ();
  4714.     XSETFASTINT (w->column_number_displayed, col);
  4715.     pint2str (decode_mode_spec_buf, spec_width, col);
  4716.     return decode_mode_spec_buf;
  4717.       }
  4718.  
  4719.     case 'F':
  4720.       /* %F displays the frame name.  */
  4721.       /* Systems that can only display a single frame at a time should
  4722.      NOT replace the frame name with the (constant) frame title,
  4723.      since then they won't be able to tell which frame is that.  */
  4724.       if (FRAME_WINDOW_P (f) && !NILP (f->title))
  4725.     return (char *) XSTRING (f->title)->data;
  4726.       if (f->explicit_name || ! FRAME_WINDOW_P (f))
  4727.     return (char *) XSTRING (f->name)->data;
  4728.       return "Emacs";
  4729.  
  4730.     case 'f': 
  4731.       obj = b->filename;
  4732. #if 0
  4733.       if (NILP (obj))
  4734.     return "[none]";
  4735.       else if (STRINGP (obj) && STRING_BYTES (XSTRING (obj)) > maxwidth)
  4736.     {
  4737.       bcopy ("...", decode_mode_spec_buf, 3);
  4738.       bcopy (XSTRING (obj)->data + STRING_BYTES (XSTRING (obj)) - maxwidth + 3,
  4739.          decode_mode_spec_buf + 3, maxwidth - 3);
  4740.       return decode_mode_spec_buf;
  4741.     }
  4742. #endif
  4743.       break;
  4744.  
  4745.     case 'l':
  4746.       {
  4747.     int startpos = XMARKER (w->start)->charpos;
  4748.     int startpos_byte = marker_byte_position (w->start);
  4749.     int line, linepos, linepos_byte, topline;
  4750.     int nlines, junk;
  4751.     Lisp_Object tem;
  4752.     int height = XFASTINT (w->height);
  4753.  
  4754.     /* If we decided that this buffer isn't suitable for line numbers, 
  4755.        don't forget that too fast.  */
  4756.     if (EQ (w->base_line_pos, w->buffer))
  4757.       goto no_value;
  4758.     /* But do forget it, if the window shows a different buffer now.  */
  4759.     else if (BUFFERP (w->base_line_pos))
  4760.       w->base_line_pos = Qnil;
  4761.  
  4762.     /* If the buffer is very big, don't waste time.  */
  4763.     if (BUF_ZV (b) - BUF_BEGV (b) > line_number_display_limit)
  4764.       {
  4765.         w->base_line_pos = Qnil;
  4766.         w->base_line_number = Qnil;
  4767.         goto no_value;
  4768.       }
  4769.  
  4770.     if (!NILP (w->base_line_number)
  4771.         && !NILP (w->base_line_pos)
  4772.         && XFASTINT (w->base_line_pos) <= startpos)
  4773.       {
  4774.         line = XFASTINT (w->base_line_number);
  4775.         linepos = XFASTINT (w->base_line_pos);
  4776.         linepos_byte = buf_charpos_to_bytepos (b, linepos);
  4777.       }
  4778.     else
  4779.       {
  4780.         line = 1;
  4781.         linepos = BUF_BEGV (b);
  4782.         linepos_byte = BUF_BEGV_BYTE (b);
  4783.       }
  4784.  
  4785.     /* Count lines from base line to window start position.  */
  4786.     nlines = display_count_lines (linepos, linepos_byte,
  4787.                       startpos_byte,
  4788.                       startpos, &junk);
  4789.  
  4790.     topline = nlines + line;
  4791.  
  4792.     /* Determine a new base line, if the old one is too close
  4793.        or too far away, or if we did not have one.
  4794.        "Too close" means it's plausible a scroll-down would
  4795.        go back past it.  */
  4796.     if (startpos == BUF_BEGV (b))
  4797.       {
  4798.         XSETFASTINT (w->base_line_number, topline);
  4799.         XSETFASTINT (w->base_line_pos, BUF_BEGV (b));
  4800.       }
  4801.     else if (nlines < height + 25 || nlines > height * 3 + 50
  4802.          || linepos == BUF_BEGV (b))
  4803.       {
  4804.         int limit = BUF_BEGV (b);
  4805.         int limit_byte = BUF_BEGV_BYTE (b);
  4806.         int position;
  4807.         int distance = (height * 2 + 30) * 200;
  4808.  
  4809.         if (startpos - distance > limit)
  4810.           {
  4811.         limit = startpos - distance;
  4812.         limit_byte = CHAR_TO_BYTE (limit);
  4813.           }
  4814.  
  4815.         nlines = display_count_lines (startpos, startpos_byte,
  4816.                       limit_byte,
  4817.                       - (height * 2 + 30),
  4818.                       &position);
  4819.         /* If we couldn't find the lines we wanted within 
  4820.            200 chars per line,
  4821.            give up on line numbers for this window.  */
  4822.         if (position == limit_byte && limit == startpos - distance)
  4823.           {
  4824.         w->base_line_pos = w->buffer;
  4825.         w->base_line_number = Qnil;
  4826.         goto no_value;
  4827.           }
  4828.  
  4829.         XSETFASTINT (w->base_line_number, topline - nlines);
  4830.         XSETFASTINT (w->base_line_pos, BYTE_TO_CHAR (position));
  4831.       }
  4832.  
  4833.     /* Now count lines from the start pos to point.  */
  4834.     nlines = display_count_lines (startpos, startpos_byte,
  4835.                       PT_BYTE, PT, &junk);
  4836.  
  4837.     /* Record that we did display the line number.  */
  4838.     line_number_displayed = 1;
  4839.  
  4840.     /* Make the string to show.  */
  4841.     pint2str (decode_mode_spec_buf, spec_width, topline + nlines);
  4842.     return decode_mode_spec_buf;
  4843.     no_value:
  4844.         {
  4845.       char* p = decode_mode_spec_buf;
  4846.       for (spec_width -= 2; spec_width > 0; --spec_width) *p++ = ' ';
  4847.       strcpy (p, "??");
  4848.       return decode_mode_spec_buf;
  4849.     }
  4850.       }
  4851.       break;
  4852.  
  4853.     case 'm': 
  4854.       obj = b->mode_name;
  4855.       break;
  4856.  
  4857.     case 'n':
  4858.       if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
  4859.     return " Narrow";
  4860.       break;
  4861.  
  4862.     case 'p':
  4863.       {
  4864.     int pos = marker_position (w->start);
  4865.     int total = BUF_ZV (b) - BUF_BEGV (b);
  4866.  
  4867.     if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
  4868.       {
  4869.         if (pos <= BUF_BEGV (b))
  4870.           return "All";
  4871.         else
  4872.           return "Bottom";
  4873.       }
  4874.     else if (pos <= BUF_BEGV (b))
  4875.       return "Top";
  4876.     else
  4877.       {
  4878.         if (total > 1000000)
  4879.           /* Do it differently for a large value, to avoid overflow.  */
  4880.           total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
  4881.         else
  4882.           total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
  4883.         /* We can't normally display a 3-digit number,
  4884.            so get us a 2-digit number that is close.  */
  4885.         if (total == 100)
  4886.           total = 99;
  4887.         sprintf (decode_mode_spec_buf, "%2d%%", total);
  4888.         return decode_mode_spec_buf;
  4889.       }
  4890.       }
  4891.  
  4892.       /* Display percentage of size above the bottom of the screen.  */
  4893.     case 'P':
  4894.       {
  4895.     int toppos = marker_position (w->start);
  4896.     int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
  4897.     int total = BUF_ZV (b) - BUF_BEGV (b);
  4898.  
  4899.     if (botpos >= BUF_ZV (b))
  4900.       {
  4901.         if (toppos <= BUF_BEGV (b))
  4902.           return "All";
  4903.         else
  4904.           return "Bottom";
  4905.       }
  4906.     else
  4907.       {
  4908.         if (total > 1000000)
  4909.           /* Do it differently for a large value, to avoid overflow.  */
  4910.           total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
  4911.         else
  4912.           total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
  4913.         /* We can't normally display a 3-digit number,
  4914.            so get us a 2-digit number that is close.  */
  4915.         if (total == 100)
  4916.           total = 99;
  4917.         if (toppos <= BUF_BEGV (b))
  4918.           sprintf (decode_mode_spec_buf, "Top%2d%%", total);
  4919.         else
  4920.           sprintf (decode_mode_spec_buf, "%2d%%", total);
  4921.         return decode_mode_spec_buf;
  4922.       }
  4923.       }
  4924.  
  4925.     case 's':
  4926.       /* status of process */
  4927.       obj = Fget_buffer_process (w->buffer);
  4928.       if (NILP (obj))
  4929.     return "no process";
  4930. #ifdef subprocesses
  4931.       obj = Fsymbol_name (Fprocess_status (obj));
  4932. #endif
  4933.       break;
  4934.  
  4935.     case 't':            /* indicate TEXT or BINARY */
  4936. #ifdef MODE_LINE_BINARY_TEXT
  4937.       return MODE_LINE_BINARY_TEXT (b);
  4938. #else
  4939.       return "T";
  4940. #endif
  4941.  
  4942.     case 'z':
  4943.       /* coding-system (not including end-of-line format) */
  4944.     case 'Z':
  4945.       /* coding-system (including end-of-line type) */
  4946.       {
  4947.     int eol_flag = (c == 'Z');
  4948.     char *p = decode_mode_spec_buf;
  4949.  
  4950.     if (! FRAME_WINDOW_P (f))
  4951.       {
  4952.         /* No need to mention EOL here--the terminal never needs
  4953.            to do EOL conversion.  */
  4954.         p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
  4955.         p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
  4956.       }
  4957.     p = decode_mode_spec_coding (b->buffer_file_coding_system,
  4958.                      p, eol_flag);
  4959.  
  4960. #if 0 /* This proves to be annoying; I think we can do without.  -- rms.  */
  4961. #ifdef subprocesses
  4962.     obj = Fget_buffer_process (Fcurrent_buffer ());
  4963.     if (PROCESSP (obj))
  4964.       {
  4965.         p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
  4966.                      p, eol_flag);
  4967.         p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
  4968.                      p, eol_flag);
  4969.       }
  4970. #endif /* subprocesses */
  4971. #endif /* 0 */
  4972.     *p = 0;
  4973.     return decode_mode_spec_buf;
  4974.       }
  4975.     }
  4976.  
  4977.   if (STRINGP (obj))
  4978.     return (char *) XSTRING (obj)->data;
  4979.   else
  4980.     return "";
  4981. }
  4982.  
  4983. /* Count up to COUNT lines starting from START / START_BYTE.
  4984.    But don't go beyond LIMIT_BYTE.
  4985.    Return the number of lines thus found (always nonnegative).
  4986.  
  4987.    Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT.  */
  4988.  
  4989. static int
  4990. display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
  4991.      int start, start_byte, limit_byte, count;
  4992.      int *byte_pos_ptr;
  4993. {
  4994.   register unsigned char *cursor;
  4995.   unsigned char *base;
  4996.  
  4997.   register int ceiling;
  4998.   register unsigned char *ceiling_addr;
  4999.   int orig_count = count;
  5000.  
  5001.   /* If we are not in selective display mode,
  5002.      check only for newlines.  */
  5003.   int selective_display = (!NILP (current_buffer->selective_display)
  5004.                && !INTEGERP (current_buffer->selective_display));
  5005.  
  5006.   if (count > 0)
  5007.     {
  5008.       while (start_byte < limit_byte)
  5009.     {
  5010.       ceiling =  BUFFER_CEILING_OF (start_byte);
  5011.       ceiling = min (limit_byte - 1, ceiling);
  5012.       ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
  5013.       base = (cursor = BYTE_POS_ADDR (start_byte));
  5014.       while (1)
  5015.         {
  5016.           if (selective_display)
  5017.         while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
  5018.           ;
  5019.           else
  5020.         while (*cursor != '\n' && ++cursor != ceiling_addr)
  5021.           ;
  5022.  
  5023.           if (cursor != ceiling_addr)
  5024.         {
  5025.           if (--count == 0)
  5026.             {
  5027.               start_byte += cursor - base + 1;
  5028.               *byte_pos_ptr = start_byte;
  5029.               return orig_count;
  5030.             }
  5031.           else
  5032.             if (++cursor == ceiling_addr)
  5033.               break;
  5034.         }
  5035.           else
  5036.         break;
  5037.         }
  5038.       start_byte += cursor - base;
  5039.     }
  5040.     }
  5041.   else
  5042.     {
  5043.       while (start_byte > limit_byte)
  5044.     {
  5045.       ceiling = BUFFER_FLOOR_OF (start_byte - 1);
  5046.       ceiling = max (limit_byte, ceiling);
  5047.       ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
  5048.       base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
  5049.       while (1)
  5050.         {
  5051.           if (selective_display)
  5052.         while (--cursor != ceiling_addr
  5053.                && *cursor != '\n' && *cursor != 015)
  5054.           ;
  5055.           else
  5056.         while (--cursor != ceiling_addr && *cursor != '\n')
  5057.           ;
  5058.  
  5059.           if (cursor != ceiling_addr)
  5060.         {
  5061.           if (++count == 0)
  5062.             {
  5063.               start_byte += cursor - base + 1;
  5064.               *byte_pos_ptr = start_byte;
  5065.               /* When scanning backwards, we should
  5066.              not count the newline posterior to which we stop.  */
  5067.               return - orig_count - 1;
  5068.             }
  5069.         }
  5070.           else
  5071.         break;
  5072.         }
  5073.       /* Here we add 1 to compensate for the last decrement
  5074.          of CURSOR, which took it past the valid range.  */
  5075.       start_byte += cursor - base + 1;
  5076.     }
  5077.     }
  5078.  
  5079.   *byte_pos_ptr = limit_byte;
  5080.  
  5081.   if (count < 0)
  5082.     return - orig_count + count;
  5083.   return orig_count - count;
  5084.  
  5085. }
  5086.  
  5087. /* Display STRING on one line of window W, starting at HPOS.
  5088.    Display at position VPOS.  Caller should have done get_display_line.
  5089.    If VPOS == -1, display it as the current frame's title.
  5090.    LENGTH is the length of STRING, or -1 meaning STRING is null-terminated.
  5091.  
  5092.   TRUNCATE is GLYPH to display at end if truncated.  Zero for none.
  5093.  
  5094.   MINCOL is the first column ok to end at.  (Pad with spaces to this col.)
  5095.   MAXCOL is the last column ok to end at.  Truncate here.
  5096.     -1 for MINCOL or MAXCOL means no explicit minimum or maximum.
  5097.   Both count from the left edge of the frame, as does HPOS.
  5098.   The right edge of W is an implicit maximum.
  5099.   If TRUNCATE is nonzero, the implicit maximum is one column before the edge.
  5100.  
  5101.   OBEY_WINDOW_WIDTH says to put spaces or vertical bars
  5102.   at the place where the current window ends in this line
  5103.   and not display anything beyond there.  Otherwise, only MAXCOL
  5104.   controls where to stop output.
  5105.  
  5106.   MULTIBYTE can be 0 meaning do not display multibyte chars,
  5107.   1 meaning do display them, or -1 meaning obey the current buffer's
  5108.   value of enable_multibyte_characters.
  5109.  
  5110.   Returns ending hpos.  */
  5111.  
  5112. static int
  5113. display_string (w, vpos, string, length, hpos, truncate,
  5114.         obey_window_width, mincol, maxcol, multibyte)
  5115.      struct window *w;
  5116.      unsigned char *string;
  5117.      int length;
  5118.      int vpos, hpos;
  5119.      GLYPH truncate;
  5120.      int obey_window_width;
  5121.      int mincol, maxcol;
  5122.      int multibyte;
  5123. {
  5124.   register int c;
  5125.   int truncated;
  5126.   register GLYPH *p1;
  5127.   int hscroll = XINT (w->hscroll);
  5128.   int tab_width = XINT (XBUFFER (w->buffer)->tab_width);
  5129.   register GLYPH *start;
  5130.   register GLYPH *end;
  5131.   FRAME_PTR f = XFRAME (WINDOW_FRAME (w));
  5132.   struct frame_glyphs *desired_glyphs = FRAME_DESIRED_GLYPHS (f);
  5133.   GLYPH *p1start = desired_glyphs->glyphs[vpos] + hpos;
  5134.   int window_width = XFASTINT (w->width);
  5135.  
  5136.   /* Use the standard display table, not the window's display table.
  5137.      We don't want the mode line in rot13.  */
  5138.   register struct Lisp_Char_Table *dp = 0;
  5139.   int i;
  5140.  
  5141.   if (multibyte == -1)
  5142.     multibyte = !NILP (current_buffer->enable_multibyte_characters);
  5143.   /* Now multibyte is 1 if we should display multibyte characters.  */
  5144.  
  5145.   if (DISP_TABLE_P (Vstandard_display_table))
  5146.     dp = XCHAR_TABLE (Vstandard_display_table);
  5147.  
  5148.   if (tab_width <= 0 || tab_width > 1000) tab_width = 8;
  5149.  
  5150.   p1 = p1start;
  5151.   start = desired_glyphs->glyphs[vpos];
  5152.  
  5153.   if (obey_window_width)
  5154.     {
  5155.       start += XFASTINT (w->left);
  5156.       end = start + window_width - (truncate != 0);
  5157.  
  5158.       if (!WINDOW_RIGHTMOST_P (w))
  5159.     {
  5160.       if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
  5161.         {
  5162.           int i;
  5163.  
  5164.           for (i = 0; i < FRAME_SCROLL_BAR_COLS (f); i++)
  5165.         *end-- = ' ';
  5166.         }
  5167.       else if (!FRAME_HAS_VERTICAL_SCROLL_BARS (f))
  5168.         *end-- = '|';
  5169.     }
  5170.     }
  5171.  
  5172.   if (! obey_window_width
  5173.       || (maxcol >= 0 && end - desired_glyphs->glyphs[vpos] > maxcol))
  5174.     end = desired_glyphs->glyphs[vpos] + maxcol;
  5175.  
  5176.   /* Store 0 in charstart for these columns.  */
  5177.   for (i = (hpos >= 0 ? hpos : 0); i < end - p1start + hpos; i++)
  5178.     desired_glyphs->charstarts[vpos][i] = 0;
  5179.  
  5180.   if (maxcol >= 0 && mincol > maxcol)
  5181.     mincol = maxcol;
  5182.  
  5183.   if (length < 0)
  5184.     /* We need this value for multibyte characters.  */
  5185.     length = strlen (string);
  5186.  
  5187.   /* We set truncated to 1 if we get stopped by trying to pass END
  5188.      (that is, trying to pass MAXCOL.)  */
  5189.   truncated = 0;
  5190.   while (1)
  5191.     {
  5192.       int len;
  5193.  
  5194.       if (length <= 0)
  5195.     break;
  5196.       if (multibyte)
  5197.     c = STRING_CHAR_AND_LENGTH (string, length, len);
  5198.       else
  5199.     c = *string, len = 1;
  5200.  
  5201.       string += len, length -= len;
  5202.  
  5203.       if (p1 >= end)
  5204.     {
  5205.       truncated = 1;
  5206.       break;
  5207.     }
  5208.  
  5209.       if (dp != 0 && VECTORP (DISP_CHAR_VECTOR (dp, c)))
  5210.     {
  5211.       p1 = copy_part_of_rope (f, p1, start,
  5212.                   XVECTOR (DISP_CHAR_VECTOR (dp, c))->contents,
  5213.                   XVECTOR (DISP_CHAR_VECTOR (dp, c))->size,
  5214.                   0);
  5215.     }
  5216.       else if (c >= 040 && c < 0177)
  5217.     {
  5218.       if (p1 >= start)
  5219.         *p1 = c;
  5220.       p1++;
  5221.     }
  5222.       else if (c == '\t')
  5223.     {
  5224.       do
  5225.         {
  5226.           if (p1 >= start && p1 < end)
  5227.         *p1 = SPACEGLYPH;
  5228.           p1++;
  5229.         }
  5230.       while ((p1 - start + hscroll - (hscroll > 0)) % tab_width);
  5231.     }
  5232.       else if (c < 0200 && ! NILP (buffer_defaults.ctl_arrow))
  5233.     {
  5234.       if (p1 >= start)
  5235.         *p1 = (fix_glyph
  5236.            (f, (dp && INTEGERP (DISP_CTRL_GLYPH (dp))
  5237.             && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (dp)))
  5238.             ? XINT (DISP_CTRL_GLYPH (dp)) : '^'),
  5239.             0));
  5240.       p1++;
  5241.       if (p1 >= start && p1 < end)
  5242.         *p1 = c ^ 0100;
  5243.       p1++;
  5244.     }
  5245.       else if (len == 1)
  5246.     {
  5247.       /* C is a control character or a binary byte data.  */
  5248.       if (p1 >= start)
  5249.         *p1 = (fix_glyph
  5250.            (f, (dp && INTEGERP (DISP_ESCAPE_GLYPH (dp))
  5251.             && GLYPH_CHAR_VALID_P (XINT (DISP_ESCAPE_GLYPH (dp)))
  5252.             ? XINT (DISP_ESCAPE_GLYPH (dp)) : '\\'),
  5253.             0));
  5254.       p1++;
  5255.       if (p1 >= start && p1 < end)
  5256.         *p1 = (c >> 6) + '0';
  5257.       p1++;
  5258.       if (p1 >= start && p1 < end)
  5259.         *p1 = (7 & (c >> 3)) + '0';
  5260.       p1++;
  5261.       if (p1 >= start && p1 < end)
  5262.         *p1 = (7 & c) + '0';
  5263.       p1++;
  5264.     }
  5265.       else
  5266.     {
  5267.       /* C is a multibyte character.  */      
  5268.       int charset = CHAR_CHARSET (c);
  5269.       int columns = (charset == CHARSET_COMPOSITION
  5270.              ? cmpchar_table[COMPOSITE_CHAR_ID (c)]->width
  5271.              : CHARSET_WIDTH (charset));
  5272.  
  5273.       if (p1 < start)
  5274.         {
  5275.           /* Since we can't show the left part of C, fill all
  5276.                  columns with spaces.  */
  5277.           columns -= start - p1;
  5278.           p1 = start;
  5279.           while (columns--)
  5280.         {
  5281.           if (p1 < end)
  5282.             *p1 = SPACEGLYPH;
  5283.           p1++;
  5284.         }
  5285.         }
  5286.       else if (p1 + columns > end)
  5287.         {
  5288.           /* Since we can't show the right part of C, fill all
  5289.                  columns with TRUNCATE if TRUNCATE is specified.  */
  5290.           if (truncate)
  5291.         {
  5292.           while (p1 < end)
  5293.             *p1++ = fix_glyph (f, truncate, 0);
  5294.           /* And tell the line is truncated.  */
  5295.           truncated = 1;
  5296.         }
  5297.           break;
  5298.         }
  5299.       else
  5300.         {
  5301.           /* We can show the whole glyph of C.  */
  5302.           *p1++ = c;
  5303.           while (--columns)
  5304.         *p1++ = c | GLYPH_MASK_PADDING;
  5305.         }
  5306.     }
  5307.     }
  5308.  
  5309.   if (truncated)
  5310.     {
  5311.       p1 = end;
  5312.       if (truncate) *p1++ = fix_glyph (f, truncate, 0);
  5313.     }
  5314.   else if (mincol >= 0)
  5315.     {
  5316.       end = desired_glyphs->glyphs[vpos] + mincol;
  5317.       while (p1 < end)
  5318.     *p1++ = SPACEGLYPH;
  5319.     }
  5320.  
  5321.   {
  5322.     register int len = p1 - desired_glyphs->glyphs[vpos];
  5323.  
  5324.     if (len > desired_glyphs->used[vpos])
  5325.       desired_glyphs->used[vpos] = len;
  5326.     desired_glyphs->glyphs[vpos][desired_glyphs->used[vpos]] = 0;
  5327.  
  5328.     return len;
  5329.   }
  5330. }
  5331.  
  5332. /* This is like a combination of memq and assq.
  5333.    Return 1 if PROPVAL appears as an element of LIST
  5334.    or as the car of an element of LIST.
  5335.    If PROPVAL is a list, compare each element against LIST
  5336.    in that way, and return 1 if any element of PROPVAL is found in LIST.
  5337.    Otherwise return 0.
  5338.    This function cannot quit.  */
  5339.  
  5340. int
  5341. invisible_p (propval, list)
  5342.      register Lisp_Object propval;
  5343.      Lisp_Object list;
  5344. {
  5345.   register Lisp_Object tail, proptail;
  5346.   for (tail = list; CONSP (tail); tail = XCONS (tail)->cdr)
  5347.     {
  5348.       register Lisp_Object tem;
  5349.       tem = XCONS (tail)->car;
  5350.       if (EQ (propval, tem))
  5351.     return 1;
  5352.       if (CONSP (tem) && EQ (propval, XCONS (tem)->car))
  5353.     return 1;
  5354.     }
  5355.   if (CONSP (propval))
  5356.     for (proptail = propval; CONSP (proptail);
  5357.      proptail = XCONS (proptail)->cdr)
  5358.       {
  5359.     Lisp_Object propelt;
  5360.     propelt = XCONS (proptail)->car;
  5361.     for (tail = list; CONSP (tail); tail = XCONS (tail)->cdr)
  5362.       {
  5363.         register Lisp_Object tem;
  5364.         tem = XCONS (tail)->car;
  5365.         if (EQ (propelt, tem))
  5366.           return 1;
  5367.         if (CONSP (tem) && EQ (propelt, XCONS (tem)->car))
  5368.           return 1;
  5369.       }
  5370.       }
  5371.   return 0;
  5372. }
  5373.  
  5374. /* Return 1 if PROPVAL appears as the car of an element of LIST
  5375.    and the cdr of that element is non-nil.
  5376.    If PROPVAL is a list, check each element of PROPVAL in that way,
  5377.    and the first time some element is found,
  5378.    return 1 if the cdr of that element is non-nil.
  5379.    Otherwise return 0.
  5380.    This function cannot quit.  */
  5381.  
  5382. int
  5383. invisible_ellipsis_p (propval, list)
  5384.      register Lisp_Object propval;
  5385.      Lisp_Object list;
  5386. {
  5387.   register Lisp_Object tail, proptail;
  5388.   for (tail = list; CONSP (tail); tail = XCONS (tail)->cdr)
  5389.     {
  5390.       register Lisp_Object tem;
  5391.       tem = XCONS (tail)->car;
  5392.       if (CONSP (tem) && EQ (propval, XCONS (tem)->car))
  5393.     return ! NILP (XCONS (tem)->cdr);
  5394.     }
  5395.   if (CONSP (propval))
  5396.     for (proptail = propval; CONSP (proptail);
  5397.      proptail = XCONS (proptail)->cdr)
  5398.       {
  5399.     Lisp_Object propelt;
  5400.     propelt = XCONS (proptail)->car;
  5401.     for (tail = list; CONSP (tail); tail = XCONS (tail)->cdr)
  5402.       {
  5403.         register Lisp_Object tem;
  5404.         tem = XCONS (tail)->car;
  5405.         if (CONSP (tem) && EQ (propelt, XCONS (tem)->car))
  5406.           return ! NILP (XCONS (tem)->cdr);
  5407.       }
  5408.       }
  5409.   return 0;
  5410. }
  5411.  
  5412. void
  5413. syms_of_xdisp ()
  5414. {
  5415.   staticpro (&Qinhibit_redisplay);
  5416.   Qinhibit_redisplay = intern ("inhibit-redisplay");
  5417.  
  5418.   staticpro (&Qmenu_bar_update_hook);
  5419.   Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
  5420.  
  5421.   staticpro (&Qoverriding_terminal_local_map);
  5422.   Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
  5423.  
  5424.   staticpro (&Qoverriding_local_map);
  5425.   Qoverriding_local_map = intern ("overriding-local-map");
  5426.  
  5427.   staticpro (&Qwindow_scroll_functions);
  5428.   Qwindow_scroll_functions = intern ("window-scroll-functions");
  5429.  
  5430.   staticpro (&Qredisplay_end_trigger_functions);
  5431.   Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
  5432.  
  5433.   staticpro (&Qinhibit_point_motion_hooks);
  5434.   Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
  5435.  
  5436.   staticpro (&last_arrow_position);
  5437.   staticpro (&last_arrow_string);
  5438.   last_arrow_position = Qnil;
  5439.   last_arrow_string = Qnil;
  5440.  
  5441.   DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
  5442.     "Non-nil means don't actually do any redisplay.\n\
  5443. This is used for internal purposes.");
  5444.   Vinhibit_redisplay = Qnil;
  5445.  
  5446.   DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
  5447.     "String (or mode line construct) included (normally) in `mode-line-format'.");
  5448.   Vglobal_mode_string = Qnil;
  5449.  
  5450.   DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
  5451.     "Marker for where to display an arrow on top of the buffer text.\n\
  5452. This must be the beginning of a line in order to work.\n\
  5453. See also `overlay-arrow-string'.");
  5454.   Voverlay_arrow_position = Qnil;
  5455.  
  5456.   DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
  5457.     "String to display as an arrow.  See also `overlay-arrow-position'.");
  5458.   Voverlay_arrow_string = Qnil;
  5459.  
  5460.   DEFVAR_INT ("scroll-step", &scroll_step,
  5461.     "*The number of lines to try scrolling a window by when point moves out.\n\
  5462. If that fails to bring point back on frame, point is centered instead.\n\
  5463. If this is zero, point is always centered after it moves off frame.");
  5464.  
  5465.   DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
  5466.     "*Scroll up to this many lines, to bring point back on screen.");
  5467.   scroll_conservatively = 0;
  5468.  
  5469.   DEFVAR_INT ("scroll-margin", &scroll_margin,
  5470.     "*Number of lines of margin at the top and bottom of a window.\n\
  5471. Recenter the window whenever point gets within this many lines\n\
  5472. of the top or bottom of the window.");
  5473.   scroll_margin = 0;
  5474.  
  5475.   DEFVAR_INT ("debug-end-pos", &debug_end_pos, "Don't ask");
  5476.  
  5477.   DEFVAR_BOOL ("truncate-partial-width-windows",
  5478.            &truncate_partial_width_windows,
  5479.     "*Non-nil means truncate lines in all windows less than full frame wide.");
  5480.   truncate_partial_width_windows = 1;
  5481.  
  5482.   DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
  5483.     "*Non-nil means use inverse video for the mode line.");
  5484.   mode_line_inverse_video = 1;
  5485.  
  5486.   DEFVAR_INT ("line-number-display-limit", &line_number_display_limit,
  5487.     "*Maximum buffer size (in characters) for line number display\n\
  5488. If the buffer is bigger than this, the line number does not appear\n\
  5489. in the mode line..");
  5490.   line_number_display_limit = 1000000;
  5491.  
  5492.   DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
  5493.     "*Non-nil means highlight region even in nonselected windows.");
  5494.   highlight_nonselected_windows = 0;
  5495.  
  5496.   DEFVAR_BOOL ("multiple-frames", &multiple_frames,
  5497.     "Non-nil if more than one frame is visible on this display.\n\
  5498. Minibuffer-only frames don't count, but iconified frames do.\n\
  5499. This variable is not guaranteed to be accurate except while processing\n\
  5500. `frame-title-format' and `icon-title-format'.");
  5501.  
  5502.   DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
  5503.     "Template for displaying the titlebar of visible frames.\n\
  5504. \(Assuming the window manager supports this feature.)\n\
  5505. This variable has the same structure as `mode-line-format' (which see),\n\
  5506. and is used only on frames for which no explicit name has been set\n\
  5507. \(see `modify-frame-parameters').");
  5508.   DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
  5509.     "Template for displaying the titlebar of an iconified frame.\n\
  5510. \(Assuming the window manager supports this feature.)\n\
  5511. This variable has the same structure as `mode-line-format' (which see),\n\
  5512. and is used only on frames for which no explicit name has been set\n\
  5513. \(see `modify-frame-parameters').");
  5514.   Vicon_title_format
  5515.     = Vframe_title_format
  5516.     = Fcons (intern ("multiple-frames"),
  5517.          Fcons (build_string ("%b"),
  5518.             Fcons (Fcons (build_string (""),
  5519.                   Fcons (intern ("invocation-name"),
  5520.                      Fcons (build_string ("@"),
  5521.                         Fcons (intern ("system-name"),
  5522.                                    Qnil)))),
  5523.                Qnil)));
  5524.  
  5525.   DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
  5526.     "Maximum number of lines to keep in the message log buffer.\n\
  5527. If nil, disable message logging.  If t, log messages but don't truncate\n\
  5528. the buffer when it becomes large.");
  5529.   XSETFASTINT (Vmessage_log_max, 50);
  5530.  
  5531.   DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
  5532.     "Functions called before redisplay, if window sizes have changed.\n\
  5533. The value should be a list of functions that take one argument.\n\
  5534. Just before redisplay, for each frame, if any of its windows have changed\n\
  5535. size since the last redisplay, or have been split or deleted,\n\
  5536. all the functions in the list are called, with the frame as argument.");
  5537.   Vwindow_size_change_functions = Qnil;
  5538.  
  5539.   DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
  5540.     "List of functions to call before redisplaying a window with scrolling.\n\
  5541. Each function is called with two arguments, the window\n\
  5542. and its new display-start position.  Note that the value of `window-end'\n\
  5543. is not valid when these functions are called.");
  5544.   Vwindow_scroll_functions = Qnil;
  5545.  
  5546.   DEFVAR_INT ("minibuffer-scroll-overlap", &minibuffer_scroll_overlap,
  5547.     "*Number of characters of overlap when scrolling a one-line window.\n\
  5548. This commonly affects the minibuffer window, hence the name of the variable.");
  5549.   minibuffer_scroll_overlap = 20;
  5550. }
  5551.  
  5552. /* initialize the window system */
  5553. void
  5554. init_xdisp ()
  5555. {
  5556.   Lisp_Object root_window;
  5557. #ifndef COMPILER_REGISTER_BUG
  5558.   register
  5559. #endif /* COMPILER_REGISTER_BUG */
  5560.     struct window *mini_w;
  5561.  
  5562.   this_line_bufpos = 0;
  5563.  
  5564.   mini_w = XWINDOW (minibuf_window);
  5565.   root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
  5566.  
  5567.   echo_area_glyphs = 0;
  5568.   previous_echo_glyphs = 0;
  5569.  
  5570.   if (!noninteractive)
  5571.     {
  5572.       FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
  5573.       XSETFASTINT (XWINDOW (root_window)->top, FRAME_MENU_BAR_LINES (f));
  5574.       set_window_height (root_window,
  5575.              FRAME_HEIGHT (f) - 1 - FRAME_MENU_BAR_LINES (f),
  5576.              0);
  5577.       XSETFASTINT (mini_w->top, FRAME_HEIGHT (f) - 1);
  5578.       set_window_height (minibuf_window, 1, 0);
  5579.  
  5580.       XSETFASTINT (XWINDOW (root_window)->width, FRAME_WIDTH (f));
  5581.       XSETFASTINT (mini_w->width, FRAME_WIDTH (f));
  5582.     }
  5583. }
  5584.