{line,column}_option highlighters use an expanded string instead of an option name

That allows access to non option values, such as cursor_column or cursor_line with
:addhl line '%val{cursor_line}' default,rgb:404040

Fixes #38
This commit is contained in:
Maxime Coste 2015-05-04 17:12:51 +01:00
parent cfdf03ab31
commit 1a1db1cb3a
3 changed files with 17 additions and 18 deletions

View File

@ -26,7 +26,7 @@ def -shell-params -file-completion \
hook global WinSetOption filetype=grep %{ hook global WinSetOption filetype=grep %{
addhl group grep addhl group grep
addhl -group grep regex "^((?:\w:)?[^:]+):(\d+):(\d+)?" 1:cyan 2:green 3:green addhl -group grep regex "^((?:\w:)?[^:]+):(\d+):(\d+)?" 1:cyan 2:green 3:green
addhl -group grep line_option _grep_current_line default+b addhl -group grep line %{%opt{_grep_current_line}} default+b
hook buffer -group grep-hooks NormalKey <c-m> jump hook buffer -group grep-hooks NormalKey <c-m> jump
} }

View File

@ -20,7 +20,7 @@ def -shell-params make %{ %sh{
addhl -group / group make addhl -group / group make
addhl -group /make regex "^((?:\w:)?[^:\n]+):(\d+):(?:(\d+):)?\h+(?:((?:fatal )?error)|(warning)|(note)|(required from(?: here)?))?.*?$" 1:cyan 2:green 3:green 4:red 5:yellow 6:blue 7:yellow addhl -group /make regex "^((?:\w:)?[^:\n]+):(\d+):(?:(\d+):)?\h+(?:((?:fatal )?error)|(warning)|(note)|(required from(?: here)?))?.*?$" 1:cyan 2:green 3:green 4:red 5:yellow 6:blue 7:yellow
addhl -group /make line_option _make_current_error_line default+b addhl -group /make line %{%opt{_make_current_error_line}} default+b
hook global WinSetOption filetype=make %{ hook global WinSetOption filetype=make %{
addhl ref make addhl ref make

View File

@ -4,6 +4,7 @@
#include "buffer_utils.hh" #include "buffer_utils.hh"
#include "context.hh" #include "context.hh"
#include "containers.hh" #include "containers.hh"
#include "command_manager.hh"
#include "display_buffer.hh" #include "display_buffer.hh"
#include "face_registry.hh" #include "face_registry.hh"
#include "highlighter_group.hh" #include "highlighter_group.hh"
@ -491,7 +492,7 @@ HighlighterAndId create_regex_option_highlighter(HighlighterParameters params)
return {"hloption_" + option_name, make_dynamic_regex_highlighter(get_regex, get_face)}; return {"hloption_" + option_name, make_dynamic_regex_highlighter(get_regex, get_face)};
} }
HighlighterAndId create_line_option_highlighter(HighlighterParameters params) HighlighterAndId create_line_highlighter(HighlighterParameters params)
{ {
if (params.size() != 2) if (params.size() != 2)
throw runtime_error("wrong parameter count"); throw runtime_error("wrong parameter count");
@ -500,20 +501,19 @@ HighlighterAndId create_line_option_highlighter(HighlighterParameters params)
String option_name = params[0]; String option_name = params[0];
get_face(facespec); // validate facespec get_face(facespec); // validate facespec
GlobalScope::instance().options()[option_name].get<int>(); // verify option type now
auto func = [=](const Context& context, HighlightFlags flags, auto func = [=](const Context& context, HighlightFlags flags,
DisplayBuffer& display_buffer, BufferRange) DisplayBuffer& display_buffer, BufferRange)
{ {
int line = context.options()[option_name].get<int>(); if (auto line = str_to_int_ifp(expand(option_name, context, {}, EnvVarMap{})))
highlight_range(display_buffer, {line-1, 0}, {line, 0}, false, highlight_range(display_buffer, {*line-1, 0}, {*line, 0}, false,
apply_face(get_face(facespec))); apply_face(get_face(facespec)));
}; };
return {"hlline_" + params[0], make_simple_highlighter(std::move(func))}; return {"hlline_" + params[0], make_simple_highlighter(std::move(func))};
} }
HighlighterAndId create_column_option_highlighter(HighlighterParameters params) HighlighterAndId create_column_highlighter(HighlighterParameters params)
{ {
if (params.size() != 2) if (params.size() != 2)
throw runtime_error("wrong parameter count"); throw runtime_error("wrong parameter count");
@ -522,12 +522,11 @@ HighlighterAndId create_column_option_highlighter(HighlighterParameters params)
String option_name = params[0]; String option_name = params[0];
get_face(facespec); // validate facespec get_face(facespec); // validate facespec
GlobalScope::instance().options()[option_name].get<int>(); // verify option type now
auto func = [=](const Context& context, HighlightFlags flags, auto func = [=](const Context& context, HighlightFlags flags,
DisplayBuffer& display_buffer, BufferRange) DisplayBuffer& display_buffer, BufferRange)
{ {
const CharCount column = context.options()[option_name].get<int>() - 1; const CharCount column = str_to_int_ifp(expand(option_name, context, {}, EnvVarMap{})).value_or(0) - 1;
if (column < 0) if (column < 0)
return; return;
@ -1341,15 +1340,15 @@ void register_highlighters()
"Display flags specified in the line-flag-list option <option name>\n" "Display flags specified in the line-flag-list option <option name>\n"
"A line-flag is written: <line>|<fg color>|<text>, the list is : separated" } }); "A line-flag is written: <line>|<fg color>|<text>, the list is : separated" } });
registry.append({ registry.append({
"line_option", "line",
{ create_line_option_highlighter, { create_line_highlighter,
"Parameters: <option name> <face>\n" "Parameters: <value string> <face>\n"
"Highlight the line stored in <option name> with <face>" } }); "Highlight the line given by evaluating <value string> with <face>" } });
registry.append({ registry.append({
"column_option", "column",
{ create_column_option_highlighter, { create_column_highlighter,
"Parameters: <option name> <face>\n" "Parameters: <value string> <face>\n"
"Highlight the column stored in <option name> with <face>" } }); "Highlight the column given by evaluating <value string> with <face>" } });
registry.append({ registry.append({
"ref", "ref",
{ create_reference_highlighter, { create_reference_highlighter,