# Autoformatting Module # Provides utilities to make managing filetype-based autoformatters easier # Usage example: # autofmt-register rust rustfmt define-command -docstring %{ autofmt-register : Register as the formatter for . Will be automatically enabled and disabled. } -params 2 \ autofmt-register %{ declare-option str "formatcmd_%arg{1}" "%arg{2}" hook -group "autofmt-%arg{1}" global WinSetOption "filetype=%arg{1}" %{ autofmt-enable } } define-command -docstring %{ autofmt-unregister : don't automatically enable autoformatting for } -params 1 autofmt-unregister %{ remove-hooks global "autofmt-%arg{1}" } define-command -docstring %{ autofmt-disable: Disable registered formatter for this filetype, for this window } autofmt-disable %{ unset-option window formatcmd remove-hooks window "autofmt-%opt{filetype}" } define-command -docstring %{ autofmt-enable: Enable registered formatter for this filetype, for this window } autofmt-enable %{ evaluate-commands "set-option window formatcmd %%opt{formatcmd_%opt{filetype}}" hook -group "autofmt-%opt{filetype}" window BufWritePre .* %{ try format-buffer } hook -group "autofmt-%opt{filetype}" -once -always window WinSetOption filetype=.* %{ autofmt-disable } } hook global ModuleLoaded kak %{ add-highlighter shared/kakrc/code/autofmt regex (?:\s|\A)\Kautofmt-((en|dis)able|(un)?register)(?:(?=\s)|\z) 0:keyword }