*** empty log message ***
[m17n/m17n-lib-cs.git] / MText.cs
index de090a9..cbf5c8a 100644 (file)
--- a/MText.cs
+++ b/MText.cs
@@ -8,52 +8,84 @@ namespace M17N.Core
 {
 #if false
   public enum MTextFormat
-  {
-    MTEXT_FORMAT_US_ASCII,
-    MTEXT_FORMAT_UTF_8,
-    MTEXT_FORMAT_UTF_16BE,
-    MTEXT_FORMAT_UTF_16LE,
-    MTEXT_FORMAT_UTF_32BE,
-    MTEXT_FORMAT_UTF_32LE,
-  }
+    {
+      MTEXT_FORMAT_US_ASCII,
+      MTEXT_FORMAT_UTF_8,
+      MTEXT_FORMAT_UTF_16BE,
+      MTEXT_FORMAT_UTF_16LE,
+      MTEXT_FORMAT_UTF_32BE,
+      MTEXT_FORMAT_UTF_32LE,
+    }
 #endif
 
   public class MTextProperty
   {
-    internal MProperty prop;
-    internal bool front_sticky;
-    internal bool rear_sticky;
-    internal bool merginable;
-    public MProperty Prop { get { return prop; } }
-    public bool FrontSticky { get { return front_sticky; } }
-    public bool RearSticky { get { return rear_sticky; } }
-    public bool Merginable { get { return merginable; } }
+    internal MSymbol key;
+    internal object val;
+
+    [FlagsAttribute]
+    internal enum Flag : byte
+      {
+       None =          0,
+       FrontSticky =   1,
+       RearSticky =    2,
+       Sensitive =     4
+      };
+    internal Flag flags;
+
+    public MSymbol Key { get { return key; } }
+    public object Val { get { return val; } }
+    public bool FrontSticky
+    {
+      get { return (flags & Flag.FrontSticky) != Flag.None; }
+    }
+    public bool RearSticky
+    {
+      get { return (flags & Flag.RearSticky) != Flag.None; }
+    }
+    public bool Sensitive
+    {
+      get { return (flags & Flag.Sensitive) != Flag.None; }
+    }
+
+    public MTextProperty (MSymbol key, object val)
+    {
+      this.key = key;
+      this.val = val;
+      flags |= Flag.RearSticky;
+    }
 
-    public MTextProperty (bool front_sticky, bool rear_sticky)
+    public MTextProperty (MSymbol key, object val,
+                         bool front_sticky, bool rear_sticky, bool sensitive)
     {
-      this.front_sticky = front_sticky;
-      this.rear_sticky = rear_sticky;
+      this.key = key;
+      this.val = val;
+      if (front_sticky)
+       flags |= Flag.FrontSticky;
+      if (rear_sticky)
+       flags |= Flag.RearSticky;
+      if (sensitive)
+       flags |= Flag.Sensitive;
     }
-    public MTextProperty (bool front_sticky, bool rear_sticky, bool merginable)
+
+    public override string ToString ()
     {
-      this.front_sticky = front_sticky;
-      this.rear_sticky = rear_sticky;
-      this.merginable = merginable;
+      return key.ToString () + ":" + val;
     }
   }
 
-  public class MText
+  public class MText : IEnumerable, IEquatable<MText>, IComparable<MText>
   {
 #if false
     public enum MTextFormat format;
 #endif
-
     private StringBuilder sb;
     private int nchars;
     private int cache_pos;
     private int cache_idx;
-    private MInterval root_interval;
-    private bool unmodifiable;
+    private MPlist intervals;
+    private MPlist default_property;
+    private bool read_only;
 
     private static UTF8Encoding utf8 = new UTF8Encoding ();
 
@@ -61,8 +93,9 @@ namespace M17N.Core
     {
       int len = str.Length, n = 0;
 
-      for (int i = 0; i < len; i++) 
-       n += surrogate_high_p (str[i]) ? 2 : 1;
+      for (int i = 0; i < len; i++, n++) 
+       if (surrogate_high_p (str[i]))
+         i++;
       return n;
     }
 
@@ -70,48 +103,69 @@ namespace M17N.Core
     {
       int len = str.Length, n = 0;
 
-      for (int i = 0; i < len; i++) 
-       n += surrogate_high_p (str[i]) ? 2 : 1;
+      for (int i = 0; i < len; i++, n++) 
+       if (surrogate_high_p (str[i]))
+         i++;
       return n;
     }
 
     public MText ()
-    {
-      sb = new StringBuilder ();
-    }
+      {
+       sb = new StringBuilder ();
+       intervals = new MPlist ();
+      }
 
     public MText (byte[] str)
-    {
-      sb = new StringBuilder (utf8.GetString (str));
-      nchars = count_chars (sb);
-    }
+      {
+       sb = new StringBuilder (utf8.GetString (str));
+       nchars = count_chars (sb);
+       intervals = new MPlist ();
+      }
 
     public MText (String str)
-    {
-      sb = new StringBuilder (str);
-      nchars = count_chars (str);
-    }
+      {
+       sb = new StringBuilder (str);
+       nchars = count_chars (str);
+       intervals = new MPlist ();
+      }
 
     public MText (StringBuilder str)
-    {
-      sb = str;
-      nchars = count_chars (str);
-    }
+      {
+       sb = str;
+       nchars = count_chars (str);
+       intervals = new MPlist ();
+      }
 
     public static MText operator+ (MText mt1, MText mt2)
     {
-      MText mt = new MText (mt1.sb);
+      MText mt = new MText ();
 
+      mt.sb.Append (mt1.sb);
       mt.sb.Append (mt2.sb);
       mt.nchars = mt1.nchars + mt2.nchars;
       return mt;
     }
 
-    public override string ToString ()
+    // Public properties
+    public bool ReadOnly { get { return read_only; } }
+    public int Length { get { return nchars; } }
+
+    // Public methods
+
+    // for IEnumerable interface
+    public IEnumerator GetEnumerator() { return new MTextEnum (this); }
+
+    // for IEquatable interface
+    public bool Equals (MText other) { return this.sb.Equals (other.sb); }
+
+    // for IComparable interface
+    public int CompareTo (MText other)
     {
-      return sb.ToString ();
+      return this.sb.ToString ().CompareTo (other.sb.ToString ());
     }
 
+    public override String ToString () { return "\"" + sb.ToString () + "\""; }
+
     private static bool surrogate_high_p (char c)
     {
       return (c >= 0xD800 && c < 0xDC00);
@@ -179,14 +233,45 @@ namespace M17N.Core
       return i;
     }
 
+    private void check_pos (int pos, bool tail_ok)
+    {
+      if (pos < 0 || (tail_ok ? pos > nchars : pos >= nchars))
+       throw new Exception ("Invalid MText position:" + pos);
+    }
+
+    private bool check_range (int from, int to, bool zero_ok)
+    {
+      if (from < 0 || (zero_ok ? from > to : from >= to)
+         || to > nchars)
+       throw new Exception ("Invalid MText range");
+      return (from == to);
+    }
+
     private void insert (int pos, MText mt2, int from, int to)
     {
+      check_pos (pos, true);
+
       int pos_idx = pos_to_idx (this, pos);
       int from_idx = pos_to_idx (mt2, from);
       int to_idx = pos_to_idx (mt2, to);
 
       sb.Insert (pos_idx, mt2.sb.ToString (from_idx, to_idx - from_idx));
       nchars += to - from;
+
+      foreach (MPlist plist in mt2.intervals)
+       if (intervals.Find (plist.Key) == null)
+         intervals.Push (plist.Key, new MInterval (plist.Key, this));
+      foreach (MPlist plist in intervals)
+       {
+         MPlist p = mt2.intervals.Find (plist.Key);
+         MInterval interval;
+
+         if (p == null)
+           interval = new MInterval (plist.Key, to - from);
+         else
+           interval = ((MInterval) p.Val).copy (from, to);
+         ((MInterval) plist.Val).Insert (pos, interval);
+       }
     }
 
     public int this[int i]
@@ -218,182 +303,796 @@ namespace M17N.Core
       }
     }
 
