/// <summary> /// Replaces all the search option holders in the given text. /// </summary> /// <param name="text">Text containing holders</param> /// <param name="grep">Grep Object containing options</param> /// <returns>Text with holders replaced</returns> /// <history> /// [Curtis_Beard] 09/05/2006 Created /// </history> public static string ReplaceSearchOptions(string text, AstroGrepBase.Grep grep) { text = text.Replace("%%filetypes%%", "File Types: " + grep.FileFilter); text = text.Replace("%%regex%%", "Regular Expressions: " + grep.UseRegularExpressions.ToString()); text = text.Replace("%%casesen%%", "Case Sensitive: " + grep.UseCaseSensitivity.ToString()); text = text.Replace("%%wholeword%%", "Whole Word: " + grep.UseWholeWordMatching.ToString()); text = text.Replace("%%recurse%%", "Recurse: " + grep.UseRecursion.ToString()); text = text.Replace("%%filenameonly%%", "Show File Names Only: " + grep.ReturnOnlyFileNames.ToString()); text = text.Replace("%%negation%%", "Negation: " + grep.UseNegation.ToString()); text = text.Replace("%%linenumbers%%", "Line Numbers: " + grep.IncludeLineNumbers.ToString()); text = text.Replace("%%contextlines%%", "Context Lines: " + grep.ContextLines.ToString()); text = text.Replace("%%totalfiles%%", grep.Greps.Count.ToString()); text = text.Replace("%%searchterm%%", grep.SearchText); if (grep.UseNegation) { text = text.Replace("%%usenegation%%", "not "); } else { text = text.Replace("%%usenegation%%", string.Empty); } return(text); }
/// <summary> /// Returns the given line with the search text highlighted. /// </summary> /// <param name="line">Line to check</param> /// <param name="grep">Grep Object containing options</param> /// <returns>Line with search text highlighted</returns> /// <history> /// [Curtis_Beard] 09/05/2006 Created /// </history> public static string GetHighlightLine(string line, AstroGrepBase.Grep grep) { string newLine; if (grep.UseRegularExpressions) { newLine = HighlightRegEx(line, grep); } else { newLine = HighlightNormal(line, grep); } return(newLine + "<br />"); }
/// <summary> /// Returns the given line with the search text highlighted. /// </summary> /// <param name="line">Line to check</param> /// <param name="grep">Grep Object containing options</param> /// <returns>Line with search text highlighted</returns> /// <history> /// [Curtis_Beard] 09/05/2006 Created /// </history> private static string HighlightRegEx(string line, AstroGrepBase.Grep grep) { string _textToSearch = string.Empty; string _tempstring = string.Empty; int _lastPos = 0; int _counter = 0; Regex _regEx = new Regex(grep.SearchText); MatchCollection _col; Match _item; string _newLine = string.Empty; //Retrieve hit text _textToSearch = line; // 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]; // 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)) { _newLine += _tempstring; } // set the hit text _newLine += string.Format("<span class=\"searchtext\">{0}</span>", _textToSearch.Substring(_item.Index, _item.Length)); // set the end text if (_counter + 1 >= _col.Count) { // no more hits so just set the rest _newLine += _textToSearch.Substring(_item.Index + _item.Length); _lastPos = _item.Index + _item.Length; } else { // another hit so just set inbetween _newLine += _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 _newLine += _textToSearch; } return(_newLine); }
/// <summary> /// Returns the given line with the search text highlighted. /// </summary> /// <param name="line">Line to check</param> /// <param name="grep">Grep Object containing options</param> /// <returns>Line with search text highlighted</returns> /// <history> /// [Curtis_Beard] 09/05/2006 Created /// </history> private static string HighlightNormal(string line, AstroGrepBase.Grep grep) { string _textToSearch = string.Empty; string _searchText = grep.SearchText; string _tempLine = string.Empty; string _begin = string.Empty; string _text = string.Empty; string _end = string.Empty; int _pos = 0; bool _highlight = false; string _newLine = string.Empty; // Retrieve hit text _textToSearch = line; _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) { while (_pos > -1) { _highlight = false; //retrieve parts of text _begin = _tempLine.Substring(0, _pos); _text = _tempLine.Substring(_pos, _searchText.Length); _end = _tempLine.Substring(_pos + _searchText.Length); _newLine += _begin; // do a check to see if begin and end are valid for wholeword searches if (grep.UseWholeWordMatching) { _highlight = AstroGrepBase.Grep.WholeWordOnly(_begin, _end); } else { _highlight = true; } // set highlight color for searched text if (_highlight) { _newLine += string.Format("<span class=\"searchtext\">{0}</span>", _text); } else { _newLine += _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) { _newLine += _end; } } } else { _newLine += _textToSearch; } return(_newLine); }
/// <summary> /// Start a search. /// </summary> /// <param name="sender">system parameter</param> /// <param name="e">system parameter</param> private void btnSearch_Clicked(object sender, EventArgs e) { if (!ValidateInput()) return; // gui state SetSearchState(null, new SearchEventArgs(false)); sbStatus.Pop(1); txtViewer.Buffer.Clear(); (tvFiles.Model as ListStore).Clear(); alSearchErrors = null; AddComboSelectionSpecial(cboSearchStart, cboSearchStart.ActiveText); AddComboSelection(cboSearchFilter, cboSearchFilter.ActiveText); AddComboSelection(cboSearchText, cboSearchText.ActiveText); // search pattern gpGrep = new Grep(); gpGrep.StartDirectory = cboSearchStart.ActiveText; gpGrep.FileFilter = cboSearchFilter.ActiveText; gpGrep.SearchText = cboSearchText.ActiveText; // exclusion list string[] extensions = Core.GeneralSettings.ExtensionExcludeList.Split(';'); foreach (string ext in extensions) gpGrep.AddExclusionExtension(ext.ToLower()); // options gpGrep.UseRegularExpressions = chkRegularExpressions.Active; gpGrep.UseCaseSensitivity = chkCaseSensitive.Active; gpGrep.UseWholeWordMatching = chkWholeWord.Active; gpGrep.UseRecursion = chkRecurse.Active; gpGrep.ReturnOnlyFileNames = chkFileNamesOnly.Active; gpGrep.UseNegation = chkNegation.Active; gpGrep.IncludeLineNumbers = chkLineNumbers.Active; gpGrep.ContextLines = cboContextLines.Active; // events gpGrep.SearchingFile += new Grep.SearchingFileHandler(gpGrep_SearchingFile); gpGrep.FileHit += new Grep.FileHitHandler(gpGrep_FileHit); gpGrep.LineHit += new Grep.LineHitHandler(gpGrep_LineHit); gpGrep.SearchCancel += new Grep.SearchCancelHandler(gpGrep_SearchCancel); gpGrep.SearchComplete += new Grep.SearchCompleteHandler(gpGrep_SearchComplete); gpGrep.SearchError += new Grep.SearchErrorHandler(gpGrep_SearchError); gpGrep.BeginExecute(); }
/// <summary> /// Start the searching /// </summary> /// <history> /// [Curtis_Beard] 10/17/2005 Created /// [Curtis_Beard] 07/03/2006 FIX: 1516775, Remove trim on the search expression /// [Curtis_Beard] 07/12/2006 CHG: moved thread actions to grep class /// [Curtis_Beard] 11/22/2006 CHG: Remove use of browse in combobox /// </history> private void StartSearch() { string _path; string _fileName; int _index; string _expression; try { _fileName = cboFileName.Text; _path = cboFilePath.Text.Trim(); _expression = cboSearchForText.Text; // update combo selections AddComboSelection(cboSearchForText, _expression); AddComboSelection(cboFileName, _fileName); AddComboSelection(cboFilePath, _path); // Ensure that there is a backslash. if (!_path.EndsWith("\\")) _path += "\\"; // update path and fileName if fileName has a path in it for (_index = _fileName.Length; _index >= 1; _index--) { //if (Mid(_fileName, _index, 1) = "\\") if (_fileName.Substring(_index-1, 1).Equals("\\")) { _path += _fileName.Substring(0, _index); _fileName = _fileName.Substring(_fileName.Length - _index + 1); break; } } // disable gui SetSearchState(false); // Clear the display UpdateStatusBar(string.Empty); ClearItems(); txtHits.Clear(); // begin searching __Grep = new Grep(); SetGrepOptions(); __Grep.StartDirectory = _path; __Grep.FileFilter = _fileName; __Grep.SearchText = _expression; // attach events __Grep.FileHit += new AstroGrep.AstroGrepBase.Grep.FileHitHandler(ReceiveFileHit); __Grep.LineHit += new AstroGrep.AstroGrepBase.Grep.LineHitHandler(ReceiveLineHit); __Grep.SearchCancel += new AstroGrep.AstroGrepBase.Grep.SearchCancelHandler(ReceiveSearchCancel); __Grep.SearchComplete += new AstroGrep.AstroGrepBase.Grep.SearchCompleteHandler(ReceiveSearchComplete); __Grep.SearchError += new AstroGrep.AstroGrepBase.Grep.SearchErrorHandler(ReceiveSearchError); __Grep.SearchingFile += new AstroGrep.AstroGrepBase.Grep.SearchingFileHandler(ReceiveSearchingFile); __Grep.BeginExecute(); } catch (Exception ex) { MessageBox.Show(ex.Message); } }