From 50d9c4dfda725731c9c848aa080efede2be52708 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Thu, 14 Mar 2013 13:42:07 +0100 Subject: [PATCH] add support for regex options, make ignored_files one --- src/commands.cc | 4 ++-- src/file.cc | 10 ++++------ src/file.hh | 2 +- src/option_manager.cc | 22 +++++++++++++++++++++- 4 files changed, 28 insertions(+), 10 deletions(-) diff --git a/src/commands.cc b/src/commands.cc index 49555f88..93505450 100644 --- a/src/commands.cc +++ b/src/commands.cc @@ -424,7 +424,7 @@ void define_command(const CommandParameters& params, Context& context) { const String& prefix = token_to_complete < params.size() ? params[token_to_complete] : String(); - return complete_filename(context.options()["ignored_files"].get(), prefix, pos_in_token); + return complete_filename(prefix, context.options()["ignored_files"].get(), pos_in_token); }; } else if (parser.has_option("shell-completion")) @@ -792,7 +792,7 @@ void register_commands() PerArgumentCommandCompleter filename_completer({ [](const Context& context, const String& prefix, ByteCount cursor_pos) - { return complete_filename(prefix, context.options()["ignored_files"].get(), cursor_pos); } + { return complete_filename(prefix, context.options()["ignored_files"].get(), cursor_pos); } }); cm.register_commands({ "e", "edit" }, edit, filename_completer); cm.register_commands({ "e!", "edit!" }, edit, filename_completer); diff --git a/src/file.cc b/src/file.cc index 2922a835..bc8ce4a8 100644 --- a/src/file.cc +++ b/src/file.cc @@ -217,7 +217,7 @@ static boost::regex make_regex_ifp(const String& ex) } std::vector complete_filename(const String& prefix, - const String& ignored_regex, + const Regex& ignored_regex, ByteCount cursor_pos) { String real_prefix = parse_filename(prefix.substr(0, cursor_pos)); @@ -225,8 +225,6 @@ std::vector complete_filename(const String& prefix, String dirprefix; String fileprefix = real_prefix; - boost::regex ignored_files = make_regex_ifp(ignored_regex); - ByteCount dir_end = -1; for (ByteCount i = 0; i < real_prefix.length(); ++i) { @@ -247,8 +245,8 @@ std::vector complete_filename(const String& prefix, if (not dir) return result; - const bool check_ignored_files = not ignored_files.empty() and - not boost::regex_match(fileprefix.c_str(), ignored_files); + const bool check_ignored_regex = not ignored_regex.empty() and + not boost::regex_match(fileprefix.c_str(), ignored_regex); boost::regex file_regex = make_regex_ifp(fileprefix); std::vector regex_result; @@ -258,7 +256,7 @@ std::vector complete_filename(const String& prefix, if (filename.empty()) continue; - if (check_ignored_files and boost::regex_match(filename.c_str(), ignored_files)) + if (check_ignored_regex and boost::regex_match(filename.c_str(), ignored_regex)) continue; const bool match_prefix = (filename.substr(0, fileprefix.length()) == fileprefix); diff --git a/src/file.hh b/src/file.hh index eb2f79ba..7fa349c6 100644 --- a/src/file.hh +++ b/src/file.hh @@ -33,7 +33,7 @@ void write_buffer_to_file(const Buffer& buffer, const String& filename); String find_file(const String& filename, const memoryview& paths); std::vector complete_filename(const String& prefix, - const String& ignore_regex, + const Regex& ignore_regex, ByteCount cursor_pos = -1); } diff --git a/src/option_manager.cc b/src/option_manager.cc index a78bd029..f561d685 100644 --- a/src/option_manager.cc +++ b/src/option_manager.cc @@ -52,6 +52,23 @@ void option_from_string(const String& str, std::vector& opt) } } +String option_to_string(const Regex& re) +{ + return String{re.str()}; +} + +void option_from_string(const String& str, Regex& re) +{ + try + { + re = Regex{str.begin(), str.end()}; + } + catch (boost::regex_error& err) + { + throw runtime_error("unable to create regex: "_str + err.what()); + } +} + } template @@ -124,6 +141,9 @@ template void Option::set>(const std::vector&); template const std::vector& Option::get>() const; template void Option::set>(const std::vector&); +template const Regex& Option::get() const; +template void Option::set(const Regex&); + OptionManager::OptionManager(OptionManager& parent) : m_parent(&parent) { @@ -236,7 +256,7 @@ GlobalOptions::GlobalOptions() declare_option("shell", "sh"); declare_option("complete_prefix", true); declare_option("incsearch", true); - declare_option("ignored_files", R"(^(\..*|.*\.(o|so|a))$)"); + declare_option("ignored_files", Regex{R"(^(\..*|.*\.(o|so|a))$)"}); declare_option("filetype", ""); declare_option>("completions", {}); declare_option>("path", { "./", "/usr/include" });