From 3262a6902c021a0e2b1b978012132e61bf697940 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Fri, 25 Sep 2015 13:47:57 +0100 Subject: [PATCH] Allow custom separator for line numbers MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit the number_lines highlighter accepts a -separator switch to change the default 'ā”‚' separator. Fixes #295 --- src/highlighters.cc | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/src/highlighters.cc b/src/highlighters.cc index 9c818a89..3bbccca8 100644 --- a/src/highlighters.cc +++ b/src/highlighters.cc @@ -707,9 +707,10 @@ HighlighterAndId create_show_whitespaces_highlighter(HighlighterParameters param return {"show_whitespaces", make_simple_highlighter(show_whitespaces)}; } -template void show_line_numbers(const Context& context, HighlightFlags flags, - DisplayBuffer& display_buffer, BufferRange) + DisplayBuffer& display_buffer, BufferRange, + bool relative, bool hl_cursor_line, + StringView separator) { const Face face = get_face("LineNumbers"); const Face face_absolute = get_face("LineNumberCursor"); @@ -718,8 +719,8 @@ void show_line_numbers(const Context& context, HighlightFlags flags, for (LineCount c = last_line; c > 0; c /= 10) ++digit_count; - char format[] = "%?dā”‚"; - format[1] = '0' + digit_count + (relative ? 1 : 0); + char format[16]; + format_to(format, "%{}d{}", digit_count + (relative ? 1 : 0), separator); int main_selection = (int)context.selections().main().cursor().line + 1; for (auto& line : display_buffer.lines()) { @@ -739,24 +740,23 @@ HighlighterAndId number_lines_factory(HighlighterParameters params) { static const ParameterDesc param_desc{ { { "relative", { false, "" } }, + { "separator", { true, "" } }, { "hlcursor", { false, "" } } }, ParameterDesc::Flags::None, 0, 0 }; ParametersParser parser(params, param_desc); - constexpr struct { - StringView name; - void (*func)(const Context&, HighlightFlags, DisplayBuffer&, BufferRange); - } funcs[] = { - { "number_lines", show_line_numbers }, - { "number_lines", show_line_numbers }, - { "number_lines_relative", show_line_numbers }, - { "number_lines_relative", show_line_numbers }, - }; - const int index = (parser.get_switch("relative") ? 1 : 0) * 2 + - (parser.get_switch("hlcursor") ? 1 : 0); + StringView separator = parser.get_switch("separator").value_or("ā”‚"); + if (separator.length() > 10) + throw runtime_error("Separator length is limited to 10 bytes"); - return {funcs[index].name.str(), make_simple_highlighter(funcs[index].func)}; + using namespace std::placeholders; + auto func = std::bind(show_line_numbers, _1, _2, _3, _4, + (bool)parser.get_switch("relative"), + (bool)parser.get_switch("hlcursor"), + separator.str()); + + return {"number_lines", make_simple_highlighter(std::move(func))}; } void show_matching_char(const Context& context, HighlightFlags flags, DisplayBuffer& display_buffer, BufferRange) @@ -1346,7 +1346,7 @@ void register_highlighters() "number_lines", { number_lines_factory, "Display line numbers \n" - "Parameters: -relative, -hlcursor\n" } }); + "Parameters: -relative, -hlcursor, -separator \n" } }); registry.append({ "show_matching", { create_matching_char_highlighter,