Remove defhl, use addhl -group

This commit is contained in:
Maxime Coste 2014-06-12 21:52:23 +01:00
parent af750ce186
commit 9130f0334b
11 changed files with 121 additions and 97 deletions

View File

@ -1,13 +1,13 @@
hook global BufCreate .*\.asciidoc %{ set buffer filetype asciidoc } hook global BufCreate .*\.asciidoc %{ set buffer filetype asciidoc }
defhl asciidoc addhl -group / group asciidoc
addhl -def-group asciidoc regex (\A|\n\n)[^\n]+\n={2,}\h*\n\h*$ 0:blue addhl -group /asciidoc regex (\A|\n\n)[^\n]+\n={2,}\h*\n\h*$ 0:blue
addhl -def-group asciidoc regex (\A|\n\n)[^\n]+\n-{2,}\h*\n\h*$ 0:cyan addhl -group /asciidoc regex (\A|\n\n)[^\n]+\n-{2,}\h*\n\h*$ 0:cyan
addhl -def-group asciidoc regex ^\h+([-\*])\h+[^\n]*(\n\h+[^-\*]\S+[^\n]*)*$ 0:yellow 1:cyan addhl -group /asciidoc regex ^\h+([-\*])\h+[^\n]*(\n\h+[^-\*]\S+[^\n]*)*$ 0:yellow 1:cyan
addhl -def-group asciidoc regex ^([-=~]+)\n[^\n\h].*?\n\1$ 0:magenta addhl -group /asciidoc regex ^([-=~]+)\n[^\n\h].*?\n\1$ 0:magenta
addhl -def-group asciidoc regex (?<!\w)\+[^\n]+?\+(?!\w) 0:green addhl -group /asciidoc regex (?<!\w)\+[^\n]+?\+(?!\w) 0:green
addhl -def-group asciidoc regex (?<!\w)_[^\n]+?_(?!\w) 0:yellow addhl -group /asciidoc regex (?<!\w)_[^\n]+?_(?!\w) 0:yellow
addhl -def-group asciidoc regex (?<!\w)\*[^\n]+?\*(?!\w) 0:red addhl -group /asciidoc regex (?<!\w)\*[^\n]+?\*(?!\w) 0:red
hook global WinSetOption filetype=asciidoc %{ addhl ref asciidoc } hook global WinSetOption filetype=asciidoc %{ addhl ref asciidoc }
hook global WinSetOption filetype=(?!asciidoc).* %{ rmhl asciidoc } hook global WinSetOption filetype=(?!asciidoc).* %{ rmhl asciidoc }

View File

