2011-12-02 15:28:27 +01:00
|
|
|
#include "filters.hh"
|
|
|
|
#include "filter_registry.hh"
|
|
|
|
#include "buffer.hh"
|
|
|
|
|
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
2011-12-06 19:58:43 +01:00
|
|
|
void preserve_indent(Buffer& buffer, Modification& modification)
|
2011-12-02 15:28:27 +01:00
|
|
|
{
|
2011-12-06 19:58:43 +01:00
|
|
|
if (modification.type == Modification::Insert and
|
2011-12-02 15:28:27 +01:00
|
|
|
modification.content == "\n")
|
|
|
|
{
|
|
|
|
BufferIterator line_begin = buffer.iterator_at_line_begin(modification.position - 1);
|
|
|
|
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;
|
|
|
|
|
|
|
|
modification.content += buffer.string(line_begin, first_non_white);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-20 15:27:01 +01:00
|
|
|
void cleanup_whitespaces(Buffer& buffer, Modification& modification)
|
|
|
|
{
|
|
|
|
if (modification.type == Modification::Insert and
|
|
|
|
modification.content[0] == '\n' and not modification.position.is_begin())
|
|
|
|
{
|
|
|
|
BufferIterator position = modification.position-1;
|
|
|
|
while (*position == ' ' or *position == '\t' and not position.is_begin())
|
|
|
|
--position;
|
|
|
|
++position;
|
|
|
|
if (position != modification.position)
|
|
|
|
{
|
|
|
|
buffer.modify(Modification::make_erase(position, modification.position));
|
|
|
|
modification.position = position;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-06 19:58:43 +01:00
|
|
|
void expand_tabulations(Buffer& buffer, Modification& modification)
|
2011-12-03 21:25:05 +01:00
|
|
|
{
|
|
|
|
const int tabstop = 8;
|
2011-12-06 19:58:43 +01:00
|
|
|
if (modification.type == Modification::Insert and
|
2011-12-03 21:25:05 +01:00
|
|
|
modification.content == "\t")
|
|
|
|
{
|
|
|
|
int column = 0;
|
|
|
|
BufferCoord pos = buffer.line_and_column_at(modification.position);
|
|
|
|
for (auto line_it = buffer.iterator_at({pos.line, 0});
|
|
|
|
line_it != modification.position; ++line_it)
|
|
|
|
{
|
|
|
|
assert(*line_it != '\n');
|
|
|
|
if (*line_it == '\t')
|
|
|
|
column += tabstop - (column % tabstop);
|
|
|
|
else
|
|
|
|
++column;
|
|
|
|
}
|
|
|
|
|
|
|
|
int count = tabstop - (column % tabstop);
|
|
|
|
modification.content = std::string(count, ' ');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-06 19:58:43 +01:00
|
|
|
template<void (*filter_func)(Buffer&, Modification&)>
|
2011-12-02 15:28:27 +01:00
|
|
|
class SimpleFilterFactory
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
SimpleFilterFactory(const std::string& id) : m_id(id) {}
|
|
|
|
|
2011-12-07 15:29:10 +01:00
|
|
|
FilterAndId operator()(Window& window,
|
2011-12-02 15:28:27 +01:00
|
|
|
const FilterParameters& params) const
|
|
|
|
{
|
|
|
|
return FilterAndId(m_id, FilterFunc(filter_func));
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
std::string m_id;
|
|
|
|
};
|
|
|
|
|
|
|
|
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"));
|
2011-12-02 15:28:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|