feat(n³): file selector
This commit is contained in:
parent
0756bd5272
commit
d45e45e011
42
.zshrc
42
.zshrc
@ -267,11 +267,43 @@ n()
|
||||
}
|
||||
|
||||
nnn-file-widget() {
|
||||
local item
|
||||
nnn -p- <"$TTY" |
|
||||
while read item; do
|
||||
LBUFFER+="${(q-)item} "
|
||||
done
|
||||
# Extract "shell word" at cursor position (respecting quotes)
|
||||
local _cursor=$CURSOR
|
||||
zle .select-a-shell-word -N
|
||||
|
||||
# Helper
|
||||
nnn-pick()
|
||||
{
|
||||
IFS=$'\n'
|
||||
local items=( $(command nnn -p- $1 < "$TTY") )
|
||||
if (( #items > 0 )); then
|
||||
items=( ${(q-)items} )
|
||||
print -n "${(j: :)items} "
|
||||
fi
|
||||
}
|
||||
|
||||
# Adapt behaviour to buffer content
|
||||
if (($CURSOR < _cursor)); then
|
||||
# Nothing at cursor, restore state before calling nnn
|
||||
CURSOR=${_cursor}
|
||||
REGION_ACTIVE=0
|
||||
zle -U "$(nnn-pick)"
|
||||
else
|
||||
# Provide content under cursor to nnn
|
||||
zle .kill-region
|
||||
local result="$(nnn-pick ${CUTBUFFER## })"
|
||||
if (( #result == 0 )); then
|
||||
# Nothing selected, restore content
|
||||
zle .yank
|
||||
else
|
||||
# Replace content with what nnn returned
|
||||
zle -U " $result"
|
||||
fi
|
||||
fi
|
||||
|
||||
# TODO single-op undo
|
||||
# TODO Handle vicmd properly
|
||||
zle .redisplay
|
||||
}
|
||||
zle -N nnn-file-widget
|
||||
bindkey -M vicmd '\en' nnn-file-widget
|
||||
|
Loading…
Reference in New Issue
Block a user