add support for regular expressions (basic and extended)
it intruduces regex support in search engine and filtering/searching in all screens
This commit is contained in:
@@ -279,6 +279,11 @@ void Browser::GetSelectedSongs(MPD::SongList &v)
|
||||
}
|
||||
}
|
||||
|
||||
void Browser::ApplyFilter(const std::string &s)
|
||||
{
|
||||
w->ApplyFilter(s, itsBrowsedDir == "/" ? 0 : 1, REG_ICASE | Config.regex_type);
|
||||
}
|
||||
|
||||
bool Browser::hasSupportedExtension(const string &file)
|
||||
{
|
||||
size_t last_dot = file.rfind(".");
|
||||
|
||||
@@ -44,7 +44,7 @@ class Browser : public Screen< Menu<MPD::Item> >
|
||||
virtual void ReverseSelection();
|
||||
virtual void GetSelectedSongs(MPD::SongList &);
|
||||
|
||||
virtual void ApplyFilter(const std::string &s) { w->ApplyFilter(s, itsBrowsedDir == "/" ? 0 : 1); }
|
||||
virtual void ApplyFilter(const std::string &);
|
||||
|
||||
virtual List *GetList() { return w; }
|
||||
|
||||
|
||||
@@ -280,6 +280,11 @@ void MediaLibrary::GetSelectedSongs(MPD::SongList &v)
|
||||
}
|
||||
}
|
||||
|
||||
void MediaLibrary::ApplyFilter(const std::string &s)
|
||||
{
|
||||
GetList()->ApplyFilter(s, 0, REG_ICASE | Config.regex_type);
|
||||
}
|
||||
|
||||
void MediaLibrary::NextColumn()
|
||||
{
|
||||
if (w == Artists)
|
||||
|
||||
@@ -53,7 +53,7 @@ class MediaLibrary : public Screen<Window>
|
||||
virtual void ReverseSelection() { Songs->ReverseSelection(); }
|
||||
virtual void GetSelectedSongs(MPD::SongList &);
|
||||
|
||||
virtual void ApplyFilter(const std::string &s) { GetList()->ApplyFilter(s); }
|
||||
virtual void ApplyFilter(const std::string &);
|
||||
|
||||
virtual List *GetList();
|
||||
|
||||
|
||||
47
src/menu.h
47
src/menu.h
@@ -21,6 +21,7 @@
|
||||
#ifndef _MENU_H
|
||||
#define _MENU_H
|
||||
|
||||
#include <regex.h>
|
||||
#include <set>
|
||||
|
||||
#include "window.h"
|
||||
@@ -54,12 +55,12 @@ namespace NCurses
|
||||
void ReverseSelection(size_t = 0);
|
||||
bool Deselect();
|
||||
|
||||
virtual bool Search(const std::string &, size_t = 0, bool = 0) = 0;
|
||||
virtual bool Search(const std::string &, size_t = 0, int = 0) = 0;
|
||||
virtual const std::string &GetSearchConstraint() = 0;
|
||||
virtual void NextFound(bool) = 0;
|
||||
virtual void PrevFound(bool) = 0;
|
||||
|
||||
virtual void ApplyFilter(const std::string &, size_t = 0, bool = 0) = 0;
|
||||
virtual void ApplyFilter(const std::string &, size_t = 0, int = 0) = 0;
|
||||
virtual const std::string &GetFilter() = 0;
|
||||
virtual std::string GetOption(size_t) = 0;
|
||||
|
||||
@@ -132,12 +133,12 @@ namespace NCurses
|
||||
virtual size_t Choice() const;
|
||||
virtual size_t RealChoice() const;
|
||||
|
||||
virtual bool Search(const std::string &constraint, size_t beginning = 0, bool case_sensitive = 0);
|
||||
virtual bool Search(const std::string &constraint, size_t beginning = 0, int flags = 0);
|
||||
virtual const std::string &GetSearchConstraint() { return itsSearchConstraint; }
|
||||
virtual void NextFound(bool wrap);
|
||||
virtual void PrevFound(bool wrap);
|
||||
|
||||
virtual void ApplyFilter(const std::string &filter, size_t beginning = 0, bool case_sensitive = 0);
|
||||
virtual void ApplyFilter(const std::string &filter, size_t beginning = 0, int flags = 0);
|
||||
virtual const std::string &GetFilter();
|
||||
virtual std::string GetOption(size_t pos);
|
||||
|
||||
@@ -596,22 +597,23 @@ template <class T> size_t NCurses::Menu<T>::RealChoice() const
|
||||
return result;
|
||||
}
|
||||
|
||||
template <class T> bool NCurses::Menu<T>::Search(const std::string &constraint, size_t beginning, bool case_sensitive)
|
||||
template <class T> bool NCurses::Menu<T>::Search(const std::string &constraint, size_t beginning, int flags)
|
||||
{
|
||||
itsFound.clear();
|
||||
itsSearchConstraint.clear();
|
||||
if (constraint.empty())
|
||||
return false;
|
||||
itsSearchConstraint = constraint;
|
||||
std::string option;
|
||||
for (size_t i = beginning; i < itsOptionsPtr->size(); i++)
|
||||
regex_t rx;
|
||||
if (regcomp(&rx, itsSearchConstraint.c_str(), flags) == 0)
|
||||
{
|
||||
option = GetOption(i);
|
||||
if (!case_sensitive)
|
||||
ToLower(option);
|
||||
if (option.find(itsSearchConstraint) != std::string::npos)
|
||||
itsFound.insert(i);
|
||||
for (size_t i = beginning; i < itsOptionsPtr->size(); i++)
|
||||
{
|
||||
if (regexec(&rx, GetOption(i).c_str(), 0, 0, 0) == 0)
|
||||
itsFound.insert(i);
|
||||
}
|
||||
}
|
||||
regfree(&rx);
|
||||
return !itsFound.empty();
|
||||
}
|
||||
|
||||
@@ -637,15 +639,13 @@ template <class T> void NCurses::Menu<T>::PrevFound(bool wrap)
|
||||
Highlight(*itsFound.rbegin());
|
||||
}
|
||||
|
||||
template <class T> void NCurses::Menu<T>::ApplyFilter(const std::string &filter, size_t beginning, bool case_sensitive)
|
||||
template <class T> void NCurses::Menu<T>::ApplyFilter(const std::string &filter, size_t beginning, int flags)
|
||||
{
|
||||
if (filter == itsFilter)
|
||||
return;
|
||||
itsFound.clear();
|
||||
ClearFiltered();
|
||||
itsFilter = filter;
|
||||
if (!case_sensitive)
|
||||
ToLower(itsFilter);
|
||||
if (itsFilter.empty())
|
||||
return;
|
||||
for (size_t i = 0; i < beginning; i++)
|
||||
@@ -653,18 +653,19 @@ template <class T> void NCurses::Menu<T>::ApplyFilter(const std::string &filter,
|
||||
itsFilteredRealPositions.push_back(i);
|
||||
itsFilteredOptions.push_back(itsOptions[i]);
|
||||
}
|
||||
std::string option;
|
||||
for (size_t i = beginning; i < itsOptions.size(); i++)
|
||||
regex_t rx;
|
||||
if (regcomp(&rx, itsFilter.c_str(), flags) == 0)
|
||||
{
|
||||
option = GetOption(i);
|
||||
if (!case_sensitive)
|
||||
ToLower(option);
|
||||
if (option.find(itsFilter) != std::string::npos)
|
||||
for (size_t i = beginning; i < itsOptions.size(); i++)
|
||||
{
|
||||
itsFilteredRealPositions.push_back(i);
|
||||
itsFilteredOptions.push_back(itsOptions[i]);
|
||||
if (regexec(&rx, GetOption(i).c_str(), 0, 0, 0) == 0)
|
||||
{
|
||||
itsFilteredRealPositions.push_back(i);
|
||||
itsFilteredOptions.push_back(itsOptions[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
regfree(&rx);
|
||||
itsOptionsPtr = &itsFilteredOptions;
|
||||
if (itsOptionsPtr->empty()) // oops, we didn't find anything
|
||||
Window::Clear();
|
||||
|
||||
@@ -1493,7 +1493,7 @@ int main(int argc, char *argv[])
|
||||
if (!findme.empty())
|
||||
ShowMessage("Searching...");
|
||||
|
||||
bool success = mList->Search(findme, myScreen == mySearcher ? SearchEngine::StaticOptions : 0);
|
||||
bool success = mList->Search(findme, myScreen == mySearcher ? SearchEngine::StaticOptions : 0, REG_ICASE | Config.regex_type);
|
||||
|
||||
if (findme.empty())
|
||||
continue;
|
||||
|
||||
@@ -138,6 +138,11 @@ void Playlist::GetSelectedSongs(MPD::SongList &v)
|
||||
}
|
||||
}
|
||||
|
||||
void Playlist::ApplyFilter(const std::string &s)
|
||||
{
|
||||
w->ApplyFilter(s, 0, REG_ICASE | Config.regex_type);
|
||||
}
|
||||
|
||||
void Playlist::Sort()
|
||||
{
|
||||
if (w->GetWidth() < SortDialogWidth || w->GetHeight() < SortDialogHeight)
|
||||
|
||||
@@ -48,7 +48,7 @@ class Playlist : public Screen< Menu<MPD::Song> >
|
||||
virtual void ReverseSelection() { w->ReverseSelection(); }
|
||||
virtual void GetSelectedSongs(MPD::SongList &);
|
||||
|
||||
virtual void ApplyFilter(const std::string &s) { w->ApplyFilter(s); }
|
||||
virtual void ApplyFilter(const std::string &);
|
||||
|
||||
virtual List *GetList() { return w; }
|
||||
|
||||
|
||||
@@ -287,6 +287,11 @@ void PlaylistEditor::GetSelectedSongs(MPD::SongList &v)
|
||||
}
|
||||
}
|
||||
|
||||
void PlaylistEditor::ApplyFilter(const std::string &s)
|
||||
{
|
||||
GetList()->ApplyFilter(s, 0, REG_ICASE | Config.regex_type);
|
||||
}
|
||||
|
||||
List *PlaylistEditor::GetList()
|
||||
{
|
||||
if (w == Playlists)
|
||||
|
||||
@@ -44,7 +44,7 @@ class PlaylistEditor : public Screen<Window>
|
||||
virtual void ReverseSelection() { Content->ReverseSelection(); }
|
||||
virtual void GetSelectedSongs(MPD::SongList &);
|
||||
|
||||
virtual void ApplyFilter(const std::string &s) { GetList()->ApplyFilter(s); }
|
||||
virtual void ApplyFilter(const std::string &);
|
||||
|
||||
virtual List *GetList();
|
||||
|
||||
|
||||
@@ -32,7 +32,7 @@ using std::string;
|
||||
|
||||
SearchEngine *mySearcher = new SearchEngine;
|
||||
|
||||
const char *SearchEngine::NormalMode = "Match if tag contains searched phrase";
|
||||
const char *SearchEngine::NormalMode = "Match if tag contains searched phrase (regexes supported)";
|
||||
const char *SearchEngine::StrictMode = "Match only if both values are the same";
|
||||
|
||||
size_t SearchEngine::StaticOptions = 20;
|
||||
@@ -307,6 +307,11 @@ void SearchEngine::GetSelectedSongs(MPD::SongList &v)
|
||||
}
|
||||
}
|
||||
|
||||
void SearchEngine::ApplyFilter(const std::string &s)
|
||||
{
|
||||
w->ApplyFilter(s, StaticOptions, REG_ICASE | Config.regex_type);
|
||||
}
|
||||
|
||||
void SearchEngine::UpdateFoundList()
|
||||
{
|
||||
bool bold = 0;
|
||||
@@ -392,7 +397,7 @@ void SearchEngine::Search()
|
||||
bool any_found = 1;
|
||||
bool found = 1;
|
||||
|
||||
if (!CaseSensitive)
|
||||
if (!CaseSensitive && !MatchToPattern)
|
||||
{
|
||||
string t;
|
||||
t = s.Any();
|
||||
@@ -434,9 +439,10 @@ void SearchEngine::Search()
|
||||
|
||||
for (SongList::const_iterator it = list.begin(); it != list.end(); it++)
|
||||
{
|
||||
(*it)->CopyPtr(CaseSensitive || MatchToPattern);
|
||||
Song copy = **it;
|
||||
|
||||
if (!CaseSensitive)
|
||||
if (!CaseSensitive && !MatchToPattern)
|
||||
{
|
||||
string t;
|
||||
t = copy.GetArtist();
|
||||
@@ -471,41 +477,83 @@ void SearchEngine::Search()
|
||||
ToLower(t);
|
||||
copy.SetComment(t);
|
||||
}
|
||||
else
|
||||
copy.SetFile(copy.GetName());
|
||||
|
||||
if (MatchToPattern)
|
||||
{
|
||||
regex_t rx;
|
||||
|
||||
if (!s.Any().empty())
|
||||
any_found =
|
||||
copy.GetArtist().find(s.Any()) != string::npos
|
||||
|| copy.GetTitle().find(s.Any()) != string::npos
|
||||
|| copy.GetAlbum().find(s.Any()) != string::npos
|
||||
|| copy.GetFile().find(s.Any()) != string::npos
|
||||
|| copy.GetComposer().find(s.Any()) != string::npos
|
||||
|| copy.GetPerformer().find(s.Any()) != string::npos
|
||||
|| copy.GetGenre().find(s.Any()) != string::npos
|
||||
|| copy.GetYear().find(s.Any()) != string::npos
|
||||
|| copy.GetComment().find(s.Any()) != string::npos;
|
||||
{
|
||||
if (regcomp(&rx, s.Any().c_str(), ((CaseSensitive ? 0 : REG_ICASE) | Config.regex_type) | Config.regex_type) == 0)
|
||||
{
|
||||
any_found =
|
||||
regexec(&rx, copy.GetArtist().c_str(), 0, 0, 0) == 0
|
||||
|| regexec(&rx, copy.GetTitle().c_str(), 0, 0, 0) == 0
|
||||
|| regexec(&rx, copy.GetAlbum().c_str(), 0, 0, 0) == 0
|
||||
|| regexec(&rx, copy.GetName().c_str(), 0, 0, 0) == 0
|
||||
|| regexec(&rx, copy.GetComposer().c_str(), 0, 0, 0) == 0
|
||||
|| regexec(&rx, copy.GetPerformer().c_str(), 0, 0, 0) == 0
|
||||
|| regexec(&rx, copy.GetGenre().c_str(), 0, 0, 0) == 0
|
||||
|| regexec(&rx, copy.GetYear().c_str(), 0, 0, 0) == 0
|
||||
|| regexec(&rx, copy.GetComment().c_str(), 0, 0, 0) == 0;
|
||||
}
|
||||
regfree(&rx);
|
||||
}
|
||||
|
||||
if (found && !s.GetArtist().empty())
|
||||
found = copy.GetArtist().find(s.GetArtist()) != string::npos;
|
||||
{
|
||||
if (regcomp(&rx, s.GetArtist().c_str(), (CaseSensitive ? 0 : REG_ICASE) | Config.regex_type) == 0)
|
||||
found = regexec(&rx, copy.GetArtist().c_str(), 0, 0, 0) == 0;
|
||||
regfree(&rx);
|
||||
}
|
||||
if (found && !s.GetTitle().empty())
|
||||
found = copy.GetTitle().find(s.GetTitle()) != string::npos;
|
||||
{
|
||||
if (regcomp(&rx, s.GetTitle().c_str(), (CaseSensitive ? 0 : REG_ICASE) | Config.regex_type) == 0)
|
||||
found = regexec(&rx, copy.GetTitle().c_str(), 0, 0, 0) == 0;
|
||||
regfree(&rx);
|
||||
}
|
||||
if (found && !s.GetAlbum().empty())
|
||||
found = copy.GetAlbum().find(s.GetAlbum()) != string::npos;
|
||||
{
|
||||
if (regcomp(&rx, s.GetAlbum().c_str(), (CaseSensitive ? 0 : REG_ICASE) | Config.regex_type) == 0)
|
||||
found = regexec(&rx, copy.GetAlbum().c_str(), 0, 0, 0) == 0;
|
||||
regfree(&rx);
|
||||
}
|
||||
if (found && !s.GetFile().empty())
|
||||
found = copy.GetFile().find(s.GetFile()) != string::npos;
|
||||
{
|
||||
if (regcomp(&rx, s.GetFile().c_str(), (CaseSensitive ? 0 : REG_ICASE) | Config.regex_type) == 0)
|
||||
found = regexec(&rx, copy.GetName().c_str(), 0, 0, 0) == 0;
|
||||
regfree(&rx);
|
||||
}
|
||||
if (found && !s.GetComposer().empty())
|
||||
found = copy.GetComposer().find(s.GetComposer()) != string::npos;
|
||||
{
|
||||
if (regcomp(&rx, s.GetComposer().c_str(), (CaseSensitive ? 0 : REG_ICASE) | Config.regex_type) == 0)
|
||||
found = regexec(&rx, copy.GetComposer().c_str(), 0, 0, 0) == 0;
|
||||
regfree(&rx);
|
||||
}
|
||||
if (found && !s.GetPerformer().empty())
|
||||
found = copy.GetPerformer().find(s.GetPerformer()) != string::npos;
|
||||
{
|
||||
if (regcomp(&rx, s.GetPerformer().c_str(), (CaseSensitive ? 0 : REG_ICASE) | Config.regex_type) == 0)
|
||||
found = regexec(&rx, copy.GetPerformer().c_str(), 0, 0, 0) == 0;
|
||||
regfree(&rx);
|
||||
}
|
||||
if (found && !s.GetGenre().empty())
|
||||
found = copy.GetGenre().find(s.GetGenre()) != string::npos;
|
||||
{
|
||||
if (regcomp(&rx, s.GetGenre().c_str(), (CaseSensitive ? 0 : REG_ICASE) | Config.regex_type) == 0)
|
||||
found = regexec(&rx, copy.GetGenre().c_str(), 0, 0, 0) == 0;
|
||||
regfree(&rx);
|
||||
}
|
||||
if (found && !s.GetYear().empty())
|
||||
found = copy.GetYear().find(s.GetYear()) != string::npos;
|
||||
{
|
||||
if (regcomp(&rx, s.GetYear().c_str(), (CaseSensitive ? 0 : REG_ICASE) | Config.regex_type) == 0)
|
||||
found = regexec(&rx, copy.GetYear().c_str(), 0, 0, 0) == 0;
|
||||
regfree(&rx);
|
||||
}
|
||||
if (found && !s.GetComment().empty())
|
||||
found = copy.GetComment().find(s.GetComment()) != string::npos;
|
||||
{
|
||||
if (regcomp(&rx, s.GetComment().c_str(), (CaseSensitive ? 0 : REG_ICASE) | Config.regex_type) == 0)
|
||||
found = regexec(&rx, copy.GetComment().c_str(), 0, 0, 0) == 0;
|
||||
regfree(&rx);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -514,7 +562,7 @@ void SearchEngine::Search()
|
||||
copy.GetArtist() == s.Any()
|
||||
|| copy.GetTitle() == s.Any()
|
||||
|| copy.GetAlbum() == s.Any()
|
||||
|| copy.GetFile() == s.Any()
|
||||
|| copy.GetName() == s.Any()
|
||||
|| copy.GetComposer() == s.Any()
|
||||
|| copy.GetPerformer() == s.Any()
|
||||
|| copy.GetGenre() == s.Any()
|
||||
@@ -528,7 +576,7 @@ void SearchEngine::Search()
|
||||
if (found && !s.GetAlbum().empty())
|
||||
found = copy.GetAlbum() == s.GetAlbum();
|
||||
if (found && !s.GetFile().empty())
|
||||
found = copy.GetFile() == s.GetFile();
|
||||
found = copy.GetName() == s.GetFile();
|
||||
if (found && !s.GetComposer().empty())
|
||||
found = copy.GetComposer() == s.GetComposer();
|
||||
if (found && !s.GetPerformer().empty())
|
||||
@@ -541,6 +589,9 @@ void SearchEngine::Search()
|
||||
found = copy.GetComment() == s.GetComment();
|
||||
}
|
||||
|
||||
copy.NullMe();
|
||||
(*it)->CopyPtr(0);
|
||||
|
||||
if (found && any_found)
|
||||
{
|
||||
Song *ss = Config.search_in_db ? *it : new Song(**it);
|
||||
|
||||
@@ -55,7 +55,7 @@ class SearchEngine : public Screen< Menu< std::pair<Buffer *, MPD::Song *> > >
|
||||
virtual void ReverseSelection() { w->ReverseSelection(StaticOptions); }
|
||||
virtual void GetSelectedSongs(MPD::SongList &);
|
||||
|
||||
virtual void ApplyFilter(const std::string &s) { w->ApplyFilter(s, StaticOptions); }
|
||||
virtual void ApplyFilter(const std::string &);
|
||||
|
||||
virtual List *GetList() { return w->Size() >= StaticOptions ? w : 0; }
|
||||
|
||||
|
||||
@@ -276,6 +276,7 @@ void DefaultConfiguration(ncmpcpp_config &conf)
|
||||
conf.playlist_disable_highlight_delay = 5;
|
||||
conf.message_delay_time = 4;
|
||||
conf.lyrics_db = 0;
|
||||
conf.regex_type = 0;
|
||||
}
|
||||
|
||||
void ReadKeys(ncmpcpp_keys &keys)
|
||||
@@ -646,6 +647,10 @@ void ReadConfiguration(ncmpcpp_config &conf)
|
||||
{
|
||||
conf.set_window_title = v == "yes";
|
||||
}
|
||||
else if (cl.find("regular_expressions") != string::npos)
|
||||
{
|
||||
conf.regex_type = REG_EXTENDED * (v != "basic");
|
||||
}
|
||||
else if (cl.find("lyrics_database") != string::npos)
|
||||
{
|
||||
if (!v.empty())
|
||||
|
||||
@@ -168,6 +168,7 @@ struct ncmpcpp_config
|
||||
int playlist_disable_highlight_delay;
|
||||
int message_delay_time;
|
||||
int lyrics_db;
|
||||
int regex_type;
|
||||
};
|
||||
|
||||
extern ncmpcpp_config Config;
|
||||
|
||||
@@ -198,6 +198,7 @@ void Song::SetFile(const string &str)
|
||||
if (itsSong->file)
|
||||
str_pool_put(itsSong->file);
|
||||
itsSong->file = str.empty() ? 0 : str_pool_get(str.c_str());
|
||||
CountLastSlashPosition();
|
||||
}
|
||||
|
||||
void Song::SetArtist(const string &str)
|
||||
@@ -544,8 +545,9 @@ string Song::ShowTime(int length)
|
||||
|
||||
void Song::CountLastSlashPosition()
|
||||
{
|
||||
if (!itsSong->file)
|
||||
return;
|
||||
char *tmp = strrchr(itsSong->file, '/');
|
||||
if (tmp)
|
||||
itsSlash = tmp-itsSong->file;
|
||||
itsSlash = tmp ? tmp-itsSong->file : string::npos;
|
||||
}
|
||||
|
||||
|
||||
@@ -741,11 +741,11 @@ void TagEditor::GetSelectedSongs(MPD::SongList &v)
|
||||
void TagEditor::ApplyFilter(const std::string &s)
|
||||
{
|
||||
if (w == Dirs)
|
||||
Dirs->ApplyFilter(s, 1);
|
||||
Dirs->ApplyFilter(s, 1, REG_ICASE | Config.regex_type);
|
||||
else if (w == Albums)
|
||||
Albums->ApplyFilter(s);
|
||||
Albums->ApplyFilter(s, 0, REG_ICASE | Config.regex_type);
|
||||
else if (w == Tags)
|
||||
Tags->ApplyFilter(s);
|
||||
Tags->ApplyFilter(s, 0, REG_ICASE | Config.regex_type);
|
||||
}
|
||||
|
||||
List *TagEditor::GetList()
|
||||
|
||||
Reference in New Issue
Block a user