akinsho / toggleterm.nvim

A neovim lua plugin to help easily manage multiple terminal windows
GNU General Public License v3.0
4.21k stars 170 forks source link

[BUG] set_opfunc is undefined #542

Closed 10b14224cc closed 4 months ago

10b14224cc commented 6 months ago

Is there an existing issue for this?

Current Behavior

I'm putting this keybinding in my config file, as suggested in the README:

-- Send motion to terminal
vim.keymap.set("n", "<leader>im", function()
  set_opfunc(function(motion_type)
    require("toggleterm").send_lines_to_terminal(motion_type, false, { args = vim.v.count })
  end)
  vim.api.nvim_feedkeys("g@", "n", false)
end)

But when I issue <leader>im I get:

E5108: Error executing lua: .../USERNAME/.config/nvim/lua/plugins/config/toggleterm.lua:83: attempt to call global 'set_opfunc' (a nil value)
stack traceback:
    .../USERNAME/.config/nvim/lua/plugins/config/toggleterm.lua:83: in function <.../USERNAME/.config/nvim/lua/plugins/config/toggleterm.lua:82>

Expected Behavior

<leader>imw should send a word to the terminal

Steps To Reproduce

See above

Environment

- OS: Arch Linux
- neovim version:
NVIM v0.9.5
Build type: Release
LuaJIT 2.1.1702233742

   system vimrc file: "$VIM/sysinit.vim"
  fall-back for $VIM: "/usr/share/nvim"
- Shell: bash

Anything else?

No response

akinsho commented 6 months ago

Thanks @10b14224cc seems the contributor who added that doc added in a function that doesn't exist so you won't be able to use that example as is. I don't use that functionality so can't guide you specifically as that whole thing was user contributed. You can play around with it to see what works.

ShaolinXU commented 6 months ago

check 283-383 from https://github.com/BlueDrink9/env/blob/359f5863e1f4f85a607d883f1dbcdab2bceb0fec/editors/vim/runtimepath/lua/plugins/main.lua#L287

life00 commented 6 months ago

check 283-383 from https://github.com/BlueDrink9/env/blob/359f5863e1f4f85a607d883f1dbcdab2bceb0fec/editors/vim/runtimepath/lua/plugins/main.lua#L287

Idk why it needs vimscript inside, but it works. Probably just adding this somewhere in the relevant code will fix the issue.

local set_opfunc = vim.fn[vim.api.nvim_exec(
  [[
    func s:set_opfunc(val)
    let &opfunc = a:val
    endfunc
    echon get(function('s:set_opfunc'), 'name')
  ]],
  true
)]
10b14224cc commented 5 months ago

check 283-383 from https://github.com/BlueDrink9/env/blob/359f5863e1f4f85a607d883f1dbcdab2bceb0fec/editors/vim/runtimepath/lua/plugins/main.lua#L287

Idk why it needs vimscript inside, but it works. Probably just adding this somewhere in the relevant code will fix the issue.

local set_opfunc = vim.fn[vim.api.nvim_exec(
  [[
    func s:set_opfunc(val)
    let &opfunc = a:val
    endfunc
    echon get(function('s:set_opfunc'), 'name')
  ]],
  true
)]

Uhm now it works but the cursor is moved one step to the right at the end of the motion for some reason

10b14224cc commented 5 months ago

Here is a pure LUA implementation

-- Send motion
opts["desc"] = "Send motion"
opts["expr"] = true
_G.send_motion = function(motion_type)
  require("toggleterm").send_lines_to_terminal(motion_type, false, { args = vim.v.count })
end
_G.send_motion_d = function()
  vim.go.operatorfunc = "v:lua.send_motion"
  return "g@"
end
vim.keymap.set("n", ll .. "m", send_motion_d, opts)

It still moves the cursor to the right at the end, which I think is due to https://github.com/akinsho/toggleterm.nvim/blob/193786e0371e3286d3bc9aa0079da1cd41beaa62/lua/toggleterm.lua#L253