2019-09-12 16:05:24 +02:00
|
|
|
declare-option -docstring "shell command used for the 'format-selections' and 'format-buffer' commands" \
|
2017-05-16 13:35:43 +02:00
|
|
|
str formatcmd
|
|
|
|
|
2019-09-12 16:05:24 +02:00
|
|
|
define-command format-buffer -docstring "Format the contents of the buffer" %{
|
|
|
|
evaluate-commands -draft %{
|
|
|
|
execute-keys '%'
|
|
|
|
format-selections
|
|
|
|
}
|
|
|
|
}
|
2017-06-23 14:09:18 +02:00
|
|
|
|
2019-09-12 16:05:24 +02:00
|
|
|
define-command format-selections -docstring "Format the selections individually" %{
|
|
|
|
evaluate-commands %sh{
|
|
|
|
if [ -z "${kak_opt_formatcmd}" ]; then
|
2019-11-14 08:32:55 +01:00
|
|
|
echo "fail 'The option ''formatcmd'' must be set'"
|
2015-10-22 15:41:19 +02:00
|
|
|
fi
|
|
|
|
}
|
2022-04-09 14:38:32 +02:00
|
|
|
evaluate-commands -draft -no-hooks -save-regs 'e|' %{
|
|
|
|
set-register e nop
|
2019-09-12 16:05:24 +02:00
|
|
|
set-register '|' %{
|
2020-11-30 12:24:06 +01:00
|
|
|
format_in="$(mktemp "${TMPDIR:-/tmp}"/kak-formatter.XXXXXX)"
|
|
|
|
format_out="$(mktemp "${TMPDIR:-/tmp}"/kak-formatter.XXXXXX)"
|
2019-09-12 16:05:24 +02:00
|
|
|
|
|
|
|
cat > "$format_in"
|
|
|
|
eval "$kak_opt_formatcmd" < "$format_in" > "$format_out"
|
|
|
|
if [ $? -eq 0 ]; then
|
|
|
|
cat "$format_out"
|
|
|
|
else
|
2022-04-09 14:38:32 +02:00
|
|
|
echo "set-register e fail formatter returned an error (exit code $?)" >"$kak_command_fifo"
|
2019-09-12 16:05:24 +02:00
|
|
|
cat "$format_in"
|
|
|
|
fi
|
|
|
|
rm -f "$format_in" "$format_out"
|
|
|
|
}
|
|
|
|
execute-keys '|<ret>'
|
2022-04-09 14:38:32 +02:00
|
|
|
%reg{e}
|
2019-09-12 16:05:24 +02:00
|
|
|
}
|
|
|
|
}
|
2019-09-15 02:52:06 +02:00
|
|
|
|
|
|
|
alias global format format-buffer
|