コード例 #1
0
ファイル: frmMain.cs プロジェクト: joshball/astrogrep
        /// <summary>
        /// Highlight the found matches in the given HitObject based on a regular expression.
        /// </summary>
        /// <param name="hit">HitObject to highlight</param>
        /// <remarks>Called from HighlightText only.</remarks>
        private void HighlightTextRegEx(HitObject hit)
        {
            string _textToSearch = string.Empty;
             string _tempString = string.Empty;
             int _lastPos = 0;
             Regex _regEx = new Regex(gpGrep.SearchText);
             MatchCollection _col;
             Match _item;

             // Loop through hits and highlight search for text
             for (int _index = 0; _index < hit.LineCount; _index++)
             {
            TextIter locIter = txtViewer.Buffer.GetIterAtOffset(txtViewer.Buffer.Text.Length);

            //Retrieve hit text
            _textToSearch = hit.RetrieveLine(_index);

            // find all reg ex matches in line
            if (gpGrep.UseCaseSensitivity && gpGrep.UseWholeWordMatching)
            {
               _regEx = new Regex("\\b\\w*" + gpGrep.SearchText + "\\w*\\b");
               _col = _regEx.Matches(_textToSearch);
            }
            else if (gpGrep.UseCaseSensitivity)
            {
               _regEx = new Regex(gpGrep.SearchText);
               _col = _regEx.Matches(_textToSearch);
            }
            else if (gpGrep.UseWholeWordMatching)
            {
               _regEx = new Regex("\\b\\w*" + gpGrep.SearchText + "\\w*\\b", RegexOptions.IgnoreCase);
               _col = _regEx.Matches(_textToSearch);
            }
            else
            {
               _regEx = new Regex(gpGrep.SearchText, RegexOptions.IgnoreCase);
               _col = _regEx.Matches(_textToSearch);
            }

            //loop through the matches
            _lastPos = 0;
            for (int _counter = 0; _counter < _col.Count; _counter++)
            {
               _item = _col[_counter];

               // set the start text
               // check for empty string to prevent assigning nothing to selection text preventing
               //   a system beep
               _tempString = _textToSearch.Substring(_lastPos, _item.Index - _lastPos);
               if (!_tempString.Equals(string.Empty))
                  txtViewer.Buffer.Insert(ref locIter, _tempString);

               //set the hit text
               txtViewer.Buffer.InsertWithTagsByName(ref locIter, _textToSearch.Substring(_item.Index, _item.Length), new string[1] {"highlight"});

               //set the end text
               if (_counter + 1 >= _col.Count)
               {
                  // no more hits so just set the rest
                  txtViewer.Buffer.Insert(ref locIter, _textToSearch.Substring(_item.Index + _item.Length));
                  _lastPos = _item.Index + _item.Length;
               }
               else
               {
                  // another hit so just set inbetween
                  txtViewer.Buffer.Insert(ref locIter, _textToSearch.Substring(_item.Index + _item.Length, _col[_counter + 1].Index - (_item.Index + _item.Length)));
                  _lastPos = _col[_counter + 1].Index;
               }
            }

            // no match, just a context line
            if (_col.Count == 0)
               txtViewer.Buffer.Insert(ref locIter, _textToSearch);
             }
        }
