From a8bbdc71d9c9e7ac1d50fdaee6ed3918ef93902b Mon Sep 17 00:00:00 2001 From: Lennard Date: Sun, 12 Jan 2020 10:35:56 +0000 Subject: [PATCH 1/4] [sh] Separate keywords and builtins; fix variables MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Previously, the keywords were a mess. They contained the shell’s reserved words and some arbitrarily selected builtins. I generated the word list using bash because it contains all POSIX builtins and is common for scripting. In variable assignments some characters that are allowed to be in variables used to not be highlighted, e.g. hyphens. With this commit all characters except whitespace are considered to be part of the variable. --- rc/filetype/sh.kak | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/rc/filetype/sh.kak b/rc/filetype/sh.kak index 272f2fa8..76c50264 100644 --- a/rc/filetype/sh.kak +++ b/rc/filetype/sh.kak @@ -30,11 +30,9 @@ add-highlighter shared/sh/double_string/fill fill string evaluate-commands %sh{ # Grammar - keywords="alias bind builtin caller case cd command coproc declare do done - echo elif else enable esac exit fi for function help - if in let local logout mapfile printf read readarray - readonly return select set shift source test then - time type typeset ulimit unalias until while break continue" + # Generated with `compgen -k` in bash + keywords="if then else elif fi case esac for select while until do done in + function time coproc" join() { sep=$2; eval set -- $1; IFS="$sep"; echo "$*"; } @@ -43,10 +41,30 @@ evaluate-commands %sh{ # Highlight keywords printf %s "add-highlighter shared/sh/code/ regex \b($(join "${keywords}" '|'))\b 0:keyword" + +} + +# For some reasons this needs to be in its own %sh block +evaluate-commands %sh{ + # Generated with `compgen -b` in bash + builtins="alias bg bind break builtin caller cd command compgen complete + compopt continue declare dirs disown echo enable eval exec + exit export false fc fg getopts hash help history jobs kill + let local logout mapfile popd printf pushd pwd read readarray + readonly return set shift shopt source suspend test times trap + true type typeset ulimit umask unalias unset wait" + + join() { sep=$2; eval set -- $1; IFS="$sep"; echo "$*"; } + + # Add the language's grammar to the static completion list + printf %s\\n "set-option -add global sh_static_words $(join "${builtins}" ' ')" + + # Highlight builtins + printf %s "add-highlighter shared/sh/code/builtin regex \b($(join "${builtins}" '|'))\b 0:builtin" } add-highlighter shared/sh/code/operators regex [\[\]\(\)&|]{1,2} 0:operator -add-highlighter shared/sh/code/variable regex (\w+)= 1:variable +add-highlighter shared/sh/code/variable regex (\S+)= 1:variable add-highlighter shared/sh/code/function regex ^\h*(\w+)\h*\(\) 1:function add-highlighter shared/sh/code/unscoped_expansion regex \$(\w+|#|@|\?|\$|!|-|\*) 0:value From b84d85272364175b2467958f9d659c7549fc4313 Mon Sep 17 00:00:00 2001 From: Lennard Hofmann Date: Mon, 13 Jan 2020 17:51:18 +0100 Subject: [PATCH 3/4] [sh] Merge two %sh blocks into one It simply needed a newline --- rc/filetype/sh.kak | 19 +++++-------------- 1 file changed, 5 insertions(+), 14 deletions(-) diff --git a/rc/filetype/sh.kak b/rc/filetype/sh.kak index 76c50264..bd71acf5 100644 --- a/rc/filetype/sh.kak +++ b/rc/filetype/sh.kak @@ -34,18 +34,6 @@ evaluate-commands %sh{ keywords="if then else elif fi case esac for select while until do done in function time coproc" - join() { sep=$2; eval set -- $1; IFS="$sep"; echo "$*"; } - - # Add the language's grammar to the static completion list - printf %s\\n "declare-option str-list sh_static_words $(join "${keywords}" ' ')" - - # Highlight keywords - printf %s "add-highlighter shared/sh/code/ regex \b($(join "${keywords}" '|'))\b 0:keyword" - -} - -# For some reasons this needs to be in its own %sh block -evaluate-commands %sh{ # Generated with `compgen -b` in bash builtins="alias bg bind break builtin caller cd command compgen complete compopt continue declare dirs disown echo enable eval exec @@ -54,10 +42,13 @@ evaluate-commands %sh{ readonly return set shift shopt source suspend test times trap true type typeset ulimit umask unalias unset wait" - join() { sep=$2; eval set -- $1; IFS="$sep"; echo "$*"; } + join() { sep=$2; eval set -- $1; IFS="$sep"; echo "$*"; } # Add the language's grammar to the static completion list - printf %s\\n "set-option -add global sh_static_words $(join "${builtins}" ' ')" + printf %s\\n "declare-option str-list sh_static_words $(join "${keywords}" ' ') $(join "${builtins}" ' ')" + + # Highlight keywords + printf %s\\n "add-highlighter shared/sh/code/ regex \b($(join "${keywords}" '|'))\b 0:keyword" # Highlight builtins printf %s "add-highlighter shared/sh/code/builtin regex \b($(join "${builtins}" '|'))\b 0:builtin" From 71e4ac457475b590359c90c436f32197597da897 Mon Sep 17 00:00:00 2001 From: Lennard Hofmann Date: Wed, 15 Jan 2020 13:56:25 +0000 Subject: [PATCH 4/4] [sh] Allow hyphens in variables but not every character Co-Authored-By: Frank LENORMAND --- rc/filetype/sh.kak | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rc/filetype/sh.kak b/rc/filetype/sh.kak index bd71acf5..107d6372 100644 --- a/rc/filetype/sh.kak +++ b/rc/filetype/sh.kak @@ -55,7 +55,7 @@ evaluate-commands %sh{ } add-highlighter shared/sh/code/operators regex [\[\]\(\)&|]{1,2} 0:operator -add-highlighter shared/sh/code/variable regex (\S+)= 1:variable +add-highlighter shared/sh/code/variable regex ([\w-]+)= 1:variable add-highlighter shared/sh/code/function regex ^\h*(\w+)\h*\(\) 1:function add-highlighter shared/sh/code/unscoped_expansion regex \$(\w+|#|@|\?|\$|!|-|\*) 0:value