"exec -with-hooks" for only specific hook/group

I messing around with writing a command to fix the indentation of text using the indent hooks but I only want to use those hooks, not trigger every other, is there a way I could do that?

define-command fix-indent -docstring "Fix indentation of selection" %{
  # Does not work correctly when selections are at the end of the document
  exec x
  eval -save-regs 'c' %sh{
    echo "exec <a-d>"
    IFS=$'\n'
    keys=$(
      echo -e "$(echo "$kak_selection" | sed -E 's/\\/\\\\/g;s/^[ \t]+//g')\n"  |
      sed 's/</<lt>/g'                                                          |
      sed 's/;/<semicolon>/g'                                                   |
      sed 's/$/<ret>/g;'                                                        |
      sed 's/ /<space>/g'                                                       |
      sed 's/%/%%/g'                                                            |
      sed 's/\t/<tab>/g'                                                        |
      sed 's/"/<dquote>/g'                                                      |
      sed "s/'/<quote>/g"                                                       |
      sed ':a;N;$!ba;s/\n//g'                                                   |
      sed 's/<ret>$//'
    )
    echo "exec -with-hooks 'O' \""$keys"\" '<esc>x<a-d>:select $kak_selection_desc<ret>x<a-:><a-semicolon>'"

    # echo "exec <dquote>cyuu<dquote>cR" # Needed to clear BufWrite hooks changes
  }
}

If you’re asking about exec -with-hooks, then no, it’s not possible to include only certain types of hooks. Although the indentation hooks shipped in different Kakoune filetype plugins are often copied from one plugin to the next, so there’s at least a minimum of consistency, they don’t have to be. In addition, a lot of people have the indentation hooks specifically disabled, so you can’t rely on them in general.

If your want to change the way indentation works, your best bet is to disable the existing hooks and replace them with your own, perhaps copy/pasted and edited from the originals, rather than trying to integrate with them as-is.

If you’re asking about exec -with-hooks

Seeing as there is no way to use -with-hooks with specific hooks then that’s that on that matter but to come back to the next point

If your want to change the way indentation works, your best bet is to disable the existing hooks and replace them with your own, perhaps copy/pasted and edited from the originals, rather than trying to integrate with them as-is.

Even if I use my own custom indent hook, I don’t see any way to insert text using the exec command with that hook, which is the whole point of what I am trying to do here. Thank you for the response though

A hook is just a hook, you can execute any command you like, including execute-keys.

Different hooks execute in different situations; indent hooks are usually triggered by an InsertChar \n hook, which means they run in insert mode, which means execute-keys should insert whatever keys you want to press. For example:

kak -n -e 'hook global InsertChar \n %{ execute-keys world }'

If I type “hello” and hit Enter, Kakoune types “world” immediately after it.

I think you’re misunderstanding the intention of the command I’m trying to create, the command takes your selection and reinserts it using the indent hooks to “fix” the indentation of the selection. I don’t currently see how that is possible with what it is you are describing

How would I copy the current text, delete the selection then reinsert them with my own custom insert hook. I’m not retyping the code manually

Ah, you’re correct, I didn’t understand what you were trying to do - I thought you were trying to take text that had been indented by the existing hooks, and then retype it slightly differently to get a different result.

If the goal is to use Kakoune’s indenting hooks as an auto-formatting tool, then what you have is I think the best you can hope for.

I figured, thanks for responding. I have decided to use a way simpler approach that isn’t as nice as full indenting but at least will align some of the code

define-command align-to-prev-indent -docstring "Align selection lines to previous line's indent" %{
  eval -save-regs 'l' %{
    exec x<dquote>lZ<a-:><a-semicolon>K<a-s>)<a-&><dquote>lzx
  }
}
map global normal <s-tab> ":align-to-prev-indent<ret>"
1 Like