Merge remote-tracking branch 'lenormf/fix-formatter'

This commit is contained in:
Maxime Coste 2017-06-25 07:22:34 +01:00
commit 718adc9c88

View File

@ -1,17 +1,35 @@
decl -docstring "shell command to which the contents of the current buffer is piped" \ decl -docstring "shell command to which the contents of the current buffer is piped" \
str formatcmd str formatcmd
def format -docstring "Format the contents of the current buffer" %{ def format -docstring "Format the contents of the current buffer" %{ eval -draft %{
%sh{ %sh{
if [ ! -z "${kak_opt_formatcmd}" ]; then if [ -n "${kak_opt_formatcmd}" ]; then
readonly kak_opt_formatcmd=$(printf '%s' "${kak_opt_formatcmd}" | sed 's/ /<space>/g')
## Save the current position of the cursor ## Save the current position of the cursor
readonly x=$((kak_cursor_column - 1)) readonly x=$((kak_cursor_column - 1))
readonly y="${kak_cursor_line}" readonly y="${kak_cursor_line}"
printf %s\\n "exec -draft %{%|${kak_opt_formatcmd}<ret>}" path_file_tmp=$(mktemp "${TMPDIR:-/tmp}"/kak-formatter-XXXXXX)
## Try to restore the position of the cursor as it was prior to formatting printf %s\\n "
printf %s\\n "exec gg ${y}g ${x}l" 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}\"
}
"
fi fi
} }
} } }