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

Vimscript plugin not loading when key is activated

Open medwatt opened this issue 2 years ago • 20 comments

Describe the bug

I am trying testing out this plugin. Everything seems to be going fine, except when lazy loading some vim plugins.

This is how things are setup in packer.

use {
    'zirrostig/vim-schlepp',
    keys = {'<Plug>SchleppUp', '<Plug>SchleppDown'},
}

Kepmapping defined in some other file:

map ("v", [[<s-up>]], [[<Plug>SchleppUp]], {noremap = false})
map ("v", [[<s-down>]], [[<Plug>SchleppDown]], {noremap = false})

I tried this with lazy, but it is not loaded when I press the trigger keys.

  {
    'zirrostig/vim-schlepp',
     keys = {'<plug>SchleppUp', '<plug>SchleppDown'},
  },

Which version of Neovim are you using? v0.8.1

medwatt avatar Dec 22 '22 13:12 medwatt

The keys should be <s-up> and <s-down> probably? You're setting the rhs, but it should be the lhs?

You also use <plug> instead of <Plug>, so maybe that's the issue?

folke avatar Dec 22 '22 13:12 folke

I tried using <s-up> and <plug>. The plugin doesn't load.

medwatt avatar Dec 22 '22 13:12 medwatt

Looking at some of the recently closed issues, I tried the following:

{
  'zirrostig/vim-schlepp',
   keys = {
    {'<plug>SchleppUp', mode = "v"},
    {'<s-down>', mode = "v"},
  },
},

Pressing <s-down> doesn't do anything. However, pressing <s-up>, which is mapped to <plug>SchleppUp gives the following error.

E5108: Error executing lua: ...local/share/nvim/lazy/lazy.nvim/lua/lazy/core/loader.lua:103: attempt to index local 'plugins' (a nil value)                                               
stack traceback:                                                                                                                                                                          
        ...local/share/nvim/lazy/lazy.nvim/lua/lazy/core/loader.lua:103: in function 'load'                                                                                               
        ...share/nvim/lazy/lazy.nvim/lua/lazy/core/handler/keys.lua:51: in function <...share/nvim/lazy/lazy.nvim/lua/lazy/core/handler/keys.lua:48>                                      
Failed to source `/home/medwatt/.local/share/nvim/lazy/vim-schlepp/plugin/schlepp.vim`                                                                                                    
...local/share/nvim/lazy/lazy.nvim/lua/lazy/core/loader.lua:189: Vim(noremap):E227: mapping already exists for <80><fd>SSchleppUp  

medwatt avatar Dec 22 '22 14:12 medwatt

Can you provide a minimal init.lua? And add in the keys that worked with packer.

folke avatar Dec 22 '22 14:12 folke

Here's a minimal config for packer.

local install_path = vim.fn.stdpath('data')..'/site/pack/packer/start/packer.nvim'

if vim.fn.empty(vim.fn.glob(install_path)) > 0 then
    vim.fn.system({'git', 'clone', 'https://github.com/wbthomason/packer.nvim', install_path})
    vim.api.nvim_command('packadd packer.nvim')
end

local packer = require('packer')
local use = require('packer').use

local plugins = function()

    -- Packer
    use {'wbthomason/packer.nvim'}

    -- Firenvim
    use {
    'zirrostig/vim-schlepp',
    keys = {'<Plug>SchleppUp', '<Plug>SchleppDown'}
    }

end

packer.startup(plugins)

local map = vim.keymap.set
map ("v", [[<s-up>]], [[<Plug>SchleppUp]], {noremap = false})
map ("v", [[<s-down>]], [[<Plug>SchleppDown]], {noremap = false})

Here's for lazy:

local lazypath = vim.fn.stdpath("data") .. "/lazy/lazy.nvim"
if not vim.loop.fs_stat(lazypath) then
  vim.fn.system({
    "git",
    "clone",
    "--filter=blob:none",
    "--single-branch",
    "https://github.com/folke/lazy.nvim.git",
    lazypath,
  })
end
vim.opt.runtimepath:prepend(lazypath)


require("lazy").setup {
    {
        'zirrostig/vim-schlepp',
        keys = {'<plug>SchleppUp', '<Plug>SchleppDown'},
    },
}

