format.kak (1279B)
1 declare-option -docstring "shell command used for the 'format-selections' and 'format-buffer' commands" \ 2 str formatcmd 3 4 define-command format-buffer -docstring "Format the contents of the buffer" %{ 5 evaluate-commands -draft %{ 6 execute-keys '%' 7 format-selections 8 } 9 } 10 11 define-command format-selections -docstring "Format the selections individually" %{ 12 evaluate-commands %sh{ 13 if [ -z "${kak_opt_formatcmd}" ]; then 14 echo "fail 'The option ''formatcmd'' must be set'" 15 fi 16 } 17 evaluate-commands -draft -no-hooks -save-regs 'e|' %{ 18 set-register e nop 19 set-register '|' %{ 20 format_in="$(mktemp "${TMPDIR:-/tmp}"/kak-formatter.XXXXXX)" 21 format_out="$(mktemp "${TMPDIR:-/tmp}"/kak-formatter.XXXXXX)" 22 23 cat > "$format_in" 24 eval "$kak_opt_formatcmd" < "$format_in" > "$format_out" 25 if [ $? -eq 0 ]; then 26 cat "$format_out" 27 else 28 echo "set-register e fail formatter returned an error (exit code $?)" >"$kak_command_fifo" 29 cat "$format_in" 30 fi 31 rm -f "$format_in" "$format_out" 32 } 33 execute-keys '|<ret>' 34 %reg{e} 35 } 36 } 37 38 alias global format format-buffer