Skip to content

Commit

Permalink
Add multi_windows support
Browse files Browse the repository at this point in the history
  • Loading branch information
yehuohan committed Dec 18, 2021
1 parent 563ccb6 commit f37e36d
Show file tree
Hide file tree
Showing 6 changed files with 213 additions and 102 deletions.
14 changes: 10 additions & 4 deletions doc/hop.txt
Original file line number Diff line number Diff line change
Expand Up @@ -93,18 +93,19 @@ as many features as possible via the Vim commands but ultimately, you will
have access to more features by using the Lua API directly. Have a look at
|hop-lua-api| for more documentation.

Some of the commands have a suffix, such as `BC` and `AC`. Those are
Some of the commands have a suffix, such as `BC` ,`AC` and `MW`. Those are
variations of the commands without the suffix, applying to the visible part of
the buffer before and after the cursor, respectively. Another kind of suffix
(that can be mixed with `BC` and `AC`) is `CurrentLine`. This creates a
variant of the command that will only run for the current line.
the buffer before and after the cursor and and multiple windows, respectively.
Another kind of suffix (that can be mixed with `BC` and `AC`) is `CurrentLine`. This
creates a variant of the command that will only run for the current line.

`:HopWord` *:HopWord*
`:HopWordBC` *:HopWordBC*
`:HopWordAC` *:HopWordAC*
`:HopWordCurrentLine` *:HopWordCurrentLine*
`:HopWordCurrentLineBC` *:HopWordCurrentLineBC*
`:HopWordCurrentLineAC` *:HopWordCurrentLineAC*
`:HopWordMW` *:HopWordMW*
Annotate all |word|s in the current window with key sequences. Typing a
first key will visually filter the sequences and reduce them. Continue
typing key sequences until you reduce a sequence completely, which will
Expand All @@ -118,6 +119,7 @@ variant of the command that will only run for the current line.
`:HopPatternCurrentLine` *:HopPatternCurrentLine*
`:HopPatternCurrentLineBC` *:HopPatternCurrentLineBC*
`:HopPatternCurrentLineAC` *:HopPatternCurrentLineAC*
`:HopPatternMW` *:HopPatternMW*
Ask the user for a pattern and hint the document with it.

This is akin to calling the |hop.hint_patterns| Lua function
Expand All @@ -129,6 +131,7 @@ variant of the command that will only run for the current line.
`:HopChar1CurrentLine` *:HopChar1CurrentLine*
`:HopChar1CurrentLineBC` *:HopChar1CurrentLineBC*
`:HopChar1CurrentLineAC` *:HopChar1CurrentLineAC*
`:HopChar1MW` *:HopChar1MW*
Type a key and immediately hint the document for this key.

This is akin to calling the |hop.hint_char1| Lua Function
Expand All @@ -139,6 +142,7 @@ variant of the command that will only run for the current line.
`:HopChar2CurrentLine` *:HopChar2CurrentLine*
`:HopChar2CurrentLineBC` *:HopChar2CurrentLineBC*
`:HopChar2CurrentLineAC` *:HopChar2CurrentLineAC*
`:HopChar2MW` *:HopChar2MW*
Type two keys and immediately hint the document for this bigram.

This is akin to calling the |hop.hint_char2| Lua Function
Expand All @@ -149,6 +153,7 @@ variant of the command that will only run for the current line.
`:HopLineCurrentLine` *:HopLineCurrentLine*
`:HopLineCurrentLineBC` *:HopLineCurrentLineBC*
`:HopLineCurrentLineAC` *:HopLineCurrentLineAC*
`:HopLineMW` *:HopLineMW*
Jump to the beginning of the line of your choice inside your buffer.

This is akin to calling the |hop.hint_lines| Lua function.
Expand All @@ -159,6 +164,7 @@ variant of the command that will only run for the current line.
`:HopLineStartCurrentLine` *:HopLineStartCurrentLine*
`:HopLineStartCurrentLineBC` *:HopLineStartCurrentLineBC*
`:HopLineStartCurrentLineAC` *:HopLineStartCurrentLineAC*
`:HopLineStartMW` *:HopLineStartMW*
Like `HopLine` but skips leading whitespace on every line.
Blank lines are skipped over.