local map = vim.keymap.set
map ("v", [[<s-up>]], [[<Plug>SchleppUp]], {noremap = false})
map ("v", [[<s-down>]], [[<Plug>SchleppDown]], {noremap = false})

medwatt avatar Dec 22 '22 14:12 medwatt

I see this issue has been closed. I just updated to the latest version of lazy. The plugin is still not loading.

medwatt avatar Dec 22 '22 17:12 medwatt

Sorry, forgot to mention that you need to fix your mapping:

  {
    "zirrostig/vim-schlepp",
    keys = { { "<Plug>SchleppUp", mode = "v" }, { "<Plug>SchleppDown", mode = "v" } },
  },

folke avatar Dec 22 '22 17:12 folke

Thanks for the fix, but it still doesn't work. Holding shift and up (as defined by the keybinding) works once and then stops.

Here's a video.

https://user-images.githubusercontent.com/17733465/209195867-9ddfff8f-4b34-4b96-b7e7-58b5758a59e7.mp4

medwatt avatar Dec 22 '22 17:12 medwatt

Just pushed another fix. Can you verify?

folke avatar Dec 22 '22 17:12 folke

Issue fixed, thanks!!!! Sorry for taking too much of your time.

medwatt avatar Dec 22 '22 17:12 medwatt

@folke, I updated today lazy today and I started experiencing the behavior I reported here.

medwatt avatar Dec 24 '22 12:12 medwatt

This might also be related. I use the plugin called vim-sandwich and I have the keymapping below to replace a pair of parenthesis, brackets, etc.

map ("n", [[gsr]],  [[<plug>(operator-sandwich-replace)<plug>(textobj-sandwich-query-a)]], {noremap = false})
map ("n", [[gsrr]], [[<plug>(operator-sandwich-replace)<plug>(textobj-sandwich-auto-a)]],  {noremap = false})

This is how I call the plugin.

    {
        'machakann/vim-sandwich',
        keys = {
            { "<plug>(operator-sandwich-replace)", mode = "n" },
        },
        config = function() require('main.plugin.sandwich') end
    },

When I press gsrr, the plugin loads, but the action is not executed. Pressing gsrr a second time works. The correct behavior is that the plugin should load and the action should be executed.

I tried doing this, but it got an error when loading vim.

    {
        'machakann/vim-sandwich',
        keys = {
            { "<plug>(operator-sandwich-replace)<plug>(textobj-sandwich-query-a)", mode = "n" },
        },
        config = function() require('main.plugin.sandwich') end
    },

medwatt avatar Dec 24 '22 12:12 medwatt

Just checked that vimtex also doesn't seem to work. The plugin is supposed to load inside .tex files.

{
    'lervag/vimtex',
    ft = { 'tex' },
    config = function() vim.cmd('source ' .. path .. 'vimtex.vim') end
},

Inside a .tex file, running execute "normal \<plug>(vimtex-status)" is supposed to display something. However, lazy loading the plugin as shown above results in nothing being displayed.

medwatt avatar Dec 24 '22 13:12 medwatt

Do not load filetype plugins (like vimtex) on FileType events -- this will not work, since the plugins need to set things up before the filetype is set.

Just set lazy=false; VimTeX itself is sufficiently "lazy".

clason avatar Dec 24 '22 13:12 clason

@clason,

  • with packer, I was lazy loading it the same way and it worked fine. Not lazy loading it takes about 1ms on my machine. Not a lot of time, but since I am not always editing latex files, seems a bit wasteful to be loading it every time vim starts.
  • what about filetype plugins that are not sufficiently "lazy", what do you do about them?

medwatt avatar Dec 24 '22 13:12 medwatt

with packer, I was lazy loading it the same way and it worked fine. Not lazy loading it takes about 1ms on my machine. Not a lot of time, but since I am not always editing latex files, seems a bit wasteful to be loading it every time vim starts.

Sorry, that can't be -- this is a known and documented fact that loading VimTeX on a FileType event does not work. Either the loading was subtly different, or it also didn't work and you just didn't notice.

what about filetype plugins that are not sufficiently "lazy", what do you do about them?

Report it to the plugin author so they can fix it.

clason avatar Dec 24 '22 13:12 clason

As I am just a user, I assumed that packer was indeed not loading the plugin. Here's a video showing that. Is packer faking it (that the plugin is not loaded) ?

