Beispiel #1
0
        private void SearchForm_VisibleChanged(object sender, EventArgs e)
        {
            if (!Visible)
            {
                return;
            }

            /*
             * If we have an editor see if we have any selected text.
             */

            ScintillaEditForm sci = GetActiveEditor();

            if (sci != null)
            {
                /*
                 * May be multiline so we split at first newline
                 * after normalizing for Windows/Mac/Unix differences.
                 */

                string text = sci.Editor.Selection.Text;
                text = text.Replace("\n", "\r");
                int i = text.IndexOf("\r");
                if (i != -1)
                {
                    text = text.Substring(0, i);
                }

                _findTextComboBox.Text = text;
                _findTextComboBox.SelectAll();
            }
        }
Beispiel #2
0
        private void UI_EDIT_MENU_LINE_ENDING_DropDownOpening(
            object sender, EventArgs e)
        {
            ScintillaEditForm document =
                _mainForm.ActiveDocument as ScintillaEditForm;

            if (document == null)
            {
                return;
            }

            foreach (ToolStripMenuItem item in
                     _editMenuLineEnding.DropDownItems)
            {
                item.Checked = false;
            }

            if (document.LineEnding == EndOfLineMode.Crlf)
            {
                _editMenuLineEndingCRLF.Checked = true;
            }
            if (document.LineEnding == EndOfLineMode.LF)
            {
                _editMenuLineEndingLF.Checked = true;
            }
            if (document.LineEnding == EndOfLineMode.CR)
            {
                _editMenuLineEndingCR.Checked = true;
            }
        }
Beispiel #3
0
        private void UI_EDIT_MENU_ENCODING_DropDownOpening(
            object sender, EventArgs e)
        {
            ScintillaEditForm document =
                _mainForm.ActiveDocument as ScintillaEditForm;

            if (document == null)
            {
                return;
            }

            foreach (ToolStripMenuItem item in
                     _editMenuEncoding.DropDownItems)
            {
                item.Checked = false;
            }

            if (document.FileEncoding == Encoding.ASCII)
            {
                _editMenuEncodingANSI.Checked = true;
            }
            if (document.FileEncoding == Encoding.UTF8)
            {
                _editMenuEncodingUTF8.Checked = true;
            }
            if (document.FileEncoding == Encoding.BigEndianUnicode)
            {
                _editMenuEncodingUTF16BE.Checked = true;
            }
            if (document.FileEncoding == Encoding.Unicode)
            {
                _editMenuEncodingUTF16LE.Checked = true;
            }
        }
Beispiel #4
0
        private void InsertSnippet_Click(object sender, EventArgs e)
        {
            ToolStripMenuItem item = sender as ToolStripMenuItem;

            if (item == null)
            {
                return;
            }

            ScintillaEditForm document =
                _mainForm.ActiveDocument as ScintillaEditForm;

            if (document == null)
            {
                return;
            }

            string snippet = FileTools.ReadFile(item.Name);

            if (String.IsNullOrEmpty(snippet))
            {
                return;
            }

            document.InsertSnippet(snippet);
        }
Beispiel #5
0
        private void FindNextRegex(string searchText, string replaceText, bool isReplace)
        {
            ScintillaEditForm sci = GetActiveEditor();

            if (sci == null)
            {
                return;
            }

            Regex searchRegex = new Regex(searchText);

            // Is there an instance anywhere in the document?
            string text = sci.Editor.Text;

            Match match = searchRegex.Match(text);

            if (!match.Success)
            {
                _searchMessageLabel.Text = Resources.FindDialogMessageNotFound;
                return;
            }

            // Have at least one instance, search from the cursor if not at document end.
            if (sci.Editor.CurrentPos < sci.Editor.Text.Length)
            {
                match = searchRegex.Match(text, sci.Editor.CurrentPos);
            }

            if (!match.Success)    // Search from the start of the document
            {
                match = searchRegex.Match(text);
            }

            if (!match.Success)    // Shouldn't happen
            {
                return;
            }

            sci.Editor.Selection.Start  = match.Index;
            sci.Editor.Selection.Length = match.Length;

            if (_isReplace)
            {
                sci.Editor.Selection.Text   = replaceText;
                sci.Editor.Selection.Start  = match.Index;
                sci.Editor.Selection.Length = replaceText.Length;
            }
        }
        /// <summary>
        /// Toggle the current document as read-only.
        /// </summary>
        /// <returns>True on success.</returns>
        public bool SetReadOnly()
        {
            ToolStripMenuItem menuItem = _mainForm.GetMenuItemByName(
                Constants.UI_EDIT_MENU_SET_READ_ONLY);

            if (menuItem == null) return false;

            if (scintilla.IsReadOnly)
            {
                /*
                 * Check not already open as read/write.
                 */

                foreach (IDockContent dockContent in 
                    _mainForm.ClientWindow.Documents)
                {
                    ScintillaEditForm f = dockContent as ScintillaEditForm;
                    if (f == null) continue;
                    if (f.FilePath == null) continue;
                    if (documentFilePath == null) continue;
                    if (FileTools.MatchPaths(f.FilePath, documentFilePath) &&
                        !f.Editor.IsReadOnly)
                    {
                        MessageBox.Show(
                            Resources.ReadWriteMessage,
                            Resources.ReadWriteTitle,
                            MessageBoxButtons.OK, MessageBoxIcon.Information);

                        UpdateTab();
                        f.Activate();
                        return false;
                    }
                }

                menuItem.Checked = false;
                scintilla.IsReadOnly = false;
            }
            else
            {
                menuItem.Checked = true;
                scintilla.IsReadOnly = true;
            }

            UpdateTab();

            return true;
        }
