wbthomason / packer.nvim

A use-package inspired plugin manager for Neovim. Uses native packages, supports Luarocks dependencies, written in Lua, allows for expressive config
MIT License
7.83k stars 266 forks source link

Allow mapping definitions in `keys` specification #925

Open Janfel opened 2 years ago

Janfel commented 2 years ago

Describe the feature

The keys key in a packer.use() declaration should be able to accept mapping definitions like the following, similar to the :bind key of use-package.

require("packer").startup(function()
  use {
    "some/plugin",
    keys = {
      {"n", "<Key-1>", "<Plug>DoFoo"},
      {"n", "<Key-2>", "<Plug>DoBar", silent = true},
      {"v", "<Key-3>", "<Plug>DoBaz", silent = true, remap = true},
    },
  }
end)

In addition to defining those mappings as autoloads, the config block of packer_compiled.lua should then contain the corresponding vim.keymap.set() calls. This would allow the user to keep their plugin-specific bindings in one place and have them autoload the plugin with almost no extra effort, just like they would do with use-package in Emacs.

Because the translation of mapping specs into vim.keymap.set() calls would happen at compiletime, there should be no additional startup cost to this feature.

Currently this feature can be emulated by using a config value of function(_, plugin) require("config.utils").bind(plugin.keys) end with a corresponding config.utils.bind() function, because extraneous values in a keys spec are passed along and ignored.

tricktux commented 2 years ago

This would be so amazing!!!!

985 should have gotten us closer!

akinsho commented 2 years ago

985 had to be reverted because it broke compilation for specific keymaps. I think this issue is probably best deferred till compilation is refactored/removed, currently keymaps are set as strings that are written to the output compiled lua file. Adding keys to these strings that can include characters that need to be escaped is a real pain as it stands, once these keymaps are set in lua then it will be much easier to do this.

tricktux commented 2 years ago

once these keymaps are set in lua then it will be much easier to do this.

@akinsho I would like to take a stab at a PR for this. Could you please provide quick pointers/guidance on where to start or todo items?

akinsho commented 2 years ago

@tricktux this isn't possible currently, lazy loads by key are created as string for compilation, until they are created in lua which will require v2 (a rewrite almost) of packer then you will not be able to do this as lua functions rather than strings.

tricktux commented 2 years ago

I see... Okay.