Window: support adding and removing filters with :addfilter :rmfilter

This commit is contained in:
Maxime Coste 2011-11-08 14:29:52 +00:00
parent 1f3f5ea1ba
commit 3af1b89034
3 changed files with 64 additions and 6 deletions

View File

@ -427,6 +427,32 @@ void show_buffer(const CommandParameters& params)
current_window = buffer->get_or_create_window(); 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() void do_command()
{ {
try try
@ -585,6 +611,8 @@ int main(int argc, char* argv[])
PerArgumentCommandCompleter{ complete_filename }); PerArgumentCommandCompleter{ complete_filename });
command_manager.register_command(std::vector<std::string>{ "b", "buffer" }, show_buffer, command_manager.register_command(std::vector<std::string>{ "b", "buffer" }, show_buffer,
PerArgumentCommandCompleter { complete_buffername }); PerArgumentCommandCompleter { complete_buffername });
command_manager.register_command(std::vector<std::string>{ "af", "addfilter" }, add_filter);
command_manager.register_command(std::vector<std::string>{ "rf", "rmfilter" }, rm_filter);
register_filters(); register_filters();

View File

@ -114,10 +114,8 @@ Window::Window(Buffer& buffer)
m_current_inserter(nullptr) m_current_inserter(nullptr)
{ {
m_selections.push_back(Selection(buffer.begin(), buffer.begin())); m_selections.push_back(Selection(buffer.begin(), buffer.begin()));
m_filters.push_back(colorize_cplusplus); m_filters.push_back(FilterAndId("show_tabs", expand_tabulations));
m_filters.push_back(expand_tabulations); m_filters.push_back(FilterAndId("show_selection", HighlightSelections(*this)));
m_filters.push_back(HighlightSelections(*this));
m_filters.push_back(show_line_numbers);
} }
void Window::check_invariant() const void Window::check_invariant() const
@ -329,7 +327,7 @@ void Window::update_display_buffer()
for (auto& filter : m_filters) for (auto& filter : m_filters)
{ {
filter(m_display_buffer); filter.second(m_display_buffer);
m_display_buffer.check_invariant(); m_display_buffer.check_invariant();
} }
} }
@ -377,6 +375,28 @@ std::string Window::status_line() const
return oss.str(); 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) IncrementalInserter::IncrementalInserter(Window& window, Mode mode)
: m_window(window) : m_window(window)
{ {

View File

@ -6,6 +6,7 @@
#include "buffer.hh" #include "buffer.hh"
#include "dynamic_buffer_iterator.hh" #include "dynamic_buffer_iterator.hh"
#include "display_buffer.hh" #include "display_buffer.hh"
#include "filter.hh"
namespace Kakoune namespace Kakoune
{ {
@ -69,6 +70,15 @@ public:
std::string status_line() const; 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: private:
friend class Buffer; friend class Buffer;
@ -93,7 +103,7 @@ private:
SelectionList m_selections; SelectionList m_selections;
DisplayBuffer m_display_buffer; DisplayBuffer m_display_buffer;
typedef std::vector<std::function<void (DisplayBuffer&)>> FilterList; typedef std::vector<FilterAndId> FilterList;
FilterList m_filters; FilterList m_filters;
}; };