From 0859b20bcfd47d28fbd1e4b5e1aa6873f7bf617f Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Tue, 29 Nov 2011 22:37:20 +0000 Subject: [PATCH] Rename Filter to Highlighter to be more explicit --- README.asciidoc | 12 ++++---- src/filter.hh | 16 ---------- src/filter_registry.cc | 45 ----------------------------- src/filter_registry.hh | 40 ------------------------- src/filters.hh | 11 ------- src/highlighter.hh | 16 ++++++++++ src/highlighter_registry.cc | 45 +++++++++++++++++++++++++++++ src/highlighter_registry.hh | 40 +++++++++++++++++++++++++ src/{filters.cc => highlighters.cc} | 38 ++++++++++++------------ src/highlighters.hh | 11 +++++++ src/kakrc | 2 +- src/main.cc | 38 ++++++++++++------------ src/window.cc | 36 +++++++++++------------ src/window.hh | 20 ++++++------- 14 files changed, 185 insertions(+), 185 deletions(-) delete mode 100644 src/filter.hh delete mode 100644 src/filter_registry.cc delete mode 100644 src/filter_registry.hh delete mode 100644 src/filters.hh create mode 100644 src/highlighter.hh create mode 100644 src/highlighter_registry.cc create mode 100644 src/highlighter_registry.hh rename src/{filters.cc => highlighters.cc} (89%) create mode 100644 src/highlighters.hh diff --git a/README.asciidoc b/README.asciidoc index db1b4945..a44527c0 100644 --- a/README.asciidoc +++ b/README.asciidoc @@ -107,14 +107,14 @@ from the selection. While inserting, _^B_ key followed by a digit inserts the designated capture. -Filters -------- +Highlighters +------------ -Manipulation of the displayed text is done through filters, which can be added -or removed with the command :addfilter -and :rmfilter +Manipulation of the displayed text is done through highlighters, which can be added +or removed with the command :addhl +and :rmhl -existing filters are: +existing highlighters are: * *highlight_selections*: used to make current selection visible * *expand_tabs*: expand tabs to next 8 multiple column (to make configurable) diff --git a/src/filter.hh b/src/filter.hh deleted file mode 100644 index 3efa664e..00000000 --- a/src/filter.hh +++ /dev/null @@ -1,16 +0,0 @@ -#ifndef filter_hh_INCLUDED -#define filter_hh_INCLUDED - -#include - -namespace Kakoune -{ - -class DisplayBuffer; - -typedef std::function FilterFunc; -typedef std::pair FilterAndId; - -} - -#endif // filter_hh_INCLUDED diff --git a/src/filter_registry.cc b/src/filter_registry.cc deleted file mode 100644 index a4f1d817..00000000 --- a/src/filter_registry.cc +++ /dev/null @@ -1,45 +0,0 @@ -#include "filter_registry.hh" - -#include "exception.hh" -#include "window.hh" - -namespace Kakoune -{ - -struct factory_not_found : public runtime_error -{ - factory_not_found() : runtime_error("factory not found") {} -}; - -void FilterRegistry::register_factory(const std::string& name, - const FilterFactory& factory) -{ - assert(m_factories.find(name) == m_factories.end()); - m_factories[name] = factory; -} - -void FilterRegistry::add_filter_to_window(Window& window, - const std::string& name, - const FilterParameters& parameters) -{ - auto it = m_factories.find(name); - if (it == m_factories.end()) - throw factory_not_found(); - - window.add_filter(it->second(window, parameters)); -} - -CandidateList FilterRegistry::complete_filter(const std::string& prefix, - size_t cursor_pos) -{ - std::string real_prefix = prefix.substr(0, cursor_pos); - CandidateList result; - for (auto& filter : m_factories) - { - if (filter.first.substr(0, real_prefix.length()) == real_prefix) - result.push_back(filter.first); - } - return result; -} - -} diff --git a/src/filter_registry.hh b/src/filter_registry.hh deleted file mode 100644 index 572e30af..00000000 --- a/src/filter_registry.hh +++ /dev/null @@ -1,40 +0,0 @@ -#ifndef filter_registry_h_INCLUDED -#define filter_registry_h_INCLUDED - -#include -#include - -#include "filter.hh" -#include "utils.hh" -#include "completion.hh" - -namespace Kakoune -{ - -class Window; - -typedef std::vector FilterParameters; - -typedef std::function FilterFactory; - -class FilterRegistry : public Singleton -{ -public: - void register_factory(const std::string& name, - const FilterFactory& factory); - - void add_filter_to_window(Window& window, - const std::string& factory_name, - const FilterParameters& parameters); - - CandidateList complete_filter(const std::string& prefix, - size_t cursor_pos); - -private: - std::unordered_map m_factories; -}; - -} - -#endif // filter_registry_h_INCLUDED diff --git a/src/filters.hh b/src/filters.hh deleted file mode 100644 index 0d88da46..00000000 --- a/src/filters.hh +++ /dev/null @@ -1,11 +0,0 @@ -#ifndef filters_hh_INCLUDED -#define filters_hh_INCLUDED - -namespace Kakoune -{ - -void register_filters(); - -} - -#endif // filters_hh_INCLUDED diff --git a/src/highlighter.hh b/src/highlighter.hh new file mode 100644 index 00000000..3000dd27 --- /dev/null +++ b/src/highlighter.hh @@ -0,0 +1,16 @@ +#ifndef highlighter_hh_INCLUDED +#define highlighter_hh_INCLUDED + +#include + +namespace Kakoune +{ + +class DisplayBuffer; + +typedef std::function HighlighterFunc; +typedef std::pair HighlighterAndId; + +} + +#endif // highlighter_hh_INCLUDED diff --git a/src/highlighter_registry.cc b/src/highlighter_registry.cc new file mode 100644 index 00000000..fdf8d15f --- /dev/null +++ b/src/highlighter_registry.cc @@ -0,0 +1,45 @@ +#include "highlighter_registry.hh" + +#include "exception.hh" +#include "window.hh" + +namespace Kakoune +{ + +struct factory_not_found : public runtime_error +{ + factory_not_found() : runtime_error("factory not found") {} +}; + +void HighlighterRegistry::register_factory(const std::string& name, + const HighlighterFactory& factory) +{ + assert(m_factories.find(name) == m_factories.end()); + m_factories[name] = factory; +} + +void HighlighterRegistry::add_highlighter_to_window(Window& window, + const std::string& name, + const HighlighterParameters& parameters) +{ + auto it = m_factories.find(name); + if (it == m_factories.end()) + throw factory_not_found(); + + window.add_highlighter(it->second(window, parameters)); +} + +CandidateList HighlighterRegistry::complete_highlighter(const std::string& prefix, + size_t cursor_pos) +{ + std::string real_prefix = prefix.substr(0, cursor_pos); + CandidateList result; + for (auto& highlighter : m_factories) + { + if (highlighter.first.substr(0, real_prefix.length()) == real_prefix) + result.push_back(highlighter.first); + } + return result; +} + +} diff --git a/src/highlighter_registry.hh b/src/highlighter_registry.hh new file mode 100644 index 00000000..19a285ea --- /dev/null +++ b/src/highlighter_registry.hh @@ -0,0 +1,40 @@ +#ifndef highlighter_registry_h_INCLUDED +#define highlighter_registry_h_INCLUDED + +#include +#include + +#include "highlighter.hh" +#include "utils.hh" +#include "completion.hh" + +namespace Kakoune +{ + +class Window; + +typedef std::vector HighlighterParameters; + +typedef std::function HighlighterFactory; + +class HighlighterRegistry : public Singleton +{ +public: + void register_factory(const std::string& name, + const HighlighterFactory& factory); + + void add_highlighter_to_window(Window& window, + const std::string& factory_name, + const HighlighterParameters& parameters); + + CandidateList complete_highlighter(const std::string& prefix, + size_t cursor_pos); + +private: + std::unordered_map m_factories; +}; + +} + +#endif // highlighter_registry_h_INCLUDED diff --git a/src/filters.cc b/src/highlighters.cc similarity index 89% rename from src/filters.cc rename to src/highlighters.cc index 668a582a..7c4d9bb0 100644 --- a/src/filters.cc +++ b/src/highlighters.cc @@ -1,9 +1,9 @@ -#include "filters.hh" +#include "highlighters.hh" #include "assert.hh" #include "window.hh" #include "display_buffer.hh" -#include "filter_registry.hh" +#include "highlighter_registry.hh" #include namespace Kakoune @@ -80,8 +80,8 @@ Color parse_color(const std::string& color) return Color::Default; } -FilterAndId colorize_regex_factory(Window& window, - const FilterParameters params) +HighlighterAndId colorize_regex_factory(Window& window, + const HighlighterParameters params) { if (params.size() != 3) throw runtime_error("wrong parameter count"); @@ -93,7 +93,7 @@ FilterAndId colorize_regex_factory(Window& window, std::string id = "colre'" + params[0] + "'"; - return FilterAndId(id, std::bind(colorize_regex, std::placeholders::_1, + return HighlighterAndId(id, std::bind(colorize_regex, std::placeholders::_1, ex, fg_color, bg_color)); } @@ -194,16 +194,16 @@ void show_line_numbers(DisplayBuffer& display_buffer) } } -template -class SimpleFilterFactory +template +class SimpleHighlighterFactory { public: - SimpleFilterFactory(const std::string& id) : m_id(id) {} + SimpleHighlighterFactory(const std::string& id) : m_id(id) {} - FilterAndId operator()(Window& window, - const FilterParameters& params) const + HighlighterAndId operator()(Window& window, + const HighlighterParameters& params) const { - return FilterAndId(m_id, FilterFunc(filter_func)); + return HighlighterAndId(m_id, HighlighterFunc(highlighter_func)); } private: std::string m_id; @@ -280,10 +280,10 @@ public: } - static FilterAndId create(Window& window, - const FilterParameters& params) + static HighlighterAndId create(Window& window, + const HighlighterParameters& params) { - return FilterAndId("highlight_selections", + return HighlighterAndId("highlight_selections", SelectionsHighlighter(window)); } @@ -291,14 +291,14 @@ private: const Window& m_window; }; -void register_filters() +void register_highlighters() { - FilterRegistry& registry = FilterRegistry::instance(); + HighlighterRegistry& registry = HighlighterRegistry::instance(); registry.register_factory("highlight_selections", SelectionsHighlighter::create); - registry.register_factory("expand_tabs", SimpleFilterFactory("expand_tabs")); - registry.register_factory("number_lines", SimpleFilterFactory("number_lines")); - registry.register_factory("hlcpp", SimpleFilterFactory("hlcpp")); + registry.register_factory("expand_tabs", SimpleHighlighterFactory("expand_tabs")); + registry.register_factory("number_lines", SimpleHighlighterFactory("number_lines")); + registry.register_factory("hlcpp", SimpleHighlighterFactory("hlcpp")); registry.register_factory("regex", colorize_regex_factory); } diff --git a/src/highlighters.hh b/src/highlighters.hh new file mode 100644 index 00000000..53738e94 --- /dev/null +++ b/src/highlighters.hh @@ -0,0 +1,11 @@ +#ifndef highlighters_hh_INCLUDED +#define highlighters_hh_INCLUDED + +namespace Kakoune +{ + +void register_highlighters(); + +} + +#endif // highlighters_hh_INCLUDED diff --git a/src/kakrc b/src/kakrc index b6362512..23e04966 100644 --- a/src/kakrc +++ b/src/kakrc @@ -1,2 +1,2 @@ -hook WinCreate .*\.(c|cc|cpp|cxx|C|h|hh|hpp|hxx|H) addfilter hlcpp +hook WinCreate .*\.(c|cc|cpp|cxx|C|h|hh|hpp|hxx|H) addhl hlcpp diff --git a/src/main.cc b/src/main.cc index 2b1ca02e..bd1775fb 100644 --- a/src/main.cc +++ b/src/main.cc @@ -7,8 +7,8 @@ #include "selectors.hh" #include "assert.hh" #include "debug.hh" -#include "filters.hh" -#include "filter_registry.hh" +#include "highlighters.hh" +#include "highlighter_registry.hh" #include "hooks_manager.hh" #include @@ -433,17 +433,17 @@ void show_buffer(const CommandParameters& params, const Context& context) main_context = Context(*buffer->get_or_create_window()); } -void add_filter(const CommandParameters& params, const Context& context) +void add_highlighter(const CommandParameters& params, const Context& context) { if (params.size() < 1) throw wrong_argument_count(); try { - FilterRegistry& registry = FilterRegistry::instance(); - FilterParameters filter_params(params.begin()+1, params.end()); - registry.add_filter_to_window(*context.window, params[0], - filter_params); + HighlighterRegistry& registry = HighlighterRegistry::instance(); + HighlighterParameters highlighter_params(params.begin()+1, params.end()); + registry.add_highlighter_to_window(*context.window, params[0], + highlighter_params); } catch (runtime_error& err) { @@ -451,12 +451,12 @@ void add_filter(const CommandParameters& params, const Context& context) } } -void rm_filter(const CommandParameters& params, const Context& context) +void rm_highlighter(const CommandParameters& params, const Context& context) { if (params.size() != 1) throw wrong_argument_count(); - context.window->remove_filter(params[0]); + context.window->remove_highlighter(params[0]); } void add_hook(const CommandParameters& params, const Context& context) @@ -679,11 +679,11 @@ int main(int argc, char* argv[]) { init_ncurses(); - CommandManager command_manager; - BufferManager buffer_manager; - RegisterManager register_manager; - FilterRegistry filter_registry; - HooksManager hooks_manager; + CommandManager command_manager; + BufferManager buffer_manager; + RegisterManager register_manager; + HighlighterRegistry highlighter_registry; + HooksManager hooks_manager; command_manager.register_command(std::vector{ "e", "edit" }, edit, PerArgumentCommandCompleter{ complete_filename }); @@ -697,21 +697,21 @@ int main(int argc, char* argv[]) PerArgumentCommandCompleter { std::bind(&BufferManager::complete_buffername, &buffer_manager, _1, _2) }); - command_manager.register_command(std::vector{ "af", "addfilter" }, add_filter, + command_manager.register_command(std::vector{ "ah", "addhl" }, add_highlighter, PerArgumentCommandCompleter { - std::bind(&FilterRegistry::complete_filter, &filter_registry, _1, _2) + std::bind(&HighlighterRegistry::complete_highlighter, &highlighter_registry, _1, _2) }); - command_manager.register_command(std::vector{ "rf", "rmfilter" }, rm_filter, + command_manager.register_command(std::vector{ "rh", "rmhl" }, rm_highlighter, PerArgumentCommandCompleter { [&](const std::string& prefix, size_t cursor_pos) - { return main_context.window->complete_filterid(prefix, cursor_pos); } + { return main_context.window->complete_highlighterid(prefix, cursor_pos); } }); command_manager.register_command(std::vector{ "hook" }, add_hook); command_manager.register_command(std::vector{ "source" }, exec_commands_in_file, PerArgumentCommandCompleter{ complete_filename }); - register_filters(); + register_highlighters(); try { diff --git a/src/window.cc b/src/window.cc index 69a91d97..02625fc5 100644 --- a/src/window.cc +++ b/src/window.cc @@ -1,7 +1,7 @@ #include "window.hh" #include "assert.hh" -#include "filter_registry.hh" +#include "highlighter_registry.hh" #include "hooks_manager.hh" #include @@ -54,13 +54,13 @@ Window::Window(Buffer& buffer) { m_selections.push_back(Selection(buffer.begin(), buffer.begin())); - FilterRegistry& registry = FilterRegistry::instance(); + HighlighterRegistry& registry = HighlighterRegistry::instance(); HooksManager::instance().run_hook("WinCreate", buffer.name(), Context(*this)); - registry.add_filter_to_window(*this, "expand_tabs", FilterParameters()); - registry.add_filter_to_window(*this, "highlight_selections", FilterParameters()); + registry.add_highlighter_to_window(*this, "expand_tabs", HighlighterParameters()); + registry.add_highlighter_to_window(*this, "highlight_selections", HighlighterParameters()); } void Window::check_invariant() const @@ -293,9 +293,9 @@ void Window::update_display_buffer() m_display_buffer.append(DisplayAtom(DisplayCoord(0,0), begin, end)); - for (auto& filter : m_filters) + for (auto& highlighter : m_highlighters) { - filter.second(m_display_buffer); + highlighter.second(m_display_buffer); m_display_buffer.check_invariant(); } } @@ -343,37 +343,37 @@ std::string Window::status_line() const return oss.str(); } -void Window::add_filter(FilterAndId&& filter) +void Window::add_highlighter(HighlighterAndId&& highlighter) { - for (auto it = m_filters.begin(); it != m_filters.end(); ++it) + for (auto it = m_highlighters.begin(); it != m_highlighters.end(); ++it) { - if (it->first == filter.first) - throw filter_id_not_unique(filter.first); + if (it->first == highlighter.first) + throw highlighter_id_not_unique(highlighter.first); } - m_filters.push_back(filter); + m_highlighters.push_back(highlighter); } -void Window::remove_filter(const std::string& id) +void Window::remove_highlighter(const std::string& id) { - for (auto it = m_filters.begin(); it != m_filters.end(); ++it) + for (auto it = m_highlighters.begin(); it != m_highlighters.end(); ++it) { if (it->first == id) { - m_filters.erase(it); + m_highlighters.erase(it); return; } } } -CandidateList Window::complete_filterid(const std::string& prefix, +CandidateList Window::complete_highlighterid(const std::string& prefix, size_t cursor_pos) { std::string real_prefix = prefix.substr(0, cursor_pos); CandidateList result; - for (auto& filter : m_filters) + for (auto& highlighter : m_highlighters) { - if (filter.first.substr(0, real_prefix.length()) == real_prefix) - result.push_back(filter.first); + if (highlighter.first.substr(0, real_prefix.length()) == real_prefix) + result.push_back(highlighter.first); } return result; } diff --git a/src/window.hh b/src/window.hh index aa8c066e..0f989ece 100644 --- a/src/window.hh +++ b/src/window.hh @@ -7,7 +7,7 @@ #include "dynamic_buffer_iterator.hh" #include "display_buffer.hh" #include "completion.hh" -#include "filter.hh" +#include "highlighter.hh" namespace Kakoune { @@ -87,17 +87,17 @@ public: std::string status_line() const; - struct filter_id_not_unique : public runtime_error + struct highlighter_id_not_unique : public runtime_error { - filter_id_not_unique(const std::string& id) - : runtime_error("filter id not unique: " + id) {} + highlighter_id_not_unique(const std::string& id) + : runtime_error("highlighter id not unique: " + id) {} }; - void add_filter(FilterAndId&& filter); - void remove_filter(const std::string& id); + void add_highlighter(HighlighterAndId&& highlighter); + void remove_highlighter(const std::string& id); - CandidateList complete_filterid(const std::string& prefix, - size_t cursor_pos = std::string::npos); + CandidateList complete_highlighterid(const std::string& prefix, + size_t cursor_pos = std::string::npos); private: friend class Buffer; @@ -121,8 +121,8 @@ private: SelectionList m_selections; DisplayBuffer m_display_buffer; - typedef std::vector FilterList; - FilterList m_filters; + typedef std::vector HighlighterList; + HighlighterList m_highlighters; }; class IncrementalInserter