jpalardy / vim-slime

A vim plugin to give you some slime. (Emacs)
http://technotales.wordpress.com/2007/10/03/like-slime-for-vim/
MIT License
1.9k stars 227 forks source link

kitty default window ID #345

Closed avonmoll closed 2 years ago

avonmoll commented 2 years ago

I have switched slime target from tmux to kitty. With tmux, you can set the default config with something like "target_pane" : "{next}". This is really nice because vim-slime is always ready to go - I never had to do any config step. I'm wondering if something like this is possible when using the kitty target. It seems there is no clear kitty analog to tmux's {next} for specifying relationships between panes. However, for my use case, my desired kitty target window ID is almost always one greater than the kitty window running vim. Therefore, is it possible to specify a default configuration along the lines of:

let g:slime_target = kitty
let g:slime_default_config = {"target_window" : $KITTY_WINDOW_ID + 1}

I realize that the above won't actually work as written, but hopefully the idea is clear.

jpalardy commented 2 years ago

Hi @avonmoll

You've got the right idea; your config logic could be as arbitrarily complicated as you wish.

Some thoughts:

:echom ("5" + 2)
7

If you come up with the logic to determine your best-guess KITTY_WINDOW_ID, we can try a few things until it works. Try to do it from the command-line and show me 👍

avonmoll commented 2 years ago

Amazingly, this actually worked as a configuration: 🎉

let g.slime_target = kitty
let g.slime_dont_ask_default = 1
let g:slime_default_config = {"window_id" : $KITTY_WINDOW_ID + 1, "listen_on" : ""}

But now I am realizing my original assumption of $KITTY_WINDOW_ID + 1 is not as good as I thought. A better assumption (for me, at least) would be that the desired target window ID is the other window in the currently active/focused kitty tab. My thought is to parse through the output of kitty @ ls which lists all OS windows, tabs, and windows in a JSON. In there, one can find the currently active/focused kitty tab (i.e., the one running vim in one window and whatever REPL in another window). If these are the only two windows in that tab, one may use the last entry of "active_window_history": [<ID_of_vim_window>, <ID_of_REPL_window>] to set the target window ID.

Alternatively, I am thinking of creating a vim command which opens a new kitty window via kitty @ new-window (which returns the ID of the new window, BTW) and immediately sets let b:slime_config = {"window_id": <output_of_kitty_cmd>, listen_on : ""}. This approach is probably best for me as the window ID would be set properly and automatically without the hassle of having to parse the output of kitty @ ls. I will report back soon if this works.


Update: the latter option is working nicely. Here is an example to start a julia REPL (in a new kitty window) from neovim and set the slime target window (automatically) by pressing <leader>j

~/.config/nvim/ftplugin/julia.lua:

local function openREPL()
    local id = vim.fn.system('kitty @ new-window')
    vim.b.slime_config = { window_id = id, listen_on = "" }
    vim.fn.system('kitty @ send-text julia\\\\x0D')
    vim.fn.system('kitty @ focus-window --match recent:1')
end

vim.keymap.set('n', '<leader>j', openREPL, { buffer = true })
jpalardy commented 2 years ago

looking good 🎉