How to override/disable c-indent hooks selectively?

With the default C indentation rules, an open brace on a line by itself is exdented when you type it in. I
want to disable this to support the GNU C indentation style, which looks like this:

if (condition)

I found I could disable this in rc/filetype/c-family.kak by commenting out the line:

hook -group "%val{hook_param_capture_1}-indent" window InsertChar \{ c-family-indent-on-opening-curly-brace

I couldn’t work out how to disable this in my kakrc. I tried redefining c-family-indent-on-opening-curly-brace completely, with

define-command -hidden -override c-family-indent-on-opening-curly-brace %[

- however, this had the effect of disabling autoindent completely for C files. I found a message in *debug*:

error running hook WinSetOption(filetype=c)/: 2:5: 'require-module': 55:1: 'define-command': command 'c-family-indent-on-opening-curly-brace' already defined

Although it’s not clear, apparently the "%val{hook_param_capture_1}-indent" group is c-indent, but I don’t want to disable c-indent completely by adding it to disabled_hooks. remove-hooks also appears to operate on whole groups.

Is there any way of customizing this without editing the installed c-family.kak?

I think you want to put the define-command -override inside a hook global -once ModuleLoaded c-family to make sure that it’s run after the default command is defined

1 Like

Thanks, the following works:

hook global -once ModuleLoaded c-family %{
  define-command -override -hidden c-family-indent-on-opening-curly-brace nop   

I opted to alter the original commands instead of noping them.

The original c-family-indent-on-opening-curly-brace leaves the brace and previous line selected and aligned. I added the following sequence to the end of the execute-keys.

x           # Select both lines
<a-_>       # Merge selections
<a-:>       # Make sure they're in the forward direction
<semicolon> # Reduce to cursor
<gt>        # Indent

h<a-F>)M <a-k> \A\(.*\)\h*\n\h*\{\z <ret> <a-S> 1<a-&> x <a-_> <a-:> <semicolon> <gt>

hK <a-x> s \belse\b\h*(?://[^\n]+)?\n\h*\{<ret> <a-S> 1<a-&> x <a-_> <a-:> <semicolon> <gt>

Then in c-family-indent-on-newline you don’t want it to indent after an opening brace so remove the { from the pattern matching.

k <a-x> <a-k>[{(]\h*$<ret> j <a-gt>


k <a-x> <a-k>(\h*$<ret> j <a-gt>