Skip to content

Commit

Permalink
improv!: target-windows are expected to be window ID-s now
Browse files Browse the repository at this point in the history
`leap` expects `winid`s instead of `wininfo` tables as elements in
`target-windows` from now on. (This is more convenient for users, no
reason not to do the transformation ourselves.)

Additional refactorings in `get-other-windows-on-tabpage`.
  • Loading branch information
ggandor committed May 21, 2022
1 parent d4f4080 commit bb66dc9
Show file tree
Hide file tree
Showing 3 changed files with 466 additions and 492 deletions.
16 changes: 4 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -232,28 +232,20 @@ leap-custom-keymaps`.
### Search mode tweaks (bidirectional and all-windows search)

For further customization you can call the `leap` function directly. The
`target-windows` argument allows you to pass in a list of `wininfo`
dictionaries (`:h getwininfo()`).
`target-windows` argument allows you to pass in a list of window ID-s (`:h
winid`).

```lua
-- Searching in all windows (including the current one) on the tab page:
local function get_windows_on_tabpage()
local t = {}
local ids = string.gmatch(vim.fn.string(vim.fn.winlayout()), "%d+")
for id in ids do t[#t + 1] = vim.fn.getwininfo(id)[1] end
return t
end
function leap_all_windows()
require('leap').leap { ['target-windows'] = get_windows_on_tabpage() }
require'leap'.leap { ['target-windows'] = vim.api.nvim_tabpage_list_wins(0) }
end

-- Bidirectional search in the current window is just a specific case of the
-- multi-window mode - set `target-windows` to a table containing the current
-- window as the only element:
function leap_bidirectional()
require('leap').leap {
['target-windows'] = { vim.fn.getwininfo(vim.fn.win_getid())[1] }
}
require'leap'.leap { ['target-windows'] = {vim.api.nvim_get_current_win()} }
end

-- Map them to your preferred key, like:
Expand Down
43 changes: 17 additions & 26 deletions fnl/leap.fnl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

(local api vim.api)
(local empty? vim.tbl_isempty)
(local filter vim.tbl_filter)
(local map vim.tbl_map)
(local {: abs : ceil : max : min : pow} math)

Expand Down Expand Up @@ -291,27 +292,16 @@ interrupted change operation."

; Getting targets ///1

; Returns wininfo dicts.
(fn get-other-windows-on-tabpage []
(let [visual-or-OP-mode? (not= (vim.fn.mode) :n)
get-wininfo #(. (vim.fn.getwininfo $) 1)
get-buf api.nvim_win_get_buf
curr-winid (vim.fn.win_getid)
; HACK! The output of vim.fn.winlayout looks sg like:
; ['col', [['leaf', 1002], ['row', [['leaf', 1003], ['leaf', 1001]]], ['leaf', 1000]]]
; Instead of traversing the window tree, we simply extract the id-s from
; the flat string representation.
ids (string.gmatch (vim.fn.string (vim.fn.winlayout)) "%d+")
; TODO: filter on certain window types?
ids (icollect [id ids]
(when-not (or (= (tonumber id) curr-winid)
; Targeting a different buffer doesn't make
; sense in these modes.
(and visual-or-OP-mode?
(not= (get-buf (tonumber id))
(get-buf curr-winid))))
id))]
(map get-wininfo ids)))
(fn get-other-windows-on-tabpage [mode]
(let [visual-or-OP-mode? (not= mode :n)
curr-win (api.nvim_get_current_win)
curr-buf (api.nvim_get_current_buf)
wins (api.nvim_tabpage_list_wins 0)]
; TODO: filter on certain window types?
(filter #(not (or (= $ curr-win)
(and visual-or-OP-mode? ; -> no sense in buffer switching
(not= (api.nvim_win_get_buf $) curr-buf))))
wins)))


(fn get-horizontal-bounds []
Expand Down Expand Up @@ -711,14 +701,15 @@ B: Two labels occupy the same position (this can occur at EOL or window
"Entry point for Leap motions."
(let [{: reverse? : inclusive-op? : offset} (if dot-repeat? state.dot-repeat
kwargs)
?target-windows (match target-windows
[&as t] t
true (get-other-windows-on-tabpage))
source-window (. (vim.fn.getwininfo (vim.fn.win_getid)) 1)
directional? (not ?target-windows)
; We need to save the mode here, because the `:normal` command
; in `jump-to!*` can change the state. Related: vim/vim#9332.
mode (. (api.nvim_get_mode) :mode)
?target-windows (-?>> (match target-windows
[&as t] t
true (get-other-windows-on-tabpage mode))
(map #(. (vim.fn.getwininfo $) 1)))
source-window (. (vim.fn.getwininfo (vim.fn.win_getid)) 1)
directional? (not ?target-windows)
op-mode? (mode:match :o)
change-op? (and op-mode? (= vim.v.operator :c))
dot-repeatable-op? (and op-mode? directional? (not= vim.v.operator :y))
Expand Down
Loading

0 comments on commit bb66dc9

Please sign in to comment.