Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vimscript plugin not loading when key is activated #97

Closed
medwatt opened this issue Dec 22, 2022 · 41 comments · Fixed by #96
Closed

Vimscript plugin not loading when key is activated #97

medwatt opened this issue Dec 22, 2022 · 41 comments · Fixed by #96
Labels
bug Something isn't working upstream

Comments

@medwatt
Copy link

medwatt commented Dec 22, 2022

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 medwatt added the bug Something isn't working label Dec 22, 2022
@folke
Copy link
Owner

folke commented Dec 22, 2022

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?

@medwatt
Copy link
Author

medwatt commented Dec 22, 2022

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

@medwatt
Copy link
Author

medwatt commented Dec 22, 2022

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  

@folke
Copy link
Owner

folke commented Dec 22, 2022

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

@medwatt
Copy link
Author

medwatt commented Dec 22, 2022

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
Copy link
Author

medwatt commented Dec 22, 2022

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

@folke
Copy link
Owner

folke commented Dec 22, 2022

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

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

@medwatt
Copy link
Author

medwatt commented Dec 22, 2022

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.

2022-12-22.18-50-40.mp4

@folke
Copy link
Owner

folke commented Dec 22, 2022

Just pushed another fix. Can you verify?

@medwatt
Copy link
Author

medwatt commented Dec 22, 2022

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

@medwatt
Copy link
Author

medwatt commented Dec 24, 2022

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

@folke folke reopened this Dec 24, 2022
@medwatt
Copy link
Author

medwatt commented Dec 24, 2022

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
Copy link
Author

medwatt commented Dec 24, 2022

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.

@clason
Copy link
Collaborator

clason commented Dec 24, 2022

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".

@medwatt
Copy link
Author

medwatt commented Dec 24, 2022

@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?

@clason
Copy link
Collaborator

clason commented Dec 24, 2022

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.

@medwatt
Copy link
Author

medwatt commented Dec 24, 2022

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

packer_vimtex.mp4

@folke
Copy link
Owner

folke commented Dec 24, 2022

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.

@medwatt
Copy link
Author

medwatt commented Dec 24, 2022

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

@folke
Copy link
Owner

folke commented Dec 24, 2022

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

@gennaro-tedesco
Copy link

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

@medwatt
Copy link
Author

medwatt commented Dec 24, 2022

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

@GersiD
Copy link

GersiD commented Dec 25, 2022

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! ❤️

@gennaro-tedesco
Copy link

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.

@folke
Copy link
Owner

folke commented Dec 25, 2022

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
Copy link
Owner

folke commented Dec 25, 2022

I've posted an issue at Neovim's repo neovim/neovim#21530

@folke folke added the upstream label Dec 25, 2022
@folke
Copy link
Owner

folke commented Dec 25, 2022

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

@folke folke closed this as completed Dec 25, 2022
@GersiD
Copy link

GersiD commented Dec 25, 2022

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.

@gennaro-tedesco
Copy link

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

@folke
Copy link
Owner

folke commented Dec 25, 2022

@GersiD :h <Plug>

@medwatt
Copy link
Author

medwatt commented Dec 26, 2022

@folke, while the issue is solved for the plugin I initially mentioned, there are still cases that use <plug> that fail.

Here's a minimal config that you can use. Position the cursor inside a [ ] and hit gsrr. The plugin loads but the action is not triggered.

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 {
    {
      'machakann/vim-sandwich',
        keys = {
            { "<plug>(operator-sandwich-add)", mode = "n" },
            { "<plug>(operator-sandwich-delete)", mode = "n" },
            { "<plug>(operator-sandwich-replace)", mode = "n" },
        },
    },
}

vim.keymap.set ("n", [[gsrr]], [[<plug>(operator-sandwich-replace)<plug>(textobj-sandwich-auto-a)]],  {noremap = false})

Here, the plugin doesn't even load.

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 {
    {
        'booperlv/nvim-gomove',
        keys = {
            {"<plug>GoNSMDown", mode = "v"},
            {"<plug>GoNSMUp", mode = "v"}
        }
    },
}

vim.keymap.set ("v", [[<s-down>]], "<plug>GoVSMDown")
vim.keymap.set ("v", [[<s-up>]], "<plug>GoVSMUp")

@folke
Copy link
Owner

folke commented Dec 26, 2022

In your second example, the plugs are different

@folke
Copy link
Owner

folke commented Dec 26, 2022

In your first example, those are operator pending modes, so the mappings are incorrect?

@medwatt
Copy link
Author

medwatt commented Dec 26, 2022

In your first example, those are operator pending modes, so the mappings are incorrect?

It's operator pending mode with gsr, and the second r is supposed to be the one that selects what to do. You can try it to see how it works.

@folke
Copy link
Owner

folke commented Dec 26, 2022

@medwatt I think I was able to fix it. Hopefully for good now ;)

@medwatt
Copy link
Author

medwatt commented Dec 26, 2022

@medwatt I think I was able to fix it. Hopefully for good now ;)

Seems fixed. Thanks once more.

@folke
Copy link
Owner

folke commented Dec 27, 2022

I think I was able to fix the Vimtex lazy-loading on ft="tex". Would be great if you could confirm, since I don't use Vimtext myself.

@medwatt
Copy link
Author

medwatt commented Dec 27, 2022

Seems fixed. Nice !!

@medwatt
Copy link
Author

medwatt commented Dec 27, 2022

@folke, is it possible to load a module using a vim function?

For example, I am using a plugin called floaterm that allows you to create custom floating terminals by calling a vim function (see here).

For example:

vim.fn['floaterm#new'](false, cmd, {null="null"}, floaterm_config)

How do I load the plugin when vim.fn['floaterm#new'] is called?

@folke
Copy link
Owner

folke commented Dec 27, 2022

That's not possible and I'm not planning on adding this functionality

@folke
Copy link
Owner

folke commented Dec 27, 2022

Haven't tested this, but something like the below in init() probably works:

vim.fn["floaterm#new"] = function(...)
  vim.fn["floaterm#new"] = nil
  require("lazy").load({ plugins = { "vim-floaterm" } })
  return vim.fn["floaterm#new"](...)
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working upstream
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants