diff --git a/src/apps/text_search/GrepWindow.cpp b/src/apps/text_search/GrepWindow.cpp index 8f687fd86b..6e1a1a63e1 100644 --- a/src/apps/text_search/GrepWindow.cpp +++ b/src/apps/text_search/GrepWindow.cpp @@ -109,6 +109,7 @@ GrepWindow::GrepWindow(BMessage* message) fGrepper(NULL), fOldPattern(""), + fIncludeFilesGlob(""), fModel(new (nothrow) Model()), fLastNodeMonitorEvent(system_time()), fChangesIterator(NULL), @@ -235,6 +236,10 @@ void GrepWindow::MessageReceived(BMessage* message) _OnSearchText(); break; + case MSG_SEARCH_GLOB_FILTER: + _OnGlobFilterChange(); + break; + case MSG_SELECT_HISTORY: _OnHistoryItem(message); break; @@ -547,6 +552,14 @@ GrepWindow::_CreateViews() fSearchText->TextView()->SetMaxBytes(1000); fSearchText->SetModificationMessage(new BMessage(MSG_SEARCH_TEXT)); + fGlobText = new BTextControl( + "GlobText", B_TRANSLATE("File filter:"), NULL, NULL, + B_WILL_DRAW | B_FULL_UPDATE_ON_RESIZE | B_NAVIGABLE); + + fGlobText->TextView()->SetMaxBytes(20); + fGlobText->SetModificationMessage(new BMessage(MSG_SEARCH_GLOB_FILTER)); + fGlobText->SetToolTip(B_TRANSLATE("Only search files matching the given pattern, e.g. \"*.h\".")); + fButton = new BButton( "Button", B_TRANSLATE("Search"), new BMessage(MSG_START_CANCEL)); @@ -573,13 +586,16 @@ GrepWindow::_LayoutViews() BLayoutBuilder::Group<>(this, B_VERTICAL, 0) .SetInsets(0, 0, -1, -1) .Add(fMenuBar) - .AddGrid(B_USE_HALF_ITEM_INSETS) + .AddGroup(B_VERTICAL, B_USE_SMALL_SPACING) .SetInsets(B_USE_WINDOW_SPACING, B_USE_WINDOW_SPACING, B_USE_WINDOW_SPACING, B_USE_DEFAULT_SPACING) - .Add(fSearchText, 0, 0, 3) - .Add(fShowLinesCheckbox, 0, 1) - .Add(BSpaceLayoutItem::CreateGlue(), 1, 1) - .Add(fButton, 2, 1) + .Add(fSearchText) + .AddGroup(B_HORIZONTAL, B_USE_DEFAULT_SPACING) + .Add(fGlobText, 1.0) + .Add(fShowLinesCheckbox) + .AddGlue(0.5) + .Add(fButton) + .End() .End() .AddGroup(B_VERTICAL, 0) .SetInsets(-2, 0, -1, -1) @@ -737,6 +753,7 @@ GrepWindow::_OnStartCancel() // displaying the names of the files we are grepping. fSearchText->SetModificationMessage(NULL); + fGlobText->SetModificationMessage(NULL); fFileMenu->SetEnabled(false); fActionMenu->SetEnabled(false); @@ -745,6 +762,7 @@ GrepWindow::_OnStartCancel() fEncodingMenu->SetEnabled(false); fSearchText->SetEnabled(false); + fGlobText->SetEnabled(false); fButton->MakeFocus(true); fButton->SetLabel(B_TRANSLATE("Cancel")); @@ -762,7 +780,7 @@ GrepWindow::_OnStartCancel() _SetWindowTitle(); FileIterator* iterator = new (nothrow) InitialIterator(fModel); - fGrepper = new (nothrow) Grepper(fOldPattern.String(), fModel, + fGrepper = new (nothrow) Grepper(fOldPattern.String(), fIncludeFilesGlob.String(), fModel, this, iterator); if (fGrepper != NULL && fGrepper->IsValid()) fGrepper->Start(); @@ -810,6 +828,8 @@ GrepWindow::_OnSearchFinished() fSearchText->SetText(fOldPattern.String()); fSearchText->TextView()->SelectAll(); fSearchText->SetModificationMessage(new BMessage(MSG_SEARCH_TEXT)); + fGlobText->SetEnabled(true); + fGlobText->SetModificationMessage(new BMessage(MSG_SEARCH_GLOB_FILTER)); PostMessage(MSG_START_NODE_MONITORING); } @@ -968,7 +988,7 @@ GrepWindow::_OnNodeMonitorPulse() fChangesIterator->PrintToStream(); #endif - fGrepper = new (nothrow) Grepper(fOldPattern.String(), fModel, + fGrepper = new (nothrow) Grepper(fOldPattern.String(), fIncludeFilesGlob.String(), fModel, this, fChangesIterator); if (fGrepper != NULL && fGrepper->IsValid()) { fGrepper->Start(); @@ -1558,6 +1578,15 @@ GrepWindow::_OnSetTargetToParent() } +void +GrepWindow::_OnGlobFilterChange() +{ + CALLED(); + fIncludeFilesGlob = fGlobText->Text(); + _StopNodeMonitoring(); +} + + // #pragma mark - diff --git a/src/apps/text_search/GrepWindow.h b/src/apps/text_search/GrepWindow.h index 9b716f6784..d74fbdbd44 100644 --- a/src/apps/text_search/GrepWindow.h +++ b/src/apps/text_search/GrepWindow.h @@ -74,6 +74,7 @@ private: void _OnSelectAll(BMessage* message); void _OnNewWindow(); void _OnSetTargetToParent(); + void _OnGlobFilterChange(); void _ModelChanged(); bool _OpenInEditor(const entry_ref& ref, int32 lineNum); @@ -85,6 +86,7 @@ private: private: BTextControl* fSearchText; + BTextControl* fGlobText; GrepListView* fSearchResults; BMenuBar* fMenuBar; @@ -121,6 +123,7 @@ private: Grepper* fGrepper; BString fOldPattern; + BString fIncludeFilesGlob; Model* fModel; bigtime_t fLastNodeMonitorEvent; ChangesIterator* fChangesIterator; diff --git a/src/apps/text_search/Grepper.cpp b/src/apps/text_search/Grepper.cpp index 6f89456f4b..3b9a1add4b 100644 --- a/src/apps/text_search/Grepper.cpp +++ b/src/apps/text_search/Grepper.cpp @@ -88,7 +88,7 @@ strdup_from_utf8(uint32 encode, const char* src, int32 length) } -Grepper::Grepper(const char* pattern, const Model* model, +Grepper::Grepper(const char* pattern, const char* glob, const Model* model, const BHandler* target, FileIterator* iterator) : fPattern(NULL), fTarget(target), @@ -107,6 +107,8 @@ Grepper::Grepper(const char* pattern, const Model* model, free(src); } else _SetPattern(pattern); + + fGlob = strdup(glob); } @@ -114,6 +116,7 @@ Grepper::~Grepper() { Cancel(); free(fPattern); + free(fGlob); delete fIterator; } @@ -271,6 +274,13 @@ Grepper::_RunnerThread() argv[argc++] = "-i"; if (! fRegularExpression) argv[argc++] = "-F"; // no a regexp: force fixed string, + + // Limit included files to fGlob pattern, if one was given. + if (strlen(fGlob) > 0) { + argv[argc++] = "--include"; + argv[argc++] = fGlob; + } + // Add double dash argument to tell grep // it's the end of commands options argv[argc++] = "--"; diff --git a/src/apps/text_search/Grepper.h b/src/apps/text_search/Grepper.h index ad977810ea..dd59edf8df 100644 --- a/src/apps/text_search/Grepper.h +++ b/src/apps/text_search/Grepper.h @@ -13,7 +13,7 @@ class Model; // Executes "grep" in a background thread. class Grepper { public: - Grepper(const char* pattern, const Model* model, + Grepper(const char* pattern, const char* glob, const Model* model, const BHandler* target, FileIterator* iterator); virtual ~Grepper(); @@ -43,6 +43,7 @@ private: private: // The (escaped) search pattern. char* fPattern; + char* fGlob; // The settings from the model. BMessenger fTarget; diff --git a/src/apps/text_search/Model.h b/src/apps/text_search/Model.h index 95a07e4b8e..d330efc875 100644 --- a/src/apps/text_search/Model.h +++ b/src/apps/text_search/Model.h @@ -28,6 +28,7 @@ enum { MSG_INVOKE_EDITOR, MSG_CHECKBOX_SHOW_LINES, MSG_SEARCH_TEXT, + MSG_SEARCH_GLOB_FILTER, MSG_INVOKE_ITEM, MSG_SELECT_HISTORY, MSG_NODE_MONITOR_PULSE,