(fast_string_match): Give re_search byte size of
[bpt/emacs.git] / src / intervals.c
index de5e3b3..7bfdfa4 100644 (file)
@@ -1,5 +1,5 @@
 /* Code for doing intervals.
-   Copyright (C) 1993, 1994, 1995 Free Software Foundation, Inc.
+   Copyright (C) 1993, 1994, 1995, 1997, 1998 Free Software Foundation, Inc.
 
 This file is part of GNU Emacs.
 
@@ -85,7 +85,7 @@ create_root_interval (parent)
       XSTRING (parent)->intervals = new;
     }
 
-  new->parent = (INTERVAL) parent;
+  new->parent = (INTERVAL) XFASTINT (parent);
   new->position = 1;
 
   return new;
@@ -198,7 +198,7 @@ void
 traverse_intervals (tree, position, depth, function, arg)
      INTERVAL tree;
      int position, depth;
-     void (* function) ();
+     void (* function) P_ ((INTERVAL, Lisp_Object));
      Lisp_Object arg;
 {
   if (NULL_INTERVAL_P (tree))
@@ -411,7 +411,7 @@ balance_possible_root_interval (interval)
   if (interval->parent == NULL_INTERVAL)
     return interval;
 
-  parent = (Lisp_Object) (interval->parent);
+  XSETFASTINT (parent, (EMACS_INT) interval->parent);
   interval = balance_an_interval (interval);
 
   if (BUFFERP (parent))
@@ -549,7 +549,7 @@ split_interval_left (interval, offset)
    is updated in the interval found.  Other functions (e.g., next_interval)
    will update this cache based on the result of find_interval.  */
 
-INLINE INTERVAL
+INTERVAL
 find_interval (tree, position)
      register INTERVAL tree;
      register int position;
@@ -670,6 +670,45 @@ previous_interval (interval)
 
   return NULL_INTERVAL;
 }
