summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMaxime Coste <frrrwww@gmail.com>2015-03-19 00:31:11 +0000
committerMaxime Coste <frrrwww@gmail.com>2015-03-19 00:31:11 +0000
commitf5da1671fdb19c8930acc6af276ba3e8cab8cb7b (patch)
tree2520ecbdbaf1c5750dddb11cc5f241927c3c9973 /src
parent006f8ca5886549eef25bc566941f354ccf09523d (diff)
Refactor number_line higlighter, use a switch to higlhight the cursor line
Diffstat (limited to 'src')
-rw-r--r--src/face_registry.cc2
-rw-r--r--src/highlighters.cc64
2 files changed, 33 insertions, 33 deletions
diff --git a/src/face_registry.cc b/src/face_registry.cc
index ffdf5ab6..1c373be1 100644
--- a/src/face_registry.cc
+++ b/src/face_registry.cc
@@ -96,7 +96,7 @@ FaceRegistry::FaceRegistry()
{ "PrimaryCursor", Face{ Colors::Black, Colors::White } },
{ "SecondaryCursor", Face{ Colors::Black, Colors::White } },
{ "LineNumbers", Face{ Colors::Default, Colors::Default } },
- { "LineNumberAbsolute", Face{ Colors::Black, Colors::Blue } },
+ { "LineNumberCursor", Face{ Colors::Default, Colors::Default, Attribute::Reverse } },
{ "MenuForeground", Face{ Colors::White, Colors::Blue } },
{ "MenuBackground", Face{ Colors::Blue, Colors::White } },
{ "Information", Face{ Colors::Black, Colors::Yellow } },
diff --git a/src/highlighters.cc b/src/highlighters.cc
index 9e0461e7..286d9db2 100644
--- a/src/highlighters.cc
+++ b/src/highlighters.cc
@@ -594,11 +594,12 @@ void show_whitespaces(const Context& context, HighlightFlags flags, DisplayBuffe
}
}
-template<bool relative>
-void show_line_numbers(const Context& context, HighlightFlags flags, DisplayBuffer& display_buffer, BufferRange)
+template<bool relative, bool hl_cursor_line>
+void show_line_numbers(const Context& context, HighlightFlags flags,
+ DisplayBuffer& display_buffer, BufferRange)
{
const Face face = get_face("LineNumbers");
- const Face face_absolute = get_face("LineNumberAbsolute");
+ const Face face_absolute = get_face("LineNumberCursor");
LineCount last_line = context.buffer().line_count();
int digit_count = 0;
for (LineCount c = last_line; c > 0; c /= 10)
@@ -609,41 +610,40 @@ void show_line_numbers(const Context& context, HighlightFlags flags, DisplayBuff
int main_selection = (int)context.selections().main().cursor().line + 1;
for (auto& line : display_buffer.lines())
{
- int current_line = (int)line.range().first.line + 1;
+ const int current_line = (int)line.range().first.line + 1;
+ const bool is_cursor_line = main_selection == current_line;
+ const int line_to_format = (relative and not is_cursor_line) ?
+ current_line - main_selection : current_line;
char buffer[16];
- if (relative)
- {
- if (main_selection == current_line)
- {
- snprintf(buffer, 16, format, current_line);
- DisplayAtom atom{buffer};
- atom.face = face_absolute;
- line.insert(line.begin(), std::move(atom));
- }
- else {
- snprintf(buffer, 16, format, current_line - main_selection);
- DisplayAtom atom{buffer};
- atom.face = face;
- line.insert(line.begin(), std::move(atom));
- }
- }
- else {
- snprintf(buffer, 16, format, current_line);
- DisplayAtom atom{buffer};
- (main_selection == current_line) ? atom.face = face_absolute
- : atom.face = face;
- line.insert(line.begin(), std::move(atom));
- }
+ snprintf(buffer, 16, format, line_to_format);
+ DisplayAtom atom{buffer};
+ atom.face = (hl_cursor_line and is_cursor_line) ? face_absolute : face;
+ line.insert(line.begin(), std::move(atom));
}
}
HighlighterAndId number_lines_factory(HighlighterParameters params)
{
- if (params.size() > 1)
- throw runtime_error("wrong parameter count");
+ static const ParameterDesc param_desc{
+ { { "relative", { false, "" } },
+ { "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<false, false> },
+ { "number_lines", show_line_numbers<false, true> },
+ { "number_lines_relative", show_line_numbers<true, false> },
+ { "number_lines_relative", show_line_numbers<true, true> },
+ };
+ const int index = (parser.get_switch("relative") ? 1 : 0) * 2 +
+ (parser.get_switch("hlcursor") ? 1 : 0);
- return (params.size() == 1 && params[0] == "-relative") ? HighlighterAndId("number_lines_relative", make_simple_highlighter(show_line_numbers<true>))
- : HighlighterAndId("number_lines", make_simple_highlighter(show_line_numbers<false>));
+ return {funcs[index].name.str(), make_simple_highlighter(funcs[index].func)};
}
void show_matching_char(const Context& context, HighlightFlags flags, DisplayBuffer& display_buffer, BufferRange)
@@ -1239,7 +1239,7 @@ void register_highlighters()
"number_lines",
{ number_lines_factory,
"Display line numbers \n"
- "Parameters: -relative" } });
+ "Parameters: -relative, -hlcursor\n" } });
registry.append({
"show_matching",
{ simple_factory("show_matching", show_matching_char),