Customization

According to the front of the main page of the website
image

It says it is customizable in regards to macros and hooks. So I wanted to know what are exactly “hooks” for Kakoune? And can hooks be customisable via any programming language?

Is it also customisable via shell commands?

Is there a dwm-like approach to customising Kakoune where everything is modified from the source code but in a much higher level?

Kakoune has a very minimalist (no explicit loops, no straight-forward control-flow) scripting language that you can use when you type :. A “hook” tells Kakoune to execute a command when some particular event occurs, like “a new buffer was created” or “the user pressed a key” or “a configuration option changed”.

Like I say, Kakoune’s scripting language is very minimalist, but one of the things it can do easily is to run a shell-script to generate commands (or parts of commands) to run. For example:

define-command show-rot13 %{
    info "ROT13 of the current selection is: %sh{printf %s $kak_selection | tr A-Za-z M-ZA-Lm-za-l }"
}

This defines a new command called show-rot13, which wraps the info command, and passes it a string. The string contains a %sh{} block which is passed to the shell to evaluate, and replaced by whatever the shell prints to stdout.

Hooks can do the same kind of thing. For example, here’s a hook I use to enable formatting Python files with the black formatting tool, when it’s available:

# This hook is executed whenever a window's "filetype" option is set to "python"
hook global WinSetOption filetype=python %{
    # The hook will execute the commands produced by the following shell script
    evaluate-commands %sh{
        # if `black` is a command somewhere on $PATH...
        if command -V black >/dev/null 2>/dev/null; then
            # Tell Kakoune to set the `formatcmd` option to invoke `black`
            echo set-option window formatcmd %{ black --quiet --line-length 80 - }
            # Tell Kakoune to automatically reformat this window's buffer before saving it
            echo hook window BufWritePre '.*' format
        else
            # `black` is not installed, log a message to stderr,
            # which will be recorded in Kakoune`s `*debug*` buffer
            # so if formatting doesn't work I can figure out why
            echo "Could not find black formatter" 2>/dev/null
        fi
    }
}

Hooks can invoke shell-scripts as above, and shell-scripts can run programs written in any language, so if you want to write some logic in Scheme or Tcl or COBOL, feel free.

1 Like