2011-12-02 15:28:27 +01:00
|
|
|
#include "filters.hh"
|
2013-04-09 20:05:40 +02:00
|
|
|
|
2011-12-02 15:28:27 +01:00
|
|
|
#include "buffer.hh"
|
2012-11-23 18:35:05 +01:00
|
|
|
#include "selection.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
|
|
|
{
|
2013-06-03 18:58:09 +02:00
|
|
|
BufferCoord line_begin{selection.last().line, 0};
|
2013-05-27 19:20:59 +02:00
|
|
|
auto first_non_white = buffer.iterator_at(line_begin);
|
2013-01-04 18:39:13 +01:00
|
|
|
while ((*first_non_white == '\t' or *first_non_white == ' ') and
|
2013-06-04 18:53:56 +02:00
|
|
|
first_non_white != buffer.end())
|
2013-01-04 18:39:13 +01:00
|
|
|
++first_non_white;
|
2011-12-02 15:28:27 +01:00
|
|
|
|
2013-06-04 19:23:11 +02:00
|
|
|
content += buffer.string(line_begin, first_non_white.coord());
|
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
|
|
|
{
|
2013-05-27 19:20:59 +02:00
|
|
|
const auto position = buffer.iterator_at(selection.last());
|
2013-06-04 18:53:56 +02:00
|
|
|
if (content[0] == '\n' and position != buffer.begin())
|
2011-12-20 15:27:01 +01:00
|
|
|
{
|
2013-05-27 19:20:59 +02:00
|
|
|
auto whitespace_start = position-1;
|
2012-08-10 19:12:43 +02:00
|
|
|
while ((*whitespace_start == ' ' or *whitespace_start == '\t') and
|
2013-06-04 18:53:56 +02:00
|
|
|
whitespace_start != buffer.begin())
|
2012-08-10 19:12:43 +02:00
|
|
|
--whitespace_start;
|
|
|
|
++whitespace_start;
|
2013-05-27 19:20:59 +02:00
|
|
|
if (whitespace_start != position)
|
2013-06-06 19:39:53 +02:00
|
|
|
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
|
|
|
{
|
2013-03-03 17:25:40 +01:00
|
|
|
const int tabstop = buffer.options()["tabstop"].get<int>();
|
2012-08-10 19:12:43 +02:00
|
|
|
if (content == "\t")
|
2011-12-03 21:25:05 +01:00
|
|
|
{
|
|
|
|
int column = 0;
|
2013-05-27 19:20:59 +02:00
|
|
|
const auto position = buffer.iterator_at(selection.last());
|
2013-06-04 14:17:11 +02:00
|
|
|
for (auto it = buffer.iterator_at(selection.last().line);
|
2013-05-27 19:20:59 +02:00
|
|
|
it != position; ++it)
|
2011-12-03 21:25:05 +01:00
|
|
|
{
|
2013-05-27 19:20:59 +02:00
|
|
|
kak_assert(*it != '\n');
|
|
|
|
if (*it == '\t')
|
2011-12-03 21:25:05 +01:00
|
|
|
column += tabstop - (column % tabstop);
|
|
|
|
else
|
|
|
|
++column;
|
|
|
|
}
|
|
|
|
|
2013-05-27 19:20:59 +02:00
|
|
|
CharCount count = tabstop - (column % tabstop);
|
|
|
|
content = String(' ', count);
|
2011-12-03 21:25:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-19 14:19:41 +01:00
|
|
|
struct RegexFilter
|
|
|
|
{
|
|
|
|
RegexFilter(const String& line_match, const String& insert_match,
|
|
|
|
const String& replacement)
|
|
|
|
: m_line_match(line_match.c_str()), m_insert_match(insert_match.c_str()),
|
|
|
|
m_replacement(replacement.c_str()) {}
|
|
|
|
|
|
|
|
void operator() (Buffer& buffer, Selection& selection, String& content)
|
|
|
|
{
|
2013-05-27 19:20:59 +02:00
|
|
|
const auto position = buffer.iterator_at(selection.last());
|
2013-06-04 14:17:11 +02:00
|
|
|
auto line_begin = buffer.iterator_at(selection.last().line);
|
2012-11-19 14:19:41 +01:00
|
|
|
boost::match_results<BufferIterator> results;
|
|
|
|
if (boost::regex_match(content.c_str(), m_insert_match) and
|
|
|
|
boost::regex_match(line_begin, position, results, m_line_match))
|
|
|
|
{
|
|
|
|
content = results.format(m_replacement.c_str());
|
|
|
|
auto it = std::find(content.begin(), content.end(), '$');
|
|
|
|
if (it != content.end())
|
|
|
|
{
|
|
|
|
++it;
|
|
|
|
if (it != content.end() && *it == 'c')
|
|
|
|
{
|
|
|
|
String suffix(it+1, content.end());
|
|
|
|
content = String(content.begin(), it-1);
|
2013-06-06 19:39:53 +02:00
|
|
|
buffer.insert(position, suffix);
|
2012-11-19 14:19:41 +01:00
|
|
|
|
2013-06-03 18:58:09 +02:00
|
|
|
auto& first = selection.first();
|
|
|
|
auto& last = selection.last();
|
|
|
|
if (first == last)
|
|
|
|
first = buffer.advance(first, -suffix.length());
|
|
|
|
last = buffer.advance(last, -suffix.length());
|
2012-11-19 14:19:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
Regex m_line_match;
|
|
|
|
Regex m_insert_match;
|
|
|
|
String m_replacement;
|
|
|
|
};
|
|
|
|
|
|
|
|
FilterAndId regex_filter_factory(const FilterParameters& params)
|
|
|
|
{
|
|
|
|
if (params.size() != 3)
|
|
|
|
throw runtime_error("wrong parameter count");
|
|
|
|
|
|
|
|
return FilterAndId{"re" + params[0] + "__" + params[1],
|
|
|
|
RegexFilter{params[0], params[1], params[2]}};
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
|
2012-11-23 13:40:20 +01:00
|
|
|
registry.register_func("preserve_indent", SimpleFilterFactory<preserve_indent>("preserve_indent"));
|
|
|
|
registry.register_func("cleanup_whitespaces", SimpleFilterFactory<cleanup_whitespaces>("cleanup_whitespaces"));
|
|
|
|
registry.register_func("expand_tabulations", SimpleFilterFactory<expand_tabulations>("expand_tabulations"));
|
|
|
|
registry.register_func("regex", regex_filter_factory);
|
|
|
|
registry.register_func("group", filter_group_factory);
|
2011-12-02 15:28:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|