2017-05-16 13:35:43 +02:00
|
|
|
decl -docstring "shell command to which the contents of the current buffer is piped" \
|
|
|
|
str formatcmd
|
|
|
|
|
2017-06-23 14:09:18 +02:00
|
|
|
def format -docstring "Format the contents of the current buffer" %{ eval -draft %{
|
2015-10-22 15:41:19 +02:00
|
|
|
%sh{
|
2017-06-23 14:09:18 +02:00
|
|
|
if [ -n "${kak_opt_formatcmd}" ]; then
|
2015-10-22 15:41:19 +02:00
|
|
|
## Save the current position of the cursor
|
|
|
|
readonly x=$((kak_cursor_column - 1))
|
|
|
|
readonly y="${kak_cursor_line}"
|
|
|
|
|
2017-06-23 14:09:18 +02:00
|
|
|
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
|
|
|
|
printf '%s\\n' \"exec \\%|cat<space>'\${path_file_out}'<ret>\"
|
|
|
|
printf '%s\\n' \"%sh{ rm -f '\${path_file_out}' }\"
|
|
|
|
## Try to restore the position of the cursor as it was prior to formatting
|
|
|
|
printf '%s\\n' 'exec gg ${y}g ${x}l'
|
|
|
|
else
|
|
|
|
printf '%s\\n' \"
|
|
|
|
eval -client '${kak_client}' echo -color Error formatter returned an error (\$?)
|
|
|
|
\"
|
|
|
|
rm -f \"\${path_file_out}\"
|
|
|
|
fi
|
|
|
|
|
|
|
|
rm -f \"${path_file_tmp}\"
|
|
|
|
}
|
|
|
|
"
|
2015-10-22 15:41:19 +02:00
|
|
|
fi
|
|
|
|
}
|
2017-06-23 14:09:18 +02:00
|
|
|
} }
|