From 3af1b890345c05654d93c63f5842bbfa7a09fbe9 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Tue, 8 Nov 2011 14:29:52 +0000 Subject: [PATCH] Window: support adding and removing filters with :addfilter :rmfilter --- src/main.cc | 28 ++++++++++++++++++++++++++++ src/window.cc | 30 +++++++++++++++++++++++++----- src/window.hh | 12 +++++++++++- 3 files changed, 64 insertions(+), 6 deletions(-) diff --git a/src/main.cc b/src/main.cc index 2835a892..5698130e 100644 --- a/src/main.cc +++ b/src/main.cc @@ -427,6 +427,32 @@ void show_buffer(const CommandParameters& params) current_window = buffer->get_or_create_window(); } +void add_filter(const CommandParameters& params) +{ + if (params.size() < 1) + throw wrong_argument_count(); + + try + { + FilterRegistry& registry = FilterRegistry::instance(); + FilterParameters filter_params(params.begin()+1, params.end()); + FilterAndId filter_and_id = registry.get_filter(params[0], filter_params); + current_window->add_filter(std::move(filter_and_id)); + } + catch (runtime_error& err) + { + print_status("error: " + err.description()); + } +} + +void rm_filter(const CommandParameters& params) +{ + if (params.size() != 1) + throw wrong_argument_count(); + + current_window->remove_filter(params[0]); +} + void do_command() { try @@ -585,6 +611,8 @@ int main(int argc, char* argv[]) PerArgumentCommandCompleter{ complete_filename }); command_manager.register_command(std::vector{ "b", "buffer" }, show_buffer, PerArgumentCommandCompleter { complete_buffername }); + command_manager.register_command(std::vector{ "af", "addfilter" }, add_filter); + command_manager.register_command(std::vector{ "rf", "rmfilter" }, rm_filter); register_filters(); diff --git a/src/window.cc b/src/window.cc index 4854940b..05ac628d 100644 --- a/src/window.cc +++ b/src/window.cc @@ -114,10 +114,8 @@ Window::Window(Buffer& buffer) m_current_inserter(nullptr) { m_selections.push_back(Selection(buffer.begin(), buffer.begin())); - m_filters.push_back(colorize_cplusplus); - m_filters.push_back(expand_tabulations); - m_filters.push_back(HighlightSelections(*this)); - m_filters.push_back(show_line_numbers); + m_filters.push_back(FilterAndId("show_tabs", expand_tabulations)); + m_filters.push_back(FilterAndId("show_selection", HighlightSelections(*this))); } void Window::check_invariant() const @@ -329,7 +327,7 @@ void Window::update_display_buffer() for (auto& filter : m_filters) { - filter(m_display_buffer); + filter.second(m_display_buffer); m_display_buffer.check_invariant(); } } @@ -377,6 +375,28 @@ std::string Window::status_line() const return oss.str(); } +void Window::add_filter(FilterAndId&& filter) +{ + for (auto it = m_filters.begin(); it != m_filters.end(); ++it) + { + if (it->first == filter.first) + throw filter_id_not_unique(filter.first); + } + m_filters.push_back(filter); +} + +void Window::remove_filter(const std::string& id) +{ + for (auto it = m_filters.begin(); it != m_filters.end(); ++it) + { + if (it->first == id) + { + m_filters.erase(it); + return; + } + } +} + IncrementalInserter::IncrementalInserter(Window& window, Mode mode) : m_window(window) { diff --git a/src/window.hh b/src/window.hh index 6442a3fd..12003c6a 100644 --- a/src/window.hh +++ b/src/window.hh @@ -6,6 +6,7 @@ #include "buffer.hh" #include "dynamic_buffer_iterator.hh" #include "display_buffer.hh" +#include "filter.hh" namespace Kakoune { @@ -69,6 +70,15 @@ public: std::string status_line() const; + struct filter_id_not_unique : public runtime_error + { + filter_id_not_unique(const std::string& id) + : runtime_error("filter id not unique: " + id) {} + }; + + void add_filter(FilterAndId&& filter); + void remove_filter(const std::string& id); + private: friend class Buffer; @@ -93,7 +103,7 @@ private: SelectionList m_selections; DisplayBuffer m_display_buffer; - typedef std::vector> FilterList; + typedef std::vector FilterList; FilterList m_filters; };