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