No, at leas I’m not thought about it! Nice trick!
I was using this custom command:
require-module kak
add-highlighter shared/kakrc/code/if_else regex \b(if|else)\b 0:keyword
define-command -docstring "if <condition> <expression> [else [if <condition>] <expression>]: if statement that accepts shell-valid condition string" \
if -params 2.. %{ evaluate-commands %sh{
while [ true ]; do
condition="[ $1 ]"
if [ -n "$3" ] && [ "$3" != "else" ]; then
printf "%s\n" "fail %{if: unknown operator '$3'}"
elif [ $# -eq 3 ]; then
printf "%s\n" "fail %{if: wrong argument count}"
elif eval $condition; then
[ -n "${2##*&*}" ] && arg="$2" || arg="$(printf '%s' "$2" | sed 's/&/&&/g')"
printf "%s\n" "evaluate-commands %& $arg &"
elif [ $# -eq 4 ]; then
[ -n "${4##*&*}" ] && arg="$4" || arg="$(printf '%s' "$4" | sed 's/&/&&/g')"
printf "%s\n" "evaluate-commands %& $arg &"
elif [ $# -gt 4 ]; then
if [ "$4" = "if" ]; then
shift 4
continue
else
printf "%s\n" "fail %{if: wrong argument count}"
fi
fi
exit
done
}}
That allows me to chain if
else
if
… else
blocks as in C-like languages, but I mostly use it for single if else
, like this, to branch my configuration depending on where I run Kakoune:
plug "andreyorst/base16-gruvbox.kak" domain gitlab.com theme %{
if %[ -n "${PATH##*termux*}" ] %{
colorscheme base16-gruvbox-dark-soft
} else %{
colorscheme base16-gruvbox-dark-hard
}
}
The problem is that I mostly want to use shell conditionals as those are far more versatile compared to Kakoune ones, but your example can be used to make some code that uses try
catch
approach more appealing. I wonder if this can be turned into function, as in my example