1
1
mirror of https://github.com/mawww/kakoune.git synced 2024-12-11 04:34:27 +03:00
kakoune/rc/core/formatter.kak
Maxime Coste ec16969609 Do not reparse %sh{...} strings
Automatic reparsing of %sh{...}, while convenient in many cases,
can be surprising as well, and can lead to security problems:

'echo %sh{ printf "foo\necho bar" }' runs 'echo foo', then 'echo bar'.
we make this danger explicit, and we fix the 'nop %sh{...}' pattern.

To reparse %sh{...} strings, they can be passed to evaluate-commands,
which has been fixed to work in every cases where %sh{...} reparsing
was used..
2018-07-05 07:54:28 +10:00

32 lines
1.4 KiB
Plaintext

declare-option -docstring "shell command to which the contents of the current buffer is piped" \
str formatcmd
define-command format -docstring "Format the contents of the current buffer" %{ evaluate-commands -draft -no-hooks %{
evaluate-commands %sh{
if [ -n "${kak_opt_formatcmd}" ]; then
path_file_tmp=$(mktemp "${TMPDIR:-/tmp}"/kak-formatter-XXXXXX)
printf %s\\n "
write -sync \"${path_file_tmp}\"
evaluate-commands %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' \"execute-keys \\%|cat<space>'\${path_file_out}'<ret>\"
printf '%s\\n' \"nop %sh{ rm -f '\${path_file_out}' }\"
else
printf '%s\\n' \"
evaluate-commands -client '${kak_client}' echo -markup '{Error}formatter returned an error (\$?)'
\"
rm -f \"\${path_file_out}\"
fi
rm -f \"${path_file_tmp}\"
}
"
else
printf '%s\n' "evaluate-commands -client '${kak_client}' echo -markup '{Error}formatcmd option not specified'"
fi
}
} }