In the infamous series:
here is another dirty hack to compare non-negative integers in Kakoune, without calling the shell. The hack depends on subtracting an integer from another integer with the set -add
command, as well as the behavior of echo
: when followed by a non-negative integer, echo does its job, but when followed by a negative integer (e.g., -3
), echo
interprets the leading minus sign as the start of an option and protests (‘unknown option’).
Here is the code:
declare-option -hidden int int_running_sum
define-command \
-params 2 \
equal-to %{
less-or-equal %arg(1) %arg(2)
greater-or-equal %arg(1) %arg(2)
}
define-command \
-params 2 \
different-from %{
try %{
less-than %arg(1) %arg(2)
} \
catch %{
greater-than %arg(1) %arg(2)
}
}
define-command \
-params 2 \
less-than %{
set window int_running_sum %arg(2)
set -add window int_running_sum "-%arg(1)"
set -add window int_running_sum -1
evaluate-commands -draft %{
echo %opt(int_running_sum)
}
}
define-command \
-params 2 \
less-or-equal %{
set window int_running_sum %arg(2)
set -add window int_running_sum "-%arg(1)"
evaluate-commands -draft %{
echo %opt(int_running_sum)
}
}
define-command \
-params 2 \
greater-than %{
set window int_running_sum %arg(1)
set -add window int_running_sum "-%arg(2)"
set -add window int_running_sum -1
evaluate-commands %{
echo %opt(int_running_sum)
}
}
define-command \
-params 2 \
greater-or-equal %{
set window int_running_sum %arg(1)
set -add window int_running_sum "-%arg(2)"
evaluate-commands %{
echo %opt(int_running_sum)
}
}
Enjoy … or not!
(I do have one use case for this sort of trick.)