コード例 #2
0
ファイル: Grep.cs プロジェクト: joshball/astrogrep
        /// <summary>
        /// Search a given file for the searchText.
        /// </summary>
        /// <param name="file">FileInfo object for file to search for searchText</param>
        /// <param name="searchText">Text to find in file</param>
        /// <history>
        /// [Curtis_Beard]      09/08/2005	Created
        /// [Curtis_Beard]      11/21/2005	ADD: update hit count when actual line added
        /// [Curtis_Beard]      12/02/2005	CHG: use SearchingFile instead of StatusMessage
        /// [Curtis_Beard]      04/21/2006	CHG: use a regular expression match collection to get
        ///                                 correct count of hits in a line when using RegEx
        /// [Curtis_Beard]      07/03/2006	FIX: 1500174, use a FileStream to open the files readonly
        /// [Curtis_Beard]      07/07/2006	FIX: 1512029, RegEx use Case Sensitivity and WholeWords,
        ///                                 also use different whole word matching regex
        /// [Curtis_Beard]      07/26/2006	ADD: 1512026, column position
        /// [Curtis_Beard]      07/26/2006	FIX: 1530023, retrieve file with correct encoding
        /// [Curtis_Beard]      09/12/2006  CHG: Converted to C#
        /// [Curtis_Beard]      09/28/2006	FIX: check for any plugins before looping through them      
        /// </history>
        private void SearchFile(FileInfo file, string searchText)
        {
            const int MARGINSIZE = 4;

             // Raise SearchFile Event
             if (SearchingFile != null)
            SearchingFile(file);

             FileStream _stream = null;
             StreamReader _reader = null;
             int _lineNumber = 0;
             HitObject _grepHit = null;
             Regex _regularExp = null;
             MatchCollection _regularExpCol = null;
             int _posInStr = -1;
             bool _hitOccurred = false;
             bool _fileNameDisplayed = false;
             string[] _context = new string[10];
             int _contextIndex = 0;
             int _lastHit = 0;
             int _contextLinesCount = ContextLines;
             string _contextSpacer = string.Empty;
             string _spacer = string.Empty;

             try
             {
            // Process plugins
            if (__Plugins != null)
            {
               for (int i = 0; i < __Plugins.Count; i++)
               {
                  // find a valid plugin for this file type
                  if (__Plugins[i].Enabled &&
                     __Plugins[i].Plugin.IsAvailable &&
                     __Plugins[i].Plugin.Extensions.IndexOf(file.Extension) > -1)
                  {

                     // setup plugin options
                     __Plugins[i].Plugin.ContextLines = this.ContextLines;
                     __Plugins[i].Plugin.IncludeLineNumbers = this.IncludeLineNumbers;
                     __Plugins[i].Plugin.ReturnOnlyFileNames = this.ReturnOnlyFileNames;
                     __Plugins[i].Plugin.UseCaseSensitivity = this.UseCaseSensitivity;
                     __Plugins[i].Plugin.UseRegularExpressions = this.UseRegularExpressions;
                     __Plugins[i].Plugin.UseWholeWordMatching = this.UseWholeWordMatching;

                     // load plugin and perform grep
                     __Plugins[i].Plugin.Load();
                     _grepHit = __Plugins[i].Plugin.Grep(file, searchText);
                     __Plugins[i].Plugin.Unload();

                     // check for a hit
                     if (_grepHit != null)
                     {
                        // only perform is not using negation
                        if (!this.UseNegation)
                        {
                           _grepHit.Index = __grepCollection.Count;
                           __grepCollection.Add(__grepCollection.Count, _grepHit);
                           if (FileHit != null)
                              FileHit(file, _grepHit.Index);

                           if (this.ReturnOnlyFileNames)
                              _grepHit.SetHitCount();

                           if (LineHit != null)
                              LineHit(_grepHit, _grepHit.Index);
                        }
                     }
                     else if (this.UseNegation)
                     {
                        // no hit but using negation so create one
                        _grepHit = new HitObject(file);
                        _grepHit.Index = __grepCollection.Count;
                        __grepCollection.Add(__grepCollection.Count, _grepHit);
                        if (FileHit != null)
                           FileHit(file, _grepHit.Index);
                     }

                     return;
                  }
               }
            }

            _stream = file.Open(FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
            _reader = new StreamReader(_stream, System.Text.Encoding.Default);

            // Default spacer (left margin) values. If Line Numbers are on,
            // these will be reset within the loop to include line numbers.
            if (_contextLinesCount > 0)
            {
               _contextSpacer = new string(char.Parse(" "), MARGINSIZE);
               _spacer = _contextSpacer.Substring(MARGINSIZE - 2) + "> ";
            }
            else
               _spacer = new string(char.Parse(" "), MARGINSIZE);

            do
            {
               string textLine = _reader.ReadLine();

               if (textLine == null)
                  break;
               else
               {
                  _lineNumber += 1;

                  if (UseRegularExpressions)
                  {
                     _posInStr = -1;
                     if (textLine.Length > 0)
                     {
                        if (UseCaseSensitivity && UseWholeWordMatching)
                        {
                           _regularExp = new Regex("\\b\\w*" + searchText + "\\w*\\b");
                           _regularExpCol = _regularExp.Matches(textLine);
                        }
                        else if (UseCaseSensitivity)
                        {
                           _regularExp = new Regex(searchText);
                           _regularExpCol = _regularExp.Matches(textLine);
                        }
                        else if (UseWholeWordMatching)
                        {
                           _regularExp = new Regex("\\b\\w*" + searchText + "\\w*\\b", RegexOptions.IgnoreCase);
                           _regularExpCol = _regularExp.Matches(textLine);
                        }
                        else
                        {
                           _regularExp = new Regex(searchText, RegexOptions.IgnoreCase);
                           _regularExpCol = _regularExp.Matches(textLine);
                        }

                        if (_regularExpCol.Count > 0)
                        {
                           if (UseNegation)
                              _hitOccurred = true;

                           _posInStr = 1;
                        }
                     }
                  }
                  else
                  {
                     if (UseCaseSensitivity)

                        // Need to escape these characters in SearchText:
                        // < $ + * [ { ( ) .
                        // with a preceeding \

                        // If we are looking for whole worlds only, perform the check.
                        if (UseWholeWordMatching)
                        {
                           _regularExp = new Regex("\\b\\w*" + searchText + "\\w*\\b");
                           if (_regularExp.IsMatch(textLine))
                           {
                              if (UseNegation)
                                 _hitOccurred = true;

                              _posInStr = 1;
                           }
                           else
                              _posInStr = -1;
                        }
                        else
                        {
                           //_posInStr = InStr(1, textLine, searchText, CompareMethod.Binary);
                           _posInStr = textLine.IndexOf(searchText);

                           if (UseNegation && _posInStr > -1)
                              _hitOccurred = true;
                        }
                     else
                     {
                        // If we are looking for whole worlds only, perform the check.
                        if (UseWholeWordMatching)
                        {
                           _regularExp = new Regex("\\b\\w*" + searchText + "\\w*\\b", RegexOptions.IgnoreCase);
                           if (_regularExp.IsMatch(textLine))
                           {
                              if (UseNegation)
                                 _hitOccurred = true;

                              _posInStr = 1;
                           }
                           else
                              _posInStr = -1;
                        }
                        else
                        {
                           //_posInStr = InStr(1, textLine, searchText, CompareMethod.Text);
                           _posInStr = textLine.ToLower().IndexOf(searchText.ToLower());

                           if (UseNegation && _posInStr > -1)
                              _hitOccurred = true;
                        }
                     }
                  }

                  //*******************************************
                  // We found an occurrence of our search text.
                  //*******************************************
                  if (_posInStr > -1)
                  {
                     //since we have a hit, check to see if negation is checked
                     if (UseNegation)
                        break;

                     if (!_fileNameDisplayed)
                     {
                        _grepHit = new HitObject(file);
                        _grepHit.Index = __grepCollection.Count;
                        __grepCollection.Add(__grepCollection.Count, _grepHit);

                        if (FileHit != null)
                           FileHit(file, _grepHit.Index);

                        _fileNameDisplayed = true;
                     }

                     // If we are only showing filenames, go to the next file.
                     if (ReturnOnlyFileNames)
                     {
                        //notify that at least 1 hit is in file
                        _grepHit.SetHitCount();
                        if (LineHit != null)
                           LineHit(_grepHit, _grepHit.Index);

                        break;
                     }

                     // Set up line number, or just an indention in front of the line.
                     if (IncludeLineNumbers)
                     {
                        _spacer = "(" + _lineNumber.ToString().Trim();
                        if (_spacer.Length <= 5)
                           _spacer = _spacer + new string(char.Parse(" "), 6 - _spacer.Length);

                        _spacer = _spacer + ") ";
                        _contextSpacer = "(" + new string(char.Parse(" "), _spacer.Length - 3) + ") ";
                     }

                     // Display context lines if applicable.
                     if (_contextLinesCount > 0 && _lastHit == 0)
                     {
                        if (_grepHit.LineCount > 0)
                        {
                           // Insert a blank space before the context lines.
                           int _pos = _grepHit.Add("\r\n", -1);
                           if (LineHit != null)
                              LineHit(_grepHit, _pos);
                        }

                        // Display preceeding n context lines before the hit.
                        for (_posInStr = _contextLinesCount; _posInStr >= 1; _posInStr--)
                        {
                           _contextIndex = _contextIndex + 1;
                           if (_contextIndex > _contextLinesCount)
                              _contextIndex = 1;

                           // If there is a match in the first one or two lines,
                           // the entire preceeding context may not be available.
                           if (_lineNumber > _posInStr)
                           {
                              // Add the context line.
                              int _pos = _grepHit.Add(_contextSpacer + _context[_contextIndex] + "\r\n", _lineNumber - _posInStr);
                              if (LineHit != null)
                                 LineHit(_grepHit, _pos);
                           }
                        }
                     }

                     _lastHit = _contextLinesCount;

                     //
                     // Add the actual "hit".
                     //
                     // set first hit column position
                     if (UseRegularExpressions)
                     {
                        // zero based
                        _posInStr = _regularExpCol[0].Index + 1;
                     }

                     int _index = _grepHit.Add(_spacer + textLine + "\r\n", _lineNumber, _posInStr);

                     if (UseRegularExpressions)
                        _grepHit.SetHitCount(_regularExpCol.Count);
                     else
                     {
                        //determine number of hits
                        _grepHit.SetHitCount(RetrieveLineHitCount(textLine, searchText));
                     }

                     if (LineHit != null)
                        LineHit(_grepHit, _index);
                  }
                  else if (_lastHit > 0 && _contextLinesCount > 0)
                  {
                     //***************************************************
                     // We didn't find a hit, but since lastHit is > 0, we
                     // need to display this context line.
                     //***************************************************
                     int _index = _grepHit.Add(_contextSpacer + textLine + "\r\n", _lineNumber);
                     if (LineHit != null)
                        LineHit(_grepHit, _index);
                     _lastHit -= 1;

                  } // Found a hit or not.

                  // If we are showing context lines, keep the last n lines.
                  if (_contextLinesCount > 0)
                  {
                     if (_contextIndex == _contextLinesCount)
                        _contextIndex = 1;
                     else
                        _contextIndex += 1;

                     _context[_contextIndex] = textLine;
                  }
               }
            }
            while (true);

            //
            // Check for no hits through out the file
            //
            if (UseNegation && _hitOccurred == false)
            {
               //add the file to the hit list
               if (!_fileNameDisplayed)
               {
                  _grepHit = new HitObject(file);
                  _grepHit.Index = __grepCollection.Count;
                  __grepCollection.Add(__grepCollection.Count, _grepHit);
                  if (FileHit != null)
                     FileHit(file, _grepHit.Index);
               }
            }
             }
             finally
             {
            if (_reader != null)
               _reader.Close();

            if (_stream != null)
               _stream.Close();
             }
        }
コード例 #3
0
ファイル: frmMain.cs プロジェクト: joshball/astrogrep
 /// <summary>
 /// 
 /// </summary>
 /// <param name="hit"></param>
 /// <param name="index"></param>
 private void gpGrep_LineHit(HitObject hit, int index)
 {
     //Gtk.Application.Invoke(hit, null, new System.EventHandler(UpdateCount));
 }
コード例 #4
0
ファイル: frmMain.cs プロジェクト: joshball/astrogrep
        /// <summary>
        /// Highlight the found matches in the given HitObject.
        /// </summary>
        /// <param name="hit">HitObject to highlight</param>
        private void HighlightText(HitObject hit)
        {
            string textToSearch = string.Empty;
             string searchText = gpGrep.SearchText;
             string tempLine = string.Empty;
             int pos = -1;
             bool highlight = false;
             string begin = string.Empty;
             string text = string.Empty;
             string end = string.Empty;

             // clear contents
             txtViewer.Buffer.Text = "";

             if (gpGrep.UseRegularExpressions)
            HighlightTextRegEx(hit);
             else
             {
            // Loop through hits and highlight search for text
            for (int i = 0; i < hit.LineCount; i++)
            {
               TextIter locIter = txtViewer.Buffer.GetIterAtOffset(txtViewer.Buffer.Text.Length);

               //Retrieve hit text
               textToSearch = hit.RetrieveLine(i);

               tempLine = textToSearch;

               if (gpGrep.UseCaseSensitivity)
                  pos = tempLine.IndexOf(searchText);
               else
                  pos = tempLine.ToLower().IndexOf(searchText.ToLower());

               if (pos > -1)
               {
                  while (pos > -1)
                  {
                     highlight = false;

                     begin = tempLine.Substring(0, pos);
                     text = tempLine.Substring(pos, searchText.Length);
                     end = tempLine.Substring(pos + searchText.Length);

                     txtViewer.Buffer.Insert(ref locIter, begin);

                     if (gpGrep.UseWholeWordMatching)
                        highlight = Grep.WholeWordOnly(begin, end);
                     else
                        highlight = true;

                     if (highlight)
                        txtViewer.Buffer.InsertWithTagsByName(ref locIter, text, new string[1] {"highlight"});
                     else
                        txtViewer.Buffer.Insert(ref locIter, text);

                     //Check remaining for other matches
                     if (gpGrep.UseCaseSensitivity)
                        pos = end.IndexOf(searchText);
                     else
                        pos = end.ToLower().IndexOf(searchText.ToLower());

                     //if no more hits in line, insert last part
                     tempLine = end;
                     if (pos < 0)
                        txtViewer.Buffer.Insert(ref locIter, end);
                  }
               }
               else
                  txtViewer.Buffer.Insert(ref locIter, textToSearch);
            }
             }
        }
コード例 #5
0
ファイル: frmMain.cs プロジェクト: joshball/astrogrep
        /// <summary>
        /// Highlight the searched text in the results when using regular expressions
        /// </summary>
        /// <param name="hit">Hit Object containing results</param>
        /// <history>
        /// [Curtis_Beard]	   04/21/2006	Created
        /// [Curtis_Beard]	   05/11/2006	FIX: Include context lines if present and prevent system beep
        /// [Curtis_Beard]	   07/07/2006	FIX: 1512029, highlight whole word and case sensitive matches
        /// [Curtis_Beard] 	   09/28/2006	FIX: use grep object for settings instead of gui items, remove searchText parameter
        /// </history>
        private void HighlightTextRegEx(HitObject hit)
        {
            string _textToSearch = string.Empty;
             string _tempString = string.Empty;
             int _index = 0;
             int _lastPos = 0;
             int _counter = 0;
             Regex _regEx = new Regex(__Grep.SearchText);
             MatchCollection _col;
             Match _item;

             // Loop through hits and highlight search for text
             for (_index = 0; _index < hit.LineCount; _index++)
             {
            // Retrieve hit text
            _textToSearch = hit.RetrieveLine(_index);

            // Set default font
            txtHits.SelectionFont = new Font("Courier New", 9.75F, FontStyle.Regular);

            // find all reg ex matches in line
            if (__Grep.UseCaseSensitivity && __Grep.UseWholeWordMatching)
            {
               _regEx = new Regex("\\b\\w*" + __Grep.SearchText + "\\w*\\b");
               _col = _regEx.Matches(_textToSearch);
            }
            else if (__Grep.UseCaseSensitivity)
            {
               _regEx = new Regex(__Grep.SearchText);
               _col = _regEx.Matches(_textToSearch);
            }
            else if (__Grep.UseWholeWordMatching)
            {
               _regEx = new Regex("\\b\\w*" + __Grep.SearchText + "\\w*\\b", RegexOptions.IgnoreCase);
               _col = _regEx.Matches(_textToSearch);
            }
            else
            {
               _regEx = new Regex(__Grep.SearchText, RegexOptions.IgnoreCase);
               _col = _regEx.Matches(_textToSearch);
            }

            // loop through the matches
            _lastPos = 0;
            for (_counter = 0; _counter < _col.Count; _counter++)
            {
               _item = _col[_counter];

               // set the start text
               txtHits.SelectionColor = Common.ConvertStringToColor(AstroGrep.Core.GeneralSettings.ResultsForeColor);
               // txtHits.SelectionBackColor = Common.ConvertStringToColor(AstroGrep.Core.GeneralSettings.ResultsBackColor);

               // check for empty string to prevent assigning nothing to selection text preventing
               //  a system beep
               _tempString = _textToSearch.Substring(_lastPos, _item.Index - _lastPos);
               if (!_tempString.Equals(string.Empty))
                  txtHits.SelectedText = _tempString;

               // set the hit text
               txtHits.SelectionColor = Common.ConvertStringToColor(AstroGrep.Core.GeneralSettings.HighlightForeColor);
               // txtHits.SelectionBackColor = Common.ConvertStringToColor(AstroGrep.Core.GeneralSettings.HighlightBackColor);
               txtHits.SelectedText = _textToSearch.Substring(_item.Index, _item.Length);

               // set the end text
               txtHits.SelectionColor = Common.ConvertStringToColor(AstroGrep.Core.GeneralSettings.ResultsForeColor);
               // txtHits.SelectionBackColor = Common.ConvertStringToColor(AstroGrep.Core.GeneralSettings.ResultsBackColor);
               if (_counter + 1 >= _col.Count)
               {
                  //  no more hits so just set the rest
                  txtHits.SelectedText = _textToSearch.Substring(_item.Index + _item.Length);
                  _lastPos = _item.Index + _item.Length;
               }
               else
               {
                  // another hit so just set inbetween
                  txtHits.SelectedText = _textToSearch.Substring(_item.Index + _item.Length, _col[_counter + 1].Index - (_item.Index + _item.Length));
                  _lastPos = _col[_counter + 1].Index;
               }
            }

            if (_col.Count == 0)
            {
               //  no match, just a context line
               txtHits.SelectionColor = Common.ConvertStringToColor(AstroGrep.Core.GeneralSettings.ResultsForeColor);
               // txtHits.SelectionBackColor = Common.ConvertStringToColor(AstroGrep.Core.GeneralSettings.ResultsBackColor);
               txtHits.SelectedText = _textToSearch;
            }
             }
        }
コード例 #6
0
ファイル: frmMain.cs プロジェクト: joshball/astrogrep
        /// <summary>
        /// Highlight the searched text in the results
        /// </summary>
        /// <param name="hit">Hit Object containing results</param>
        /// <history>
        /// [Curtis_Beard]	   01/27/2005	Created
        /// [Curtis_Beard]	   04/12/2005	FIX: 1180741, Don't capitalize hit line
        /// [Curtis_Beard]	   11/18/2005	ADD: custom highlight colors
        /// [Curtis_Beard] 	   12/06/2005	CHG: call WholeWordOnly from Grep class
        /// [Curtis_Beard] 	   04/21/2006	CHG: highlight regular expression searches
        /// [Curtis_Beard] 	   09/28/2006	FIX: use grep object for settings instead of gui items
        /// </history>
        private void HighlightText(HitObject hit)
        {
            string _textToSearch = string.Empty;
             string _searchText = __Grep.SearchText;
             int _index = 0;
             string _tempLine = string.Empty;

             string _begin = string.Empty;
             string _text = string.Empty;
             string _end = string.Empty;
             int _pos = 0;
             bool _highlight = false;

             // Clear the contents
             txtHits.Text = string.Empty;
             txtHits.ForeColor = Common.ConvertStringToColor(AstroGrep.Core.GeneralSettings.ResultsForeColor);
             txtHits.BackColor = Common.ConvertStringToColor(AstroGrep.Core.GeneralSettings.ResultsBackColor);

             if (__Grep.UseRegularExpressions)
            HighlightTextRegEx(hit);
             else
             {
            // Loop through hits and highlight search for text
            for (_index = 0; _index < hit.LineCount; _index++)
            {
               // Retrieve hit text
               _textToSearch = hit.RetrieveLine(_index);

               // Set default font
               txtHits.SelectionFont = new Font("Courier New", 9.75F, FontStyle.Regular);

               _tempLine = _textToSearch;

               // attempt to locate the text in the line
               if (__Grep.UseCaseSensitivity)
                  _pos = _tempLine.IndexOf(_searchText);
               else
                  _pos = _tempLine.ToLower().IndexOf(_searchText.ToLower());

               if (_pos > -1)
               {
                  do
                  {
                     _highlight = false;

                     //
                     // retrieve parts of text
                     _begin = _tempLine.Substring(0, _pos);
                     _text = _tempLine.Substring(_pos, _searchText.Length);
                     _end = _tempLine.Substring(_pos + _searchText.Length);

                     // set default color for starting text
                     txtHits.SelectionColor = Common.ConvertStringToColor(AstroGrep.Core.GeneralSettings.ResultsForeColor);
                     // txtHits.SelectionBackColor = Common.ConvertStringToColor(AstroGrep.Core.GeneralSettings.ResultsBackColor);
                     txtHits.SelectedText = _begin;

                     // do a check to see if begin and end are valid for wholeword searches
                     if (__Grep.UseWholeWordMatching)
                        _highlight = Grep.WholeWordOnly(_begin, _end);
                     else
                        _highlight = true;

                     // set highlight color for searched text
                     if (_highlight)
                     {
                        txtHits.SelectionColor = Common.ConvertStringToColor(AstroGrep.Core.GeneralSettings.HighlightForeColor);
                        // txtHits.SelectionBackColor = Common.ConvertStringToColor(AstroGrep.Core.GeneralSettings.HighlightBackColor);
                     }
                     txtHits.SelectedText = _text;

                     // Check remaining string for other hits in same line
                     if (__Grep.UseCaseSensitivity)
                        _pos = _end.IndexOf(_searchText);
                     else
                        _pos = _end.ToLower().IndexOf(_searchText.ToLower());

                     // set default color for end, if no more hits in line
                     _tempLine = _end;
                     if (_pos < 0)
                     {
                        txtHits.SelectionColor = Common.ConvertStringToColor(AstroGrep.Core.GeneralSettings.ResultsForeColor);
                        // txtHits.SelectionBackColor = Common.ConvertStringToColor(AstroGrep.Core.GeneralSettings.ResultsBackColor);
                        txtHits.SelectedText = _end;
                     }

                  }while (_pos > -1);
               }
               else
               {
                  // set default color, no search text found
                  txtHits.SelectionColor = Common.ConvertStringToColor(AstroGrep.Core.GeneralSettings.ResultsForeColor);
                  // txtHits.SelectionBackColor = Common.ConvertStringToColor(AstroGrep.Core.GeneralSettings.ResultsBackColor);
                  txtHits.SelectedText = _textToSearch;
               }
            }
             }
        }
コード例 #7
0
ファイル: frmMain.cs プロジェクト: joshball/astrogrep
        /// <summary>
        /// Updates the count column (Thread safe)
        /// </summary>
        /// <param name="hit">HitObject that contains updated information</param>
        /// <history>
        ///    [Curtis_Beard]    11/21/2005   Created
        /// </history>
        private void UpdateHitCount(HitObject hit)
        {
            // Makes this a thread safe operation
             if (lstFileNames.InvokeRequired)
             {
            UpdateHitCountCallBack _delegate = new UpdateHitCountCallBack(UpdateHitCount);
            lstFileNames.Invoke(_delegate, new object[1] {hit});
            return;
             }

             // find correct item to update
             foreach (ListViewItem _item in lstFileNames.Items)
             {
            if (int.Parse(_item.SubItems[Constants.COLUMN_INDEX_GREP_INDEX].Text) == hit.Index)
            {
               _item.SubItems[Constants.COLUMN_INDEX_COUNT].Text = hit.HitCount.ToString();
               break;
            }
             }
        }
コード例 #8
0
ファイル: frmMain.cs プロジェクト: joshball/astrogrep
 /// <summary>
 /// A line has been detected to contain the searching text
 /// </summary>
 /// <param name="hit">The HitObject that contains the line</param>
 /// <param name="index">The position in the HitObject's line collection</param>
 /// <history>
 /// 	 [Curtis_Beard]    11/04/2005   Created
 /// </history>
 private void ReceiveLineHit(HitObject hit, int index)
 {
     // update count column
      UpdateHitCount(hit);
 }
コード例 #9
0
        /// <summary>

        /// Searches the given file for the given search text.

        /// </summary>

        /// <param name="path">Fully qualified file path</param>

        /// <param name="searchText">Text to locate</param>

        /// <returns>Hitobject containing grep results, null if (not found or error</returns>

        /// <history>

        ///     [Curtis_Beard]		07/27/2006	Created

        /// </history>

        public HitObject Grep(string path, string searchText)

        {
            if (this.IsAvailable && this.IsUsable)

            {
                try

                {
                    if (File.Exists(path))

                    {
                        const int MARGINSIZE = 4;

                        int count = 0;

                        HitObject hit = null;

                        int prevLine = 0;

                        int prevPage = 0;

                        string _spacer = new string(' ', MARGINSIZE);

                        string _contextSpacer = string.Empty;



                        if (__contextLines > 0)

                        {
                            _contextSpacer = new string(' ', MARGINSIZE);

                            //_spacer = Right(_contextSpacer, MARGINSIZE - 2) + "> ";

                            _spacer = _contextSpacer.Substring(_contextSpacer.Length - MARGINSIZE - 2) + "> ";
                        }

                        else
                        {
                            _spacer = new string(' ', MARGINSIZE);
                        }



                        // Open a given Word document as readonly

                        object wordDocument = OpenDocument(path, true);



                        // Get Selection Property

                        __WordSelection = __WordType.InvokeMember("Selection", BindingFlags.GetProperty,

                                                                  null, __WordApplication, null);



                        // create range and find objects

                        object range = GetProperty(wordDocument, "Content");

                        object find = GetProperty(range, "Find");



                        // setup find

                        RunRoutine(find, "ClearFormatting", null);

                        SetProperty(find, "Forward", true);

                        SetProperty(find, "Text", searchText);

                        SetProperty(find, "MatchWholeWord", __wholeWordMatch);

                        SetProperty(find, "MatchCase", __caseSensistiveMatch);



                        // start find

                        FindExecute(find);



                        // keep finding text

                        while ((bool)GetProperty(find, "Found") == true)

                        {
                            count += 1;



                            if (count == 1)

                            {
                                // create hit object

                                hit = new HitObject(path);
                            }



                            // since a hit was found and only displaying file names, quickly exit

                            if (__onlyFileNames)
                            {
                                break;
                            }



                            // retrieve find information

                            int start = (int)GetProperty(range, "Start");

                            int colNum = (int)Information(range, WdInformation.wdFirstCharacterColumnNumber);

                            int lineNum = (int)Information(range, WdInformation.wdFirstCharacterLineNumber);

                            int pageNum = (int)Information(range, WdInformation.wdActiveEndPageNumber);

                            string line = GetFindTextLine(start);



                            // don't add a hit if (on same line

                            if (!(prevLine == lineNum && prevPage == pageNum))

                            {
                                // check for line numbers

                                if (__includeLineNumbers)

                                {
                                    // setup line header

                                    _spacer = "(" + string.Format("{0},{1}", lineNum, pageNum);

                                    if (_spacer.Length <= 5)

                                    {
                                        //_spacer = _spacer + Space(6 - _spacer.Length);

                                        _spacer = _spacer + new string(' ', 6 - _spacer.Length);
                                    }

                                    _spacer = _spacer + ") ";

                                    //_contextSpacer = "(" + Space(_spacer.Length - 3) + ") ";

                                    _contextSpacer = "(" + new string(' ', _spacer.Length - 3) + ") ";
                                }



                                //  remove any odd characters from the text

                                line = RemoveSpecialCharacters(line);



                                // add context lines before

                                // if (__contextLines > 0){

                                //    For i As int = __contextLines To 1 Step -1

                                //       SetProperty(__WordSelection, "Start", start)

                                //       SelectionMoveUp(WdUnits.wdLine, i, WdMovementType.wdMove)

                                //       Dim cxt As string = GetFindTextLine()

                                //       cxt = RemoveSpecialCharacters(cxt)



                                //       if (Not HitExists(cxt, hit)){

                                //          hit.Add(_contextSpacer & cxt & NEW_LINE, lineNum - i, 1)

                                //       End If

                                //    Next

                                // End If



                                // add line

                                hit.Add(_spacer + line + NEW_LINE, lineNum, colNum);



                                // add context lines after

                                // if (__contextLines > 0){

                                //    For i As int = 1 To __contextLines

                                //       SetProperty(__WordSelection, "Start", start)

                                //       SelectionMoveDown(WdUnits.wdLine, i, WdMovementType.wdMove)

                                //       Dim cxt As string = GetFindTextLine()

                                //       cxt = RemoveSpecialCharacters(cxt)



                                //       if (Not HitExists(cxt, hit)){

                                //          hit.Add(_contextSpacer & cxt & NEW_LINE, lineNum + i, 1)

                                //       End If

                                //    Next

                                // End If
                            }

                            hit.SetHitCount();



                            prevLine = lineNum;

                            prevPage = pageNum;



                            // find again

                            FindExecute(find);
                        }



                        ReleaseSelection();

                        CloseDocument(wordDocument);



                        return(hit);
                    }
                }

                catch (Exception ex)

                {
                    Trace(ex.ToString());
                }
            }



            return(null);
        }