/// <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 /// [Curtis_Beard] 05/18/2006 FIX: 1723815, use correct whole word matching regex /// </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" + __Grep.SearchText + "\\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" + __Grep.SearchText + "\\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(FileSearch.Core.GeneralSettings.ResultsForeColor); // txtHits.SelectionBackColor = Common.ConvertStringToColor(FileSearch.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(FileSearch.Core.GeneralSettings.HighlightForeColor); // txtHits.SelectionBackColor = Common.ConvertStringToColor(FileSearch.Core.GeneralSettings.HighlightBackColor); txtHits.SelectedText = _textToSearch.Substring(_item.Index, _item.Length); // set the end text txtHits.SelectionColor = Common.ConvertStringToColor(FileSearch.Core.GeneralSettings.ResultsForeColor); // txtHits.SelectionBackColor = Common.ConvertStringToColor(FileSearch.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(FileSearch.Core.GeneralSettings.ResultsForeColor); // txtHits.SelectionBackColor = Common.ConvertStringToColor(FileSearch.Core.GeneralSettings.ResultsBackColor); txtHits.SelectedText = _textToSearch; } } }
/// <summary> /// Checks to see if the given line is already recognized as a hit. /// </summary> /// <param name="line">line to check</param> /// <param name="hit">HitObject containing all previous hits</param> /// <returns>True if found, False otherwise</returns> /// <history> /// [Curtis_Beard] 07/28/2006 Created /// </history> private bool HitExists(string line, HitObject hit) { for (int i = 0; i < hit.LineCount; i++) { if (hit.RetrieveLine(i).IndexOf(line) > -1) return true; } return false; }
/// <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(FileSearch.Core.GeneralSettings.ResultsForeColor); txtHits.BackColor = Common.ConvertStringToColor(FileSearch.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(FileSearch.Core.GeneralSettings.ResultsForeColor); // txtHits.SelectionBackColor = Common.ConvertStringToColor(FileSearch.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(FileSearch.Core.GeneralSettings.HighlightForeColor); // txtHits.SelectionBackColor = Common.ConvertStringToColor(FileSearch.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(FileSearch.Core.GeneralSettings.ResultsForeColor); // txtHits.SelectionBackColor = Common.ConvertStringToColor(FileSearch.Core.GeneralSettings.ResultsBackColor); txtHits.SelectedText = _end; } }while (_pos > -1); } else { // set default color, no search text found txtHits.SelectionColor = Common.ConvertStringToColor(FileSearch.Core.GeneralSettings.ResultsForeColor); // txtHits.SelectionBackColor = Common.ConvertStringToColor(FileSearch.Core.GeneralSettings.ResultsBackColor); txtHits.SelectedText = _textToSearch; } } } }