]> code.delx.au - gnu-emacs/blobdiff - src/indent.c
(set-auto-mode): Run multiple mode: specs in left-to-right order.
[gnu-emacs] / src / indent.c
index fa757f97676224a5a9efb2067c21eb7cdd1ed7a3..60641ae54dc57b0291f7685ba30bd1d70cff9020 100644 (file)
@@ -5,7 +5,7 @@ This file is part of GNU Emacs.
 
 GNU Emacs is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
-the Free Software Foundation; either version 1, or (at your option)
+the Free Software Foundation; either version 2, or (at your option)
 any later version.
 
 GNU Emacs is distributed in the hope that it will be useful,
@@ -15,7 +15,8 @@ GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with GNU Emacs; see the file COPYING.  If not, write to
-the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
+the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+Boston, MA 02111-1307, USA.  */
 
 
 #include <config.h>
@@ -49,19 +50,20 @@ int last_known_column_point;
 /* Value of MODIFF when current_column was called */
 int last_known_column_modified;
 
+static int current_column_1 ();
+
 /* Get the display table to use for the current buffer.  */
 
-struct Lisp_Vector *
+struct Lisp_Char_Table *
 buffer_display_table ()
 {
   Lisp_Object thisbuf;
 
   thisbuf = current_buffer->display_table;
-  if (VECTORP (thisbuf) && XVECTOR (thisbuf)->size == DISP_TABLE_SIZE)
-    return XVECTOR (thisbuf);
-  if (VECTORP (Vstandard_display_table)
-      && XVECTOR (Vstandard_display_table)->size == DISP_TABLE_SIZE)
-    return XVECTOR (Vstandard_display_table);
+  if (DISP_TABLE_P (thisbuf))
+    return XCHAR_TABLE (thisbuf);
+  if (DISP_TABLE_P (Vstandard_display_table))
+    return XCHAR_TABLE (Vstandard_display_table);
   return 0;
 }
 \f
@@ -72,7 +74,7 @@ buffer_display_table ()
 static int
 character_width (c, dp)
      int c;
