Useful binding to format comments

Hi, I started this as a question, but I figured out a solution before posting. This is a small utility command that wraps long lines of comments in different languages (using different comment prefixes).

Maybe someone else would find it useful, so I thought I’d post it here:

define-command format-comment %{
  exec "|fmt -$kak_opt_autowrap_column -g$kak_opt_autowrap_column --prefix ""%opt{comment_line}""<ret>"

It uses fmt with the --prefix option to format (only) lines with the comment prefix for the current language. It also uses $kak_opt_autowrap_column to set the width automatically.

As an alternative to the fmt utility I use prose (another utility). Maybe you find it useful, too.

1 Like

Oh, nice. I’ll have to look that up. There doesn’t seem to exist a Nix package for that though.

I use a wrapper script around fmt to automatically determine the comment prefix, if any.

input=$(cat; echo .)
prefix="$(printf %s "$input" | perl -ne 'if (m{^\h*((?://[!/]?|[#*]|>(?: >)*) )}) {print $1; exit}' )"
printf %s "$input" | fmt --prefix="$prefix" "$@"

used as map global normal = %{| -w $kak_opt_autowrap_column<ret>}

Edit: sounds like prose should do what I want