How can I set the indentwidth option for certain file types?

I like my default indentation width at 2, but sometimes I want 4, depending on the file type. Can I set the indentwidth option for all files of a given type?

It’s something like:

set-option global indentwidth 2
hook global WinSetOption filetype=java {
  set-option window indentwidth 4

Kakoune also offers basic support for

If I recall correctly It’s complete support excluding insert_final_newline since Kakoune always inserts newline. Is anything missing?

“no window in context”, so I guess I need to read about hooks now. Thanks for pointing me in a promising direction.

I did this exactly:

hook global WinSetOption filetype=java %{ set-option buffer indentwidth 4 }

Question: do I need to know anything about the difference between calling set-option on the buffer compared to on the window?

I’ve thought it weird to set these things on windows instead of buffers, but I think this is how Maxime is intending it to be used. Right now, some options can stick around if the window switches to another buffer. Maxime is planning a mechanism to make that not happen. Some kind of setting a shared group of options and then selecting which set to use for a window, then unlinking that set.

This hook should work (note the % that was missing), but it will only take effect the next times filetype is set to java. So if you do run it interactively it won’t take effect on the existing “windows” (see :doc scopes for an explanation). If you add it to your kakrc it should affect all files detected as java.