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.87k stars 227 forks source link

Restore cursor position also with `SlimeRegionSend` #354

Open fredrikekre opened 1 year ago

fredrikekre commented 1 year ago

When using vim-slime to execute script-like code I often select large portion of the code in visual mode and sending that with SlimeRegionSend. However, when back in normal mode my cursor ends up at the beginning of the previous visual block and I have to navigate down again to wherever I stopped before sending the block. It would be very convenient if the option slime_preserve_curpos also applied for SlimeRegionSend. Thanks!

jpalardy commented 1 year ago

Hi @fredrikekre

You can check the issues and pull requests … similar ideas have been proposed (for example)

If that's not what you meant, we can discuss alternatives.

If you have an example of the behavior you would be prefer, send me

Thanks

fredrikekre commented 1 year ago

Thanks for the reference, I don't know why I didn't find that in my search.

The suggestion in https://github.com/jpalardy/vim-slime/pull/353#issuecomment-1324571606 is pretty close to what I had in mind, although that is annoying if you select your visual block "backwards", e.g. moving up.

What I had in mind is to have the cursor "stay", meaning that I would like the cursor to end up at the same position after SendRegion as it does when simply Escaping the same visual mode selection. I hope this explanation make sense, I can construct a concrete example if not.

When selecting multiple paragraphs, perhaps even multiple screen-sizes, to send it is IMO pretty disorienting when the file scrolls back to the beginning of the region and you have to navigate back down again. This is what I expected slime_preserve_curpos to mean when I found that option, and it is already how it SendParagraph works with that option.

jpalardy commented 1 year ago

Tricky…

One possible workaround, use o (:help v_o) to toggle the cursor position before sending the region (esp. in the case of selecting backwards). And then using '> ?

I don't think that covers all your use cases though 🤔

ctrl-o can also help

...

if that doesn't work, send me an example — maybe I still don't understand the before-and-after

clpan commented 1 year ago

Hi! I have a little workaround that moves cursor to the line below the visual block. You can easily modify it to make the cursor stays at the last line of your visual block. This works for backward selecting as well.

vnoremap <silent> <C-A-CR> :<C-u>silent execute("'<,'>SlimeSend") \| let lnum=line("'>") \| execute("silent normal! " . (lnum+1) . "G")<CR>

or use <Plug>SlimeRegionSend still

vnoremap <silent> <C-A-CR> :<C-u>silent execute("normal! \<Plug>SlimeRegionSend") \| let lnum=line("'>") \| execute("silent normal! " . (lnum+1) . "G")<CR>
stale[bot] commented 1 year ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

fredrikekre commented 2 months ago

Was reminded of this issue and I just wanted to post my solution for this problem in Neovim in case someone is curious:

local function slime_send_region()
    local keys = ":<C-u>call slime#send_op(visualmode(), 1)<CR>"
    local mode = "x"
    vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes(keys, true, true, true), mode, true)
    return
end

vim.keymap.set(
    "x", "<S-CR>",
    function()
        local loc = vim.api.nvim_win_get_cursor(0)
        slime_send_region()
        vim.api.nvim_win_set_cursor(0, loc)
    end
)