Skip to content

Commit

Permalink
Support falling back to 1-char from 2-char mode.
Browse files Browse the repository at this point in the history
Relates to hadronized#166.
  • Loading branch information
hadronized committed Nov 17, 2021
1 parent c987423 commit 7fb2504
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ to any.

![](https://phaazon.net/media/uploads/hop_char2_mode.gif)

Note that it’s possible to _fallback to 1-char mode_ if you hit a special key as second key. This key can be controlled
via the user configuration. `:h hop-config-char2_fallback_key`.

## Pattern mode (`:HopPattern`)

Akin to `/`, this mode prompts you for a pattern (regex) to search. Occurrences will be highlighted, allowing you to
Expand Down
14 changes: 14 additions & 0 deletions doc/hop.txt
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,9 @@ Most of the functions and values you need to know about are in `hop`.
Let the user type a bigram (two concatenated keys) and immediately hint
all of its occurrences.

This function can behave like |hop.hint_char1| in some cases. See
|hop-config-char2_fallback_key|.

Arguments:~
{opts} Hop options.

Expand Down Expand Up @@ -680,6 +683,17 @@ below.
Defaults:~
`uppercase_labels = false`

`char2_fallback_key` *hop-config-char2_fallback_key*
Enable a special key that breaks |hop.hint_char2| if only one key is
pressed, falling back to the same behavior as |hop.hint_char1|. It is
recommended that, if you set this option, you not use a key that is in
your |hop-config-keys| (because it would make those keys unreachable), and
not a character that you might jump to. A good fallback key could be
`<esc>` or `<cr>`, for instance.

Defaults:~
`char2_fallback_key = nil`

`extensions` *hop-config-extensions*
List-table of extensions to enable (names). As described in |hop-extension|,
extensions for which the name is in that list must have a `register(opts)`
Expand Down
8 changes: 7 additions & 1 deletion lua/hop/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,13 @@ function M.hint_char2(opts)
return
end

local pattern = vim.fn.nr2char(a) .. vim.fn.nr2char(b)
local pattern = vim.fn.nr2char(a)

-- if we have a fallback key defined in the opts, if the second character is that key, we then fallback to the same
-- behavior as hint_char1()
if opts.char2_fallback_key == nil or b ~= vim.fn.char2nr(vim.api.nvim_replace_termcodes(opts.char2_fallback_key, true, false, true)) then
pattern = pattern .. vim.fn.nr2char(b)
end

local generator
if opts.current_line_only then
Expand Down

0 comments on commit 7fb2504

Please sign in to comment.