From e637d8f627bb50867e812a53f91c22dd36c47542 Mon Sep 17 00:00:00 2001 From: Andrzej Rybczak Date: Thu, 5 Feb 2009 21:45:34 +0100 Subject: [PATCH] add optional 'ignore leading "The" word' function while sorting --- doc/config | 7 +++++++ src/helpers.cpp | 42 +++++++++++++++++++++++++++++++++++++----- src/settings.cpp | 5 +++++ src/settings.h | 1 + 4 files changed, 50 insertions(+), 5 deletions(-) diff --git a/doc/config b/doc/config index a2114b76..c67fd73f 100644 --- a/doc/config +++ b/doc/config @@ -144,6 +144,13 @@ # #clock_display_seconds = "no" # +## +## Note: If below is enabled, ncmpcpp will ignore leading +## "The" word while sorting items in browser, tags in +## media library, etc. +## +#ignore_leading_the = "no" +# #enable_window_title = "yes" # ##### lyrics support ##### diff --git a/src/helpers.cpp b/src/helpers.cpp index 3de85c74..37bdfc7d 100644 --- a/src/helpers.cpp +++ b/src/helpers.cpp @@ -40,6 +40,22 @@ extern bool search_match_to_pattern; const string term_type = getenv("TERM") ? getenv("TERM") : ""; +namespace +{ + inline void remove_the_word(string &s) + { + size_t the_pos = s.find("the "); + if (the_pos == 0 && the_pos != string::npos) + s = s.substr(4); + } + + inline string extract_top_directory(const string &s) + { + size_t slash = s.rfind("/"); + return slash != string::npos ? s.substr(++slash) : s; + } +} + bool ConnectToMPD() { if (!Mpd->Connect()) @@ -169,6 +185,11 @@ bool CaseInsensitiveSorting::operator()(string a, string b) { ToLower(a); ToLower(b); + if (Config.ignore_leading_the) + { + remove_the_word(a); + remove_the_word(b); + } return a < b; } @@ -178,6 +199,11 @@ bool CaseInsensitiveSorting::operator()(Song *sa, Song *sb) string b = sb->GetName(); ToLower(a); ToLower(b); + if (Config.ignore_leading_the) + { + remove_the_word(a); + remove_the_word(b); + } return a < b; } @@ -185,11 +211,17 @@ bool CaseInsensitiveSorting::operator()(const Item &a, const Item &b) { if (a.type == b.type) { - string sa = a.type == itSong ? a.song->GetName() : a.name; - string sb = b.type == itSong ? b.song->GetName() : b.name; - ToLower(sa); - ToLower(sb); - return sa < sb; + switch (a.type) + { + case itDirectory: + return operator()(extract_top_directory(a.name), extract_top_directory(b.name)); + case itPlaylist: + return operator()(a.name, b.name); + case itSong: + return operator()(a.song, b.song); + default: // there's no other type, just silence compiler. + return 0; + } } else return a.type < b.type; diff --git a/src/settings.cpp b/src/settings.cpp index 7f19dfb7..1c411acb 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -279,6 +279,7 @@ void DefaultConfiguration(ncmpcpp_config &conf) conf.search_in_db = true; conf.display_screens_numbers_on_start = true; conf.clock_display_seconds = false; + conf.ignore_leading_the = false; conf.set_window_title = true; conf.mpd_port = 6600; conf.mpd_connection_timeout = 15; @@ -700,6 +701,10 @@ void ReadConfiguration(ncmpcpp_config &conf) { conf.clock_display_seconds = v == "yes"; } + else if (cl.find("ignore_leading_the") != string::npos) + { + conf.ignore_leading_the = v == "yes"; + } else if (cl.find("enable_window_title") != string::npos) { conf.set_window_title = v == "yes"; diff --git a/src/settings.h b/src/settings.h index b5bb2c1e..9cb604e0 100644 --- a/src/settings.h +++ b/src/settings.h @@ -151,6 +151,7 @@ struct ncmpcpp_config bool search_in_db; bool display_screens_numbers_on_start; bool clock_display_seconds; + bool ignore_leading_the; int mpd_port; int mpd_connection_timeout;