1
0
Fork 0
mirror of git://git.code.sf.net/p/zsh/code synced 2025-09-08 12:01:21 +02:00
zsh/Functions/Zle/replace-argument
Peter Stephenson 4e54648add 32866: new replace-argument ZLE function.
Also a couple of read-from-minibuffer fixes: don't pass numeric
argument to recursive edit, and hide the minibuffer edit from
the undo history.
2014-07-17 09:45:46 +01:00

46 lines
1 KiB
Text

# Replace an argument to a command, delimited by normal shell syntax.
# Prompts for the replacement.
# With no numeric argument, replace the current argument.
# With a numeric argument, replace that argument: 0 = command word,
# as in history expansion.
# If editing buffer is empty, use previous history line.
autoload -Uz split-shell-arguments read-from-minibuffer
if (( ${#BUFFER} == 0 )); then
(( HISTNO-- ))
CURSOR=${#BUFFER}
fi
local widget=$WIDGET
integer numeric cursor=CURSOR
if (( ${+NUMERIC} )); then
numeric=$NUMERIC
else
numeric=-1
fi
local reply REPLY REPLY2
integer index
split-shell-arguments
if (( numeric >= 0 )); then
index=$(( 2 + 2*numeric ))
else
index=$((REPLY & ~1 ))
fi
local edit
if [[ $widget = *edit* ]]; then
edit=$reply[$index]
fi
read-from-minibuffer "Replace $reply[$index] with: " $edit || return 1
integer diff=$(( ${#REPLY} - ${#reply[$index]} ))
reply[$index]=$REPLY
BUFFER=${(j..)reply}
if (( cursor > REPLY2 )); then
(( CURSOR = cursor + diff ))
else
(( CURSOR = REPLY2 ))
fi