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.
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>"
for that itās worth, iāve been using something like this for reindenting blocks of code
define-command indent-and-append -override %{
execute-keys -with-hooks %sh{
if [ "$kak_cursor_char_value" = "10" ] && [ "$kak_cursor_column" = "1" ] && [ "$kak_cursor_line" != "1" ]; then
if [ "$kak_cursor_line" = "$kak_buf_line_count" ]; then
printf "<a-d>o"
else
printf "<a-d>ko"
fi
else
printf "<s-a>"
fi
}
}
define-command reindent %{
evaluate-commands -save-regs '"^/wicp' %{
set-register p '' # for safety
try %{ disable-auto-pairs } # if auto-pairs is installed
set-register w %val{selections_desc} # add whole selection to register w. otherwise removing comments will break the mark
# save selection to register i
execute-keys '<a-:>xH"iZ'
# save and delete comments (c for contents, p for position)
try %{
set-register slash "(^[ \t]*%opt{comment_line}[^\n]*\n)"
execute-keys "s<ret>"
set-register c %val{selections}
execute-keys "<a-d>"
set-register p %val{selections_desc}
}
execute-keys '"iz'
# unindent
try %{
execute-keys '"_s^[ \t]+<ret><a-d>'
}
execute-keys '"iz'
# escape backslashes
try %{
execute-keys 's\\<ret>i\<esc>'
}
execute-keys '"iz'
# retype everything with indentation hooks enables
execute-keys -with-hooks "<a-d>:indent-and-append<ret>%sh{ echo ""$kak_selection"" | sed -e 's/</<lt>/g' }<esc>:select %val{selection_desc}<ret>"
# restore comments
try %{
select %reg{p}
execute-keys '"cP'
}
select %reg{w}
try %{ enable-auto-pairs }
}
}
map global normal '=' ":reindent<ret>"
it is a bit overly complicated, but works most of the time. from what iāve found, it completely breaks on css, doesnāt de-indent closing ā)ā in goās multiline import, and doesnāt handle extra semicolon that is automatically inserted at the end of c-family enum, union and struct declarations.
adding support for hook groups does sound like it could make things easier for what we are trying to do here, but not really. as already mentioned, not all filetype plugins are created equal - even highlighting is all over the place (c-family doesnāt highlight operators at all, go does, while python treats = as builtin and == as operator).
it would take great effort to do the overhaul and enforce one particular indenting standard across all of them. and this in turn would break somebody elseās custom tooling built on top of currently existing behavior
This seems like what I was trying to implement but it seemed really hacky and I just wanted a somewhat consistent solution. Iāll give your own a try for a while, thanks
it does not for me, even with empty kakrc, with the exception of lines that start with \. my build of kakoune is from commit 03019caf, things may have changed between the versions.
on a side note, having looked at this command again, the :select %val{selection_desc} part is completely redundant, a line comment above selected block (if the block is not at the top of buffer) would make the whole selection commented, and an empty line above it would keep the first line unindented. this is rough