caksoylar / kakoune-smooth-scroll

Smooth scrolling for Kakoune with inertial movement
MIT License
23 stars 3 forks source link

Use $kak_command_fifo for more efficient scrolling #14

Open Screwtapello opened 2 years ago

Screwtapello commented 2 years ago

Currently, the plugin works by launching a bunch of kak -p processes (in the shell fallback) or connecting directly to Kakoune's control socket (in the Python script) to inject a bunch of vj and vk keystrokes. Now that Kakoune asynchronously accepts commands from scripts via $kak_command_fifo, we should try it out and see if it's more efficient.

Screwtapello commented 2 years ago

It turns out it doesn't actually work - you can printf 'exec vj' > "$kak_command_fifo" all you like, but Kakoune doesn't actually repaint the screen until the shell block exits. As a result, this plugin still needs to use kak -p (or direct socket connections).

caksoylar commented 2 years ago

Hi @Screwtapello, thanks for bringing it up! I actually experimented with this a bit when the feature first came out, then ran into some issues which we had discussed a bit around https://discord.com/channels/717163870724161556/717165513750609991/857003103127470130.

I tried it again and at least that specific issue doesn't exist anymore; maybe it was fixed in https://github.com/mawww/kakoune/commit/19e2225153ce988674ea838989158251f1602789.

There is still a couple other issues I had encountered though, one of which you noticed:

  1. Kakoune waits until shell block is done to scroll
  2. We can no longer background the scrolling process since we are directing output to the fifo

In my limited recent experimenting 1. seems solvable by manually redrawing with <c-l>, e.g. this snippet works as expected to scroll 50 lines one by one:

nop %sh{
    python3 -c 'import sys, time
fifo = sys.argv[1]

for i in range(50):
    with open(fifo, "w") as cf:
        cf.write(f"exec jvj<c-l>")
    time.sleep(0.1)
' "$kak_command_fifo"  # >/dev/null 2>&1 </dev/null &
}

Issue 2. is not as critical but it helps us be more responsive to user input, since the user can cancel an in-progress scroll by doing something else. But it can be argued that it is better if something goes wrong since you can <c-c> the process to kill it. (It can also be distracting since you sometimes get flashes of "Waiting for shell command to finish" in the statusline.)

Overall I think it might be worth experimenting with to see if we can live with not backgrounding the process, and also see if there are other rough edges in the fifo implementation we might run into. We can then consider pros and cons of either implementation.

Screwtapello commented 2 years ago

There were a couple of reasons I decided to play with $kak_command_fifo:

When I hit the "Kakoune doesn't repaint until the shell block ends" issue, I deleted my experiment in progress, but now that you've pointed out <c-l> maybe it's worth trying again.