Popup.kak -- run shells and programs within a kakoune client info modal

These past few days I’ve been working on a new plugin, popup.kak. This lets you create interactive info modals that could contain a shell or command and make use of their standard output. Here’s a demo:

asciicast

An example invocation looks something like this:

popup --title open --kak-script %{ edit %opt{popup_output} } -- fish -c __fzf_find

And shows up like this:

I think it’s mostly free of bugs in its current state, but there are some missing features, namely:

  • Have a way to pass standard input to the command in the popup. This could be super useful through kak-lsp to find references without having to jump to a buffer.
  • Color support (this is kind of annoying, I’ll have to write a clone of kak-ansi).
  • Display where the cursor is.

Implementation

This is implemented using a tmux session, that is actually holding the state of the modal. The tmux session is periodically queried, and the info modal is updated if anything has changed. Keys are captured using a recursive on-key command, and sent to the tmux session. Lastly standard error and standard out are captured by augmenting the provided command with some bash redirection.

Why

Well, I really like switching to new files fuzzily, and from within my kakoune instance. I currently use a hacky wrapper around a terminal multiplexer to overlay an instance of fzf to switch between files. I wanted to see if it was possible to do something similar without wrapping kakoune in a multiplexer.

Let me know what you guys think!

11 Likes

Does Kakoune have to be running inside tmux, or does the plugin automatically start a private tmux session to run the popup command in?

Kakoune does not run inside tmux. The popup command runs under tmux and uses it as a terminal emulator so I can read it’s state and display it in the modal.

So if I understand correctly, this is spawning a tmux session consisting of the application/script that was run, using on-key to detect and send keystrokes to the tmux session, and then grabbing the tmux session’s output to send back to Kakoune?

Very clever! It never ceases to amaze me what is possible with Kakoune’s minimalist approach to plugins. I’ll have to consider something similar for when it comes time to implement a debug console in kak-dap…

Yep that’s it!

That’s a very cool approach. I’ve wanted nested modal terminals before, but never thought it might be possible to do using a continuously updating info.

Thanks! I’m kind of surprised on how well it works. I’ve added support for coloring where the cursor is in the modal, and I’m currently trying to add color support. The PoC for coloring already looks promising:

Looks like colors work :slight_smile:

1 Like

Hi Enricozb,

I tried your plugin for fzf and fzf + ripgrep.
It is working well but only when there is not tmux.
The display is disorganized when tmux is running.
And I need to keep it with tmux to manage windows.
I used the following commands (fr in second one is a script to call fzf+ripgrep) :

map global user -docstring 'popup fzf' F ":popup --title open --kak-script %{edit %opt{popup_output}} -- fzf --preview 'batcat --color=always {}' --preview-window '~3'<ret>"
map global user -docstring 'popup rzf' G ":popup --title open --kak-script %{edit %opt{popup_output}} -- fr<ret>"

When you say “when there is not tmux” do you mean that kakoune running under tmux causes my plugin not to work? I’ll test this out, very curious use case that I didn’t consider.

Yes, this is that kakoune running under tmux causes my plugin not to work.
Maybe this is specific to my kakrc or my environment.

So I’m testing this right now and there indeed seems to be a slight bug on the initial calculation of the popup size. Can you confirm that you are also getting the following following behavior for me:

  1. Run tmux
  2. Run kak
  3. Open a popup popup fish or something similar.
    • The popup content should not be legible
  4. Resize the window of your terminal.
    • The popup content should be legible

Does this describe the behavior you are seeing?

Yes, for me too, when I resized the window of terminal the popup content come back to be legible.
This is describing the same behaviour that for me.

It seems rg is really popular, but ug is also quite good and comes with an interative tui for grep.
From the TUI, you can use <c-y> to open the top file in $EDITOR, but I’ll have to look into seeing if it can pass the line number.
If it can, that’d be great for jumping to the right place in a kak session connected to an existing server.

Alternatively, pressing <ret><ret><c-q> will exit, outputting the top selection. Alternatively, you scroll to pick another, if that’s easier than narrowing the selection to make your preferred match the first.

> ug -Q=2 -HI --ignore-files --no-heading

is an example invocation. --no-heading is needed to get filename: line: content. Otherwise, it lists the filename in a heading and groups files together, which is visually preferable, but then returning a selected line won’t give you the filename to open…
[Seems like displayed result and output should be orthogonal, though.]

As an alternative to --no-heading, you can pass a --format arg, but it seems to lose colored output when it does that.

However, I tried basic examples like

popup --title open --kak-script %{ edit %opt{popup_output} } -- fish -c 'ug -Q=2 -HI --ignore-files --no-heading'

or even just

popup fish

but this doesn’t seem to work. I see a small window/box flash but disappear in an instant – too quick for me to tell what, if anything, rendered. (To clarify, I took the latter from the README examples, but I do have fish installed – in fact, it is my $SHELL).

Please try kak-popup v0.4.3 that I just pushed. It does an additional resize after starting the popup window. Let me know if it works for you.

Can you please share more about your setup? There have been other reports about the plugin not working at all for some people but I haven’t been able to drill this down at all.

It seems to be OK with your update.
With cargo, I updated with cargo install kak-pop again. Is there a better command to do ?

If you installed via cargo that is what I would do. I personally manage my setup via nix though.

I try to use the following command to open file with fzf using popup :

map global user -docstring ‘popup fzf’ F “:popup --title open --kak-script %{edit %opt{popup_output}} – fzf --preview ‘bat --color=always {}’ --preview-window ‘~3’”

The popup is opening correctly and I can choose a file(with color in preview).
But no file is open in Kakoune. Do you know how I could solve the command ?

I tried and got the same with (fzf is working in terminal and give back the path of the file selected) :

popup --title open --kak-script %{edit %opt{popup_output}} – fzf

This is likely because kakoune is evaluating expansions in your double quotes. Try this:

map global user -docstring 'popup fzf' F ':popup --title open --kak-script %{edit %opt{popup_output}} -- fzf --preview "bat --color=always {}" --preview-window "~3"<ret>'