private void ReplaceAll(FindDialog.SettingsInfo settings)
        {
            if (String.IsNullOrEmpty(settings.FindText))
            {
                return;
            }

            ITextStorage find = textEditControl.TextStorageFactory.FromUtf16Buffer(
                settings.FindText,
                0,
                settings.FindText.Length,
                Environment.NewLine);
            ITextStorage replace = textEditControl.TextStorageFactory.FromUtf16Buffer(
                settings.ReplaceText,
                0,
                settings.ReplaceText.Length,
                Environment.NewLine);

            using (IDisposable undoGroup = textEditControl.UndoOpenGroup())
            {
                SelPoint start, end;
                if (settings.RestrictToSelection)
                {
                    textEditControl.UndoSaveSelection();
                    start = textEditControl.SelectionStart;
                    end = textEditControl.SelectionEnd;
                }
                else
                {
                    start = new SelPoint(0, 0);
                    end = new SelPoint(textEditControl.Count - 1, textEditControl.GetLine(textEditControl.Count - 1).Length);
                }

                textEditControl.SetInsertionPoint(start);
                while (textEditControl.Find(find, settings.CaseSensitive, settings.MatchWholeWord, false/*wrap*/, false/*up*/)
                    && (textEditControl.SelectionStart <= end)
                    && (textEditControl.SelectionEnd <= end))
                {
                    end = textEditControl.AdjustForRemove(end, textEditControl.Selection);
                    end = textEditControl.AdjustForInsert(end, textEditControl.SelectionStart, replace);
                    textEditControl.SelectedTextStorage = replace;
                    textEditControl.SetInsertionPoint(textEditControl.SelectionEndLine, textEditControl.SelectionEndCharPlusOne);
                }

                if (settings.RestrictToSelection)
                {
                    textEditControl.SetSelection(start, end, false/*startIsActive*/);
                }
            }

            textEditControl.ScrollToSelection();
        }
 public SelRange(SelPoint start, SelPoint end)
 {
     if (start.CompareTo(end) > 0)
     {
         throw new ArgumentException();
     }
     this.Start = start;
     this.End = end;
 }
        private void previousUTF16SurrogatePairToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SelPoint position = textEditControl.SelectionActive;
            ITextLine currentLine = null;
            IDecodedTextLine currentLineDecoded = null;
            SelPoint zero = new SelPoint();
            while (position > zero)
            {
                if (currentLine == null)
                {
                    currentLine = textEditControl[position.Line];
                    currentLineDecoded = currentLine.Decode_MustDispose();
                }

                if (position.Column >= 2)
                {
                    if (Char.IsHighSurrogate(currentLineDecoded[position.Column - 2])
                        && Char.IsLowSurrogate(currentLineDecoded[position.Column - 1]))
                    {
                        textEditControl.SetSelection(
                            new SelPoint(position.Line, position.Column - 2),
                            position,
                            true/*selectStartIsActive*/);
                        return;
                    }
                }

                position.Column--;
                if (position.Column < 0)
                {
                    position = new SelPoint(position.Line - 1, 0);
                    currentLine = null;
                    currentLineDecoded = null;
                    if (position.Line >= 0)
                    {
                        position.Column = textEditControl[position.Line].Length;
                    }
                }
            }
            textEditControl.ErrorBeep();
        }
 public int CompareTo(SelPoint other)
 {
     int c;
     c = this.Line.CompareTo(other.Line);
     if (c == 0)
     {
         c = this.Column.CompareTo(other.Column);
     }
     return c;
 }
        // info about UTF-16 surrogates ("Supplementary Characters")
        // https://msdn.microsoft.com/en-us/library/windows/desktop/dd374069%28v=vs.85%29.aspx
        // examples here:
        // https://en.wikipedia.org/wiki/UTF-16#Examples
        // http://www.i18nguy.com/unicode-example-plane1.html
        private void nextUTF16SurrogatePairToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SelPoint position = textEditControl.SelectionActive;
            ITextLine currentLine = null;
            IDecodedTextLine currentLineDecoded = null;
            while (position < textEditControl.End)
            {
                if (currentLine == null)
                {
                    currentLine = textEditControl[position.Line];
                    currentLineDecoded = currentLine.Decode_MustDispose();
                }

                if (position.Column <= currentLineDecoded.Length - 2)
                {
                    if (Char.IsHighSurrogate(currentLineDecoded[position.Column])
                        && Char.IsLowSurrogate(currentLineDecoded[position.Column + 1]))
                    {
                        textEditControl.SetSelection(
                            position,
                            new SelPoint(position.Line, position.Column + 2),
                            false/*selectStartIsActive*/);
                        return;
                    }
                }

                position.Column++;
                if (position.Column >= currentLine.Length)
                {
                    position = new SelPoint(position.Line + 1, 0);
                    currentLine = null;
                    currentLineDecoded = null;
                }
            }
            textEditControl.ErrorBeep();
        }