]> 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 3188c00fdb7f8b19fc1881e3103c5df20fe0d964..60641ae54dc57b0291f7685ba30bd1d70cff9020 100644 (file)
@@ -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,6 +50,8 @@ 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_Char_Table *
@@ -166,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\
@@ -208,6 +287,16 @@ current_column ()
       && 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,
@@ -273,6 +362,75 @@ 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.  */
@@ -353,23 +511,23 @@ string_display_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);
@@ -388,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
@@ -420,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++)
        {
@@ -460,7 +643,6 @@ 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, "p",
   "Move point to column COLUMN in the current line.\n\
@@ -492,24 +674,34 @@ The return value is the current column.")
   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)))
        {
@@ -535,6 +727,7 @@ The return value is the current column.")
       else
        col++;
     }
+ endloop:
 
   SET_PT (pos);
 
@@ -564,7 +757,6 @@ The return value is the current column.")
   XSETFASTINT (val, col);
   return val;
 }
-
 \f
 /* compute_motion: compute buffer posn given screen posn and vice versa */
 
@@ -675,8 +867,10 @@ compute_motion (from, fromvpos, fromhpos, did_motion, to, tovpos, tohpos, width,
 
   /* 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 ())
@@ -710,47 +904,11 @@ compute_motion (from, fromvpos, fromhpos, did_motion, to, tovpos, tohpos, width,
          if (pos >= to)
            break;
 
-         {
-           Lisp_Object prop, position, end, limit, proplimit;
-
-           XSETFASTINT (position, pos);
-
-           /* 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.  */
-           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_boundary = 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 (limit) < XFASTINT (proplimit))
-                 proplimit = limit;
-               end = Fnext_single_property_change (position, Qinvisible,
-                                                   buffer, proplimit);
-               next_boundary = XFASTINT (end);
-             }
-           /* if the `invisible' property is set, we can skip to
-              the next property change */
-           prop = Fget_char_property (position, Qinvisible,
-                                      Fcurrent_buffer ());
-           if (TEXT_PROP_MEANS_INVISIBLE (prop))
-             pos = next_boundary;
-         }
+         /* 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);
        }
 
       /* Handle right margin.  */
@@ -983,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\