Skip to content

Instantly share code, notes, and snippets.

@dojoteef
Last active September 16, 2024 14:42
Show Gist options
  • Save dojoteef/3ba8d7c9445a0fc80272f8c05b414bfb to your computer and use it in GitHub Desktop.
Save dojoteef/3ba8d7c9445a0fc80272f8c05b414bfb to your computer and use it in GitHub Desktop.
Enable softwrap in kakoune and fix associated movement issues
# Softwrap long lines (and correct for improper kakoune movement behavior)
add-highlighter global/softwrap wrap -word -indent
define-command goto-center -docstring 'Jump to the center of the view' %{
execute-keys %sh{
echo $kak_window_range $kak_cursor_line | (
read y x h w l
num_lines=$((l-y-(h+1)/2))
if [ $num_lines -lt 0 ]; then
echo "${num_lines##-}j"
elif [ $num_lines -gt 0 ]; then
echo "${num_lines}k"
fi
)
}
}
define-command view-center -docstring 'Position view so the cursor is centered vertically' %{
view-bottom
execute-keys %sh{
echo $kak_window_range $kak_cursor_line | (
read y x h w l
num_lines=$((l-y-(h+1)/2))
if [ $num_lines -lt 0 ]; then
echo "${num_lines##-}vk"
elif [ $num_lines -gt 0 ]; then
echo "${num_lines}vj"
fi
)
}
}
define-command goto-bottom -docstring 'Jump to the bottom of the view' %{
execute-keys %sh{
echo $kak_window_range $kak_cursor_line | (
read y x h w l
num_lines=$((y+h-l))
if [ $num_lines -gt 0 ]; then
echo "${num_lines##-}j"
fi
)
}
}
define-command view-bottom -docstring 'Position view so the cursor is on the last line' %{
evaluate-commands %sh{
echo $kak_window_range $kak_cursor_line $kak_buf_line_count | (
read y x h w l t
bottom=$((y+h))
while test $bottom -ge $l; do
echo "evaluate-commands %{
execute-keys vk
echo -to-file $kak_response_fifo %val{window_range}
}" > $kak_command_fifo
last="$y $x $h $w"
read y x h w < $kak_response_fifo
bottom=$((y+h))
if [ "$y $x $h $w" = "$last" ]; then
break
fi
done
echo "echo -to-file $kak_response_fifo %val{cursor_line}" > $kak_command_fifo
read l2 < $kak_response_fifo
if [ $l -ne $l2 ]; then
echo "execute-keys j"
fi
)
}
}
define-command goto-top -docstring 'Jump to the top of the view' %{
execute-keys %sh{
echo $kak_window_range $kak_cursor_line | (
read y x h w l
num_lines=$((l-y-1))
if [ $num_lines -gt 0 ]; then
echo "${num_lines##-}k"
fi
)
}
}
define-command view-top -docstring 'Position view so the cursor is on the first line' %{
evaluate-commands %sh{
echo $kak_window_range $kak_cursor_line | (
read y x h w l
top=$((y+1))
while test $top -lt $l; do
echo "evaluate-commands %{
execute-keys vj
echo -to-file $kak_response_fifo %val{window_range}
}" > $kak_command_fifo
last="$y $x $h $w"
read y x h w < $kak_response_fifo
top=$((y+1))
if [ "$y $x $h $w" = "$last" ]; then
break
fi
done
)
}
}
map -docstring 'window bottom' global goto b '<esc>:goto-bottom<ret>'
map -docstring 'window center' global goto c '<esc>:goto-center<ret>'
map -docstring 'window top' global goto t '<esc>:goto-top<ret>'
map -docstring 'cursor on bottom' global view b '<esc>:view-bottom<ret>'
map -docstring 'center cursor (vertically)' global view c '<esc>:view-center<ret>'
map -docstring 'cursor on top' global view t '<esc>:view-top<ret>'
map global normal <c-b> ':goto-top<ret>kvb'
map global normal <c-f> ':goto-bottom<ret>jvt'
map global normal <c-u> ':goto-center<ret>vb'
map global normal <c-d> 'vt:goto-center<ret>'
@krobelus
Copy link

krobelus commented Aug 7, 2022

This is amazing, thanks so much! I think I like the new <c-u> behavior better.
I agree we should try to port it to C++. It might turn out a bit hacky (may need to render a couple of times) but it's probably worth it. I'll try to take a look soon.
FWIW I've started a wiki page on hacking Kakoune sources if you're interested.

@dojoteef
Copy link
Author

Glad it's helpful! Not sure I have time right now to look at the C++ side since there is a high startup cost for me. I was predominantly a C/C++ developer running Visual Studio (with Vim bindings!) on Windows early in my career, but switch to Mac and have barely touched C++ in a decade. That means I have to familiarize myself with the modern C++ tooling landscape. If I find the time, your link seems like a good start. Thanks!

@krobelus
Copy link

No worries, I'm happy to implement it too once I get around to.
I think C++ in general has gotten much better. I'm quite happy with kak-lsp + clangd. The Kakoune code is mostly very obvious, there are hardly any surprises.

Not yet sure about the exact behavior of <c-u> and <c-d>. My muscle memory was thrown off a bit, perhaps they should have Vim behavior (always maintain the cursor position if possible). That should make it easier to track the cursor while scrolling. I think I'll try that one as preliminary patch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment