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

add an option to add the body key to emitted foreign keys #100

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

tsankuanglee
Copy link

This PR adds an option prepend_body_to_foreign_keys, as well as related docs in help and README.md.

The main use is to make kepmaps with the same leading body, but defined outside of this Hydra, reachable.

For example, if you have a Hydra with the body CTRL-W and 4 heads
h, j, k, l, and nothing else, then, with prepend_body_to_foreign_keys = false (default), when you press v, Hydra will pass v to nvim for process; on the other hand, with prepend_body_to_foreign_keys = true, Hydra will send CTRL-W_v.

In practice, when we set prepend_body_to_foreign_keys=true, the demo config of Windows and buffers management:

Hydra({
   name = 'Windows',
   hint = window_hint,
   config = {
      invoke_on_body = true,
      hint = {
         border = 'rounded',
         offset = -1
      }
   },
   mode = 'n',
   body = '<C-w>',
   heads = {
      { 'h', '<C-w>h' },
      { 'j', '<C-w>j' },
      { 'k', pcmd('wincmd k', 'E11', 'close') },
      { 'l', '<C-w>l' },

      { 'H', cmd 'WinShift left' },
      { 'J', cmd 'WinShift down' },
      { 'K', cmd 'WinShift up' },
      { 'L', cmd 'WinShift right' },

      { '<C-h>', function() splits.resize_left(2)  end },
      { '<C-j>', function() splits.resize_down(2)  end },
      { '<C-k>', function() splits.resize_up(2)    end },
      { '<C-l>', function() splits.resize_right(2) end },
      { '=', '<C-w>=', { desc = 'equalize'} },

      { 's',     pcmd('split', 'E36') },
      { '<C-s>', pcmd('split', 'E36'), { desc = false } },
      { 'v',     pcmd('vsplit', 'E36') },
      { '<C-v>', pcmd('vsplit', 'E36'), { desc = false } },

      { 'w',     '<C-w>w', { exit = true, desc = false } },
      { '<C-w>', '<C-w>w', { exit = true, desc = false } },

      { 'z',     cmd 'WindowsMaximaze', { exit = true, desc = 'maximize' } },
      { '<C-z>', cmd 'WindowsMaximaze', { exit = true, desc = false } },

      { 'o',     '<C-w>o', { exit = true, desc = 'remain only' } },
      { '<C-o>', '<C-w>o', { exit = true, desc = false } },

      { 'b', choose_buffer, { exit = true, desc = 'choose buffer' } },

      { 'c',     pcmd('close', 'E444') },
      { 'q',     pcmd('close', 'E444'), { desc = 'close window' } },
      { '<C-c>', pcmd('close', 'E444'), { desc = false } },
      { '<C-q>', pcmd('close', 'E444'), { desc = false } },

      { '<Esc>', nil,  { exit = true, desc = false }}
   }
})

can be simplified as:

Hydra({
   name = 'Windows',
   hint = window_hint,
   config = {
      invoke_on_body = true,
      hint = {
         border = 'rounded',
         offset = -1
      },
      exit = true,  -- change here
      foreign_keys = nil,  -- change here
      prepend_body_to_foreign_keys = true,  -- change here
   },
   mode = 'n',
   body = '<C-w>',
   heads = {
      { 'H', cmd 'WinShift left', {exit = false} },
      { 'J', cmd 'WinShift down', {exit = false} },
      { 'K', cmd 'WinShift up', {exit = false} },
      { 'L', cmd 'WinShift right', {exit = false} },

      { '<C-h>', function() splits.resize_left(2)  end, {exit = false} },
      { '<C-j>', function() splits.resize_down(2)  end, {exit = false} },
      { '<C-k>', function() splits.resize_up(2)    end, {exit = false} },
      { '<C-l>', function() splits.resize_right(2) end, {exit = false} },

      { 'z',     cmd 'WindowsMaximaze', { exit = true, desc = 'maximize' } },
      { '<C-z>', cmd 'WindowsMaximaze', { exit = true, desc = false } },

      { 'b', choose_buffer, { exit = true, desc = 'choose buffer' } },

      { '<Esc>', nil,  { exit = true, desc = false }}
   }
})

We wouldn't need to re-define any built-in or custom keymaps here. CTRL-W_? is a great example, since nvim has 76 of them! This option can save us from having to repeat lots of the same patterns:

{ 'w', '<C-w>w', { exit = true, desc = false } },

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant