declare-option -docstring "shell command used for the 'format-selections' and 'format-buffer' commands" \ str formatcmd declare-option -docstring "whether the LSP should be preferred for formatting" bool lsp_fmt false define-command format-buffer -docstring "Format the contents of the buffer" %{ evaluate-commands -draft %sh{ if [ "$kak_opt_lsp_fmt" = "false" ]; then echo "format-buffer-pipe" else echo "lsp-formatting" fi } } define-command format-selections -docstring "Format the selections individually" %{ evaluate-commands -draft %sh{ if [ "${kak_opt_lsp_fmt}" = "false" ]; then echo "format-selections-pipe" else echo "lsp-range-formatting" fi } } define-command format-buffer-pipe -hidden -docstring "Format the contents of the buffer using pipe" %{ evaluate-commands -draft %{ execute-keys '%' format-selections } } define-command format-selections-pipe -hidden -docstring "Format the selections individually using pipe" %{ 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 define-command jinjafmt %{ evaluate-commands -draft %{ set-option local formatcmd 'uvx djlint --reformat --profile=jinja -' format } }