From dc504284c3b57c9af377012f0b955f9ae322a3eb Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Sun, 23 Aug 2015 12:13:14 +0100 Subject: [PATCH] Change flag lines highlighter to use faces instead of colors Fixes #130 --- rc/git-tools.kak | 18 ++++++++++-------- src/face.hh | 7 +++++++ src/highlighters.cc | 14 ++++++-------- src/highlighters.hh | 2 +- src/option_types.hh | 8 ++++---- 5 files changed, 28 insertions(+), 21 deletions(-) diff --git a/rc/git-tools.kak b/rc/git-tools.kak index 16dcf698..7a87ca29 100644 --- a/rc/git-tools.kak +++ b/rc/git-tools.kak @@ -22,6 +22,8 @@ hook global WinSetOption filetype=(?!git-status).* %{ decl line-flag-list git_blame_flags decl line-flag-list git_diff_flags +face GitBlame default,magenta + def -shell-params \ -docstring %sh{printf "%%{Git wrapping helper\navailable commands:\n add\n rm\n blame\n commit\n checkout\n diff\n hide-blame\n log\n show\n show-diff\n status\n update-diff}"} \ -shell-completion %{ @@ -58,7 +60,7 @@ def -shell-params \ run_git_blame() { ( echo "eval -client '$kak_client' %{ - try %{ addhl flag_lines magenta git_blame_flags } + try %{ addhl flag_lines GitBlame git_blame_flags } set buffer=$kak_bufname git_blame_flags '' }" | kak -p ${kak_session} git blame "$@" --incremental ${kak_buffile} | awk -e ' @@ -66,10 +68,10 @@ def -shell-params \ if (line == "") { return; } text=substr(sha,1,8) " " dates[sha] " " authors[sha] gsub(":", "\\:", text) - gsub(",", "\\,", text) - flag=line ",black," text + # gsub("|", "\\|", text) + flag=line "|default|" text for ( i=1; i < count; i++ ) { - flag=flag ":" line+i ",black," text + flag=flag ":" line+i "|default|" text } cmd = "kak -p " ENVIRON["kak_session"] print "set -add buffer=" ENVIRON["kak_bufname"] " git_blame_flags %{" flag "}" | cmd @@ -94,7 +96,7 @@ def -shell-params \ git diff -U0 $kak_buffile | awk -e ' BEGIN { line=0 - flags="0,red,." + flags="0|red|." } /^---.*/ {} /^@@ -[0-9]+(,[0-9]+)? \+[0-9]+(,[0-9]+)? @@.*/ { @@ -105,10 +107,10 @@ def -shell-params \ } } /^\+/ { - flags=flags ":" line ",green,+" + flags=flags ":" line "|green|+" line++ } - /^\-/ { flags=flags ":" line ",red,-" } + /^\-/ { flags=flags ":" line "|red|-" } END { print "set buffer git_diff_flags ", flags } ' } @@ -150,7 +152,7 @@ def -shell-params \ }" ;; show-diff) - echo "try %{ addhl flag_lines black git_diff_flags }" + echo "try %{ addhl flag_lines default,black git_diff_flags }" update_diff ;; update-diff) update_diff ;; diff --git a/src/face.hh b/src/face.hh index b5891c89..e650cd81 100644 --- a/src/face.hh +++ b/src/face.hh @@ -42,6 +42,13 @@ constexpr bool operator!=(const Face& lhs, const Face& rhs) return not (lhs == rhs); } +constexpr Face merge_faces(const Face& base, const Face& face) +{ + return { face.fg == Color::Default ? base.fg : face.fg, + face.bg == Color::Default ? base.bg : face.bg, + face.attributes | base.attributes }; +} + } #endif // face_hh_INCLUDED diff --git a/src/highlighters.cc b/src/highlighters.cc index eec224a5..5b0068c0 100644 --- a/src/highlighters.cc +++ b/src/highlighters.cc @@ -155,12 +155,7 @@ void apply_highlighter(const Context& context, auto apply_face = [](const Face& face) { return [&face](DisplayAtom& atom) { - if (face.fg != Color::Default) - atom.face.fg = face.fg; - if (face.bg != Color::Default) - atom.face.bg = face.bg; - if (face.attributes != Attribute::Normal) - atom.face.attributes |= face.attributes; + atom.face = merge_faces(atom.face, face); }; }; @@ -891,7 +886,8 @@ HighlighterAndId create_flag_lines_highlighter(HighlighterParameters params) throw runtime_error("wrong parameter count"); const String& option_name = params[1]; - Color bg = str_to_color(params[0]); + String default_face = params[0]; + get_face(default_face); // validate param // throw if wrong option type GlobalScope::instance().options()[option_name].get>(); @@ -902,6 +898,8 @@ HighlighterAndId create_flag_lines_highlighter(HighlighterParameters params) auto& lines_opt = context.options()[option_name]; auto& lines = lines_opt.get>(); + auto def_face = get_face(default_face); + CharCount width = 0; for (auto& l : lines) width = std::max(width, std::get<2>(l).char_length()); @@ -915,7 +913,7 @@ HighlighterAndId create_flag_lines_highlighter(HighlighterParameters params) String content = it != lines.end() ? std::get<2>(*it) : empty; content += String(' ', width - content.char_length()); DisplayAtom atom{std::move(content)}; - atom.face = { it != lines.end() ? std::get<1>(*it) : Color::Default , bg }; + atom.face = it != lines.end() ? merge_faces(get_face(std::get<1>(*it)), def_face) : def_face; line.insert(line.begin(), std::move(atom)); } }; diff --git a/src/highlighters.hh b/src/highlighters.hh index 504869c9..8c6fd3c4 100644 --- a/src/highlighters.hh +++ b/src/highlighters.hh @@ -9,7 +9,7 @@ namespace Kakoune void register_highlighters(); -using LineAndFlag = std::tuple; +using LineAndFlag = std::tuple; } diff --git a/src/option_types.hh b/src/option_types.hh index ae960261..a4d29227 100644 --- a/src/option_types.hh +++ b/src/option_types.hh @@ -98,7 +98,7 @@ void option_from_string(StringView str, UnorderedMap& opt) } } -constexpr char tuple_separator = ','; +constexpr char tuple_separator = '|'; template struct TupleOptionDetail @@ -176,9 +176,9 @@ bool option_add(T&, const T&) template inline void option_from_string(StringView str, LineAndColumn& opt) { - auto vals = split(str, tuple_separator); + auto vals = split(str, ','); if (vals.size() != 2) - throw runtime_error(format("expected {}", tuple_separator)); + throw runtime_error("expected ,"); opt.line = str_to_int(vals[0]); opt.column = str_to_int(vals[1]); } @@ -186,7 +186,7 @@ inline void option_from_string(StringView str, LineAndColumn inline String option_to_string(const LineAndColumn& opt) { - return to_string(opt.line) + tuple_separator + to_string(opt.column); + return format("{},{}", opt.line, opt.column); } enum YesNoAsk