@ -39,22 +39,19 @@ def -hidden _cpp_indent_on_closing_curly_brace %[
try %[ exec -draft "hm<space><a-?>(class|struct)<ret><a-k>\`(class|struct)[^{}\n]+(\n)?\s*\{\'<ret><a-space>ma;<esc>" ] try %[ exec -draft "hm<space><a-?>(class|struct)<ret><a-k>\`(class|struct)[^{}\n]+(\n)?\s*\{\'<ret><a-space>ma;<esc>" ]
] ]
defhl cpp addhl -group / multi_region -default code cpp \
addhl -def-group cpp multi_region -default code root \
string %{(?<!')"} %{(?<!\\)(\\\\)*"} '' \ string %{(?<!')"} %{(?<!\\)(\\\\)*"} '' \
comment /\* \*/ '' comment /\* \*/ '' \
comment // $ ''
addhl -def-group cpp/root/string fill string addhl -group /cpp/string fill string
addhl -def-group cpp/root/comment fill comment addhl -group /cpp/comment fill comment
addhl -def-group cpp/root/code regex "\<(this|true|false|NULL|nullptr|)\>|\<-?\d+[fdiu]?|'((\\.)?|[^'\\])'" 0:value addhl -group /cpp/code regex "\<(this|true|false|NULL|nullptr|)\>|\<-?\d+[fdiu]?|'((\\.)?|[^'\\])'" 0:value
addhl -def-group cpp/root/code regex "\<(void|int|char|unsigned|float|bool|size_t)\>" 0:type addhl -group /cpp/code regex "\<(void|int|char|unsigned|float|bool|size_t)\>" 0:type
addhl -def-group cpp/root/code regex "\<(while|for|if|else|do|switch|case|default|goto|break|continue|return|using|try|catch|throw|new|delete|and|or|not|operator|explicit)\>" 0:keyword addhl -group /cpp/code regex "\<(while|for|if|else|do|switch|case|default|goto|break|continue|return|using|try|catch|throw|new|delete|and|or|not|operator|explicit)\>" 0:keyword
addhl -def-group cpp/root/code regex "\<(const|mutable|auto|namespace|inline|static|volatile|class|struct|enum|union|public|protected|private|template|typedef|virtual|friend|extern|typename|override|final)\>" 0:attribute addhl -group /cpp/code regex "\<(const|mutable|auto|namespace|inline|static|volatile|class|struct|enum|union|public|protected|private|template|typedef|virtual|friend|extern|typename|override|final)\>" 0:attribute
addhl -def-group cpp/root/code regex "^\h*?#.*?(?<!\\)$" 0:macro addhl -group /cpp/code regex "^\h*?#.*?(?<!\\)$" 0:macro
addhl -def-group cpp/root/code regex "(//[^\n]*\n)" 0:comment
hook global WinSetOption filetype=cpp %[ hook global WinSetOption filetype=cpp %[
addhl ref cpp addhl ref cpp

View File

@ -19,12 +19,12 @@ def -shell-params -file-completion \
}} }}
hook global WinSetOption filetype=grep %{ hook global WinSetOption filetype=grep %{
addhl group grep-highlight addhl group grep
addhl -group grep-highlight regex "^([^:]+):(\d+):(\d+)?" 1:cyan 2:green 3:green addhl -group grep regex "^([^:]+):(\d+):(\d+)?" 1:cyan 2:green 3:green
hook buffer -id grep-hooks NormalKey <c-m> jump hook buffer -id grep-hooks NormalKey <c-m> jump
} }
hook global WinSetOption filetype=(?!grep).* %{ rmhl grep-highlight; rmhooks buffer grep-hooks } hook global WinSetOption filetype=(?!grep).* %{ rmhl grep; rmhooks buffer grep-hooks }
decl str jumpclient decl str jumpclient

View File

@ -2,23 +2,22 @@ hook global BufCreate (.*/)?(kakrc|.*.kak) %{
set buffer filetype kak set buffer filetype kak
} }
defhl kakrc addhl -group / multi_region -default code kakrc \
addhl -def-group kakrc multi_region -default kakrc root \ comment (^|\h)\K\# $ '' \
comment (^|\h)\K\# \n '' \
double_string %{(^|\h)"} %{(?<!\\)(\\\\)*"} '' \ double_string %{(^|\h)"} %{(?<!\\)(\\\\)*"} '' \
single_string %{(^|\h)'} %{(?<!\\)(\\\\)*'} '' \ single_string %{(^|\h)'} %{(?<!\\)(\\\\)*'} '' \
shell '%sh\{' '\}' '\{' shell '%sh\{' '\}' '\{'
addhl -def-group kakrc/root/kakrc regex \<(hook|rmhooks|defhl|addhl|rmhl|add|exec|eval|source|runtime|def|decl|echo|edit|set)\> 0:keyword addhl -group /kakrc/code regex \<(hook|rmhooks|addhl|rmhl|add|exec|eval|source|runtime|def|decl|echo|edit|set)\> 0:keyword
addhl -def-group kakrc/root/kakrc regex \<(default|black|red|green|yellow|blue|magenta|cyan|white)\> 0:value addhl -group /kakrc/code regex \<(default|black|red|green|yellow|blue|magenta|cyan|white)\> 0:value
addhl -def-group kakrc/root/kakrc regex (?<=\<hook)\h+((global|buffer|window)|(\S+))\h+(\S+)\h+(\H+) 2:attribute 3:error 4:identifier 5:string addhl -group /kakrc/code regex (?<=\<hook)\h+((global|buffer|window)|(\S+))\h+(\S+)\h+(\H+) 2:attribute 3:error 4:identifier 5:string
addhl -def-group kakrc/root/kakrc regex (?<=\<set)\h+((global|buffer|window)|(\S+))\h+(\S+)\h+(\S+) 2:attribute 3:error 4:identifier 5:value addhl -group /kakrc/code regex (?<=\<set)\h+((global|buffer|window)|(\S+))\h+(\S+)\h+(\S+) 2:attribute 3:error 4:identifier 5:value
addhl -def-group kakrc/root/kakrc regex (?<=\<regex)\h+(\S+) 1:string addhl -group /kakrc/code regex (?<=\<regex)\h+(\S+) 1:string
addhl -def-group kakrc/root/double_string fill string addhl -group /kakrc/double_string fill string
addhl -def-group kakrc/root/single_string fill string addhl -group /kakrc/single_string fill string
addhl -def-group kakrc/root/comment fill comment addhl -group /kakrc/comment fill comment
addhl -def-group kakrc/root/shell ref sh addhl -group /kakrc/shell ref sh
hook global WinSetOption filetype=kak %{ addhl ref kakrc } hook global WinSetOption filetype=kak %{ addhl ref kakrc }
hook global WinSetOption filetype=(?!kak).* %{ rmhl kakrc } hook global WinSetOption filetype=(?!kak).* %{ rmhl kakrc }

