From 6bb96db315911faa1e2e3de3184541c91754dda9 Mon Sep 17 00:00:00 2001 From: Tsan-Kuang Lee Date: Sun, 12 Nov 2023 03:06:07 -0600 Subject: [PATCH] prepend_body_to_foreign_keys is the option to also include the body key --- README.md | 18 ++++++++++++++++++ doc/hydra.txt | 22 ++++++++++++++++++++++ lua/hydra/init.lua | 5 +++++ lua/hydra/lib/types.lua | 1 + 4 files changed, 46 insertions(+) diff --git a/README.md b/README.md index f8853d3..fdbdfdd 100644 --- a/README.md +++ b/README.md @@ -57,6 +57,7 @@ If you want to quickly understand the concept, you can watch * [`config`](#config) * [`exit`](#exit) * [`foreign_keys`](#foreign_keys) + * [`prepend_body_to_foreign_keys`](#prepend_body_to_foreign_keys) * [`color`](#color) * [More about colors concept](#more-about-colors-concept) * [Amaranth color](#amaranth-color) @@ -228,6 +229,23 @@ that doesn't belong to any head: without running the foreign key. - `foreign_keys = "run"` will not stop the hydra state, and try to run the foreign key. +#### `prepend_body_to_foreign_keys` +`boolean`\ +default: `false`\ +parent table: `config` + +This option is only relevant when `foreign_keys` is set to `nil` or `run`. + +- `prepend_body_to_foreign_keys = false` (the default) will pass the foreign key so it + will do whatever it is supposed to do. +- `prepend_body_to_foreign_keys = true` will add the body key(s) before the foreign key. + +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`, +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`. + #### `color` `"red" | "amaranth" | "teal" | "pink"`\ default: `"red"`\ diff --git a/doc/hydra.txt b/doc/hydra.txt index 4fdbd2b..e39cff9 100644 --- a/doc/hydra.txt +++ b/doc/hydra.txt @@ -258,6 +258,28 @@ config table `foreign_keys = "run"` will not stop the hydra state, and try to run the foreign key. + + *hydra-config.prepend_body_to_foreign_keys* + prepend_body_to_foreign_keys boolean + default: `false` + parent table: `config` + + This option is only relevant when `foreign_keys=nil`. + + `prepend_body_to_foreign_keys = false` (the default) will pass + the foreign key so it will do whatever it is supposed to do. + + `prepend_body_to_foreign_keys = true` will add the body key(s) + before the foreign key. + + 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`, + 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`. + *hydra-config.color* color `"red"` | `"amaranth"` | `"teal"` | `"pink"` default: `"red"` diff --git a/lua/hydra/init.lua b/lua/hydra/init.lua index 4290b64..add84c3 100644 --- a/lua/hydra/init.lua +++ b/lua/hydra/init.lua @@ -27,6 +27,7 @@ local default_config = { debug = false, exit = false, foreign_keys = nil, + prepend_body_to_foreign_keys = false, color = 'red', timeout = false, invoke_on_body = false, @@ -53,6 +54,7 @@ function Hydra:initialize(input) on_enter = { input.config.on_enter, 'function', true }, on_exit = { input.config.on_exit, 'function', true }, exit = { input.config.exit, 'boolean', true }, + prepend_body_to_foreign_keys = { input.config.prepend_body_to_foreign_keys, 'boolean', true }, timeout = { input.config.timeout, { 'boolean', 'number' }, true }, buffer = { input.config.buffer, { 'boolean', 'number' }, true }, hint = { input.config.hint, { 'boolean', 'string', 'table' }, true }, @@ -499,6 +501,9 @@ function Hydra:_leave() self:_wait() return false else + if self.config.prepend_body_to_foreign_keys and vim.fn.getchar(1) ~= 0 then + api.nvim_feedkeys(termcodes(self.body), 'nix', false) + end self:exit() return true end diff --git a/lua/hydra/lib/types.lua b/lua/hydra/lib/types.lua index 3cd9f2d..6f25d00 100644 --- a/lua/hydra/lib/types.lua +++ b/lua/hydra/lib/types.lua @@ -8,6 +8,7 @@ ---@field buffer? integer ---@field exit boolean ---@field foreign_keys hydra.foreign_keys +---@field prepend_body_to_foreign_keys boolean ---@field color hydra.color ---@field on_enter? function Before entering hydra ---@field on_exit? function After leaving hydra