https://user-images.githubusercontent.com/17733465/209438057-f725474b-45a0-4aed-97ed-3a5f012a21f6.mp4

medwatt avatar Dec 24 '22 13:12 medwatt

I will check if this is lazy, but @clason is probably right.

What lazy does when lazy-loading on a filetype:

  • we make a list of all the augroups that are listening to filetype events
  • we load the plugin (inccluding /pugin and /ftdetect)
  • we check if any new augroups/autocmds have been created by the plugin
  • we trigger the filetype event for the new groups

But since there is a specific message in vimtext docs about lazy-loading, there's probably a very specific reason why this does not work there.

The plugins I'm using ft for, work as expected.

folke avatar Dec 24 '22 15:12 folke

Thanks for the update. Can you tell whether packer is actually loading the plugin even when it says "not loaded"?

medwatt avatar Dec 24 '22 16:12 medwatt

I can confirm that the original issue is back. Will look into it further tomorrow.

@medwatt I can't tell you anthing about packer. no idea

folke avatar Dec 24 '22 16:12 folke

Just checked that vimtex also doesn't seem to work. The plugin is supposed to load inside .tex files.

I confirm that vimtex does work if you remove ft = { 'tex' } (I stumbled across the same problem some time ago).

gennaro-tedesco avatar Dec 24 '22 18:12 gennaro-tedesco

It should obviously work when you remove that line since it is no longer lazy-loaded.

medwatt avatar Dec 24 '22 19:12 medwatt

If I can add something to the conversation, the ft table now works for the vimtex plugin for me. Not sure if anything changed.

Config

 {
        "lervag/vimtex",
        ft = { "tex" },
        config = function()
          return {
            ["vimtex_view_general_viewer"] = "SumatraPDF",
            ["vimtex_view_method"] = "SumatraPDF",
            ["vimtex_view_general_options"] = "-reuse-instance -forward-search @tex @line @pdf",
            ["vimtex_view_general_options_latexmk"] = "-reuse-instance",
          }
        end,
      },

When opening my init.lua file. image

When opening my .tex file image

I really hope everyone gets it to work for them. Lazy loading is sick and I think this project is awesome! ❤️

GersiD avatar Dec 25 '22 02:12 GersiD

When opening my .tex file

are you able to operate on the .tex file with vimtex though, in this case? If I do so too (namely if I do specify ft=tex), many of the vimtex commands and options do not work.

gennaro-tedesco avatar Dec 25 '22 09:12 gennaro-tedesco

About the original issue with the keys, I suspect this to be a Neovim issue, but might be a feature, I don't know

vim.keymap.set("n", "<Plug>Foobar", error)
vim.keymap.set("n", "F", "<Plug>Foobar")
vim.keymap.del("n", "<Plug>Foobar")
vim.keymap.del("n", "<Plug>Foobar")

The code above correctly gives an error on line 4 that the keymap does not exist.

vim.keymap.set("n", "<Plug>Foobar", error)
vim.keymap.set("n", "<c-f>", "<Plug>Foobar")
vim.keymap.del("n", "<Plug>Foobar")
vim.keymap.del("n", "<Plug>Foobar")

Same code, but now I use <c-f> instead of F. No error and keymaps on line 1 and 2 are deleted Which doesn't seem right?

folke avatar Dec 25 '22 14:12 folke

I've posted an issue at Neovim's repo https://github.com/neovim/neovim/issues/21530

folke avatar Dec 25 '22 14:12 folke

I was able to fix this, by making sure we only delete the lazy key mapping once

folke avatar Dec 25 '22 15:12 folke

I would like to understand what the "Plug" directive (in brackets) does. Can anyone point me where I can find the documentation for that?

@gennaro-tedesco make sure ft is a table of strings which signify filetypes. Try that and lemme know if that doesn't work.

GersiD avatar Dec 25 '22 16:12 GersiD

make sure ft is a table of strings

As per the examples in the README, ft = ... needs not be a table necessarily, a string is fine too (or am i wrong?). This said, probably we should move this discussion elsewhere to avoid polluting this issue which is now closed and solved :)

gennaro-tedesco avatar Dec 25 '22 16:12 gennaro-tedesco

@GersiD :h <Plug>

folke avatar Dec 25 '22 16:12 folke