Add support for relative line numbers

This commit is contained in:
Eike Plack 2015-03-18 22:07:57 +01:00
parent 12a732d1fa
commit 5627d33ac3
3 changed files with 46 additions and 9 deletions

View File

@ -637,7 +637,8 @@ general highlighters are:
<option_name>. <option_name>.
* +show_matching+: highlight matching char of the character under the selections * +show_matching+: highlight matching char of the character under the selections
cursor using +MatchingChar+ face. cursor using +MatchingChar+ face.
* +number_lines+: show line numbers * +number_lines <-relative>+: show line numbers. The -relative switch will enable relative
line numbers.
* +fill <face>+: fill using given face, mostly useful with the +regions+ highlighter * +fill <face>+: fill using given face, mostly useful with the +regions+ highlighter
(see below) (see below)
@ -895,6 +896,8 @@ there are some builtins faces used by internal Kakoune functionalities:
* +PrimaryCursor+: cursor of the primary selection * +PrimaryCursor+: cursor of the primary selection
* +SecondaryCursor+: cursor of the secondary selection * +SecondaryCursor+: cursor of the secondary selection
* +LineNumbers+: face used by the number_lines highlighter * +LineNumbers+: face used by the number_lines highlighter
* +LineNumberAbsolute+: face used to highlight the line number of the main
selection
* +MenuForeground+: face for the selected element in menus * +MenuForeground+: face for the selected element in menus
* +MenuBackground+: face for the not selected elements in menus * +MenuBackground+: face for the not selected elements in menus
* +Information+: face for the informations windows and information messages * +Information+: face for the informations windows and information messages

View File

@ -96,6 +96,7 @@ FaceRegistry::FaceRegistry()
{ "PrimaryCursor", Face{ Colors::Black, Colors::White } }, { "PrimaryCursor", Face{ Colors::Black, Colors::White } },
{ "SecondaryCursor", Face{ Colors::Black, Colors::White } }, { "SecondaryCursor", Face{ Colors::Black, Colors::White } },
{ "LineNumbers", Face{ Colors::Default, Colors::Default } }, { "LineNumbers", Face{ Colors::Default, Colors::Default } },
{ "LineNumberAbsolute", Face{ Colors::Black, Colors::Blue } },
{ "MenuForeground", Face{ Colors::White, Colors::Blue } }, { "MenuForeground", Face{ Colors::White, Colors::Blue } },
{ "MenuBackground", Face{ Colors::Blue, Colors::White } }, { "MenuBackground", Face{ Colors::Blue, Colors::White } },
{ "Information", Face{ Colors::Black, Colors::Yellow } }, { "Information", Face{ Colors::Black, Colors::Yellow } },

View File

@ -594,24 +594,56 @@ 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) 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");
LineCount last_line = context.buffer().line_count(); LineCount last_line = context.buffer().line_count();
int digit_count = 0; int digit_count = 0;
for (LineCount c = last_line; c > 0; c /= 10) for (LineCount c = last_line; c > 0; c /= 10)
++digit_count; ++digit_count;
char format[] = "%?d│"; char format[] = "%?d│";
format[1] = '0' + digit_count; format[1] = '0' + digit_count + (relative ? 1 : 0);
const Face face = get_face("LineNumbers"); int main_selection = (int)context.selections().main().cursor().line + 1;
for (auto& line : display_buffer.lines()) for (auto& line : display_buffer.lines())
{ {
int current_line = (int)line.range().first.line + 1;
char buffer[16]; char buffer[16];
snprintf(buffer, 16, format, (int)line.range().first.line + 1); 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}; DisplayAtom atom{buffer};
atom.face = face; atom.face = face;
line.insert(line.begin(), std::move(atom)); 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));
}
}
}
HighlighterAndId number_lines_factory(HighlighterParameters params)
{
if (params.size() > 1)
throw runtime_error("wrong parameter count");
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>));
} }
void show_matching_char(const Context& context, HighlightFlags flags, DisplayBuffer& display_buffer, BufferRange) void show_matching_char(const Context& context, HighlightFlags flags, DisplayBuffer& display_buffer, BufferRange)
@ -1208,8 +1240,9 @@ void register_highlighters()
registry.append({ registry.append({
"number_lines", "number_lines",
{ simple_factory("number_lines", show_line_numbers), { number_lines_factory,
"Display line numbers" } }); "Display line numbers \n"
"Parameters: -relative" } });
registry.append({ registry.append({
"show_matching", "show_matching",
{ simple_factory("show_matching", show_matching_char), { simple_factory("show_matching", show_matching_char),