declare-option -hidden str fmt_command %{ # fmt --uniform-spacing "--goal=$kak_opt_fmt_width" "--width=$kak_opt_fmt_width" | perl -CS -pe 's/([.!?])\s{2,}/\1 /g' fmt -u -w "$kak_opt_fmt_width" -g "$kak_opt_fmt_goal" | perl -CS -pe 's/([.!?])\s{2,}/\1 /g' } declare-option -hidden str fmt_mode fail declare-option int fmt_width 75 declare-option int fmt_goal 70 define-command -docstring "run fmt for each selection" fmt %{ evaluate-commands -save-regs | %{ set-register | %opt{fmt_command} execute-keys -itersel '|' } } define-command -docstring "disable autofmt at window scope" fmt-disable-window %{ set-option window fmt_mode fail trigger-user-hook fmt-disable-window remove-hooks window fmt } define-command -docstring "enable autofmt at window scope" fmt-enable-window %{ set-option window fmt_mode nop trigger-user-hook fmt-enable-window hook -group fmt window InsertChar [^\s] fmt-impl-window } define-command -hidden fmt-impl-window %{ try %{ # do nothing if the line isn't too long execute-keys -draft "x^[^\n]{%opt{fmt_goal},}[^\n]" evaluate-commands -save-regs "c|s" %{ evaluate-commands -draft %{ execute-keys ';' # Insert a newline after selections which close a # paragraph. try %{ execute-keys -draft 'L\n\n' } # If we were to yank newlines and paste them in the below # 'execute-keys', we would end up typing in reverse, # as the newline would be repeatedly pasted and removed # before the typed content. So, we remove them. evaluate-commands %exp{ set-register c %sh{ printf %s "$kak_quoted_selections" | tr -d "\n" }} set-register | %opt{fmt_command} # We use '' (U+E000) to save and restore the cursor # position after the pipe. execute-keys 'cp_S\n+\z|sd"cP' set-register s "%val{selections_desc}" } evaluate-commands %exp{ select %reg{s} } } } } define-command -docstring "toggle autofmt at window scope" fmt-toggle-window %{ try %{ eval %opt{fmt_mode} fmt-disable-window } catch %{ fmt-enable-window } } declare-option str fmt_info hook global User fmt-enable-window %{ set-option window fmt_info "fmt " info -title fmt 'enabled autowrap' } hook global User fmt-disable-window %{ set-option window fmt_info "" info -title fmt 'disabled autowrap' }