nvim-neoclip.lua icon indicating copy to clipboard operation
nvim-neoclip.lua copied to clipboard

Bind two different keystrokes to one action

Open lukoshkin opened this issue 2 years ago • 4 comments

Can we have key-mapping like in telescope.nvim? So that one action could be possible to do in different ways. Particularly, I'm interested in mapping <CR> to paste without overwriting <C-p>

lukoshkin avatar Jun 12 '22 15:06 lukoshkin

Hi @lukoshkin! How would you suggest the structure of the settings should look like in this case? I'd prefer to not have a breaking change of the settings, do you think that is possible?

AckslD avatar Jun 20 '22 08:06 AckslD

Thank you @AckslD for you reply. I am a bit busy right now, but I think I will take a closer look at your code soon. And if it is possible (in general and particularly for me) to reframe the settings customization in a backward-compatible way, I will prepare a PR

lukoshkin avatar Jun 24 '22 16:06 lukoshkin

Hey, @AckslD! I've got back to the issue recently and discovered that I am pretty satisfied with the default mapping for <CR>. Therefore, I don't have to map two keys to one action. However, it is not difficult to adjust the functionality to allow this feature. I tried the following and it worked for me:

lua/neoclip/settings.lua

 local function setup(opts, subsettings)
+    local function is_setable (t)
+        return type(t) == 'table'
+            and not (#t > 0 and next(t, #t) == nil)
+    end
+
     if opts == nil then
         opts = {}
     end
+
     for key, value in pairs(opts) do
-        if type(subsettings[key]) == 'table' then
+        if is_setable(subsettings[key]) then
             setup(value, subsettings[key])
         else
             subsettings[key] = value
        end
    end
    check_deprecated_entries()
end

lua/neoclip/telescope.lua

-local function map_if_set(map, mode, key, desc, handler)
-    if key ~= nil then
+local function map_if_set(map, mode, keys, desc, handler)
+    if keys == nil then
+        return
+    end
+
+    if type(keys) ~= 'table' then
+        keys = { keys }
+    end
+
+    for _, key in pairs(keys) do
         map(mode, key, setmetatable({desc}, {
-          __call = function(_, ...)
-            return handler(...)
-          end,
+            __call = function(_, ...)
+                return handler(...)
+            end,
         }))
     end
 end

After these small amends, a user will be able to set an action with a list of mappings:

 ...
      i = {
        paste = { '<C-p>', '<CR>' },
      },
...

What do you think? Is it worth opening a PR?

lukoshkin avatar Jul 12 '22 11:07 lukoshkin

Nice! This looks good to me so I'd be happy to get an PR if you find it useful :+1:

AckslD avatar Jul 12 '22 16:07 AckslD