summaryrefslogtreecommitdiff
path: root/Functions/Zle/replace-argument
diff options
context:
space:
mode:
authorAxel Beckert <abe@deuxchevaux.org>2014-08-11 17:39:42 +0200
committerAxel Beckert <abe@deuxchevaux.org>2014-08-11 17:39:42 +0200
commit382c26acfc62a56744ab9eb603efc56130764fd3 (patch)
tree1b7cb9d3e372481007fc4546e7d06ce5bb1bc104 /Functions/Zle/replace-argument
parent73508e345b4925f33b7f652aba9bd313169e5ac2 (diff)
parentf8ae47f29b766dc0330b19d7fdb35859d6aab930 (diff)
downloadzsh-382c26acfc62a56744ab9eb603efc56130764fd3.tar.gz
zsh-382c26acfc62a56744ab9eb603efc56130764fd3.zip
New upstream release candidate: Merge branch 'upstream' into debian
Diffstat (limited to 'Functions/Zle/replace-argument')
-rw-r--r--Functions/Zle/replace-argument48
1 files changed, 48 insertions, 0 deletions
diff --git a/Functions/Zle/replace-argument b/Functions/Zle/replace-argument
new file mode 100644
index 000000000..0ef3de7b5
--- /dev/null
+++ b/Functions/Zle/replace-argument
@@ -0,0 +1,48 @@
+# 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 numeric
+integer cursor=CURSOR
+if (( ${+NUMERIC} )); then
+ numeric=$NUMERIC
+fi
+local reply REPLY REPLY2
+integer index
+split-shell-arguments
+
+if [[ -n $numeric ]]; then
+ if (( numeric < 0 )); then
+ (( index = ${#reply} - 1 + 2*(numeric+1) ))
+ else
+ (( index = 2 + 2*numeric ))
+ fi
+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