.
This commit is contained in:
parent
a41c40d911
commit
624db5d7bf
8 changed files with 133 additions and 81 deletions
1
files/.config/vis/plugins/vis-sneak/.gitignore
vendored
Normal file
1
files/.config/vis/plugins/vis-sneak/.gitignore
vendored
Normal file
|
|
@ -0,0 +1 @@
|
|||
.vscode
|
||||
7
files/.config/vis/plugins/vis-sneak/.luarc.json
Normal file
7
files/.config/vis/plugins/vis-sneak/.luarc.json
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
{
|
||||
"Lua": {
|
||||
"diagnostics": {
|
||||
"globals": ["vis"]
|
||||
}
|
||||
}
|
||||
}
|
||||
21
files/.config/vis/plugins/vis-sneak/LICENSE
Normal file
21
files/.config/vis/plugins/vis-sneak/LICENSE
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
MIT License
|
||||
|
||||
Copyright (c) 2020 Erlend Lind Madsen
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
20
files/.config/vis/plugins/vis-sneak/README.md
Normal file
20
files/.config/vis/plugins/vis-sneak/README.md
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
# vis-sneak
|
||||
|
||||
Jump to a location specified by two characters.
|
||||
|
||||
A minimal motion [plugin](https://github.com/martanne/vis/wiki/Plugins) for [vis](https://github.com/martanne/vis) inspired by [vim-sneak](https://github.com/justinmk/vim-sneak).
|
||||
|
||||
## Usage
|
||||
|
||||
Type `s{char}{char}` to move to the next char combo.
|
||||
|
||||
Type `S{char}{char}` to move back.
|
||||
|
||||
Matches are highlighted until you move outside the matches.
|
||||
|
||||
Type `n` or `N` to find the next or prev match, or append a number to the motion.
|
||||
|
||||
|
||||
## Install
|
||||
|
||||
Download and require manually, or add `erf/vis-sneak` using [vis-plug](https://github.com/erf/vis-plug).
|
||||
83
files/.config/vis/plugins/vis-sneak/init.lua
Normal file
83
files/.config/vis/plugins/vis-sneak/init.lua
Normal file
|
|
@ -0,0 +1,83 @@
|
|||
local pattern = nil
|
||||
local matches = {}
|
||||
|
||||
-- iterater for doing find in pattern until nil
|
||||
local pattern_iterator = function(content, pattern)
|
||||
local offset = 1
|
||||
return function()
|
||||
local starts, ends = string.find(content, pattern, offset)
|
||||
if starts == nil then return nil end
|
||||
offset = ends + 1
|
||||
return starts, ends
|
||||
end
|
||||
end
|
||||
|
||||
-- is cursor pos is in one of the matches?
|
||||
local cursor_on_match = function(win)
|
||||
local pos = win.selection.pos
|
||||
for _, range in ipairs(matches) do
|
||||
if pos >= range.start and pos <= range.finish then
|
||||
return true
|
||||
end
|
||||
end
|
||||
return false
|
||||
end
|
||||
|
||||
-- check if range is in viewport range
|
||||
local range_in_viewport = function(viewport, range)
|
||||
return range.start >= viewport.bytes.start and range.finish <= viewport.bytes.finish
|
||||
end
|
||||
|
||||
-- highlihght current matches
|
||||
local highlight = function(win)
|
||||
|
||||
-- clear matches if cursor is not on a match
|
||||
if not cursor_on_match(win) then
|
||||
matches = {}
|
||||
return
|
||||
end
|
||||
|
||||
-- style matches in viewport
|
||||
for _, range in ipairs(matches) do
|
||||
if range_in_viewport(win.viewport, range) then
|
||||
win:style(win.STYLE_CURSOR, range.start, range.finish)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- collect matches (ranges) for pattern in file
|
||||
local collect_matches = function()
|
||||
local file = vis.win.file
|
||||
local content = file:content(0, file.size)
|
||||
matches = {}
|
||||
for starts, ends in pattern_iterator(content, pattern) do
|
||||
table.insert(matches, { start = starts - 1, finish = ends - 1 })
|
||||
end
|
||||
end
|
||||
|
||||
-- highlight matches on WIN_HIGHLIGHT
|
||||
vis.events.subscribe(vis.events.WIN_HIGHLIGHT, function(win)
|
||||
highlight(win)
|
||||
end)
|
||||
|
||||
-- create search for two chars and collect matches for highlighting
|
||||
local sneak = function(keys, search_char)
|
||||
if #keys < 2 then
|
||||
pattern = nil
|
||||
return -1
|
||||
end
|
||||
vis:feedkeys(search_char .. keys .. '<Enter>')
|
||||
pattern = keys
|
||||
collect_matches()
|
||||
return 2
|
||||
end
|
||||
|
||||
-- sneak forward on 's'
|
||||
vis:map(vis.modes.NORMAL, 's', function(keys)
|
||||
return sneak(keys, '/')
|
||||
end)
|
||||
|
||||
-- sneak backwards on 'S'
|
||||
vis:map(vis.modes.NORMAL, 'S', function(keys)
|
||||
return sneak(keys, '?')
|
||||
end)
|
||||
Loading…
Add table
Add a link
Reference in a new issue