toggleterm.nvim icon indicating copy to clipboard operation
toggleterm.nvim copied to clipboard

[BUG] set_opfunc is undefined

Open 10b14224cc opened this issue 1 year ago • 3 comments

Is there an existing issue for this?

  • [X] I have searched the existing issues

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

10b14224cc avatar Feb 15 '24 08:02 10b14224cc

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.

akinsho avatar Feb 19 '24 17:02 akinsho

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

ShaolinXU avatar Feb 22 '24 14:02 ShaolinXU

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
)]

life00 avatar Feb 24 '24 07:02 life00

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

ghost avatar Apr 11 '24 07:04 ghost

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

ghost avatar Apr 11 '24 08:04 ghost

Can we update the documentation as well. I don't really understand how the above code will work.

contrun avatar Mar 16 '25 01:03 contrun