Beispiel #7
0
        private void FindAllRegex(string searchText, string replaceText, bool isReplace)
        {
            ScintillaEditForm sci = GetActiveEditor();

            if (sci == null)
            {
                return;
            }

            // Clear all bookmarks
            sci.Editor.Markers.DeleteAll(Constants.BOOKMARK_MARKER);

            Regex           searchRegex = new Regex(searchText);
            MatchCollection matches     = searchRegex.Matches(sci.Editor.Text);

            int lineNumber = -1;
            int lineCount  = 0;

            foreach (Match match in matches)
            {
                Line line = sci.Editor.Lines.FromPosition(match.Index);
                if (line == null)
                {
                    continue;
                }

                // Only report each line once.
                if (lineNumber != line.Number)
                {
                    if (!isReplace)
                    {
                        line.AddMarker(Constants.BOOKMARK_MARKER);
                    }

                    lineNumber = line.Number;
                    lineCount++;
                }
            }

            if (isReplace)
            {
                /*
                 * Can't track the actual change locations using this method so
                 * we add a sentinel string to mark the lines on which the changes
                 * occur. Then we bookmark each line with a sentinel and remove it.
                 * This is a bit hacky and would break if the sentinel actually
                 * appears in the text but it's the only way to do multi-line
                 * replaces and still preserve the bookmarks in the right places.
                 */

                string sentinel = "{^¬%`^}";

                sci.Editor.UndoRedo.BeginUndoAction();

                sci.Editor.Text = searchRegex.Replace(
                    sci.Editor.Text, sentinel + replaceText);

                foreach (Line line in sci.Editor.Lines)
                {
                    if (line.Text.IndexOf(sentinel) != -1)
                    {
                        string s = line.Text.Replace(sentinel, String.Empty);

                        /*
                         * Line needs to be trimmed of any EOL characters to prevent
                         * extra blank lines being inserted.
                         */

                        line.Text = s.TrimEnd(new Char[] { '\n', '\r' });
                        line.AddMarker(Constants.BOOKMARK_MARKER);
                    }
                }

                sci.Editor.UndoRedo.EndUndoAction();
            }

            if (lineCount == 1)
            {
                _searchMessageLabel.Text = String.Format(isReplace ?
                                                         Resources.FindDialogMessageReplacedAllSingle :
                                                         Resources.FindDialogMessageFoundSingle, lineCount);
            }
            else
            {
                _searchMessageLabel.Text = String.Format(isReplace ?
                                                         Resources.FindDialogMessageReplacedAllMulti :
                                                         Resources.FindDialogMessageFoundMulti, lineCount);
            }
        }
