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
|
|
|
|
|
2018-06-01 12:22:08 +02:00
|
|
|
define-command format -docstring "Format the contents of the current buffer" %{ evaluate-commands -draft -no-hooks %{
|
2018-05-06 23:29:52 +02:00
|
|
|
evaluate-commands %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 "
|
2018-07-04 08:55:05 +02:00
|
|
|
write -sync \"${path_file_tmp}\"
|
2017-06-23 14:09:18 +02:00
|
|
|
|
2018-05-06 23:29:52 +02:00
|
|
|
evaluate-commands %sh{
|
2017-06-23 14:09:18 +02:00
|
|
|
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>\"
|
2018-05-06 23:29:52 +02:00
|
|
|
printf '%s\\n' \"nop %sh{ rm -f '\${path_file_out}' }\"
|
2017-06-23 14:09:18 +02:00
|
|
|
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
|
|
|
} }
|