2011-12-02 15:28:27 +01:00
|
|
|
#include "filters.hh"
|
|
|
|
#include "filter_registry.hh"
|
|
|
|
#include "buffer.hh"
|
2012-06-12 20:24:29 +02:00
|
|
|
#include "filter_group.hh"
|
2011-12-02 15:28:27 +01:00
|
|
|
|
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
2012-11-19 13:40:23 +01:00
|
|
|
void preserve_indent(Buffer& buffer, Selection& selection, String& content)
|
2011-12-02 15:28:27 +01:00
|
|
|
{
|
2012-08-10 19:12:43 +02:00
|
|
|
if (content == "\n")
|
2011-12-02 15:28:27 +01:00
|
|
|
{
|
2012-11-19 13:40:23 +01:00
|
|
|
BufferIterator line_begin = buffer.iterator_at_line_begin(selection.last() - 1);
|
2011-12-02 15:28:27 +01:00
|
|
|
BufferIterator first_non_white = line_begin;
|
|
|
|
while ((*first_non_white == '\t' or *first_non_white == ' ') and
|
|
|
|
not first_non_white.is_end())
|
|
|
|
++first_non_white;
|
|
|
|
|
2012-08-10 19:12:43 +02:00
|
|
|
content += buffer.string(line_begin, first_non_white);
|
2011-12-02 15:28:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-19 13:40:23 +01:00
|
|
|
void cleanup_whitespaces(Buffer& buffer, Selection& selection, String& content)
|
2011-12-20 15:27:01 +01:00
|
|
|
{
|
2012-11-19 13:40:23 +01:00
|
|
|
const BufferIterator& position = selection.last();
|
2012-08-10 19:12:43 +02:00
|
|
|
if (content[0] == '\n' and not position.is_begin())
|
2011-12-20 15:27:01 +01:00
|
|
|
{
|
2012-08-10 19:12:43 +02:00
|
|
|
BufferIterator whitespace_start = position-1;
|
|
|
|
while ((*whitespace_start == ' ' or *whitespace_start == '\t') and
|
|
|
|
not whitespace_start .is_begin())
|
|
|
|
--whitespace_start;
|
|
|
|
++whitespace_start;
|
|
|
|
if (whitespace_start!= position)
|
|
|
|
buffer.erase(whitespace_start, position);
|
2011-12-20 15:27:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-19 13:40:23 +01:00
|
|
|
void expand_tabulations(Buffer& buffer, Selection& selection, String& content)
|
2011-12-03 21:25:05 +01:00
|
|
|
{
|
2012-06-12 20:26:20 +02:00
|
|
|
const int tabstop = buffer.option_manager()["tabstop"].as_int();
|
2012-08-10 19:12:43 +02:00
|
|
|
if (content == "\t")
|
2011-12-03 21:25:05 +01:00
|
|
|
{
|
|
|
|
int column = 0;
|
2012-11-19 13:40:23 +01:00
|
|
|
const BufferIterator& position = selection.last();
|
2012-08-15 17:55:58 +02:00
|
|
|
for (auto line_it = buffer.iterator_at_line_begin(position);
|
2012-08-10 19:12:43 +02:00
|
|
|
line_it != position; ++line_it)
|
2011-12-03 21:25:05 +01:00
|
|
|
{
|
|
|
|
assert(*line_it != '\n');
|
|
|
|
if (*line_it == '\t')
|
|
|
|
column += tabstop - (column % tabstop);
|
|
|
|
else
|
|
|
|
++column;
|
|
|
|
}
|
|
|
|
|
|
|
|
int count = tabstop - (column % tabstop);
|
2012-08-10 19:12:43 +02:00
|
|
|
content = String();
|
2012-04-14 03:17:09 +02:00
|
|
|
for (int i = 0; i < count; ++i)
|
2012-08-10 19:12:43 +02:00
|
|
|
content += ' ';
|
2011-12-03 21:25:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-19 13:40:23 +01:00
|
|
|
template<void (*filter_func)(Buffer&, Selection&, String&)>
|
2011-12-02 15:28:27 +01:00
|
|
|
class SimpleFilterFactory
|
|
|
|
{
|
|
|
|
public:
|
2012-04-14 03:17:09 +02:00
|
|
|
SimpleFilterFactory(const String& id) : m_id(id) {}
|
2011-12-02 15:28:27 +01:00
|
|
|
|
2012-06-12 20:24:29 +02:00
|
|
|
FilterAndId operator()(const FilterParameters& params) const
|
2011-12-02 15:28:27 +01:00
|
|
|
{
|
|
|
|
return FilterAndId(m_id, FilterFunc(filter_func));
|
|
|
|
}
|
|
|
|
private:
|
2012-04-14 03:17:09 +02:00
|
|
|
String m_id;
|
2011-12-02 15:28:27 +01:00
|
|
|
};
|
|
|
|
|
2012-06-12 20:24:29 +02:00
|
|
|
FilterAndId filter_group_factory(const FilterParameters& params)
|
|
|
|
{
|
|
|
|
if (params.size() != 1)
|
|
|
|
throw runtime_error("wrong parameter count");
|
|
|
|
|
|
|
|
return FilterAndId(params[0], FilterGroup());
|
|
|
|
}
|
|
|
|
|
2011-12-02 15:28:27 +01:00
|
|
|
void register_filters()
|
|
|
|
{
|
|
|
|
FilterRegistry& registry = FilterRegistry::instance();
|
|
|
|
|
|
|
|
registry.register_factory("preserve_indent", SimpleFilterFactory<preserve_indent>("preserve_indent"));
|
2011-12-20 15:27:01 +01:00
|
|
|
registry.register_factory("cleanup_whitespaces", SimpleFilterFactory<cleanup_whitespaces>("cleanup_whitespaces"));
|
2011-12-03 21:25:05 +01:00
|
|
|
registry.register_factory("expand_tabulations", SimpleFilterFactory<expand_tabulations>("expand_tabulations"));
|
2012-06-12 20:24:29 +02:00
|
|
|
registry.register_factory("group", filter_group_factory);
|
2011-12-02 15:28:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|