Beispiel #8
0
        private void FindAll(string searchText, string replaceText,
                             bool isReplace, bool matchCase)
        {
            ScintillaEditForm sci = GetActiveEditor();

            if (sci == null)
            {
                return;
            }

            // Clear all bookmarks
            sci.Editor.Markers.DeleteAll(Constants.BOOKMARK_MARKER);

            // Replace is always case-sensitive
            if (isReplace)
            {
                matchCase = true;
            }

            if (!matchCase)
            {
                searchText = searchText.ToLower();
            }

            int lineCount = 0;

            if (isReplace)
            {
                sci.Editor.UndoRedo.BeginUndoAction();

                foreach (Line line in sci.Editor.Lines)
                {
                    string text = line.Text;

                    if (text.IndexOf(searchText) != -1)
                    {
                        string s = line.Text.Replace(searchText, replaceText);

                        /*
                         * Line needs to be trimmed of any EOL characters to prevent
                         * extra blank lines being inserted.
                         */

                        line.Text = s.TrimEnd(new Char[] { '\n', '\r' });
                        line.AddMarker(Constants.BOOKMARK_MARKER);
                        lineCount++;
                    }
                }

                sci.Editor.UndoRedo.EndUndoAction();
            }
            else
            {
                foreach (Line line in sci.Editor.Lines)
                {
                    string text = matchCase ? line.Text : line.Text.ToLower();

                    if (text.IndexOf(searchText) != -1)
                    {
                        line.AddMarker(Constants.BOOKMARK_MARKER);
                        lineCount++;
                    }
                }
            }

            if (lineCount == 1)
            {
                _searchMessageLabel.Text = String.Format(isReplace ?
                                                         Resources.FindDialogMessageReplacedAllSingle :
                                                         Resources.FindDialogMessageFoundSingle, lineCount);
            }
            else
            {
                _searchMessageLabel.Text = String.Format(isReplace ?
                                                         Resources.FindDialogMessageReplacedAllMulti :
                                                         Resources.FindDialogMessageFoundMulti, lineCount);
            }
        }
Beispiel #9
0
        private void FindNext(string searchText, string replaceText,
                              bool isReplace, bool matchCase)
        {
            ScintillaEditForm sci = GetActiveEditor();

            if (sci == null)
            {
                return;
            }

            // Replace is always case-sensitive
            if (isReplace)
            {
                matchCase = true;
            }

            if (!matchCase)
            {
                searchText = searchText.ToLower();
            }

            /*
             * Is there an instance anywhere in the document?
             */

            string text = matchCase ?
                          sci.Editor.Text : sci.Editor.Text.ToLower();

            int index = text.IndexOf(searchText);

            if (index == -1)
            {
                _searchMessageLabel.Text = Resources.FindDialogMessageNotFound;
                return;
            }

            /*
             * We have at least one instance, search from the cursor if not at document end.
             */

            if (sci.Editor.CurrentPos < sci.Editor.Text.Length)
            {
                index = text.IndexOf(searchText, sci.Editor.CurrentPos);
            }

            if (index == -1)    // Search from the start of the document
            {
                index = text.IndexOf(searchText);
            }

            if (index == -1)    // Shouldn't happen
            {
                return;
            }

            sci.Editor.Selection.Start  = index;
            sci.Editor.Selection.Length = searchText.Length;

            if (_isReplace)
            {
                sci.Editor.Selection.Text   = replaceText;
                sci.Editor.Selection.Start  = index;
                sci.Editor.Selection.Length = replaceText.Length;
            }
        }
Beispiel #10
0
        private void Search(bool searchAll)
        {
            _isSearchAll = searchAll;

            ScintillaEditForm sci = GetActiveEditor();

            if (sci == null)
            {
                _searchMessageLabel.Text = Resources.FindDialogMessageCantSearch;
                return;
            }

            _findTextComboBox.BackColor = Color.Empty;
            _searchMessageLabel.Text    = String.Empty;

            string searchText = _findTextComboBox.Text;

            if (searchText == String.Empty)
            {
                _findTextComboBox.BackColor = Color.Yellow;
                return;
            }

            AddHistoryItem(_findTextComboBox, searchText);

            string replaceText = String.Empty;

            if (_isReplace)
            {
                replaceText = _replaceTextComboBox.Text;
                AddHistoryItem(_replaceTextComboBox, replaceText);
            }

            bool isRegex   = _useRegexCheckBox.Checked;
            bool matchCase = _matchCaseCheckBox.Checked;

            try
            {
                Cursor = Cursors.WaitCursor;

                if (_isSearchAll)
                {
                    if (isRegex)
                    {
                        FindAllRegex(searchText, replaceText, _isReplace);
                    }
                    else
                    {
                        FindAll(searchText, replaceText, _isReplace, matchCase);
                    }
                }
                else
                {
                    if (isRegex)
                    {
                        FindNextRegex(searchText, replaceText, _isReplace);
                    }
                    else
                    {
                        FindNext(searchText, replaceText, _isReplace, matchCase);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(String.Format("{0}:\n{1}",
                                              Resources.FindDialogExceptionMessage, ex.Message),
                                _isReplace ?
                                Resources.FindDialogTitleReplace :
                                Resources.FindDialogTitleFind,
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Error);
            }
            finally
            {
                Cursor = Cursors.Default;
            }
        }