View File

@ -1,9 +1,9 @@
hook global BufSetOption mimetype=message/rfc822 %{ set buffer filetype mail } hook global BufSetOption mimetype=message/rfc822 %{ set buffer filetype mail }
defhl mail addhl -group / group mail
addhl -def-group mail regex ^(From|To|Cc|Bcc|Subject|Reply-To|In-Reply-To):([^\n]*(?:\n\h+[^\n]+)*)$ 1:keyword 2:attribute addhl -group /mail regex ^(From|To|Cc|Bcc|Subject|Reply-To|In-Reply-To):([^\n]*(?:\n\h+[^\n]+)*)$ 1:keyword 2:attribute
addhl -def-group mail regex <[^@>]+@.*?> 0:string addhl -group /mail regex <[^@>]+@.*?> 0:string
addhl -def-group mail regex ^>.*?$ 0:comment addhl -group /mail regex ^>.*?$ 0:comment
hook global WinSetOption filetype=mail %{ addhl ref mail } hook global WinSetOption filetype=mail %{ addhl ref mail }
hook global WinSetOption filetype=(?!mail).* %{ rmhl mail } hook global WinSetOption filetype=(?!mail).* %{ rmhl mail }

View File

@ -13,8 +13,8 @@ def -shell-params make %{ %sh{
}" }"
}} }}
defhl make addhl -group / group make
addhl -def-group make regex "^([^:\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 "^([^:\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
hook global WinSetOption filetype=make %{ hook global WinSetOption filetype=make %{
addhl ref make addhl ref make

View File

@ -6,14 +6,21 @@ hook global BufSetOption mimetype=text/x-shellscript %{
set buffer filetype sh set buffer filetype sh
} }
defhl sh addhl -group / group sh
addhl -def-group sh regex \<(if|then|fi|while|for|do|done|case|esac|echo|cd|shift|return|exit|local)\> 0:keyword addhl -group /sh regex \<(if|then|fi|while|for|do|done|case|esac|echo|cd|shift|return|exit|local)\> 0:keyword
addhl -def-group sh regex [\[\]\(\)&|]{2} 0:operator addhl -group /sh regex [\[\]\(\)&|]{2} 0:operator
addhl -def-group sh regex (\w+)= 1:identifier addhl -group /sh regex (\w+)= 1:identifier
addhl -def-group sh regex ^\h*(\w+)\h*\(\) 1:identifier addhl -group /sh regex ^\h*(\w+)\h*\(\) 1:identifier
addhl -def-group sh regex "(^|\h)#.*?$" 0:comment addhl -group /sh regex "(^|\h)#.*?$" 0:comment
addhl -def-group sh regex (["'])(?:\\\1|.)*?\1 0:string #addhl -group /sh regex (["'])(?:\\\1|.)*?\1 0:string
addhl -def-group sh regex \$(\w+|\{.+?\}) 0:identifier
addhl -group /sh region double_string %{(^|\h)"} %{(?<!\\)(\\\\)*"}
addhl -group /sh/double_string/content fill string
addhl -group /sh region single_string %{(^|\h)'} %{(?<!\\)(\\\\)*'}
addhl -group /sh/single_string/content fill string
addhl -group /sh regex \$(\w+|\{.+?\}) 0:identifier
hook global WinSetOption filetype=sh %{ addhl ref sh } hook global WinSetOption filetype=sh %{ addhl ref sh }
hook global WinSetOption filetype=(?!sh).* %{ rmhl sh } hook global WinSetOption filetype=(?!sh).* %{ rmhl sh }

View File

@ -350,20 +350,6 @@ const CommandDesc namebuf_cmd = {
} }
}; };
const CommandDesc define_highlighter_cmd = {
"defhl",
"dh",
"defhl <name>: define a new reusable highlighter",
single_name_param,
CommandFlags::None,
CommandCompleter{},
[](const ParametersParser& parser, Context& context)
{
const String& name = parser[0];
DefinedHighlighters::instance().append({name, HighlighterGroup{}});
}
};
template<typename GetRootGroup> template<typename GetRootGroup>
CommandCompleter group_rm_completer(GetRootGroup get_root_group) CommandCompleter group_rm_completer(GetRootGroup get_root_group)
{ {
@ -401,12 +387,11 @@ HighlighterGroup& get_highlighters(const Context& c) { return c.window().highlig
const CommandDesc add_highlighter_cmd = { const CommandDesc add_highlighter_cmd = {
"addhl", "addhl",
"ah", "ah",
"addhl <switches> <type> <type params>...: add an highlighter to current window", "addhl <scope>/<group>/<id> <type> <type params>...: add an highlighter\n"
"<scope> can be shared or window",
ParameterDesc{ ParameterDesc{
SwitchMap{ { "group", { true, "add highlighter to named group" } }, SwitchMap{ { "group", { true, "specify the group in which to put the highlighter" } } },
{ "def-group", { true, "add highlighter to reusable defined group" } } }, ParameterDesc::Flags::SwitchesOnlyAtStart, 1 },
ParameterDesc::Flags::SwitchesOnlyAtStart, 1
},
CommandFlags::None, CommandFlags::None,
group_add_completer<HighlighterRegistry>(get_highlighters), group_add_completer<HighlighterRegistry>(get_highlighters),
[](const ParametersParser& parser, Context& context) [](const ParametersParser& parser, Context& context)
@ -414,26 +399,31 @@ const CommandDesc add_highlighter_cmd = {
HighlighterRegistry& registry = HighlighterRegistry::instance(); HighlighterRegistry& registry = HighlighterRegistry::instance();
auto begin = parser.begin(); auto begin = parser.begin();
const String& name = *begin; const String& name = *begin++;
std::vector<String> highlighter_params; std::vector<String> highlighter_params;
for (++begin; begin != parser.end(); ++begin) for (; begin != parser.end(); ++begin)
highlighter_params.push_back(*begin); highlighter_params.push_back(*begin);
if (parser.has_option("group") and parser.has_option("def-group"))
throw runtime_error("-group and -def-group cannot be specified together");
HighlighterGroup* group = nullptr; HighlighterGroup* group = nullptr;
if (parser.has_option("group"))
if (parser.has_option("def-group"))
group = &DefinedHighlighters::instance().get_group(parser.option_value("def-group"), '/');
else
{ {
HighlighterGroup& window_hl = context.window().highlighters(); StringView path = parser.option_value("group");
group = parser.has_option("group") ? if (path.empty())
&window_hl.get_group(parser.option_value("group"), '/') throw runtime_error("group param should not be empty");
: &window_hl;
}
if (path[0] == '/')
{
group = &DefinedHighlighters::instance();
path = path.substr(1_byte);
}
else
group = &context.window().highlighters();
if (not path.empty())
group = &group->get_group(path, '/');
}
else
group = &context.window().highlighters();
group->append(registry[name](highlighter_params)); group->append(registry[name](highlighter_params));
} }
}; };
@ -1333,7 +1323,6 @@ void register_commands()
register_command(cm, delbuf_cmd); register_command(cm, delbuf_cmd);
register_command(cm, force_delbuf_cmd); register_command(cm, force_delbuf_cmd);
register_command(cm, namebuf_cmd); register_command(cm, namebuf_cmd);
register_command(cm, define_highlighter_cmd);
register_command(cm, add_highlighter_cmd); register_command(cm, add_highlighter_cmd);
register_command(cm, rm_highlighter_cmd); register_command(cm, rm_highlighter_cmd);
register_command(cm, add_hook_cmd); register_command(cm, add_hook_cmd);

View File

@ -3,7 +3,7 @@
namespace Kakoune namespace Kakoune
{ {
void HighlighterGroup::operator()(const Context& context, HighlightFlags flags, DisplayBuffer& display_buffer) void HighlighterGroup::operator()(const Context& context, HighlightFlags flags, DisplayBuffer& display_buffer) const
{ {
for (auto& hl : m_highlighters) for (auto& hl : m_highlighters)
hl.second(context, flags, display_buffer); hl.second(context, flags, display_buffer);
@ -44,6 +44,23 @@ HighlighterGroup& HighlighterGroup::get_group(StringView path, Codepoint path_se
throw group_not_found("not a group: "_str + id); throw group_not_found("not a group: "_str + id);
} }
HighlighterFunc HighlighterGroup::get_highlighter(StringView path, Codepoint path_separator) const
{
auto sep_it = std::find(path.begin(), path.end(), path_separator);
StringView id(path.begin(), sep_it);
auto it = m_highlighters.find(id);
if (it == m_highlighters.end())
throw group_not_found("no such id: "_str + id);
if (sep_it == path.end())
return HighlighterFunc{std::ref(it->second)};
else if (auto* group = it->second.target<HighlighterGroup>())
return group->get_highlighter({sep_it+1, path.end()}, path_separator);
else if (auto* hier_group = it->second.target<HierachicalHighlighter>())
return hier_group->get_highlighter({sep_it+1, path.end()}, path_separator);
else
throw group_not_found("not a group: "_str + id);
}
CandidateList HighlighterGroup::complete_id(StringView prefix, ByteCount cursor_pos) const CandidateList HighlighterGroup::complete_id(StringView prefix, ByteCount cursor_pos) const
{ {
return m_highlighters.complete_id(prefix, cursor_pos); return m_highlighters.complete_id(prefix, cursor_pos);
@ -70,4 +87,17 @@ HighlighterGroup& HierachicalHighlighter::get_group(StringView path, Codepoint p
return it->second; return it->second;
} }
HighlighterFunc HierachicalHighlighter::get_highlighter(StringView path, Codepoint path_separator) const
{
auto sep_it = std::find(path.begin(), path.end(), path_separator);
StringView id(path.begin(), sep_it);
auto it = m_groups.find(id);
if (it == m_groups.end())
throw group_not_found("no such id: "_str + id);
if (sep_it != path.end())
return it->second.get_highlighter(StringView(sep_it+1, path.end()), path_separator);
else
return HighlighterFunc(std::ref(it->second));
}
} }

View File

@ -19,12 +19,13 @@ class HighlighterGroup
public: public:
void operator()(const Context& context, void operator()(const Context& context,
HighlightFlags flags, HighlightFlags flags,
DisplayBuffer& display_buffer); DisplayBuffer& display_buffer) const;
void append(HighlighterAndId&& hl); void append(HighlighterAndId&& hl);
void remove(StringView id); void remove(StringView id);
HighlighterGroup& get_group(StringView path, Codepoint path_separator = 0); HighlighterGroup& get_group(StringView path, Codepoint path_separator = 0);
HighlighterFunc get_highlighter(StringView path, Codepoint path_separator = 0) const;
CandidateList complete_id(StringView prefix, ByteCount cursor_pos) const; CandidateList complete_id(StringView prefix, ByteCount cursor_pos) const;
CandidateList complete_group_id(StringView prefix, ByteCount cursor_pos) const; CandidateList complete_group_id(StringView prefix, ByteCount cursor_pos) const;
@ -53,6 +54,7 @@ public:
} }
HighlighterGroup& get_group(StringView path, Codepoint path_separator = 0); HighlighterGroup& get_group(StringView path, Codepoint path_separator = 0);
HighlighterFunc get_highlighter(StringView path, Codepoint path_separator = 0) const;
protected: protected:
Callback m_callback; Callback m_callback;

View File

@ -655,17 +655,17 @@ HighlighterAndId reference_factory(HighlighterParameters params)
// throw if not found // throw if not found
//DefinedHighlighters::instance().get_group(name, '/'); //DefinedHighlighters::instance().get_group(name, '/');
return HighlighterAndId(name, return {name,
[name](const Context& context, HighlightFlags flags, DisplayBuffer& display_buffer) [name](const Context& context, HighlightFlags flags, DisplayBuffer& display_buffer)
{ {
try try
{ {
DefinedHighlighters::instance().get_group(name, '/')(context, flags, display_buffer); DefinedHighlighters::instance().get_highlighter(name, '/')(context, flags, display_buffer);
} }
catch (group_not_found&) catch (group_not_found&)
{ {
} }
}); }};
} }
namespace RegionHighlight namespace RegionHighlight