2017-11-03 08:34:41 +01:00
|
|
|
declare-option -docstring "shell command to which the contents of the current buffer is piped" \
|
2017-05-16 13:35:43 +02:00
|
|
|
str formatcmd
|
|
|
|
|
2017-11-03 09:09:45 +01:00
|
|
|
define-command format -docstring "Format the contents of the current buffer" %{ evaluate-commands -draft %{
|
2015-10-22 15:41:19 +02:00
|
|
|
%sh{
|
2017-06-23 14:09:18 +02:00
|
|
|
if [ -n "${kak_opt_formatcmd}" ]; then
|
|
|
|
path_file_tmp=$(mktemp "${TMPDIR:-/tmp}"/kak-formatter-XXXXXX)
|
|
|
|
printf %s\\n "
|
|
|
|
write \"${path_file_tmp}\"
|
|
|
|
|
|
|
|
%sh{
|
|
|
|
readonly path_file_out=\$(mktemp \"${TMPDIR:-/tmp}\"/kak-formatter-XXXXXX)
|
|
|
|
|
|
|
|
if cat \"${path_file_tmp}\" | eval \"${kak_opt_formatcmd}\" > \"\${path_file_out}\"; then
|
2017-11-03 09:09:45 +01:00
|
|
|
printf '%s\\n' \"execute-keys \\%|cat<space>'\${path_file_out}'<ret>\"
|
2017-06-23 14:09:18 +02:00
|
|
|
printf '%s\\n' \"%sh{ rm -f '\${path_file_out}' }\"
|
|
|
|
else
|
|
|
|
printf '%s\\n' \"
|
2017-11-03 09:09:45 +01:00
|
|
|
evaluate-commands -client '${kak_client}' echo -markup '{Error}formatter returned an error (\$?)'
|
2017-06-23 14:09:18 +02:00
|
|
|
\"
|
|
|
|
rm -f \"\${path_file_out}\"
|
|
|
|
fi
|
|
|
|
|
|
|
|
rm -f \"${path_file_tmp}\"
|
|
|
|
}
|
|
|
|
"
|
2017-06-26 22:10:04 +02:00
|
|
|
else
|
2017-11-03 09:09:45 +01:00
|
|
|
printf '%s\n' "evaluate-commands -client '${kak_client}' echo -markup '{Error}formatcmd option not specified'"
|
2015-10-22 15:41:19 +02:00
|
|
|
fi
|
|
|
|
}
|
2017-06-23 14:09:18 +02:00
|
|
|
} }
|