Expand Down
1 change: 1 addition & 0 deletions lua/hop/defaults.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ M.create_hl_autocmd = true
M.current_line_only = false
M.inclusive_jump = false
M.uppercase_labels = false
M.multi_windows = false

return M
40 changes: 27 additions & 13 deletions lua/hop/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ function M.move_cursor_to(w, line, column, inclusive)

-- update the jump list
vim.cmd("normal! m'")
vim.api.nvim_set_current_win(w)
vim.api.nvim_win_set_cursor(w, { line, column})
end

Expand All @@ -141,8 +142,7 @@ function M.hint_with_callback(jump_target_gtr, opts, callback)
return
end

local context = window.get_window_context()
window.clip_window_context(context, opts.direction)
local all_ctxs = window.get_window_context(opts.multi_windows)

-- create the highlight groups; the highlight groups will allow us to clean everything at once when Hop quits
local hl_ns = vim.api.nvim_create_namespace('hop_hl')
Expand Down Expand Up @@ -174,12 +174,18 @@ function M.hint_with_callback(jump_target_gtr, opts, callback)
hints = hints,
hl_ns = hl_ns,
dim_ns = dim_ns,
top_line = context.top_line,
bot_line = context.bot_line,
}

-- dim everything out, add the virtual cursor and hide diagnostics
apply_dimming(0, dim_ns, context.top_line, context.bot_line, context.cursor_pos, opts.direction, opts.current_line_only)
local buf_list = {}
for _, bctx in ipairs(all_ctxs) do
buf_list[#buf_list + 1] = bctx.hbuf
for _, wctx in ipairs(bctx) do
window.clip_window_context(wctx, opts.direction)
-- dim everything out, add the virtual cursor and hide diagnostics
apply_dimming(bctx.hbuf, dim_ns, wctx.top_line, wctx.bot_line, wctx.cursor_pos, opts.direction, opts.current_line_only)
end
end

add_virt_cur(hl_ns)
if vim.fn.has("nvim-0.6") == 1 then
hint_state.diag_ns = vim.diagnostic.get_namespaces()
Expand All @@ -191,7 +197,9 @@ function M.hint_with_callback(jump_target_gtr, opts, callback)
while h == nil do
local ok, key = pcall(vim.fn.getchar)
if not ok then
M.quit(0, hint_state)
for _, buf in ipairs(buf_list) do
M.quit(buf, hint_state)
end
break
end
local not_special_key = true
Expand All @@ -209,11 +217,13 @@ function M.hint_with_callback(jump_target_gtr, opts, callback)

if not_special_key and opts.keys:find(key, 1, true) then
-- If this is a key used in Hop (via opts.keys), deal with it in Hop
h = M.refine_hints(0, key, hint_state, callback, opts)
h = M.refine_hints(buf_list, key, hint_state, callback, opts)
vim.cmd('redraw')
else
-- If it's not, quit Hop
M.quit(0, hint_state)

for _, buf in ipairs(buf_list) do
M.quit(buf, hint_state)
end
-- If the key captured via getchar() is not the quit_key, pass it through
-- to nvim to be handled normally (including mappings)
if key ~= vim.api.nvim_replace_termcodes(opts.quit_key, true, false, true) then
Expand All @@ -228,7 +238,7 @@ end
--
-- Refining hints allows to advance the state machine by one step. If a terminal step is reached, this function jumps to
-- the location. Otherwise, it stores the new state machine.
function M.refine_hints(buf_handle, key, hint_state, callback, opts)
function M.refine_hints(buf_list, key, hint_state, callback, opts)
local h, hints = hint.reduce_hints(hint_state.hints, key)

if h == nil then
Expand All @@ -239,11 +249,15 @@ function M.refine_hints(buf_handle, key, hint_state, callback, opts)

hint_state.hints = hints

clear_namespace(buf_handle, hint_state.hl_ns)
for _, buf in ipairs(buf_list) do
clear_namespace(buf, hint_state.hl_ns)
end
hint.set_hint_extmarks(hint_state.hl_ns, hints, opts)
vim.cmd('redraw')
else
M.quit(buf_handle, hint_state)
for _, buf in ipairs(buf_list) do
M.quit(buf, hint_state)
end

-- prior to jump, register the current position into the jump list
vim.cmd("normal! m'")
Expand Down
Loading

0 comments on commit f37e36d

Please sign in to comment.