2011-11-29 23:37:20 +01:00
|
|
|
#include "highlighters.hh"
|
2011-09-30 21:16:23 +02:00
|
|
|
|
2011-11-14 15:23:02 +01:00
|
|
|
#include "assert.hh"
|
2011-11-10 00:56:22 +01:00
|
|
|
#include "window.hh"
|
2011-11-09 15:06:05 +01:00
|
|
|
#include "display_buffer.hh"
|
2011-11-29 23:37:20 +01:00
|
|
|
#include "highlighter_registry.hh"
|
2012-01-19 21:37:29 +01:00
|
|
|
#include "highlighter_group.hh"
|
2012-04-14 03:17:09 +02:00
|
|
|
#include "regex.hh"
|
2011-11-08 15:28:01 +01:00
|
|
|
|
2011-09-30 21:16:23 +02:00
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
2012-04-03 15:39:20 +02:00
|
|
|
using namespace std::placeholders;
|
|
|
|
|
2012-05-03 09:25:13 +02:00
|
|
|
typedef boost::regex_iterator<BufferIterator> RegexIterator;
|
|
|
|
|
2011-11-24 20:11:58 +01:00
|
|
|
void colorize_regex_range(DisplayBuffer& display_buffer,
|
|
|
|
const BufferIterator& range_begin,
|
|
|
|
const BufferIterator& range_end,
|
2012-04-14 03:17:09 +02:00
|
|
|
const Regex& ex,
|
2011-11-24 20:11:58 +01:00
|
|
|
Color fg_color, Color bg_color = Color::Default)
|
2011-09-30 21:16:23 +02:00
|
|
|
{
|
2011-11-26 19:34:49 +01:00
|
|
|
assert(range_begin <= range_end);
|
|
|
|
|
|
|
|
if (range_begin >= display_buffer.back().end() or
|
|
|
|
range_end <= display_buffer.front().begin())
|
|
|
|
return;
|
|
|
|
|
2011-11-24 20:11:58 +01:00
|
|
|
BufferIterator display_begin = std::max(range_begin,
|
|
|
|
display_buffer.front().begin());
|
|
|
|
BufferIterator display_end = std::min(range_end,
|
|
|
|
display_buffer.back().end());
|
2011-10-23 22:26:51 +02:00
|
|
|
|
2012-04-14 03:17:09 +02:00
|
|
|
RegexIterator re_it(display_begin, display_end, ex, boost::match_nosubs);
|
|
|
|
RegexIterator re_end;
|
2011-10-23 22:26:51 +02:00
|
|
|
DisplayBuffer::iterator atom_it = display_buffer.begin();
|
|
|
|
for (; re_it != re_end; ++re_it)
|
2011-09-30 21:16:23 +02:00
|
|
|
{
|
2011-10-23 22:26:51 +02:00
|
|
|
BufferIterator begin = (*re_it)[0].first;
|
|
|
|
BufferIterator end = (*re_it)[0].second;
|
2011-11-14 15:23:02 +01:00
|
|
|
assert(begin != end);
|
|
|
|
|
2011-10-23 22:26:51 +02:00
|
|
|
auto begin_atom_it = display_buffer.atom_containing(begin, atom_it);
|
2011-11-14 15:23:02 +01:00
|
|
|
assert(begin_atom_it != display_buffer.end());
|
|
|
|
if (begin_atom_it->begin() != begin)
|
2012-07-03 23:23:07 +02:00
|
|
|
{
|
|
|
|
if (begin_atom_it->splitable())
|
|
|
|
begin_atom_it = ++display_buffer.split(begin_atom_it, begin);
|
|
|
|
else
|
|
|
|
++begin_atom_it;
|
|
|
|
}
|
2011-11-14 15:23:02 +01:00
|
|
|
|
|
|
|
auto end_atom_it = display_buffer.atom_containing(end, begin_atom_it);
|
|
|
|
if (end_atom_it != display_buffer.end() and
|
2012-07-03 23:23:07 +02:00
|
|
|
end_atom_it->begin() != end and end_atom_it->splitable())
|
2011-11-14 15:23:02 +01:00
|
|
|
end_atom_it = ++display_buffer.split(end_atom_it, end);
|
2011-09-30 21:16:23 +02:00
|
|
|
|
2011-11-14 15:23:02 +01:00
|
|
|
for (auto it = begin_atom_it; it != end_atom_it; ++it)
|
|
|
|
{
|
2012-02-22 22:48:14 +01:00
|
|
|
if (it->attribute() & Attributes::Final)
|
|
|
|
continue;
|
|
|
|
|
2011-11-14 15:23:02 +01:00
|
|
|
it->fg_color() = fg_color;
|
|
|
|
it->bg_color() = bg_color;
|
2011-09-30 21:16:23 +02:00
|
|
|
}
|
2011-11-14 15:23:02 +01:00
|
|
|
|
|
|
|
atom_it = end_atom_it;
|
2011-09-30 21:16:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-24 20:11:58 +01:00
|
|
|
void colorize_regex(DisplayBuffer& display_buffer,
|
2012-04-14 03:17:09 +02:00
|
|
|
const Regex& ex,
|
2011-11-24 20:11:58 +01:00
|
|
|
Color fg_color, Color bg_color = Color::Default)
|
|
|
|
{
|
|
|
|
colorize_regex_range(display_buffer, display_buffer.front().begin(),
|
|
|
|
display_buffer.back().end(), ex, fg_color, bg_color);
|
|
|
|
}
|
|
|
|
|
2012-04-14 03:17:09 +02:00
|
|
|
Color parse_color(const String& color)
|
2011-11-10 15:28:38 +01:00
|
|
|
{
|
|
|
|
if (color == "default") return Color::Default;
|
|
|
|
if (color == "black") return Color::Black;
|
|
|
|
if (color == "red") return Color::Red;
|
|
|
|
if (color == "green") return Color::Green;
|
|
|
|
if (color == "yellow") return Color::Yellow;
|
|
|
|
if (color == "blue") return Color::Blue;
|
|
|
|
if (color == "magenta") return Color::Magenta;
|
|
|
|
if (color == "cyan") return Color::Cyan;
|
|
|
|
if (color == "white") return Color::White;
|
|
|
|
return Color::Default;
|
|
|
|
}
|
|
|
|
|
2011-11-29 23:37:20 +01:00
|
|
|
HighlighterAndId colorize_regex_factory(Window& window,
|
2011-11-30 15:11:53 +01:00
|
|
|
const HighlighterParameters params)
|
2011-11-10 15:28:38 +01:00
|
|
|
{
|
|
|
|
if (params.size() != 3)
|
|
|
|
throw runtime_error("wrong parameter count");
|
|
|
|
|
2012-04-14 03:17:09 +02:00
|
|
|
Regex ex(params[0].begin(), params[0].end());
|
2011-11-10 15:28:38 +01:00
|
|
|
|
|
|
|
Color fg_color = parse_color(params[1]);
|
|
|
|
Color bg_color = parse_color(params[2]);
|
|
|
|
|
2012-04-14 03:17:09 +02:00
|
|
|
String id = "colre'" + params[0] + "'";
|
2011-11-15 15:28:03 +01:00
|
|
|
|
2012-04-03 15:39:20 +02:00
|
|
|
return HighlighterAndId(id, std::bind(colorize_regex, _1,
|
2012-04-03 15:22:07 +02:00
|
|
|
ex, fg_color, bg_color));
|
2011-11-10 15:28:38 +01:00
|
|
|
}
|
|
|
|
|
2012-04-03 15:39:20 +02:00
|
|
|
void expand_tabulations(Window& window, DisplayBuffer& display_buffer)
|
2011-10-12 20:52:22 +02:00
|
|
|
{
|
2012-06-12 20:26:20 +02:00
|
|
|
const int tabstop = window.option_manager()["tabstop"].as_int();
|
2011-10-12 20:52:22 +02:00
|
|
|
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);
|
2012-04-14 03:17:09 +02:00
|
|
|
String padding;
|
|
|
|
for (int i = 0; i < count; ++i)
|
|
|
|
padding += ' ';
|
|
|
|
display_buffer.replace_atom_content(atom_it, padding);
|
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());
|
|
|
|
|
2012-02-27 15:24:14 +01:00
|
|
|
int last_line = buffer.line_count();
|
2012-02-22 21:43:59 +01:00
|
|
|
int digit_count = 0;
|
|
|
|
for (int c = last_line; c > 0; c /= 10)
|
|
|
|
++digit_count;
|
|
|
|
|
|
|
|
char format[] = "%?d ";
|
|
|
|
format[1] = '0' + digit_count;
|
2011-10-17 21:01:36 +02:00
|
|
|
|
2012-02-27 15:24:14 +01:00
|
|
|
for (; coord.line <= last_line-1; ++coord.line)
|
2011-10-17 21:01:36 +02:00
|
|
|
{
|
|
|
|
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
|
|
|
}
|
2012-07-03 23:23:07 +02:00
|
|
|
atom_it = display_buffer.insert_empty_atom_before(atom_it);
|
|
|
|
atom_it->fg_color() = Color::Black;
|
|
|
|
atom_it->bg_color() = Color::White;
|
|
|
|
atom_it->attribute() = Attributes::Final;
|
2011-10-17 21:01:36 +02:00
|
|
|
|
2012-02-22 21:43:59 +01:00
|
|
|
char buffer[10];
|
|
|
|
snprintf(buffer, 10, format, coord.line + 1);
|
2011-10-17 21:01:36 +02:00
|
|
|
display_buffer.replace_atom_content(atom_it, buffer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-03 15:22:07 +02:00
|
|
|
void highlight_selections(Window& window, DisplayBuffer& display_buffer)
|
|
|
|
{
|
|
|
|
typedef std::pair<BufferIterator, BufferIterator> BufferRange;
|
|
|
|
|
|
|
|
std::vector<BufferRange> selections;
|
|
|
|
for (auto& sel : window.selections())
|
|
|
|
selections.push_back(BufferRange(sel.begin(), sel.end()));
|
|
|
|
|
|
|
|
std::sort(selections.begin(), selections.end(),
|
|
|
|
[](const BufferRange& lhs, const BufferRange& rhs)
|
|
|
|
{ return lhs.first < rhs.first; });
|
|
|
|
|
|
|
|
auto atom_it = display_buffer.begin();
|
|
|
|
auto sel_it = selections.begin();
|
|
|
|
|
|
|
|
// underline each selections
|
|
|
|
while (atom_it != display_buffer.end()
|
|
|
|
and sel_it != selections.end())
|
|
|
|
{
|
|
|
|
BufferRange& sel = *sel_it;
|
|
|
|
DisplayAtom& atom = *atom_it;
|
|
|
|
|
|
|
|
if (atom.attribute() & Attributes::Final)
|
|
|
|
{
|
|
|
|
++atom_it;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// [###------]
|
|
|
|
if (atom.begin() >= sel.first and atom.begin() < sel.second and
|
|
|
|
atom.end() > sel.second)
|
|
|
|
{
|
|
|
|
atom_it = display_buffer.split(atom_it, sel.second);
|
|
|
|
atom_it->attribute() |= Attributes::Underline;
|
|
|
|
++atom_it;
|
|
|
|
++sel_it;
|
|
|
|
}
|
|
|
|
// [---###---]
|
|
|
|
else if (atom.begin() < sel.first and atom.end() > sel.second)
|
|
|
|
{
|
|
|
|
atom_it = display_buffer.split(atom_it, sel.first);
|
|
|
|
atom_it = display_buffer.split(++atom_it, sel.second);
|
|
|
|
atom_it->attribute() |= Attributes::Underline;
|
|
|
|
++atom_it;
|
|
|
|
++sel_it;
|
|
|
|
}
|
|
|
|
// [------###]
|
|
|
|
else if (atom.begin() < sel.first and atom.end() > sel.first)
|
|
|
|
{
|
|
|
|
atom_it = ++display_buffer.split(atom_it, sel.first);
|
|
|
|
atom_it->attribute() |= Attributes::Underline;
|
|
|
|
++atom_it;
|
|
|
|
}
|
|
|
|
// [#########]
|
|
|
|
else if (atom.begin() >= sel.first and atom.end() <= sel.second)
|
|
|
|
{
|
|
|
|
atom_it->attribute() |= Attributes::Underline;
|
|
|
|
++atom_it;
|
|
|
|
}
|
|
|
|
// [---------]
|
|
|
|
else if (atom.begin() >= sel.second)
|
|
|
|
++sel_it;
|
|
|
|
// [---------]
|
|
|
|
else if (atom.end() <= sel.first)
|
|
|
|
++atom_it;
|
|
|
|
else
|
|
|
|
assert(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
// invert selection last char
|
|
|
|
for (auto& sel : window.selections())
|
|
|
|
{
|
|
|
|
const BufferIterator& last = sel.last();
|
|
|
|
|
|
|
|
DisplayBuffer::iterator atom_it = display_buffer.atom_containing(last);
|
2012-07-03 23:23:07 +02:00
|
|
|
if (atom_it == display_buffer.end() or not atom_it->splitable())
|
2012-04-03 15:22:07 +02:00
|
|
|
continue;
|
|
|
|
|
|
|
|
if (atom_it->begin() < last)
|
|
|
|
atom_it = ++display_buffer.split(atom_it, last);
|
|
|
|
if (atom_it->end() > last + 1)
|
|
|
|
atom_it = display_buffer.split(atom_it, last + 1);
|
|
|
|
|
|
|
|
atom_it->attribute() |= Attributes::Reverse;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-11-29 23:37:20 +01:00
|
|
|
template<void (*highlighter_func)(DisplayBuffer&)>
|
|
|
|
class SimpleHighlighterFactory
|
2011-11-08 15:28:01 +01:00
|
|
|
{
|
|
|
|
public:
|
2012-04-14 03:17:09 +02:00
|
|
|
SimpleHighlighterFactory(const String& id) : m_id(id) {}
|
2011-11-08 15:28:01 +01:00
|
|
|
|
2011-11-29 23:37:20 +01:00
|
|
|
HighlighterAndId operator()(Window& window,
|
|
|
|
const HighlighterParameters& params) const
|
2011-11-08 15:28:01 +01:00
|
|
|
{
|
2011-11-29 23:37:20 +01:00
|
|
|
return HighlighterAndId(m_id, HighlighterFunc(highlighter_func));
|
2011-11-08 15:28:01 +01:00
|
|
|
}
|
|
|
|
private:
|
2012-04-14 03:17:09 +02:00
|
|
|
String m_id;
|
2011-11-08 15:28:01 +01:00
|
|
|
};
|
|
|
|
|
2012-04-03 15:22:07 +02:00
|
|
|
template<void (*highlighter_func)(Window&, DisplayBuffer&)>
|
|
|
|
class WindowHighlighterFactory
|
2011-11-10 00:56:22 +01:00
|
|
|
{
|
|
|
|
public:
|
2012-04-14 03:17:09 +02:00
|
|
|
WindowHighlighterFactory(const String& id) : m_id(id) {}
|
2011-11-10 00:56:22 +01:00
|
|
|
|
2012-04-03 15:22:07 +02:00
|
|
|
HighlighterAndId operator()(Window& window,
|
|
|
|
const HighlighterParameters& params) const
|
2011-11-10 00:56:22 +01:00
|
|
|
{
|
2012-04-03 15:22:07 +02:00
|
|
|
return HighlighterAndId(m_id, std::bind(highlighter_func, std::ref(window), _1));
|
2011-11-10 00:56:22 +01:00
|
|
|
}
|
|
|
|
private:
|
2012-04-14 03:17:09 +02:00
|
|
|
String m_id;
|
2011-11-10 00:56:22 +01:00
|
|
|
};
|
|
|
|
|
2012-01-19 21:37:29 +01:00
|
|
|
HighlighterAndId highlighter_group_factory(Window& window,
|
|
|
|
const HighlighterParameters& params)
|
2012-01-15 14:45:18 +01:00
|
|
|
{
|
|
|
|
if (params.size() != 1)
|
|
|
|
throw runtime_error("wrong parameter count");
|
|
|
|
|
|
|
|
return HighlighterAndId(params[0], HighlighterGroup());
|
|
|
|
}
|
|
|
|
|
2011-11-29 23:37:20 +01:00
|
|
|
void register_highlighters()
|
2011-11-08 15:28:01 +01:00
|
|
|
{
|
2011-11-29 23:37:20 +01:00
|
|
|
HighlighterRegistry& registry = HighlighterRegistry::instance();
|
2011-11-15 15:28:03 +01:00
|
|
|
|
2012-04-03 15:22:07 +02:00
|
|
|
registry.register_factory("highlight_selections", WindowHighlighterFactory<highlight_selections>("highlight_selections"));
|
2012-04-03 15:39:20 +02:00
|
|
|
registry.register_factory("expand_tabs", WindowHighlighterFactory<expand_tabulations>("expand_tabs"));
|
2011-11-29 23:37:20 +01:00
|
|
|
registry.register_factory("number_lines", SimpleHighlighterFactory<show_line_numbers>("number_lines"));
|
2011-11-10 15:28:38 +01:00
|
|
|
registry.register_factory("regex", colorize_regex_factory);
|
2012-01-19 21:37:29 +01:00
|
|
|
registry.register_factory("group", highlighter_group_factory);
|
2011-11-08 15:28:01 +01:00
|
|
|
}
|
|
|
|
|
2011-09-30 21:16:23 +02:00
|
|
|
}
|