2011-09-30 21:16:23 +02:00
|
|
|
#include "filters.hh"
|
|
|
|
|
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
|
|
|
void colorize_regex(DisplayBuffer& display_buffer,
|
|
|
|
const boost::regex& ex, Color color)
|
|
|
|
{
|
|
|
|
for (auto atom_it = display_buffer.begin();
|
|
|
|
atom_it != display_buffer.end(); ++atom_it)
|
|
|
|
{
|
2011-10-07 16:19:58 +02:00
|
|
|
boost::match_results<BufferIterator> matches;
|
2011-10-15 06:45:49 +02:00
|
|
|
if (boost::regex_search(atom_it->begin(), atom_it->end(),
|
|
|
|
matches, ex, boost::match_nosubs))
|
2011-09-30 21:16:23 +02:00
|
|
|
{
|
2011-10-07 16:19:58 +02:00
|
|
|
const BufferIterator& begin = matches.begin()->first;
|
2011-10-15 06:45:49 +02:00
|
|
|
if (begin != atom_it->begin())
|
2011-10-18 15:59:32 +02:00
|
|
|
atom_it = ++display_buffer.split(atom_it, begin);
|
2011-09-30 21:16:23 +02:00
|
|
|
|
2011-10-07 16:19:58 +02:00
|
|
|
const BufferIterator& end = matches.begin()->second;
|
2011-10-15 06:45:49 +02:00
|
|
|
if (end != atom_it->end())
|
2011-10-07 16:19:58 +02:00
|
|
|
atom_it = display_buffer.split(atom_it, end);
|
2011-09-30 21:16:23 +02:00
|
|
|
|
2011-10-15 06:45:49 +02:00
|
|
|
atom_it->fg_color() = color;
|
2011-09-30 21:16:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-03 16:30:14 +02:00
|
|
|
void colorize_cplusplus(DisplayBuffer& display_buffer)
|
|
|
|
{
|
2011-10-07 16:28:38 +02:00
|
|
|
static boost::regex preprocessor("(\\`|(?<=\\n))\\h*#\\h*[^\\n]*(?=\\n)");
|
2011-10-03 16:30:14 +02:00
|
|
|
colorize_regex(display_buffer, preprocessor, Color::Magenta);
|
|
|
|
|
2011-10-04 21:08:34 +02:00
|
|
|
static boost::regex comments("//[^\\n]*\\n");
|
|
|
|
colorize_regex(display_buffer, comments, Color::Cyan);
|
|
|
|
|
|
|
|
static boost::regex strings("(?<!')\"(\\\\\"|[^\"])*\"");
|
2011-10-03 16:30:14 +02:00
|
|
|
colorize_regex(display_buffer, strings, Color::Magenta);
|
|
|
|
|
2011-10-07 16:28:38 +02:00
|
|
|
static boost::regex values("\\<(true|false|NULL|nullptr)\\>|-?\\d+[fdiu]?|'\\\\?[^']?'");
|
2011-10-03 16:30:14 +02:00
|
|
|
colorize_regex(display_buffer, values, Color::Red);
|
|
|
|
|
|
|
|
static boost::regex builtin_types("\\<(void|int|float|bool|size_t)\\>");
|
|
|
|
colorize_regex(display_buffer, builtin_types, Color::Yellow);
|
|
|
|
|
2011-10-04 21:08:34 +02:00
|
|
|
static boost::regex control_keywords("\\<(while|for|if|else|do|switch|case|default|goto|break|continue|return|using|try|catch|throw)\\>");
|
2011-10-03 16:30:14 +02:00
|
|
|
colorize_regex(display_buffer, control_keywords, Color::Blue);
|
|
|
|
|
2011-10-04 21:08:34 +02:00
|
|
|
//static boost::regex operators("->|\\+|\\-|\\*|/|\\\\|\\&|\\|\\^|[<>=!+-]=|=|\\(|\\)|\\[|\\]|\\{|\\}|\\<(not|and|or|xor)\\>");
|
|
|
|
//colorize_regex(display_buffer, operators, Color::Green);
|
|
|
|
|
2011-10-06 23:12:16 +02:00
|
|
|
static boost::regex types_keywords("\\<(const|auto|namespace|static|volatile|class|struct|enum|union|public|protected|private|template)\\>");
|
2011-10-03 16:30:14 +02:00
|
|
|
colorize_regex(display_buffer, types_keywords, Color::Green);
|
|
|
|
}
|
|
|
|
|
2011-10-12 20:52:22 +02:00
|
|
|
void expand_tabulations(DisplayBuffer& display_buffer)
|
|
|
|
{
|
|
|
|
const int tabstop = 8;
|
|
|
|
for (auto atom_it = display_buffer.begin();
|
|
|
|
atom_it != display_buffer.end(); ++atom_it)
|
|
|
|
{
|
2011-10-15 06:45:49 +02:00
|
|
|
for (BufferIterator it = atom_it->begin(); it != atom_it->end(); ++it)
|
2011-10-12 20:52:22 +02:00
|
|
|
{
|
|
|
|
if (*it == '\t')
|
|
|
|
{
|
2011-10-15 06:45:49 +02:00
|
|
|
if (it != atom_it->begin())
|
2011-10-18 15:59:32 +02:00
|
|
|
atom_it = ++display_buffer.split(atom_it, it);
|
2011-10-12 20:52:22 +02:00
|
|
|
|
2011-10-15 06:45:49 +02:00
|
|
|
if (it+1 != atom_it->end())
|
2011-10-12 20:52:22 +02:00
|
|
|
atom_it = display_buffer.split(atom_it, it+1);
|
|
|
|
|
|
|
|
BufferCoord pos = it.buffer().line_and_column_at(it);
|
2011-10-17 21:01:36 +02:00
|
|
|
|
2011-10-15 06:45:49 +02:00
|
|
|
int column = 0;
|
|
|
|
for (auto line_it = it.buffer().iterator_at({pos.line, 0});
|
|
|
|
line_it != it; ++line_it)
|
|
|
|
{
|
|
|
|
assert(*line_it != '\n');
|
|
|
|
if (*line_it == '\t')
|
|
|
|
column += tabstop - (column % tabstop);
|
|
|
|
else
|
|
|
|
++column;
|
|
|
|
}
|
|
|
|
|
|
|
|
int count = tabstop - (column % tabstop);
|
|
|
|
display_buffer.replace_atom_content(atom_it,
|
|
|
|
std::string(count, ' '));
|
2011-10-12 20:52:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-17 21:01:36 +02:00
|
|
|
void show_line_numbers(DisplayBuffer& display_buffer)
|
|
|
|
{
|
|
|
|
const Buffer& buffer = display_buffer.front().begin().buffer();
|
|
|
|
BufferCoord coord = buffer.line_and_column_at(display_buffer.begin()->begin());
|
|
|
|
|
|
|
|
int last_line = buffer.line_and_column_at(display_buffer.back().end()-1).line;
|
|
|
|
|
|
|
|
for (; coord.line <= last_line; ++coord.line)
|
|
|
|
{
|
|
|
|
BufferIterator line_start = buffer.iterator_at(coord);
|
|
|
|
DisplayBuffer::iterator atom_it = display_buffer.atom_containing(line_start);
|
|
|
|
if (atom_it != display_buffer.end())
|
|
|
|
{
|
|
|
|
if (atom_it->begin() != line_start)
|
|
|
|
{
|
|
|
|
if (not atom_it->splitable())
|
|
|
|
continue;
|
|
|
|
|
2011-10-18 15:59:32 +02:00
|
|
|
atom_it = ++display_buffer.split(atom_it, line_start);
|
2011-10-17 21:01:36 +02:00
|
|
|
}
|
|
|
|
atom_it = display_buffer.insert(
|
|
|
|
atom_it,
|
|
|
|
DisplayAtom(atom_it->coord(),
|
|
|
|
atom_it->begin(), atom_it->begin(),
|
|
|
|
Color::Black, Color::White));
|
|
|
|
|
|
|
|
char buffer[6];
|
|
|
|
snprintf(buffer, 6, "%3d ", coord.line);
|
|
|
|
display_buffer.replace_atom_content(atom_it, buffer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-30 21:16:23 +02:00
|
|
|
}
|