From 904cec3c4a329cf89fc3219d359239910d61f3f6 Mon Sep 17 00:00:00 2001 From: thing1 Date: Tue, 28 Jan 2025 09:14:32 +0000 Subject: init commit --- autoload/tools/format.kak | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 autoload/tools/format.kak (limited to 'autoload/tools/format.kak') diff --git a/autoload/tools/format.kak b/autoload/tools/format.kak new file mode 100644 index 0000000..2435af0 --- /dev/null +++ b/autoload/tools/format.kak @@ -0,0 +1,38 @@ +declare-option -docstring "shell command used for the 'format-selections' and 'format-buffer' commands" \ + str formatcmd + +define-command format-buffer -docstring "Format the contents of the buffer" %{ + evaluate-commands -draft %{ + execute-keys '%' + format-selections + } +} + +define-command format-selections -docstring "Format the selections individually" %{ + evaluate-commands %sh{ + if [ -z "${kak_opt_formatcmd}" ]; then + echo "fail 'The option ''formatcmd'' must be set'" + fi + } + evaluate-commands -draft -no-hooks -save-regs 'e|' %{ + set-register e nop + set-register '|' %{ + format_in="$(mktemp "${TMPDIR:-/tmp}"/kak-formatter.XXXXXX)" + format_out="$(mktemp "${TMPDIR:-/tmp}"/kak-formatter.XXXXXX)" + + cat > "$format_in" + eval "$kak_opt_formatcmd" < "$format_in" > "$format_out" + if [ $? -eq 0 ]; then + cat "$format_out" + else + echo "set-register e fail formatter returned an error (exit code $?)" >"$kak_command_fifo" + cat "$format_in" + fi + rm -f "$format_in" "$format_out" + } + execute-keys '|' + %reg{e} + } +} + +alias global format format-buffer -- cgit v1.2.3