]> 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 7f0540c5ba8887774c60f26d4fa3f0d94398ce39..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,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.  */
@@ -289,7 +446,7 @@ string_display_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_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);
@@ -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,30 +668,40 @@ 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)))
        {
@@ -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 */
 
@@ -649,7 +842,7 @@ compute_motion (from, fromvpos, fromhpos, did_motion, to, tovpos, tohpos, width,
   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)
@@ -674,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 ())
@@ -709,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.  */
@@ -765,6 +924,11 @@ compute_motion (from, fromvpos, fromhpos, did_motion, to, tovpos, tohpos, width,
              /* 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
            {
@@ -915,6 +1079,11 @@ compute_motion (from, fromvpos, fromhpos, did_motion, to, tovpos, tohpos, width,
                 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)
                {
@@ -972,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\
@@ -1140,7 +1310,7 @@ vmotion (from, vtarget, w)
                                 lmargin + (XFASTINT (prevline) == BEG
                                            ? start_hpos : 0),
                                 0,
-                                from, 1 << (INTBITS - 2), 0,
+                                from, 1 << (BITS_PER_INT - 2), 0,
                                 width, hscroll, 0, w);
          vpos -= pos.vpos;
          first = 0;
@@ -1186,7 +1356,7 @@ vmotion (from, vtarget, w)
                             lmargin + (XFASTINT (prevline) == BEG
                                        ? start_hpos : 0),
                             0,
-                            from, 1 << (INTBITS - 2), 0,
+                            from, 1 << (BITS_PER_INT - 2), 0,
                             width, hscroll, 0, w);
       did_motion = 1;
     }
@@ -1197,7 +1367,7 @@ vmotion (from, vtarget, w)
       did_motion = 0;
     }
   return compute_motion (from, vpos, pos.hpos, did_motion,
-                        ZV, vtarget, - (1 << (INTBITS - 2)),
+                        ZV, vtarget, - (1 << (BITS_PER_INT - 2)),
                         width, hscroll, pos.vpos * width, w);
 }