From 316e84f119ba11a4f71e99648d46edb2819a5c3b Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Mon, 17 Oct 2011 19:01:36 +0000 Subject: [PATCH] Filters: add a show line numbers filter --- src/filters.cc | 35 ++++++++++++++++++++++++++++++++++- src/filters.hh | 1 + src/window.cc | 1 + 3 files changed, 36 insertions(+), 1 deletion(-) diff --git a/src/filters.cc b/src/filters.cc index 7e534618f..2f6450f5f 100644 --- a/src/filters.cc +++ b/src/filters.cc @@ -70,7 +70,7 @@ void expand_tabulations(DisplayBuffer& display_buffer) atom_it = display_buffer.split(atom_it, it+1); BufferCoord pos = it.buffer().line_and_column_at(it); - + int column = 0; for (auto line_it = it.buffer().iterator_at({pos.line, 0}); line_it != it; ++line_it) @@ -90,4 +90,37 @@ void expand_tabulations(DisplayBuffer& display_buffer) } } +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; + + atom_it = display_buffer.split(atom_it, line_start) + 1; + } + 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); + } + } +} + } diff --git a/src/filters.hh b/src/filters.hh index 5d084eed2..6ac371820 100644 --- a/src/filters.hh +++ b/src/filters.hh @@ -13,6 +13,7 @@ void colorize_regex(DisplayBuffer& display_buffer, void colorize_cplusplus(DisplayBuffer& display_buffer); void colorize_cplusplus(DisplayBuffer& display_buffer); void expand_tabulations(DisplayBuffer& display_buffer); +void show_line_numbers(DisplayBuffer& display_buffer); } diff --git a/src/window.cc b/src/window.cc index 50a5e9086..39676066c 100644 --- a/src/window.cc +++ b/src/window.cc @@ -114,6 +114,7 @@ Window::Window(Buffer& buffer) m_filters.push_back(colorize_cplusplus); m_filters.push_back(expand_tabulations); m_filters.push_back(HighlightSelections(*this)); + m_filters.push_back(show_line_numbers); } void Window::check_invariant() const