From 548e10597c7145babb7b68cf3bf42a4c6ed81ec9 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Sun, 28 Feb 2016 18:30:35 +0000 Subject: [PATCH] Code cleanup --- src/containers.hh | 6 ++++++ src/enum.hh | 4 ++-- src/insert_completer.cc | 5 ++--- src/normal.cc | 15 ++++++--------- src/shell_manager.cc | 2 +- 5 files changed, 17 insertions(+), 15 deletions(-) diff --git a/src/containers.hh b/src/containers.hh index 56e5fc1f..6bf3bea6 100644 --- a/src/containers.hh +++ b/src/containers.hh @@ -162,6 +162,12 @@ bool contains(Container&& container, const T& value) return find(container, value) != end(container); } +template +bool contains_that(Container&& container, T op) +{ + return find_if(container, op) != end(container); +} + template void unordered_erase(Container&& vec, U&& value) { diff --git a/src/enum.hh b/src/enum.hh index af532391..77979e59 100644 --- a/src/enum.hh +++ b/src/enum.hh @@ -66,8 +66,8 @@ EnableIfWithoutBitOps option_from_string(StringView str, Enum& e) { constexpr auto desc = enum_desc(Enum{}); auto it = find_if(desc, [str](const EnumDesc& d) { return d.name == str; }); - if (it == desc.end()) - throw runtime_error(format("invalid enum value '{}'", str)); + if (it == desc.end()) + throw runtime_error(format("invalid enum value '{}'", str)); e = it->value; } diff --git a/src/insert_completer.cc b/src/insert_completer.cc index 5a4464bb..27275363 100644 --- a/src/insert_completer.cc +++ b/src/insert_completer.cc @@ -241,9 +241,8 @@ InsertCompletion complete_option(const Buffer& buffer, ByteCoord cursor_pos, } size_t timestamp = (size_t)str_to_int({match[4].first, match[4].second}); auto changes = buffer.changes_since(timestamp); - if (find_if(changes, [&](const Buffer::Change& change){ - return change.begin < coord; - }) != changes.end()) + if (contains_that(changes, [&](const Buffer::Change& change) + { return change.begin < coord; })) return {}; if (cursor_pos.line == coord.line and cursor_pos.column >= coord.column) diff --git a/src/normal.cc b/src/normal.cc index eaa33159..81a337dd 100644 --- a/src/normal.cc +++ b/src/normal.cc @@ -172,18 +172,15 @@ void goto_commands(Context& context, NormalParams params) } case 'f': { - const Selection& sel = context.selections().main(); - String filename = content(buffer, sel); + auto filename = content(buffer, context.selections().main()); static constexpr char forbidden[] = { '\'', '\\', '\0' }; - for (auto c : filename) - if (contains(forbidden, c)) - return; + if (contains_that(filename, [](char c){ return contains(forbidden, c); })) + return; auto paths = context.options()["path"].get>(); - const String& buffer_name = buffer.name(); - auto it = find(reversed(buffer_name), '/'); - if (it != buffer_name.rend()) - paths.insert(paths.begin(), String{buffer_name.begin(), it.base()}); + StringView buffer_dir = split_path(buffer.name()).first; + if (not buffer_dir.empty()) + paths.insert(paths.begin(), buffer_dir.str()); String path = find_file(filename, paths); if (path.empty()) diff --git a/src/shell_manager.cc b/src/shell_manager.cc index d8ae6d78..26010e12 100644 --- a/src/shell_manager.cc +++ b/src/shell_manager.cc @@ -90,7 +90,7 @@ Vector generate_env(StringView cmdline, const Context& context, const Sh return s.length() > name.length() and prefix_match(s, name) and s[name.length()] == '='; }; - if (find_if(kak_env, match_name) != kak_env.end()) + if (contains_that(kak_env, match_name)) continue; auto var_it = shell_context.env_vars.find(name);