-    public MText dup ()
+    public MText Dup ()
     {
       return (new MText (sb.ToString ()));
     }
 
-    public MText ins (int pos, MText mt)
+    public MText Ins (int pos, MText mt)
     {
       insert (pos, mt, 0, mt.nchars);
       return this;
     }
 
-    public MText ins (int pos, MText mt, int from, int to)
+    public MText Ins (int pos, MText mt, int from, int to)
     {
       insert (pos, mt, from, to);
       return this;
     }
 
-    public MText del (int from, int to)
+    public MText Del (int from, int to)
     {
+      if (check_range (from, to, true))
+       return this;
+
       sb.Remove (from, pos_to_idx (this, to) - pos_to_idx (this, from));
       nchars -= to - from;
+
+      if (nchars > 0)
+       foreach (MPlist plist in intervals)
+         ((MInterval) plist.Val).Delete (from, to);
+      else
+       intervals = new MPlist ();
       return this;
     }
 
+    public object GetProp (int pos, MSymbol key)
+    {
+      check_pos (pos, false);
+
+      MInterval i = (MInterval) intervals.Find (key).Val;
+
+      if (i == null)
+       return null;
+
+      MTextProperty prop = i.Get (pos);
+      return (prop != null ? prop.Val : null);
+    }
+
+    public object GetProp (int pos, MSymbol key, out MTextProperty prop)
+    {
+      check_pos (pos, false);
+
+      MInterval i = (MInterval) intervals.Find (key).Val;
+
+      if (i == null)
+       return (prop = null);
+      prop = i.Get (pos);
+      return (prop != null ? prop.Val : null);
+    }
+
+    public object GetProp (int pos, MSymbol key, out MTextProperty[] array)
+    {
+      check_pos (pos, false);
+
+      MInterval i = (MInterval) intervals.Find (key).Val;
+
+      if (i == null)
+       return (array = null);
+      MTextProperty prop = i.Get (pos, out array);
+      return (prop != null ? prop.Val : null);
+    }
+
+    public void PushProp (int from, int to, MSymbol key, object val)
+    {
+      if (! check_range (from, to, true))
+       PushProp (from, to, new MTextProperty (key, val));
+    }
+
+    public void PushProp (int from, int to, MTextProperty prop)
+    {
+      if (from < 0)
+       {
+         if (default_property == null)
+           default_property = new MPlist ();
+         default_property.Push (prop.key, prop.val);
+       }
+      else
+       {
+         if (check_range (from, to, true))
+           return;
+
+         MPlist p = intervals.Find (prop.key);
+         MInterval root;
+
+         if (p == null)
+           {
+             root = new MInterval (prop.key, this);
+             intervals.Push (prop.key, root);
+           }
+         else
+           root = (MInterval) p.Val;
+
+         root.Push (from, to, prop);
+       }
+    }
+
+    public void PopProp (int from, int to, MSymbol key)
+    {
+      if (from < 0)
+       {
+         if (default_property == null)
+           return;
+         MPlist p = default_property.Find (key);
+
+         if (p != null)
+           p.Pop ();
+       }
+      else
+       {
+         if (check_range (from, to, true))
+           return;
+
+         MPlist p = intervals.Find (key);
+
+         if (p != null)
+           ((MInterval) p.Val).Pop (from, to);
+       }
+    }
+
+    public void DumpProp ()
+    {
+      Console.Write ("(");
+      foreach (MPlist p in intervals)
+       ((MInterval) p.Val).Dump ();
+      Console.WriteLine (")");
+    }
+
     private class MInterval
     {
-      // Start and end positions of the MText covered of this interval
-      // and its children.  The values are actually (4N +- 1), where N
-      // is a non-negative integer representing a position relative to
-      // the parent interval.
-      private int total_start, total_end;
-      // Stack of MTextProperty
-      private Stack<MTextProperty> stack;
-      // Number of child nodes
-      private int nodes;
+      // position: 0   1   2   3   4   5   6   7
+      //           | A | B | C | D | E   F | G |
+      // interval  |---|---|---|<->|-------|---|
+      //           |---|<->|---|   |<----->|---|
+      //           |<->|   |<->|           |<->|
+      // 
+      //                      [7 (3 4)]
+      //              [3 (1 2)]       [3 (4 6)]
+      //         [1 (0 1)] [2 (2 3)]      [1 (6 7)]
+      //
+      private static int count = 0;
+      private int id;
+      private int total_length;
+      private int from, to;
+      private MSymbol key;
+      private MPlist stack;
       private MInterval left, right, parent;
+      private MText mtext;
 
-      private int Start {
-       get {
-         return (left == null ? total_start : total_start + left.total_end);
-       }
+      public MInterval (MSymbol key, int length)
+      {
+       if (length <= 0)
+         throw new Exception ("Invalid interval length");
+       this.key = key;
+       total_length = length;
+       stack = new MPlist ();
+       id = count++;
       }
 
-      private int End {
-       get {
-         return (right == null ? total_end : total_start + right.total_start);
-       }
+      public MInterval (MSymbol key, MText mt)
+      {
+       this.key = key;
+       mtext = mt;
+       total_length = mt.sb.Length;
+       from = 0;
+       to = total_length;
+       stack = new MPlist ();
+       id = count++;
       }
 
-      private MInterval Left {
+      public MTextProperty Get (int pos)
+      {
+       MInterval i = find (pos);
+
+       return (i.stack.IsEmpty ? null : (MTextProperty) i.stack.Val);
+      }
+
+      public MTextProperty Get (int pos, out MTextProperty[] array)
+      {
+       MInterval i = find (pos);
+
+       if (i.stack.IsEmpty)
+         {
+           array = null;
+           return null;
+         }
+       array = new MTextProperty[i.stack.Count];
+
+       int idx;
+       MPlist p;
+       for (idx = 0, p = i.stack; ! p.IsEmpty; idx++, p = p.Next)
+         array[idx] = (MTextProperty) p.Val;
+       return array[idx - 1];
+      }
+
+      private MInterval (MSymbol key, int length, MPlist stack)
+      {
+       this.key = key;
+       total_length = length;
+       from = 0;
+       to = total_length;
+       this.stack = stack.Clone ();
+       id = count++;
+      }
+
+      private void update_from_to ()
+      {
+       if (parent == null)
+         {
+           from = LeftLength;
+           to = total_length - RightLength;
+         }
+       else if (parent.left == this)
+         {
+           from = parent.from - total_length + LeftLength;
+           to = parent.from - RightLength;
+         }
+       else
+         {
+           from = parent.to + LeftLength;
+           to = parent.to + total_length - RightLength;
+         }
+      }
+
+      private int LeftLength
+      {
+       get { return (left == null ? 0 : left.total_length); }
+      }
+
+      private int RightLength
+      {
+       get { return (right == null ? 0 : right.total_length); }
+      }
+
+      private MInterval left_most_node
+      {
+       get { return (left == null ? this : left.left_most_node); }
+      }
+
+      private MInterval right_most_node
+      {
+       get { return (right == null ? this : right.right_most_node); }
+      }
+
+      private MInterval prev {
        get {
-         MInterval interval;
+         MInterval i;
 
          if (left != null)
-           {
-             for (interval = left; interval.right != null;
-                  interval = interval.right);
-           }
+           for (i = left; i.right != null; i = i.right);
          else
-           {
-             for (interval = parent;
-                  interval != null && interval.total_start == total_start;
-                  interval = interval.parent);
-           }
-         return interval;
+           for (i = parent; i != null && i.left == null; i = i.parent);
+         return i;
        }
       }
 
-      private MInterval Right {
+      private MInterval next {
        get {
-         MInterval interval;
+         MInterval i;
 
          if (right != null)
-           {
-             for (interval = right; interval.left != null;
-                  interval = interval.left);
-           }
+           for (i = right; i.left != null; i = i.left);
          else
-           {
-             for (interval = parent;
-                  interval != null && interval.total_end == total_end;
-                  interval = interval.parent);
-           }
-         return interval;
+           for (i = parent; i != null && i.right == null; i = i.parent);
+         return i;
        }
       }
 
-      private static int MakePosition (int pos, bool front_inclusive)
+      private MInterval find (int pos)
       {
-       return (pos << 2) + (front_inclusive ? -1 : 1);
+       update_from_to ();
+       if (pos < from)
+         return left.find (pos);
+       if (pos >= to)
+         return right.find (pos);
+       return this;
       }
 
-      private MInterval (int start, int end)
+      //      p-. or .-p              p-. or .-p
+      //       .-this-.                .-right-.
+      //    left   .-right-.  ->   .-this-.    c2
+      //          c1       c2    left     c1
+      private MInterval promote_right ()
       {
-       if (start > end)
-         throw new Exception ("Invalid Interval Range");
-       this.total_start = (start << 2) + 1;
-       this.total_end = (end << 2) + -1;
-       this.stack = new Stack<MTextProperty> ();
-       this.nodes = 1;
+       int right_length = right.total_length;
+       MInterval c1;
+
+       if (parent == null)
+         mtext.intervals.Put (key, right);
+       else if (parent.left == this)
+         parent.left = right;
+       else
+         parent.right = right;
+       right.parent = parent;
+       c1 = right.left;
+       right.left = this;
+
+       parent = right;
+       right = c1;
+       parent.total_length += total_length;
+       total_length -= right_length;
+       if (c1 != null)
+         {
+           c1.parent = this;
+           parent.total_length -= c1.total_length;
+           total_length += c1.total_length;
+         }
+       return parent;
       }
 
-      public MInterval (int start, bool front_inclusive,
-                       int end, bool rear_inclusive)
+      //      p-. or .-p              p-. or .-p
+      //       .-this-.                .-left-.
+      //  .-left-.  .-right-.  ->     c1    .-this-.
+      // c1      c2                        c2     right
+      private MInterval promote_left ()
       {
-       if (start > end)
-         throw new Exception ("Invalid Interval Range");
-       this.total_start = (start << 2) + (front_inclusive ? -1 : 1);
-       this.total_end = (end << 2) + (rear_inclusive ? 1 : -1);
-       this.stack = new Stack<MTextProperty> ();
-       this.nodes = 1;
+       int left_length = left.total_length;
+       MInterval c1;
+
+       if (parent == null)
+         mtext.intervals.Put (key, left);
+       else if (parent.left == this)
+         parent.left = left;
+       else
+         parent.right = left;
+       left.parent = parent;
+       c1 = left.left;
+       left.right = this;
+
+       parent = left;
+       left = c1;
+       parent.total_length += total_length;
+       total_length -= left_length;
+       if (c1 != null)
+         {
+           c1.parent = this;
+           parent.total_length -= c1.total_length;
+           total_length += c1.total_length;
+         }
+       return parent;
       }
 
-      public void Push (MTextProperty prop, int start, int end)
+      private MInterval balance ()
       {
-       start <<= 2;
-       if (prop.FrontSticky)
-         start--;
-       else
-         start++;
-       end <<= 2;
-       if (prop.RearSticky)
-         end++;
-       else
-         end--;
-       if (start >= end)
-         throw new Exception ("Invalid Text Property Range");
+       MInterval i = this;
+
+       while (true)
+         {
+           //       .-this-.
+           //  .-left-.  .-right-.
+           // c1     c2  c3      c4
+           int diff = i.LeftLength - i.RightLength;
+           int new_diff;
+
+           if (diff > 0)
+             {
+               new_diff = (i.total_length - i.LeftLength
+                           + i.left.RightLength - i.left.LeftLength);
+               if (Math.Abs (new_diff) >= diff)
+                 break;
+               i = i.promote_left ();
+               i.right.balance ();
+             }
+           else if (diff < 0)
+             {
+               new_diff = (i.total_length - i.RightLength
+                           + i.right.LeftLength - i.right.RightLength);
+               if (Math.Abs (new_diff) >= diff)
+                 break;
+               i = i.promote_right ();
+               i.left.balance ();
+             }
+         }
+       return i;
+      }
+
+      public MInterval copy (int start, int end)
+      {
+       MInterval this_copy, left_copy = null, right_copy = null;
 
-       push (prop, start, end);
+       update_from_to ();
+       if (start < from)
+         {
+           if (end <= from)
+             return left.copy (start, end);
+           left_copy = left.copy (start, from);
+         }
+       else if (end > to)
+         {
+           if (start >= to)
+             return right.copy (start, end);
+           right_copy = right.copy (to, end);
+         }
+       this_copy = new MInterval (key, end - start, stack);
+       this_copy.left = left_copy;
+       this_copy.right = right_copy;
+
+       return this_copy;
       }
 
+      //  this-.   ==>   this-.
+      //     right          newright-.
+      //                            right
       private MInterval divide_right (int pos)
       {
-       MInterval interval = new MInterval (pos, End);
+       MInterval interval = new MInterval (key, to - pos, stack);
 
+       Console.Write ("divide-right({0}) at ", pos); DumpOne (false, true);
+       to = pos;
+       if (right != null)
+         {
+           interval.right = right;
+           right.parent = interval;
+           interval.total_length += right.total_length;
+         }
+       interval.parent = this;
+       right = interval;
        return interval;
       }
 
+      //    .-this   ==>       .-this
+      //  left             .-newleft
+      //                 left
       private MInterval divide_left (int pos)
       {
-       MInterval interval = new MInterval (Start, pos);
+       MInterval interval = new MInterval (key, pos - from, stack);
 
+       Console.Write ("divide-reft({0}) at ", pos); DumpOne (false, true);
+       from = pos;
+       if (left != null)
+         {
+           interval.left = left;
+           left.parent = interval;
+           interval.total_length += left.total_length;
+         }
+       interval.parent = this;
+       left = interval;
        return interval;
       }
 
-      private void push (MTextProperty prop, int start, int end)
+      private void remove_properties (MTextProperty.Flag flags)
       {
-       int this_start = Start;
-       int this_end = End;
+       for (MPlist p = stack; ! p.IsEmpty;)
+         {
+           MTextProperty prop = (MTextProperty) p.Val;
 
-       if (start < this_start)
+           if ((prop.flags & flags) == flags)
+             p.Pop ();
+           else
+             p = p.Next;
+         }
+      }
+
+      private void merge_front_properties (MPlist plist)
+      {
+       for (MInterval i = left_most_node; i != null; i = i.next)
          {
-           if (end <= this_start)
-             Left.push (prop, start, end);
+           if (! stack.IsEmpty)
+             break;
+           for (MPlist p = plist; ! p.IsEmpty; p = p.Next)
+             {
+               MTextProperty prop = (MTextProperty) p.Val;
+
+               if ((prop.flags & MTextProperty.Flag.RearSticky)
+                   == MTextProperty.Flag.RearSticky)
+                 i.stack.Add (prop.key, prop);
+             }
+         }
+      }
+
+      private void merge_rear_properties (MPlist plist)
+      {
+       for (MInterval i = right_most_node; i != null; i = i.prev)
+         {
+           if (! stack.IsEmpty)
+             break;
+           for (MPlist p = plist; ! p.IsEmpty; p = p.Next)
+             {
+               MTextProperty prop = (MTextProperty) p.Val;
+
+               if ((prop.flags & MTextProperty.Flag.FrontSticky)
+                   == MTextProperty.Flag.FrontSticky)
+                 i.stack.Add (prop.key, prop);
+             }
+         }
+      }
+
+      public void Insert (int pos, MInterval interval)
+      {
+       update_from_to ();
+       Console.Write ("insert({0}) at ", pos); DumpOne (false, true);
+       if (pos < from || (pos == from && left == null && pos > 0))
+         {
+           prev.Insert (pos, interval);
+           return;
+         }
+       if (pos > to || (pos == to && right == null && next != null))
+         {
+           next.Insert (pos, interval);
+           return;
+         }
+       if (pos > from && pos < to)
+         {
+           remove_properties (MTextProperty.Flag.Sensitive);
+           divide_right (pos).Insert (pos, interval);
+           return;
+         }         
+       if (pos == from)
+         {
+           if (pos > 0)
+             {
+               prev.remove_properties
+                 (MTextProperty.Flag.Sensitive|MTextProperty.Flag.RearSticky);
+               interval.merge_front_properties (prev.stack);
+             }
+           remove_properties
+             (MTextProperty.Flag.Sensitive|MTextProperty.Flag.FrontSticky);
+           interval.merge_rear_properties (stack);
+
+           // INTERVAL is ready to insert.
+           //
+           //    .-this-.   ==>          .-this-.
+           // left-.                  left-.
+           //     child                  .-interval
+           //                         child
+
+           if (pos > 0)
+             {
+               MInterval i = left.right_most_node;
+       
+               i.left = interval;
+               interval.parent = i;
+               for (; i != null; i = i.parent)
+                 i.total_length += interval.total_length;
+             }
            else
              {
-               Left.push (prop, start, this_start);
-               if (end < this_end)
-                 {
-                   divide_right (end);
-                   stack.Push (prop);
-                 }
-               else
+               left = interval;
+
+               for (MInterval i = this; i != null; i = i.parent)
+                 i.total_length += interval.total_length;
+             }
+         }
+       else                    // pos == to
+         {
+           if (right != null)
+             {
+               MInterval left_most = right.left_most_node;
+
+               left_most.remove_properties
+                 (MTextProperty.Flag.Sensitive|MTextProperty.Flag.FrontSticky);
+               interval.merge_rear_properties (left_most.stack);
+             }
+           remove_properties
+             (MTextProperty.Flag.Sensitive|MTextProperty.Flag.RearSticky);
+           interval.merge_front_properties (stack);
+
+           // INTERVAL is ready to insert.
+           //
+           //    .-this-.   ==>          .-this-.
+           //         .-right                 .-right
+           //     child                  interval-.
+           //                                    child
+
+           if (right != null)
+             {
+               MInterval i = right.left_most_node;
+       
+               i.left = interval;
+               interval.parent = i;
+               for (; i != null; i = i.parent)
+                 i.total_length += interval.total_length;
+             }
+           else
+             {
+               right = interval;
+
+               for (MInterval i = this; i != null; i = i.parent)
+                 i.total_length += interval.total_length;
+             }
+         }
+      }
+
+      private void vacate_node (MInterval interval)
+      {
+       Console.WriteLine ("vacate #{0} to #{1}", id, interval.id);
+       if (interval != null)
+         interval.parent = parent;
+       if (parent == null)
+         {
+           mtext.intervals.Put (key, interval);
+         }
+       else
+         {
+           if (this == parent.right)
+             parent.right = interval;
+           else
+             parent.left = interval;
+
+           int diff = total_length;
+           if (interval != null)
+             diff -= interval.total_length;
+           for (MInterval i = parent; i != null; i = i.parent)
+             i.total_length -= diff;
+         }
+      }
+
+      public void Delete (int start, int end)
+      {
+       update_from_to ();
+       Console.Write ("delete({0} {1}) at ", start, end); DumpOne (false, true);
+       if (start < from)
+         {
+           if (end <= from)
+             {
+               left.Delete (start, end);
+               return;
+             }
+           left.Delete (start, from);
+           to -= from - start;
+           end -= from - start;
+           from = start;
+         }
+       if (end > to)
+         {
+           if (start >= to)
+             {
+               right.Delete (start, end);
+               return;
+             }
+           right.Delete (to, end);
+           end = to;
+         }
+       if (start == from && end == to)
+         {
+           if (right == null)
+             {
+               vacate_node (left);
+             }
+           else
+             {
+               if (left != null)
                  {
-                   stack.Push (prop);
-                   if (this_end < end)
-                     Right.Push (prop, this_end, end);
+                   MInterval i;
+               
+                   for (i = right; i.left != null; i = i.left)
+                     i.total_length += left.total_length;
+                   i.total_length += left.total_length;
+                   i.left = left;
+                   left.parent = i;
                  }
+               vacate_node (right);
              }
          }
-       else if (start < this_end)
+       else
          {
-           divide_right (start).Push (prop, start, end);
+           int len = end - start;
+
+           for (MInterval i = this; i != null; i = i.parent)
+             i.total_length -= len;
          }
-       else
-         Right.Push (prop, start, end);
+      }
+
+      public void Push (int start, int end, MTextProperty prop)
+      {
+       update_from_to ();
+       Console.Write ("push({0} {1}) at ", start, end); DumpOne (false, true);
+       if (start < from)
+         {
+           if (end <= from)
+             {
+               left.Push (start, end, prop);
+               return;
+             }
+           left.Push (start, from, prop);
+           start = from;
+         }
+       if (end > to)
+         {
+           if (start >= to)
+             {
+               right.Push (start, end, prop);
+               return;
+             }
+           right.Push (to, end, prop);
+           end = to;
+         }
+
+       if (start > from)
+         divide_left (start);
+       if (end < to)
+         divide_right (end);
+       stack.Push (prop.key, prop);
+      }
+
+      public void Pop (int start, int end)
+      {
+       update_from_to ();
+       Console.Write ("pop({0} {1}) at ", start, end); DumpOne (false, true);
+       if (start < from)
+         {
+           if (end <= from)
+             {
+               left.Pop (start, end);
+               return;
+             }
+           left.Pop (start, from);
+           start = from;
+         }
+       if (end > to)
+         {
+           if (start >= to)
+             {
+               right.Pop (start, end);
+               return;
+             }
+           right.Pop (to, end);
+           end = to;
+         }
+
+       if (! stack.IsEmpty)
+         {
+           if (start > from)
+             divide_left (start);
+           if (end < to)
+             divide_right (end);
+           stack.Pop ();
+         }
+      }
+
+      private void DumpOne (bool with_prop, bool newline)
+      {
+       Console.Write ("#{0}({1} {2} {3}", id, total_length, from, to);
+       if (with_prop)
+         foreach (MPlist p in stack)
+           Console.Write (" " + p.Val);
+       Console.Write (")");
+       if (newline)
+         Console.WriteLine ();
+      }
+
+      public void Dump ()
+      {
+       update_from_to ();
+
+       if (left != null)
+         left.Dump ();
+       if (from > 0)
+         Console.Write (" ");
+       DumpOne (true, false);
+       if (right != null)
+         right.Dump ();
+      }
+    }
+
+    private class MTextEnum : IEnumerator
+    {
+      private MText mt;
+      private int pos = -1;
+
+      public MTextEnum (MText mt)
+       {
+         this.mt = mt;
+       }
+
+      public bool MoveNext ()
+      {
+        pos++;
+        return (pos < mt.nchars);
+      }
+
+      public void Reset ()
+      {
+        pos = -1;
+      }
+
+      public object Current
+      {
+        get {
+         //if (pos < 0 || pos >= mt.nchars)
+         //throw new InvalidOperationException ();
+         return mt[pos];
+       }
       }
     }
   }