-     struct Lisp_Vector *dp;
+     struct Lisp_Char_Table *dp;
 {
   Lisp_Object elt;
 
@@ -106,7 +108,7 @@ character_width (c, dp)
    invalidate the buffer's width_run_cache.  */
 int
 disptab_matches_widthtab (disptab, widthtab)
-     struct Lisp_Vector *disptab;
+     struct Lisp_Char_Table *disptab;
      struct Lisp_Vector *widthtab;
 {
   int i;
@@ -126,7 +128,7 @@ disptab_matches_widthtab (disptab, widthtab)
 void
 recompute_width_table (buf, disptab)
      struct buffer *buf;
-     struct Lisp_Vector *disptab;
+     struct Lisp_Char_Table *disptab;
 {
   int i;
   struct Lisp_Vector *widthtab;
@@ -167,6 +169,82 @@ width_run_cache_on_off ()
     }
 }
 
+\f
+/* Skip some invisible characters starting from POS.
+   This includes characters invisible because of text properties
+   and characters invisible because of overlays.
+
+   If position POS is followed by invisible characters,
+   skip some of them and return the position after them.
+   Otherwise return POS itself.
+
+   Set *NEXT_BOUNDARY_P to the next position at which
+   it will be necessary to call this function again.
+
+   Don't scan past TO, and don't set *NEXT_BOUNDARY_P
+   to a value greater than TO.
+
+   If WINDOW is non-nil, and this buffer is displayed in WINDOW,
+   take account of overlays that apply only in WINDOW.
+
+   We don't necessarily skip all the invisible characters after POS
+   because that could take a long time.  We skip a reasonable number
+   which can be skipped quickly.  If there might be more invisible
+   characters immediately following, then *NEXT_BOUNDARY_P
+   will equal the return value.  */
+
+static int
+skip_invisible (pos, next_boundary_p, to, window)
+     int pos;
+     int *next_boundary_p;
+     int to;
+     Lisp_Object window;
+{
+  Lisp_Object prop, position, end, overlay_limit, proplimit;
+  Lisp_Object buffer;
+
+  XSETFASTINT (position, pos);
+  XSETBUFFER (buffer, current_buffer);
+
+  /* Give faster response for overlay lookup near POS.  */
+  recenter_overlay_lists (current_buffer, pos);
+
+  /* We must not advance farther than the next overlay change.
+     The overlay change might change the invisible property;
+     or there might be overlay strings to be displayed there.  */
+  overlay_limit = Fnext_overlay_change (position);
+  /* As for text properties, this gives a lower bound
+     for where the invisible text property could change.  */
+  proplimit = Fnext_property_change (position, buffer, Qt);
+  if (XFASTINT (overlay_limit) < XFASTINT (proplimit))
+    proplimit = overlay_limit;
+  /* PROPLIMIT is now a lower bound for the next change
+     in invisible status.  If that is plenty far away,
+     use that lower bound.  */
+  if (XFASTINT (proplimit) > pos + 100 || XFASTINT (proplimit) >= to)
+    *next_boundary_p = XFASTINT (proplimit);
+  /* Otherwise, scan for the next `invisible' property change.  */
+  else
+    {
+      /* Don't scan terribly far.  */
+      XSETFASTINT (proplimit, min (pos + 100, to));
+      /* No matter what. don't go past next overlay change.  */
+      if (XFASTINT (overlay_limit) < XFASTINT (proplimit))
+       proplimit = overlay_limit;
+      end = Fnext_single_property_change (position, Qinvisible,
+                                         buffer, proplimit);
+      *next_boundary_p = XFASTINT (end);
+    }
+  /* if the `invisible' property is set, we can skip to
+     the next property change */
+  if (!NILP (window) && EQ (XWINDOW (window)->buffer, buffer))
+    prop = Fget_char_property (position, Qinvisible, window);
+  else
+    prop = Fget_char_property (position, Qinvisible, buffer);
+  if (TEXT_PROP_MEANS_INVISIBLE (prop))
+    return *next_boundary_p;
+  return pos;
+}
 \f
 DEFUN ("current-column", Fcurrent_column, Scurrent_column, 0, 0, 0,
   "Return the horizontal position of point.  Beginning of line is column 0.\n\
@@ -202,13 +280,23 @@ current_column ()
   register int c;
   register int tab_width = XINT (current_buffer->tab_width);
   int ctl_arrow = !NILP (current_buffer->ctl_arrow);
-  register struct Lisp_Vector *dp = buffer_display_table ();
+  register struct Lisp_Char_Table *dp = buffer_display_table ();
   int stopchar;
 
   if (point == last_known_column_point
       && MODIFF == last_known_column_modified)
     return last_known_column;
 
+  /* If the buffer has overlays or text properties,
+     use a more general algorithm.  */
+  if (BUF_INTERVALS (current_buffer)
+      || !NILP (current_buffer->overlays_before)
+      || !NILP (current_buffer->overlays_after))
+    return current_column_1 (point);
+
+  /* Scan backwards from point to the previous newline,
+     counting width.  Tab characters are the only complicated case.  */
+
   /* Make a pointer for decrementing through the chars before point.  */
   ptr = &FETCH_CHAR (point - 1) + 1;
   /* Make a pointer to where consecutive chars leave off,
@@ -274,12 +362,81 @@ current_column ()
   return col;
 }
 \f
+/* Return the column number of position POS
+   by scanning forward from the beginning of the line.
+   This function handles characters that are invisible
+   due to text properties or overlays.  */
+
+static int
+current_column_1 (pos)
+     int pos;
+{
+  register int tab_width = XINT (current_buffer->tab_width);
+  register int ctl_arrow = !NILP (current_buffer->ctl_arrow);
+  register struct Lisp_Char_Table *dp = buffer_display_table ();
+
+  /* Start the scan at the beginning of this line with column number 0.  */
+  register int col = 0;
+  int scan = find_next_newline (pos, -1);
+  int next_boundary = scan;
+
+  if (tab_width <= 0 || tab_width > 1000) tab_width = 8;
+
+  /* Scan forward to the target position.  */
+  while (scan < pos)
+    {
+      int c;
+
+      /* Occasionally we may need to skip invisible text.  */
+      while (scan == next_boundary)
+       {
+         /* This updates NEXT_BOUNDARY to the next place
+            where we might need to skip more invisible text.  */
+         scan = skip_invisible (scan, &next_boundary, pos, Qnil);
+         if (scan >= pos)
+           goto endloop;
+       }
+
+      c = FETCH_CHAR (scan);
+      if (dp != 0 && VECTORP (DISP_CHAR_VECTOR (dp, c)))
+       {
+         col += XVECTOR (DISP_CHAR_VECTOR (dp, c))->size;
+         scan++;
+         continue;
+       }
+      if (c == '\n')
+       break;
+      if (c == '\r' && EQ (current_buffer->selective_display, Qt))
+       break;
+      scan++;
+      if (c == '\t')
+       {
+         int prev_col = col;
+         col += tab_width;
+         col = col / tab_width * tab_width;
+       }
+      else if (ctl_arrow && (c < 040 || c == 0177))
+        col += 2;
+      else if (c < 040 || c >= 0177)
+        col += 4;
+      else
+       col++;
+    }
+ endloop:
+
+  last_known_column = col;
+  last_known_column_point = point;
+  last_known_column_modified = MODIFF;
+
+  return col;
+}
+\f
 /* Return the width in columns of the part of STRING from BEG to END.
    If BEG is nil, that stands for the beginning of STRING.
    If END is nil, that stands for the end of STRING.  */
 
 static int
-string_width (string, beg, end)
+string_display_width (string, beg, end)
      Lisp_Object string, beg, end;
 {
   register int col;
@@ -289,7 +446,7 @@ string_width (string, beg, end)
   register int c;
   register int tab_width = XINT (current_buffer->tab_width);
   int ctl_arrow = !NILP (current_buffer->ctl_arrow);
-  register struct Lisp_Vector *dp = buffer_display_table ();
+  register struct Lisp_Char_Table *dp = buffer_display_table ();
   int b, e;
 
   if (NILP (end))
@@ -354,23 +511,23 @@ string_width (string, beg, end)
 \f
 DEFUN ("indent-to", Findent_to, Sindent_to, 1, 2, "NIndent to column: ",
   "Indent from point with tabs and spaces until COLUMN is reached.\n\
-Optional second argument MIN says always do at least MIN spaces\n\
-even if that goes past COLUMN; by default, MIN is zero.")
-  (col, minimum)
-     Lisp_Object col, minimum;
+Optional second argument MININUM says always do at least MININUM spaces\n\
+even if that goes past COLUMN; by default, MININUM is zero.")
+  (column, minimum)
+     Lisp_Object column, minimum;
 {
   int mincol;
   register int fromcol;
   register int tab_width = XINT (current_buffer->tab_width);
 
-  CHECK_NUMBER (col, 0);
+  CHECK_NUMBER (column, 0);
   if (NILP (minimum))
     XSETFASTINT (minimum, 0);
   CHECK_NUMBER (minimum, 1);
 
   fromcol = current_column ();
   mincol = fromcol + XINT (minimum);
-  if (mincol < XINT (col)) mincol = XINT (col);
+  if (mincol < XINT (column)) mincol = XINT (column);
 
   if (fromcol == mincol)
     return make_number (mincol);
@@ -389,15 +546,15 @@ even if that goes past COLUMN; by default, MIN is zero.")
        }
     }
 
-  XSETFASTINT (col, mincol - fromcol);
-  Finsert_char (make_number (' '), col, Qt);
+  XSETFASTINT (column, mincol - fromcol);
+  Finsert_char (make_number (' '), column, Qt);
 
   last_known_column = mincol;
   last_known_column_point = point;
   last_known_column_modified = MODIFF;
 
-  XSETINT (col, mincol);
-  return col;
+  XSETINT (column, mincol);
+  return column;
 }
 
 \f
@@ -421,20 +578,45 @@ position_indentation (pos)
   register int tab_width = XINT (current_buffer->tab_width);
   register unsigned char *p;
   register unsigned char *stop;
+  unsigned char *start;
+  int next_boundary = pos;
+  int ceiling = pos;
 
   if (tab_width <= 0 || tab_width > 1000) tab_width = 8;
 
-  stop = &FETCH_CHAR (BUFFER_CEILING_OF (pos)) + 1;
   p = &FETCH_CHAR (pos);
+  /* STOP records the value of P at which we will need
+     to think about the gap, or about invisible text,
+     or about the end of the buffer.  */
+  stop = p;
+  /* START records the starting value of P.  */
+  start = p;
   while (1)
     {
       while (p == stop)
        {
+         int stop_pos;
+
+         /* If we have updated P, set POS to match.
+            The first time we enter the loop, POS is already right.  */
+         if (p != start)
+           pos = PTR_CHAR_POS (p);
+         /* Consider the various reasons STOP might have been set here.  */
          if (pos == ZV)
            return column;
-         pos += p - &FETCH_CHAR (pos);
+         if (pos == next_boundary)
+           pos = skip_invisible (pos, &next_boundary, ZV, Qnil);
+         if (pos >= ceiling)
+           ceiling = BUFFER_CEILING_OF (pos) + 1;
+         /* Compute the next place we need to stop and think,
+            and set STOP accordingly.  */
+         stop_pos = min (ceiling, next_boundary);
+         /* The -1 and +1 arrange to point at the first byte of gap
+            (if STOP_POS is the position of the gap)
+            rather than at the data after the gap.  */
+            
+         stop = &FETCH_CHAR (stop_pos - 1) + 1;
          p = &FETCH_CHAR (pos);
-         stop = &FETCH_CHAR (BUFFER_CEILING_OF (pos)) + 1;
        }
       switch (*p++)
        {
@@ -461,9 +643,8 @@ indented_beyond_p (pos, column)
     pos = find_next_newline_no_quit (pos - 1, -1);
   return (position_indentation (pos) >= column);
 }
-
 \f
-DEFUN ("move-to-column", Fmove_to_column, Smove_to_column, 1, 2, 0,
+DEFUN ("move-to-column", Fmove_to_column, Smove_to_column, 1, 2, "p",
   "Move point to column COLUMN in the current line.\n\
 The column of a character is calculated by adding together the widths\n\
 as displayed of the previous characters in the line.\n\
@@ -475,7 +656,9 @@ If specified column is within a character, point goes after that character.\n\
 If it's past end of line, point goes to end of line.\n\n\
 A non-nil second (optional) argument FORCE means, if the line\n\
 is too short to reach column COLUMN then add spaces/tabs to get there,\n\
-and if COLUMN is in the middle of a tab character, change it to spaces.")
+and if COLUMN is in the middle of a tab character, change it to spaces.\n\
+\n\
+The return value is the current column.")
   (column, force)
      Lisp_Object column, force;
 {
@@ -485,36 +668,46 @@ and if COLUMN is in the middle of a tab character, change it to spaces.")
   register int end;
   register int tab_width = XINT (current_buffer->tab_width);
   register int ctl_arrow = !NILP (current_buffer->ctl_arrow);
-  register struct Lisp_Vector *dp = buffer_display_table ();
+  register struct Lisp_Char_Table *dp = buffer_display_table ();
 
   Lisp_Object val;
   int prev_col;
   int c;
 
+  int next_boundary;
+
   if (tab_width <= 0 || tab_width > 1000) tab_width = 8;
   CHECK_NATNUM (column, 0);
   goal = XINT (column);
 
- retry:
   pos = point;
   end = ZV;
+  next_boundary = pos;
 
   /* If we're starting past the desired column,
      back up to beginning of line and scan from there.  */
   if (col > goal)
     {
+      end = pos;
       pos = find_next_newline (pos, -1);
       col = 0;
     }
 
   while (col < goal && pos < end)
     {
+      while (pos == next_boundary)
+       {
+         pos = skip_invisible (pos, &next_boundary, end, Qnil);
+         if (pos >= end)
+           goto endloop;
+       }
+
       c = FETCH_CHAR (pos);
       if (dp != 0 && VECTORP (DISP_CHAR_VECTOR (dp, c)))
        {
          col += XVECTOR (DISP_CHAR_VECTOR (dp, c))->size;
          pos++;
-         break;
+         continue;
        }
       if (c == '\n')
        break;
@@ -534,6 +727,7 @@ and if COLUMN is in the middle of a tab character, change it to spaces.")
       else
        col++;
     }
+ endloop:
 
   SET_PT (pos);
 
@@ -563,7 +757,6 @@ and if COLUMN is in the middle of a tab character, change it to spaces.")
   XSETFASTINT (val, col);
   return val;
 }
-
 \f
 /* compute_motion: compute buffer posn given screen posn and vice versa */
 
@@ -576,6 +769,12 @@ struct position val_compute_motion;
    can't hit the requested column exactly (because of a tab or other
    multi-column character), overshoot.
 
+   DID_MOTION is 1 if FROMHPOS has already accounted for overlay strings
+   at FROM.  This is the case if FROMVPOS and FROMVPOS came from an
+   earlier call to compute_motion.  The other common case is that FROMHPOS
+   is zero and FROM is a position that "belongs" at column zero, but might
+   be shifted by overlay strings; in this case DID_MOTION should be 0.
+
    WIDTH is the number of columns available to display text;
    compute_motion uses this to handle continuation lines and such.
    HSCROLL is the number of columns not being displayed at the left
@@ -629,8 +828,9 @@ struct position val_compute_motion;
    the scroll bars if they are turned on.  */
 
 struct position *
-compute_motion (from, fromvpos, fromhpos, to, tovpos, tohpos, width, hscroll, tab_offset, win)
+compute_motion (from, fromvpos, fromhpos, did_motion, to, tovpos, tohpos, width, hscroll, tab_offset, win)
      int from, fromvpos, fromhpos, to, tovpos, tohpos;
+     int did_motion;
      register int width;
      int hscroll, tab_offset;
      struct window *win;
@@ -642,7 +842,7 @@ compute_motion (from, fromvpos, fromhpos, to, tovpos, tohpos, width, hscroll, ta
   register int c;
   register int tab_width = XFASTINT (current_buffer->tab_width);
   register int ctl_arrow = !NILP (current_buffer->ctl_arrow);
-  register struct Lisp_Vector *dp = window_display_table (win);
+  register struct Lisp_Char_Table *dp = window_display_table (win);
   int selective
     = (INTEGERP (current_buffer->selective_display)
        ? XINT (current_buffer->selective_display)
@@ -651,11 +851,9 @@ compute_motion (from, fromvpos, fromhpos, to, tovpos, tohpos, width, hscroll, ta
   int selective_rlen
     = (selective && dp && VECTORP (DISP_INVIS_VECTOR (dp))
        ? XVECTOR (DISP_INVIS_VECTOR (dp))->size : 0);
-#ifdef USE_TEXT_PROPERTIES
-  /* The next location where the `invisible' property changes */
-  int next_invisible = from;
-  Lisp_Object prop, position;
-#endif
+  /* The next location where the `invisible' property changes, or an
+     overlay starts or ends.  */
+  int next_boundary = from;
 
   /* For computing runs of characters with similar widths.
      Invariant: width_run_width is zero, or all the characters
@@ -669,8 +867,10 @@ compute_motion (from, fromvpos, fromhpos, to, tovpos, tohpos, width, hscroll, ta
 
   /* The next buffer pos where we should consult the width run cache. */
   int next_width_run = from;
+  Lisp_Object window;
 
   XSETBUFFER (buffer, current_buffer);
+  XSETWINDOW (window, win);
 
   width_run_cache_on_off ();
   if (dp == buffer_display_table ())
@@ -683,68 +883,70 @@ compute_motion (from, fromvpos, fromhpos, to, tovpos, tohpos, width, hscroll, ta
     width_table = 0;
 
   if (tab_width <= 0 || tab_width > 1000) tab_width = 8;
-  for (pos = from; pos < to; )
+
+  pos = from;
+  while (1)
     {
-      /* Stop if past the target screen position.  */
-      if (vpos > tovpos
-         || (vpos == tovpos && hpos >= tohpos))
-       break;
+      while (pos == next_boundary)
+       {
+         /* If the caller says that the screen position came from an earlier
+            call to compute_motion, then we've already accounted for the
+            overlay strings at point.  This is only true the first time
+            through, so clear the flag after testing it.  */
+         if (!did_motion)
+           /* We need to skip past the overlay strings.  Currently those
+              strings must contain single-column printing characters;
+              if we want to relax that restriction, something will have
+              to be changed here.  */
+           hpos += overlay_strings (pos, win, (char **)0);
+         did_motion = 0;
+
+         if (pos >= to)
+           break;
 
-      prev_vpos = vpos;
-      prev_hpos = hpos;
+         /* Advance POS past invisible characters
+            (but not necessarily all that there are here),
+            and store in next_boundary the next position where
+            we need to call skip_invisible.  */
+         pos = skip_invisible (pos, &next_boundary, to, window);
+       }
 
-#ifdef USE_TEXT_PROPERTIES
-      /* if the `invisible' property is set, we can skip to
-        the next property change */
-      while (pos == next_invisible && pos < to)
+      /* Handle right margin.  */
+      if (hpos >= width
+         && (hpos > width
+             || (pos < ZV && FETCH_CHAR (pos) != '\n')))
        {
-         XSETFASTINT (position, pos);
-
-         /* Give faster response for overlay lookup near POS.  */
-         recenter_overlay_lists (current_buffer, pos);
-
-         prop = Fget_char_property (position,
-                                    Qinvisible,
-                                    Fcurrent_buffer ());
-         {
-           Lisp_Object end, limit, proplimit;
-
-           /* We must not advance farther than the next overlay change.
-              The overlay change might change the invisible property;
-              we have no way of telling.  */
-           limit = Fnext_overlay_change (position);
-           /* As for text properties, this gives a lower bound
-              for where the invisible text property could change.  */
-           proplimit = Fnext_property_change (position, buffer, Qt);
-           if (XFASTINT (limit) < XFASTINT (proplimit))
-             proplimit = limit;
-           /* PROPLIMIT is now a lower bound for the next change
-              in invisible status.  If that is plenty far away,
-              use that lower bound.  */
-           if (XFASTINT (proplimit) > pos + 100 || XFASTINT (proplimit) >= to)
-             next_invisible = XINT (proplimit);
-           /* Otherwise, scan for the next `invisible' property change.  */
-           else
-             {
-               /* Don't scan terribly far.  */
-               XSETFASTINT (proplimit, min (pos + 100, to));
-               /* No matter what. don't go past next overlay change.  */
-               if (XFASTINT (limit) < XFASTINT (proplimit))
-                 proplimit = limit;
-               end = Fnext_single_property_change (position, Qinvisible,
-                                                   buffer, proplimit);
-               if (INTEGERP (end) && XINT (end) < to)
-                 next_invisible = XINT (end);
-               else
-                 next_invisible = to;
-             }
-           if (TEXT_PROP_MEANS_INVISIBLE (prop))
-             pos = next_invisible;
-         }
+         if (hscroll
+             || (truncate_partial_width_windows
+                 && width + 1 < FRAME_WIDTH (XFRAME (WINDOW_FRAME (win))))
+             || !NILP (current_buffer->truncate_lines))
+           {
+             /* Truncating: skip to newline.  */
+             pos = find_before_next_newline (pos, to, 1);
+             hpos = width;
+             /* If we just skipped next_boundary,
+                loop around in the main while
+                and handle it.  */
+             if (pos >= next_boundary)
+               next_boundary = pos + 1;
+           }
+         else
+           {
+             /* Continuing.  */
+             vpos += hpos / width;
+             tab_offset += hpos - hpos % width;
+             hpos %= width;
+           }
        }
+
+      /* Stop if past the target buffer position or screen position.  */
       if (pos >= to)
        break;
-#endif
+      if (vpos > tovpos || (vpos == tovpos && hpos >= tohpos))
+       break;
+
+      prev_vpos = vpos;
+      prev_hpos = hpos;
 
       /* Consult the width run cache to see if we can avoid inspecting
          the text character-by-character.  */
@@ -796,128 +998,102 @@ compute_motion (from, fromvpos, fromhpos, to, tovpos, tohpos, width, hscroll, ta
 
       /* We have to scan the text character-by-character.  */
       else
-        {
-          c = FETCH_CHAR (pos);
-          pos++;
-
-          /* Perhaps add some info to the width_run_cache.  */
-          if (current_buffer->width_run_cache)
-            {
-              /* Is this character part of the current run?  If so, extend
-                 the run.  */
-              if (pos - 1 == width_run_end
-                  && width_table[c] == width_run_width)
-                width_run_end = pos;
-
-              /* The previous run is over, since this is a character at a
-                 different position, or a different width.  */
-              else
-                {
-                  /* Have we accumulated a run to put in the cache?
-                     (Currently, we only cache runs of width == 1).  */
-                  if (width_run_start < width_run_end
-                      && width_run_width == 1)
-                    know_region_cache (current_buffer,
-                                       current_buffer->width_run_cache,
-                                       width_run_start, width_run_end);
-
-                  /* Start recording a new width run.  */
-                  width_run_width = width_table[c];
-                  width_run_start = pos - 1;
-                  width_run_end = pos;
-                }
-            }
+       {
+         c = FETCH_CHAR (pos);
+         pos++;
 
-          if (dp != 0 && VECTORP (DISP_CHAR_VECTOR (dp, c)))
-            hpos += XVECTOR (DISP_CHAR_VECTOR (dp, c))->size;
-          else if (c >= 040 && c < 0177)
-            hpos++;
-          else if (c == '\t')
-            {
-              hpos += tab_width - ((hpos + tab_offset + hscroll - (hscroll > 0)
-                                    /* Add tab_width here to make sure
-                                       positive.  hpos can be negative
-                                       after continuation but can't be
-                                       less than -tab_width.  */
-                                    + tab_width)
-                                   % tab_width);
-            }
-          else if (c == '\n')
-            {
-              if (selective > 0 && indented_beyond_p (pos, selective))
-                {
-                  /* Skip any number of invisible lines all at once */
-                  do
-                    pos = find_before_next_newline (pos, to, 1) + 1;
-                  while (pos < to
-                         && indented_beyond_p (pos, selective));
-                  /* Allow for the " ..." that is displayed for them. */
-                  if (selective_rlen)
-                    {
-                      hpos += selective_rlen;
-                      if (hpos >= width)
-                        hpos = width;
-                    }
-                 --pos;
-                  /* We have skipped the invis text, but not the
-                     newline after.  */
-                }
-              else
-                {
-                  /* A visible line.  */
-                  vpos++;
-                  hpos = 0;
-                  hpos -= hscroll;
-                  /* Count the truncation glyph on column 0 */
-                  if (hscroll > 0)
-                    hpos++;
-                  tab_offset = 0;
-                }
-            }
-          else if (c == CR && selective < 0)
-            {
-              /* In selective display mode,
-                 everything from a ^M to the end of the line is invisible.
-                 Stop *before* the real newline.  */
-              pos = find_before_next_newline (pos, to, 1);
-              /* Allow for the " ..." that is displayed for them. */
-              if (selective_rlen)
-                {
-                  hpos += selective_rlen;
-                  if (hpos >= width)
-                    hpos = width;
-                }
-            }
-          else
-            hpos += (ctl_arrow && c < 0200) ? 2 : 4;
-        }
+         /* Perhaps add some info to the width_run_cache.  */
+         if (current_buffer->width_run_cache)
+           {
+             /* Is this character part of the current run?  If so, extend
+                the run.  */
+             if (pos - 1 == width_run_end
+                 && width_table[c] == width_run_width)
+               width_run_end = pos;
+
+             /* The previous run is over, since this is a character at a
+                different position, or a different width.  */
+             else
+               {
+                 /* Have we accumulated a run to put in the cache?
+                    (Currently, we only cache runs of width == 1).  */
+                 if (width_run_start < width_run_end
+                     && width_run_width == 1)
+                   know_region_cache (current_buffer,
+                                      current_buffer->width_run_cache,
+                                      width_run_start, width_run_end);
+
+                 /* Start recording a new width run.  */
+                 width_run_width = width_table[c];
+                 width_run_start = pos - 1;
+                 width_run_end = pos;
+               }
+           }
 
-      /* Handle right margin.  */
-      if (hpos >= width
-         && (hpos > width
-             || (pos < ZV
-                 && FETCH_CHAR (pos) != '\n')))
-       {
-         if (vpos > tovpos
-             || (vpos == tovpos && hpos >= tohpos))
-           break;
-         if (hscroll
-             || (truncate_partial_width_windows
-                 && width + 1 < FRAME_WIDTH (XFRAME (WINDOW_FRAME (win))))
-             || !NILP (current_buffer->truncate_lines))
+         if (dp != 0 && VECTORP (DISP_CHAR_VECTOR (dp, c)))
+           hpos += XVECTOR (DISP_CHAR_VECTOR (dp, c))->size;
+         else if (c >= 040 && c < 0177)
+           hpos++;
+         else if (c == '\t')
            {
-             /* Truncating: skip to newline.  */
-              pos = find_before_next_newline (pos, to, 1);
-             hpos = width;
+             int tem = (hpos + tab_offset + hscroll - (hscroll > 0)) % tab_width;
+             if (tem < 0)
+               tem += tab_width;
+             hpos += tab_width - tem;
            }
-         else
+         else if (c == '\n')
            {
-             /* Continuing.  */
-             vpos++;
-             hpos -= width;
-             tab_offset += width;
+             if (selective > 0 && indented_beyond_p (pos, selective))
+               {
+                 /* Skip any number of invisible lines all at once */
+                 do
+                   pos = find_before_next_newline (pos, to, 1) + 1;
+                 while (pos < to
+                        && indented_beyond_p (pos, selective));
+                 /* Allow for the " ..." that is displayed for them. */
+                 if (selective_rlen)
+                   {
+                     hpos += selective_rlen;
+                     if (hpos >= width)
+                       hpos = width;
+                   }
+                 --pos;
+                 /* We have skipped the invis text, but not the
+                    newline after.  */
+               }
+             else
+               {
+                 /* A visible line.  */
+                 vpos++;
+                 hpos = 0;
+                 hpos -= hscroll;
+                 /* Count the truncation glyph on column 0 */
+                 if (hscroll > 0)
+                   hpos++;
+                 tab_offset = 0;
+               }
            }
-
+         else if (c == CR && selective < 0)
+           {
+             /* In selective display mode,
+                everything from a ^M to the end of the line is invisible.
+                Stop *before* the real newline.  */
+             pos = find_before_next_newline (pos, to, 1);
+             /* If we just skipped next_boundary,
+                loop around in the main while
+                and handle it.  */
+             if (pos > next_boundary)
+               next_boundary = pos;
+             /* Allow for the " ..." that is displayed for them. */
+             if (selective_rlen)
+               {
+                 hpos += selective_rlen;
+                 if (hpos >= width)
+                   hpos = width;
+               }
+           }
+         else
+           hpos += (ctl_arrow && c < 0200) ? 2 : 4;
        }
     }
 
@@ -965,9 +1141,10 @@ TAB-OFFSET is the number of columns of the first tab that aren't\n\
 being displayed, perhaps because the line was continued within it.\n\
 If OFFSETS is nil, HSCROLL and TAB-OFFSET are assumed to be zero.\n\
 \n\
-WINDOW is the window to operate on.  Currently this is used only to\n\
-find the display table.  It does not matter what buffer WINDOW displays;\n\
-`compute-motion' always operates on the current buffer.\n\
+WINDOW is the window to operate on.  It is used to choose the display table;\n\
+if it is showing the current buffer, it is used also for\n\
+deciding which overlay properties apply.\n\
+Note that `compute-motion' always operates on the current buffer.\n\
 \n\
 The value is a list of five elements:\n\
   (POS HPOS VPOS PREVHPOS CONTIN)\n\
@@ -1022,7 +1199,7 @@ DEFUN ("compute-motion", Fcompute_motion, Scompute_motion, 7, 7, 0,
     CHECK_LIVE_WINDOW (window, 0);
 
   pos = compute_motion (XINT (from), XINT (XCONS (frompos)->cdr),
-                       XINT (XCONS (frompos)->car),
+                       XINT (XCONS (frompos)->car), 0,
                        XINT (to), XINT (XCONS (topos)->cdr),
                        XINT (XCONS (topos)->car),
                        XINT (width), hscroll, tab_offset,
@@ -1068,11 +1245,12 @@ pos_tab_offset (w, pos)
 struct position val_vmotion;
 
 struct position *
-vmotion (from, vtarget, width, hscroll, window)
-     register int from, vtarget, width;
-     int hscroll;
-     Lisp_Object window;
+vmotion (from, vtarget, w)
+     register int from, vtarget;
+     struct window *w;
 {
+  int width = window_internal_width (w) - 1;
+  int hscroll = XINT (w->hscroll);
   struct position pos;
   /* vpos is cumulative vertical position, changed as from is changed */
   register int vpos = 0;
@@ -1083,30 +1261,37 @@ vmotion (from, vtarget, width, hscroll, window)
     = (INTEGERP (current_buffer->selective_display)
        ? XINT (current_buffer->selective_display)
        : !NILP (current_buffer->selective_display) ? -1 : 0);
+  Lisp_Object window;
+  int start_hpos = 0;
+  int did_motion;
+
+  XSETWINDOW (window, w);
+
   /* The omission of the clause
-         && marker_position (XWINDOW (window)->start) == BEG
+         && marker_position (w->start) == BEG
      here is deliberate; I think we want to measure from the prompt
      position even if the minibuffer window has scrolled.  */
-  int start_hpos = 0;
-
   if (EQ (window, minibuf_window))
     {
-      if (minibuf_prompt_width == 0)
-       minibuf_prompt_width = string_width (minibuf_prompt, Qnil, Qnil);
+      if (minibuf_prompt_width == 0 && STRINGP (minibuf_prompt))
+       minibuf_prompt_width
+         = string_display_width (minibuf_prompt, Qnil, Qnil);
 
       start_hpos = minibuf_prompt_width;
     }
 
- retry:
-  if (vtarget > vpos)
+  if (vpos >= vtarget)
     {
-      /* Moving downward is simple, but must calculate from beg of line
-        to determine hpos of starting point */
-      if (from > BEGV && FETCH_CHAR (from - 1) != '\n')
+      /* To move upward, go a line at a time until
+        we have gone at least far enough */
+
+      first = 1;
+
+      while ((vpos > vtarget || first) && from > BEGV)
        {
          Lisp_Object propval;
 
-         XSETFASTINT (prevline, find_next_newline_no_quit (from, -1));
+         XSETFASTINT (prevline, find_next_newline_no_quit (from - 1, -1));
          while (XFASTINT (prevline) > BEGV
                 && ((selective > 0
                      && indented_beyond_p (XFASTINT (prevline), selective))
@@ -1117,79 +1302,73 @@ vmotion (from, vtarget, width, hscroll, window)
                                                       window),
                         TEXT_PROP_MEANS_INVISIBLE (propval))
 #endif
-                ))
+                    ))
            XSETFASTINT (prevline,
                         find_next_newline_no_quit (XFASTINT (prevline) - 1,
                                                    -1));
          pos = *compute_motion (XFASTINT (prevline), 0,
-                                lmargin + (XFASTINT (prevline) == 1
+                                lmargin + (XFASTINT (prevline) == BEG
                                            ? start_hpos : 0),
-                                from, 1 << (INTBITS - 2), 0,
-                                width, hscroll, 0, XWINDOW (window));
+                                0,
+                                from, 1 << (BITS_PER_INT - 2), 0,
+                                width, hscroll, 0, w);
+         vpos -= pos.vpos;
+         first = 0;
+         from = XFASTINT (prevline);
        }
-      else
+
+      /* If we made exactly the desired vertical distance,
+        or if we hit beginning of buffer,
+        return point found */
+      if (vpos >= vtarget)
        {
-         pos.hpos = lmargin + (from == 1 ? start_hpos : 0);
-         pos.vpos = 0;
+         val_vmotion.bufpos = from;
+         val_vmotion.vpos = vpos;
+         val_vmotion.hpos = lmargin;
+         val_vmotion.contin = 0;
+         val_vmotion.prevhpos = 0;
+         return &val_vmotion;
        }
-      return compute_motion (from, vpos, pos.hpos,
-                            ZV, vtarget, - (1 << (INTBITS - 2)),
-                            width, hscroll, pos.vpos * width,
-                            XWINDOW (window));
-    }
-
-  /* To move upward, go a line at a time until
-     we have gone at least far enough */
-
-  first = 1;
 
-  while ((vpos > vtarget || first) && from > BEGV)
+      /* Otherwise find the correct spot by moving down */
+    }
+  /* Moving downward is simple, but must calculate from beg of line
+     to determine hpos of starting point */
+  if (from > BEGV && FETCH_CHAR (from - 1) != '\n')
     {
-      XSETFASTINT (prevline, from);
-      while (1)
-       {
-         Lisp_Object propval;
+      Lisp_Object propval;
 
-         XSETFASTINT (prevline,
-                      find_next_newline_no_quit (XFASTINT (prevline) - 1,
-                                                 -1));
-         if (XFASTINT (prevline) == BEGV
-             || ((selective <= 0
-                  || ! indented_beyond_p (XFASTINT (prevline), selective))
+      XSETFASTINT (prevline, find_next_newline_no_quit (from, -1));
+      while (XFASTINT (prevline) > BEGV
+            && ((selective > 0
+                 && indented_beyond_p (XFASTINT (prevline), selective))
 #ifdef USE_TEXT_PROPERTIES
-                 /* watch out for newlines with `invisible' property */
-                 && (propval = Fget_char_property (prevline, Qinvisible,
-                                                   window),
-                     ! TEXT_PROP_MEANS_INVISIBLE (propval))
+                /* watch out for newlines with `invisible' property */
+                || (propval = Fget_char_property (prevline, Qinvisible,
+                                                  window),
+                    TEXT_PROP_MEANS_INVISIBLE (propval))
 #endif
-                 ))
-           break;
-       }
+            ))
+       XSETFASTINT (prevline,
+                    find_next_newline_no_quit (XFASTINT (prevline) - 1,
+                                               -1));
       pos = *compute_motion (XFASTINT (prevline), 0,
-                            lmargin + (XFASTINT (prevline) == 1
+                            lmargin + (XFASTINT (prevline) == BEG
                                        ? start_hpos : 0),
-                            from, 1 << (INTBITS - 2), 0,
-                            width, hscroll, 0, XWINDOW (window));
-      vpos -= pos.vpos;
-      first = 0;
-      from = XFASTINT (prevline);
+                            0,
+                            from, 1 << (BITS_PER_INT - 2), 0,
+                            width, hscroll, 0, w);
+      did_motion = 1;
     }
-
-  /* If we made exactly the desired vertical distance,
-     or if we hit beginning of buffer,
-     return point found */
-  if (vpos >= vtarget)
+  else
     {
-      val_vmotion.bufpos = from;
-      val_vmotion.vpos = vpos;
-      val_vmotion.hpos = lmargin;
-      val_vmotion.contin = 0;
-      val_vmotion.prevhpos = 0;
-      return &val_vmotion;
+      pos.hpos = lmargin + (from == BEG ? start_hpos : 0);
+      pos.vpos = 0;
+      did_motion = 0;
     }
-
-  /* Otherwise find the correct spot by moving down */
-  goto retry;
+  return compute_motion (from, vpos, pos.hpos, did_motion,
+                        ZV, vtarget, - (1 << (BITS_PER_INT - 2)),
+                        width, hscroll, pos.vpos * width, w);
 }
 
 DEFUN ("vertical-motion", Fvertical_motion, Svertical_motion, 1, 2, 0,
@@ -1210,7 +1389,6 @@ if beginning or end of buffer was reached.")
      Lisp_Object lines, window;
 {
   struct position pos;
-  register struct window *w;
 
   CHECK_NUMBER (lines, 0);
   if (! NILP (window))
@@ -1218,11 +1396,7 @@ if beginning or end of buffer was reached.")
   else
     window = selected_window;
 
-  w = XWINDOW (window);
-
-  pos = *vmotion (point, XINT (lines), window_internal_width (w) - 1,
-                 /* Not XFASTINT since perhaps could be negative */
-                 XINT (w->hscroll), window);
+  pos = *vmotion (point, (int) XINT (lines), XWINDOW (window));
 
   SET_PT (pos.bufpos);
   return make_number (pos.vpos);