+
+/* Find the interval containing POS given some non-NULL INTERVAL
+   in the same tree. */
+INTERVAL
+update_interval (i, pos)
+     register INTERVAL i;
+     int pos;
+{
+  if (NULL_INTERVAL_P (i))
+    return NULL_INTERVAL;
+
+  while (1) 
+    {
+      if (pos < i->position) 
+       {
+         /* Move left. */
+         if (pos >= i->position - TOTAL_LENGTH (i->left))
+           i = i->left;                /* Move to the left child */
+         else if (NULL_PARENT (i)) 
+           error ("Point before start of properties");
+         else  i = i->parent;
+         continue;
+       }
+      else if (pos >= INTERVAL_LAST_POS (i))
+       {
+         /* Move right. */
+         if (pos < INTERVAL_LAST_POS (i) + TOTAL_LENGTH (i->right))
+           i = i->right;               /* Move to the right child */
+         else if (NULL_PARENT (i)) 
+           error ("Point after end of properties");
+         else 
+           i = i->parent;
+         continue;
+       }
+      else 
+       return i;
+    }
+}
+
 \f
 #if 0
 /* Traverse a path down the interval tree TREE to the interval
@@ -769,11 +808,43 @@ adjust_intervals_for_insertion (tree, position, length)
      So split this interval at the insertion point.  */
   if (! (position == i->position || eobp)
       && END_NONSTICKY_P (i)
-      && ! FRONT_STICKY_P (i))
+      && FRONT_NONSTICKY_P (i))
     {
-      temp = split_interval_right (i, position - i->position);
-      copy_properties (i, temp);
-      i = temp;
+      Lisp_Object tail;
+      Lisp_Object front, rear;
+
+      front = textget (i->plist, Qfront_sticky);
+      rear  = textget (i->plist, Qrear_nonsticky);
+
+      /* Does any actual property pose an actual problem?  */
+      for (tail = i->plist; ! NILP (tail); tail = Fcdr (Fcdr (tail)))
+       {
+         Lisp_Object prop;
+         prop = XCONS (tail)->car;
+
+         /* Is this particular property rear-sticky?
+            Note, if REAR isn't a cons, it must be non-nil,
+            which means that all properties are rear-nonsticky.  */
+         if (CONSP (rear) && NILP (Fmemq (prop, rear)))
+           continue;
+
+         /* Is this particular property front-sticky?
+            Note, if FRONT isn't a cons, it must be nil,
+            which means that all properties are front-nonsticky.  */
+         if (CONSP (front) && ! NILP (Fmemq (prop, front)))
+           continue;
+
+         /* PROP isn't sticky on either side => it is a real problem.  */
+         break;
+       }
+
+      /* If any property is a real problem, split the interval.  */
+      if (! NILP (tail))
+       {
+         temp = split_interval_right (i, position - i->position);
+         copy_properties (i, temp);
+         i = temp;
+       }
     }
 
   /* If we are positioned between intervals, check the stickiness of
@@ -812,7 +883,7 @@ adjust_intervals_for_insertion (tree, position, length)
          pright = NULL_INTERVAL_P (i) ? Qnil : i->plist;
          newi.plist = merge_properties_sticky (pleft, pright);
 
-         if(! prev) /* i.e. position == BEG */
+         if (! prev) /* i.e. position == BEG */
            {
              if (! intervals_equal (i, &newi))
                {
@@ -1059,10 +1130,10 @@ delete_interval (i)
   if (ROOT_INTERVAL_P (i))
     {
       Lisp_Object owner;
-      owner = (Lisp_Object) i->parent;
+      XSETFASTINT (owner, (EMACS_INT) i->parent);
       parent = delete_node (i);
       if (! NULL_INTERVAL_P (parent))
-       parent->parent = (INTERVAL) owner;
+       parent->parent = (INTERVAL) XFASTINT (owner);
 
       if (BUFFERP (owner))
        BUF_INTERVALS (XBUFFER (owner)) = parent;
@@ -1589,31 +1660,82 @@ textget (plist, prop)
 }
 
 \f
-/* Set point in BUFFER to POSITION.  If the target position is 
+/* Set point "temporarily", without checking any text properties.  */
+
+INLINE void
+temp_set_point (buffer, charpos)
+     struct buffer *buffer;
+     int charpos;
+{
+  temp_set_point_both (buffer, charpos,
+                      buf_charpos_to_bytepos (buffer, charpos));
+}
+
+/* Set point in BUFFER "temporarily" to CHARPOS, which corresponds to
+   byte position BYTEPOS.  */
+
+INLINE void
+temp_set_point_both (buffer, charpos, bytepos)
+     int charpos;
+     struct buffer *buffer;
+{
+  /* In a single-byte buffer, the two positions must be equal.  */
+  if (BUF_ZV (buffer) == BUF_ZV_BYTE (buffer)
+      && charpos != bytepos)
+    abort ();
+
+  if (charpos > bytepos)
+    abort ();
+
+  if (charpos > BUF_ZV (buffer) || charpos < BUF_BEGV (buffer))
+    abort ();
+
+  BUF_PT_BYTE (buffer) = bytepos;
+  BUF_PT (buffer) = charpos;
+}
+
+/* Set point in BUFFER to CHARPOS.  If the target position is 
    before an intangible character, move to an ok place.  */
 
 void
-set_point (position, buffer)
-     register int position;
+set_point (buffer, charpos)
+     register struct buffer *buffer;
+     register int charpos;
+{
+  set_point_both (buffer, charpos, buf_charpos_to_bytepos (buffer, charpos));
+}
+
+/* Set point in BUFFER to CHARPOS, which corresponds to byte
+   position BYTEPOS.  If the target position is 
+   before an intangible character, move to an ok place.  */
+
+void
+set_point_both (buffer, charpos, bytepos)
      register struct buffer *buffer;
+     register int charpos;
 {
   register INTERVAL to, from, toprev, fromprev, target;
   int buffer_point;
   register Lisp_Object obj;
   int old_position = BUF_PT (buffer);
-  int backwards = (position < old_position ? 1 : 0);
+  int backwards = (charpos < old_position ? 1 : 0);
   int have_overlays;
   int original_position;
 
   buffer->point_before_scroll = Qnil;
 
-  if (position == BUF_PT (buffer))
+  if (charpos == BUF_PT (buffer))
     return;
 
+  /* In a single-byte buffer, the two positions must be equal.  */
+  if (BUF_ZV (buffer) == BUF_ZV_BYTE (buffer)
+      && charpos != bytepos)
+    abort ();
+
   /* Check this now, before checking if the buffer has any intervals.
      That way, we can catch conditions which break this sanity check
      whether or not there are intervals in the buffer.  */
-  if (position > BUF_ZV (buffer) || position < BUF_BEGV (buffer))
+  if (charpos > BUF_ZV (buffer) || charpos < BUF_BEGV (buffer))
     abort ();
 
   have_overlays = (! NILP (buffer->overlays_before)
@@ -1623,17 +1745,17 @@ set_point (position, buffer)
      then we can do it quickly.  */
   if (NULL_INTERVAL_P (BUF_INTERVALS (buffer)) && ! have_overlays)
     {
-      BUF_PT (buffer) = position;
+      temp_set_point_both (buffer, charpos, bytepos);
       return;
     }
 
-  /* Set TO to the interval containing the char after POSITION,
-     and TOPREV to the interval containing the char before POSITION.
+  /* Set TO to the interval containing the char after CHARPOS,
+     and TOPREV to the interval containing the char before CHARPOS.
      Either one may be null.  They may be equal.  */
-  to = find_interval (BUF_INTERVALS (buffer), position);
-  if (position == BUF_BEGV (buffer))
+  to = find_interval (BUF_INTERVALS (buffer), charpos);
+  if (charpos == BUF_BEGV (buffer))
     toprev = 0;
-  else if (to && to->position == position)
+  else if (to && to->position == charpos)
     toprev = previous_interval (to);
   else
     toprev = to;
@@ -1660,11 +1782,11 @@ set_point (position, buffer)
   if (to == from && toprev == fromprev && INTERVAL_VISIBLE_P (to)
       && ! have_overlays)
     {
-      BUF_PT (buffer) = position;
+      temp_set_point_both (buffer, charpos, bytepos);
       return;
     }
 
-  original_position = position;
+  original_position = charpos;
 
   /* If the new position is between two intangible characters
      with the same intangible property value,
@@ -1674,16 +1796,16 @@ set_point (position, buffer)
          || have_overlays)
       /* Intangibility never stops us from positioning at the beginning
         or end of the buffer, so don't bother checking in that case.  */
-      && position != BEGV && position != ZV)
+      && charpos != BEGV && charpos != ZV)
     {
       Lisp_Object intangible_propval;
       Lisp_Object pos;
 
-      XSETINT (pos, position);
+      XSETINT (pos, charpos);
 
       if (backwards)
        {
-         intangible_propval = Fget_char_property (make_number (position),
+         intangible_propval = Fget_char_property (make_number (charpos),
                                                   Qintangible, Qnil);
 
          /* If following char is intangible,
@@ -1697,7 +1819,7 @@ set_point (position, buffer)
        }
       else
        {
-         intangible_propval = Fget_char_property (make_number (position - 1),
+         intangible_propval = Fget_char_property (make_number (charpos - 1),
                                                   Qintangible, Qnil);
 
          /* If following char is intangible,
@@ -1710,18 +1832,19 @@ set_point (position, buffer)
 
        }
 
-      position = XINT (pos);
+      charpos = XINT (pos);
+      bytepos = buf_charpos_to_bytepos (buffer, charpos);
     }
 
-  if (position != original_position)
+  if (charpos != original_position)
     {
-      /* Set TO to the interval containing the char after POSITION,
-        and TOPREV to the interval containing the char before POSITION.
+      /* Set TO to the interval containing the char after CHARPOS,
+        and TOPREV to the interval containing the char before CHARPOS.
         Either one may be null.  They may be equal.  */
-      to = find_interval (BUF_INTERVALS (buffer), position);
-      if (position == BUF_BEGV (buffer))
+      to = find_interval (BUF_INTERVALS (buffer), charpos);
+      if (charpos == BUF_BEGV (buffer))
        toprev = 0;
-      else if (to && to->position == position)
+      else if (to && to->position == charpos)
        toprev = previous_interval (to);
       else
        toprev = to;
@@ -1731,7 +1854,7 @@ set_point (position, buffer)
      and TOPREV is the interval before the stopping point.
      One or the other may be null.  */
 
-  BUF_PT (buffer) = position;
+  temp_set_point_both (buffer, charpos, bytepos);
 
   /* We run point-left and point-entered hooks here, iff the
      two intervals are not equivalent.  These hooks take
@@ -1761,25 +1884,75 @@ set_point (position, buffer)
        enter_before = Qnil;
 
       if (! EQ (leave_before, enter_before) && !NILP (leave_before))
-       call2 (leave_before, old_position, position);
+       call2 (leave_before, make_number (old_position),
+              make_number (charpos));
       if (! EQ (leave_after, enter_after) && !NILP (leave_after))
-       call2 (leave_after, old_position, position);
+       call2 (leave_after, make_number (old_position),
+              make_number (charpos));
 
       if (! EQ (enter_before, leave_before) && !NILP (enter_before))
-       call2 (enter_before, old_position, position);
+       call2 (enter_before, make_number (old_position),
+              make_number (charpos));
       if (! EQ (enter_after, leave_after) && !NILP (enter_after))
-       call2 (enter_after, old_position, position);
+       call2 (enter_after, make_number (old_position),
+              make_number (charpos));
     }
 }
+\f
+/* Move point to POSITION, unless POSITION is inside an intangible
+   segment that reaches all the way to point.  */
 
-/* Set point temporarily, without checking any text properties.  */
-
-INLINE void
-temp_set_point (position, buffer)
+void
+move_if_not_intangible (position)
      int position;
-     struct buffer *buffer;
 {
-  BUF_PT (buffer) = position;
+  Lisp_Object pos;
+  Lisp_Object intangible_propval;
+
+  XSETINT (pos, position);
+
+  if (! NILP (Vinhibit_point_motion_hooks))
+    /* If intangible is inhibited, always move point to POSITION.  */
+    ;
+  else if (PT < position && XINT (pos) < ZV)
+    {
+      /* We want to move forward, so check the text before POSITION.  */
+
+      intangible_propval = Fget_char_property (pos,
+                                              Qintangible, Qnil);
+
+      /* If following char is intangible,
+        skip back over all chars with matching intangible property.  */
+      if (! NILP (intangible_propval))
+       while (XINT (pos) > BEGV
+              && EQ (Fget_char_property (make_number (XINT (pos) - 1),
+                                         Qintangible, Qnil),
+                     intangible_propval))
+         pos = Fprevious_char_property_change (pos, Qnil);
+    }
+  else if (XINT (pos) > BEGV)
+    {
+      /* We want to move backward, so check the text after POSITION.  */
+
+      intangible_propval = Fget_char_property (make_number (XINT (pos) - 1),
+                                              Qintangible, Qnil);
+
+      /* If following char is intangible,
+        skip back over all chars with matching intangible property.  */
+      if (! NILP (intangible_propval))
+       while (XINT (pos) < ZV
+              && EQ (Fget_char_property (pos, Qintangible, Qnil),
+                     intangible_propval))
+         pos = Fnext_char_property_change (pos, Qnil);
+
+    }
+
+  /* If the whole stretch between PT and POSITION isn't intangible, 
+     try moving to POSITION (which means we actually move farther
+     if POSITION is inside of intangible text).  */
+
+  if (XINT (pos) != PT)
+    SET_PT (position);
 }
 \f
 /* Return the proper local map for position POSITION in BUFFER.
@@ -1792,7 +1965,7 @@ get_local_map (position, buffer)
      register struct buffer *buffer;
 {
   Lisp_Object prop, tem, lispy_position, lispy_buffer;
-  int old_begv, old_zv;
+  int old_begv, old_zv, old_begv_byte, old_zv_byte;
 
   /* Perhaps we should just change `position' to the limit.  */
   if (position > BUF_Z (buffer) || position < BUF_BEG (buffer))
@@ -1802,8 +1975,12 @@ get_local_map (position, buffer)
      the visible region contains no characters and hence no properties.  */
   old_begv = BUF_BEGV (buffer);
   old_zv = BUF_ZV (buffer);
+  old_begv_byte = BUF_BEGV_BYTE (buffer);
+  old_zv_byte = BUF_ZV_BYTE (buffer);
   BUF_BEGV (buffer) = BUF_BEG (buffer);
   BUF_ZV (buffer) = BUF_Z (buffer);
+  BUF_BEGV_BYTE (buffer) = BUF_BEG_BYTE (buffer);
+  BUF_ZV_BYTE (buffer) = BUF_Z_BYTE (buffer);
 
   /* There are no properties at the end of the buffer, so in that case
      check for a local map on the last character of the buffer instead.  */
@@ -1815,6 +1992,8 @@ get_local_map (position, buffer)
 
   BUF_BEGV (buffer) = old_begv;
   BUF_ZV (buffer) = old_zv;
+  BUF_BEGV_BYTE (buffer) = old_begv_byte;
+  BUF_ZV_BYTE (buffer) = old_zv_byte;
 
   /* Use the local map only if it is valid.  */
   /* Do allow symbols that are defined as keymaps.  */
@@ -1883,7 +2062,7 @@ copy_intervals_to_string (string, buffer, position, length)
   if (NULL_INTERVAL_P (interval_copy))
     return;
 
-  interval_copy->parent = (INTERVAL) string;
+  interval_copy->parent = (INTERVAL) XFASTINT (string);
   XSTRING (string)->intervals = interval_copy;
 }
 \f
@@ -1925,5 +2104,79 @@ compare_string_intervals (s1, s2)
     }
   return 1;
 }
+\f
+static void set_intervals_multibyte_1 (INTERVAL, int, int, int, int, int);
+
+/* Update the intervals of the current buffer
+   to fit the contents as multibyte (if MULTI_FLAG is 1)
+   or to fit them as non-multibyte (if MULTI_FLAG is 0).  */
+
+void
+set_intervals_multibyte (multi_flag)
+     int multi_flag;
+{
+  if (BUF_INTERVALS (current_buffer))
+    set_intervals_multibyte_1 (BUF_INTERVALS (current_buffer), multi_flag,
+                              BEG, BEG_BYTE, Z, Z_BYTE);
+}
+
+/* Recursively adjust interval I in the current buffer
+   for setting enable_multibyte_characters to MULTI_FLAG.
+   The range of interval I is START ... END in characters,
+   START_BYTE ... END_BYTE in bytes.  */
+
+static void
+set_intervals_multibyte_1 (i, multi_flag, start, start_byte, end, end_byte)
+     INTERVAL i;
+     int multi_flag;
+     int start, start_byte, end, end_byte;
+{
+  INTERVAL left, right;
+
+  /* Fix the length of this interval.  */
+  if (multi_flag)
+    i->total_length = end - start;
+  else
+    i->total_length = end_byte - start_byte;
+
+  /* Recursively fix the length of the subintervals.  */
+  if (i->left)
+    {
+      int left_end, left_end_byte;
+
+      if (multi_flag)
+       {
+         left_end_byte = start_byte + LEFT_TOTAL_LENGTH (i);
+         left_end = BYTE_TO_CHAR (left_end_byte);
+       }
+      else
+       {
+         left_end = start + LEFT_TOTAL_LENGTH (i);
+         left_end_byte = CHAR_TO_BYTE (left_end);
+       }
+
+      set_intervals_multibyte_1 (i->left, multi_flag, start, start_byte,
+                                left_end, left_end_byte);
+    }
+  if (i->right)
+    {
+      int right_start_byte, right_start;
+
+      if (multi_flag)
+       {
+         right_start_byte = end_byte - RIGHT_TOTAL_LENGTH (i);
+         right_start = BYTE_TO_CHAR (right_start_byte);
+       }
+      else
+       {
+         right_start = end - RIGHT_TOTAL_LENGTH (i);
+         right_start_byte = CHAR_TO_BYTE (right_start);
+       }
+
+      set_intervals_multibyte_1 (i->right, multi_flag,
+                                right_start, right_start_byte,
+                                end, end_byte);
+    }
+}
 
 #endif /* USE_TEXT_PROPERTIES */