Filters: add an expand_tabulation filter

This commit is contained in:
Maxime Coste 2011-10-12 18:52:22 +00:00
parent f802a9f3be
commit 22bd9f90d5
5 changed files with 30 additions and 1 deletions

View File

@ -40,6 +40,7 @@ struct DisplayAtom
Color fg_color; Color fg_color;
Color bg_color; Color bg_color;
Attribute attribute; Attribute attribute;
BufferString replacement_text;
DisplayAtom(BufferIterator begin, BufferIterator end, DisplayAtom(BufferIterator begin, BufferIterator end,
Color fg_color = Color::Default, Color fg_color = Color::Default,

View File

@ -52,4 +52,28 @@ void colorize_cplusplus(DisplayBuffer& display_buffer)
colorize_regex(display_buffer, types_keywords, Color::Green); colorize_regex(display_buffer, types_keywords, Color::Green);
} }
void expand_tabulations(DisplayBuffer& display_buffer)
{
const int tabstop = 8;
for (auto atom_it = display_buffer.begin();
atom_it != display_buffer.end(); ++atom_it)
{
for (BufferIterator it = atom_it->begin; it != atom_it->end; ++it)
{
if (*it == '\t')
{
if (it != atom_it->begin)
atom_it = display_buffer.split(atom_it, it) + 1;
if (it+1 != atom_it->end)
atom_it = display_buffer.split(atom_it, it+1);
BufferCoord pos = it.buffer().line_and_column_at(it);
int count = tabstop - (pos.column % tabstop);
atom_it->replacement_text = std::string(count, ' ');
}
}
}
}
} }

View File

@ -11,6 +11,8 @@ void colorize_regex(DisplayBuffer& display_buffer,
const boost::regex& ex, Color color); const boost::regex& ex, Color color);
void colorize_cplusplus(DisplayBuffer& display_buffer); void colorize_cplusplus(DisplayBuffer& display_buffer);
void colorize_cplusplus(DisplayBuffer& display_buffer);
void expand_tabulations(DisplayBuffer& display_buffer);
} }

View File

@ -85,7 +85,8 @@ void draw_window(Window& window)
WindowCoord position; WindowCoord position;
for (const DisplayAtom& atom : window.display_buffer()) for (const DisplayAtom& atom : window.display_buffer())
{ {
const std::string content = window.buffer().string(atom.begin, atom.end); const std::string content = atom.replacement_text.empty() ?
window.buffer().string(atom.begin, atom.end) : atom.replacement_text;
set_attribute(A_UNDERLINE, atom.attribute & Underline); set_attribute(A_UNDERLINE, atom.attribute & Underline);
set_attribute(A_REVERSE, atom.attribute & Reverse); set_attribute(A_REVERSE, atom.attribute & Reverse);

View File

@ -112,6 +112,7 @@ Window::Window(Buffer& buffer)
{ {
m_selections.push_back(Selection(buffer.begin(), buffer.begin())); m_selections.push_back(Selection(buffer.begin(), buffer.begin()));
m_filters.push_back(colorize_cplusplus); m_filters.push_back(colorize_cplusplus);
m_filters.push_back(expand_tabulations);
m_filters.push_back(HighlightSelections(*this)); m_filters.push_back(HighlightSelections(*this));
} }