Filetype hilight hooks

Like all good plugin writers (who define highlighters) I’ve copied over (and of course adapted) this, from grep.kak

hook -once -always window WinSetOption filetype=.* %{ remove-highlighter window/grep }

What is the purpose of this code; what disaster is it trying to avoid?

Imagine you’re working on a highlighter for the fictional “greo” file type. While developing it, you open your foo.greo test file, and try out your highlighter plugin with :set window filetype grep.

Oh no, you accidentally typed grep instead of greo! You can immediately try :set window filetype greo, but since you’ve already set the filetype to grep, all the WinSetOption filetype=grep hooks have fired and added various mappings and highlighters that will interfere with the mappings and highlighters you want to test.

Except, the grep plugin uses that hook you pointed out. That hook is installed inside the WinSetOption filetype=grep hook, so that when the filetype option is set to anything else, all the grep-related things will be removed and the new filetype has a clean slate to work from. Note that it has the -once flag, so once the cleanup occurs the hook is automatically removed. It also has the -always flag, so it will still be run if you happen to change the filetype in no-hooks mode (by pressing \).

2 Likes