feat(n³): file selector

This commit is contained in:
Michel 2024-01-22 10:15:07 +01:00
parent 0756bd5272
commit d45e45e011
1 changed files with 37 additions and 5 deletions

42
.zshrc
View File

@ -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