mawww / kakoune

mawww's experiment for a better code editor
http://kakoune.org
The Unlicense
9.85k stars 711 forks source link

[BUG] Write-all does not trigger BufWritePre and BufWritePost hooks #5044

Closed vosov closed 9 months ago

vosov commented 9 months ago

Version of Kakoune

v2023.07.29

Reproducer

I produced the issue by running the format command in a BufWritePre hook, so for example

hook global BufWritePre .* %{
    evaluate-commands %sh{
        if [ -n "$kak_opt_formatcmd" ]; then
            echo format
        fi
    }
}

Ditto for the BufWritePost hook.

Outcome

The hook is triggered by :write<ret>, but it is not triggered by :write-all<ret> .

Expectations

I would expect the hook to be triggered by the write-all command as well. I took a peek at the code https://github.com/mawww/kakoune/blob/990e92a5f320953bab12ba2c0419d382cd68102e/src/commands.cc#L620 It seems that the write_all_buffers procedure does attempt to run the pre- and post-hooks.

Additional information

No response

vosov commented 9 months ago

After further investigation, I've found that the issue is the scope of the hook. Something like the following does do what I intended.

hook global BufSetOption filetype=(c|cpp) %{
        set-option buffer formatcmd "astyle --indent=tab --style=allman --keep-one-line-statements"
        hook buffer BufWritePre .* format
}

This ticket can be closed.