From aeac9484b01aeeb90206a1da605600d07f097056 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Wed, 22 Feb 2012 20:43:59 +0000 Subject: [PATCH] compute the number of digits needed in number_lines highlighter --- src/highlighters.cc | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/highlighters.cc b/src/highlighters.cc index b837ca88..b11ef9c5 100644 --- a/src/highlighters.cc +++ b/src/highlighters.cc @@ -140,7 +140,13 @@ 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; + int last_line = buffer.line_count()-1; + int digit_count = 0; + for (int c = last_line; c > 0; c /= 10) + ++digit_count; + + char format[] = "%?d "; + format[1] = '0' + digit_count; for (; coord.line <= last_line; ++coord.line) { @@ -161,8 +167,8 @@ void show_line_numbers(DisplayBuffer& display_buffer) atom_it->begin(), atom_it->begin(), Color::Black, Color::White)); - char buffer[6]; - snprintf(buffer, 6, "%3d ", coord.line + 1); + char buffer[10]; + snprintf(buffer, 10, format, coord.line + 1); display_buffer.replace_atom_content(atom_it, buffer); } }