Python bridge

This bridge runs a python shell in the background and can send selections through the shell. This way you can do calculations while keeping memory of previous variables, so this enables you to use variables in later calculations.

It has some quirks because I read the output using a while read loop, I’m still looking for a better way, any suggestions?

Any feedback is welcome.

Edit: Quirks have been resolved due to the suggestion of @Screwtapello

2 Likes

what about:

tail -f

It’s like a Python REPL?

Yes, it integrates a python REPL with kakoune, I was using pipe through python a lot and wanted it to remember variables.

tail -f blocks, so that would mean that the pipe never returns.

Is there any existing REPL you can send code to?

I’m sending the code to python -i, which is the defacto python REPL.

Or is that not what you mean?

I was thinking on a view to the terminal. Start python -i in a terminal and send code from Kakoune to that session.

I could add an option to do that in addition to sending the output to the FIFO to get it back to kakoune

Python is itself a scripting language that can evaluate code, so you could move a bunch of logic from shell to Python. That is, your Kakoune script could include a Python snippet that executes an infinite loop of:

  • open the input FIFO for reading, and read the entire contents
  • feed the input into code.InteractiveInterpreter and write the result to the output FIFO
  • if writing to the output FIFO generates an error, exit

Piping code from Kakoune would look something like:

    cat output-fifo  & # start reading the output, so Python can safely write
    cat > input-fifo & # send the input to Python
    wait               # do not continue until the communication is complete

To tell the Python repl to exit, just write any expression to the input fifo, without setting up a reader on the output fifo. When the repl tries to print the result of the expression, the write fails, and it should exit.

2 Likes

Thx! This solved my problem, I haven’t encountered any error now during a quick test!

Maybe you could use a relative path for the Python script by using %val{source}.

Yeah, it’s better to

declare-option -hidden str python-bridge-source %val{source}

and use it then to call your python script. Because users may install your plugin to different location, e.g. I know that at least one guy is using .cache directory for plug.kak plugin installation path for some reason.

I didn’t know of that expansion, I’m using it now.

Also I added automatic starting and stopping.

declare-option -hidden str python_bridge_source %sh(printf '%s' "${kak_source%/*}")
1 Like

In your script, you changed to a relative path, but didn’t change the Python call to use that path.

map global normal = ':python-bridge-send<ret>'

Consider adding space after the colon like so:

map global normal = ': python-bridge-send<ret>'

It will prevent this command from cluttering the history.

1 Like

I have added support for sending the output to a second fifo in addition to kakoune.

This way you can keep track of previous outputs in that second window.

You can open it by doing

set global python_bridge_fifo_enabled true

Cool Plugin!

Some ideas:

  • What do you think about a command to toggle the log fifo, rather than an option and hooks?

  • And a command to return the result in a new line below, instead of replacing the selection? Maybe call it python-bridge-below or something. I think, ideally, a plugin like this would follow the behavior of the p and R keys.

And some nitpicks:

  • Why do you shell out for initializing the options in pyhon-bridge.kak?
declare-option -hidden str python_bridge_folder %sh{echo /tmp/kakoune_python_bridge/$kak_session}

could just be

declare-option -hidden str python_bridge_folder "/tmp/kakoune_python_bridge/%val(session)"
  • The README says python_bridge_folder is an option you can set, but it’s hidden in the script. IMO people won’t need to set that option, so you can remove that line from the README.
2 Likes

This is so awesome I added emoji responses just for it (not really, but still awesome).

1 Like