From 4e54648add79f7cb0c0fe81e46f49817d4555f2a Mon Sep 17 00:00:00 2001 From: Peter Stephenson Date: Thu, 17 Jul 2014 09:45:46 +0100 Subject: 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. --- Functions/Zle/replace-argument | 46 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 Functions/Zle/replace-argument (limited to 'Functions/Zle/replace-argument') diff --git a/Functions/Zle/replace-argument b/Functions/Zle/replace-argument new file mode 100644 index 000000000..b43fc39bb --- /dev/null +++ b/Functions/Zle/replace-argument @@ -0,0 +1,46 @@ +# 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 -- cgit 1.4.1