summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMaxime Coste <frrrwww@gmail.com>2013-03-14 13:42:07 +0100
committerMaxime Coste <frrrwww@gmail.com>2013-03-14 13:42:07 +0100
commit50d9c4dfda725731c9c848aa080efede2be52708 (patch)
tree8c6449067c5a1cc91398b2726ea686c05807d1ec /src
parent0b45a725e4aa5129339f225ed75fbff1a04dbf09 (diff)
add support for regex options, make ignored_files one
Diffstat (limited to 'src')
-rw-r--r--src/commands.cc4
-rw-r--r--src/file.cc10
-rw-r--r--src/file.hh2
-rw-r--r--src/option_manager.cc22
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<String>(), prefix, pos_in_token);
+ return complete_filename(prefix, context.options()["ignored_files"].get<Regex>(), 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<String>(), cursor_pos); }
+ { return complete_filename(prefix, context.options()["ignored_files"].get<Regex>(), cursor_pos); }
});
cm.register_commands({ "e", "edit" }, edit<false>, filename_completer);
cm.register_commands({ "e!", "edit!" }, edit<true>, 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<String> 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<String> 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<String> 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<String> regex_result;
@@ -258,7 +256,7 @@ std::vector<String> 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<String>& paths);
std::vector<String> 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<T>& 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<typename T>
@@ -124,6 +141,9 @@ template void Option::set<std::vector<int>>(const std::vector<int>&);
template const std::vector<String>& Option::get<std::vector<String>>() const;
template void Option::set<std::vector<String>>(const std::vector<String>&);
+template const Regex& Option::get<Regex>() const;
+template void Option::set<Regex>(const Regex&);
+
OptionManager::OptionManager(OptionManager& parent)
: m_parent(&parent)
{
@@ -236,7 +256,7 @@ GlobalOptions::GlobalOptions()
declare_option<String>("shell", "sh");
declare_option<bool>("complete_prefix", true);
declare_option<bool>("incsearch", true);
- declare_option<String>("ignored_files", R"(^(\..*|.*\.(o|so|a))$)");
+ declare_option<Regex>("ignored_files", Regex{R"(^(\..*|.*\.(o|so|a))$)"});
declare_option<String>("filetype", "");
declare_option<std::vector<String>>("completions", {});
declare_option<std::vector<String>>("path", { "./", "/usr/include" });