From 22bd9f90d59b9c784cc5986ca02b891eef93ee3f Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Wed, 12 Oct 2011 18:52:22 +0000 Subject: [PATCH] Filters: add an expand_tabulation filter --- src/display_buffer.hh | 1 + src/filters.cc | 24 ++++++++++++++++++++++++ src/filters.hh | 2 ++ src/main.cc | 3 ++- src/window.cc | 1 + 5 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/display_buffer.hh b/src/display_buffer.hh index 59468eea..7f022b8c 100644 --- a/src/display_buffer.hh +++ b/src/display_buffer.hh @@ -40,6 +40,7 @@ struct DisplayAtom Color fg_color; Color bg_color; Attribute attribute; + BufferString replacement_text; DisplayAtom(BufferIterator begin, BufferIterator end, Color fg_color = Color::Default, diff --git a/src/filters.cc b/src/filters.cc index 56f1a86e..5b306f2f 100644 --- a/src/filters.cc +++ b/src/filters.cc @@ -52,4 +52,28 @@ void colorize_cplusplus(DisplayBuffer& display_buffer) 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, ' '); + } + } + } +} + } diff --git a/src/filters.hh b/src/filters.hh index 2dc6370c..5d084eed 100644 --- a/src/filters.hh +++ b/src/filters.hh @@ -11,6 +11,8 @@ void colorize_regex(DisplayBuffer& display_buffer, const boost::regex& ex, Color color); void colorize_cplusplus(DisplayBuffer& display_buffer); +void colorize_cplusplus(DisplayBuffer& display_buffer); +void expand_tabulations(DisplayBuffer& display_buffer); } diff --git a/src/main.cc b/src/main.cc index e7693a8a..04a845a9 100644 --- a/src/main.cc +++ b/src/main.cc @@ -85,7 +85,8 @@ void draw_window(Window& window) WindowCoord position; 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_REVERSE, atom.attribute & Reverse); diff --git a/src/window.cc b/src/window.cc index ef797247..92b8ef5c 100644 --- a/src/window.cc +++ b/src/window.cc @@ -112,6 +112,7 @@ Window::Window(Buffer& buffer) { m_selections.push_back(Selection(buffer.begin(), buffer.begin())); m_filters.push_back(colorize_cplusplus); + m_filters.push_back(expand_tabulations); m_filters.push_back(HighlightSelections(*this)); }