about summary refs log tree commit diff
path: root/Functions
diff options
context:
space:
mode:
authorPeter Stephenson <pws@zsh.org>2014-07-17 09:45:46 +0100
committerPeter Stephenson <pws@zsh.org>2014-07-17 09:45:46 +0100
commit4e54648add79f7cb0c0fe81e46f49817d4555f2a (patch)
tree669c9f8b699ff260666265387b5923215a848dc3 /Functions
parent7d5db0d27785c61b8056c405a8ae31ec3a4eb01d (diff)
downloadzsh-4e54648add79f7cb0c0fe81e46f49817d4555f2a.tar.gz
zsh-4e54648add79f7cb0c0fe81e46f49817d4555f2a.tar.xz
zsh-4e54648add79f7cb0c0fe81e46f49817d4555f2a.zip
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.
Diffstat (limited to 'Functions')
-rw-r--r--Functions/Zle/read-from-minibuffer9
-rw-r--r--Functions/Zle/replace-argument46
2 files changed, 55 insertions, 0 deletions
diff --git a/Functions/Zle/read-from-minibuffer b/Functions/Zle/read-from-minibuffer
index 57e926884..8fec1105e 100644
--- a/Functions/Zle/read-from-minibuffer
+++ b/Functions/Zle/read-from-minibuffer
@@ -20,7 +20,9 @@ done
 (( OPTIND > 1 )) && shift $(( OPTIND - 1 ))
 
 local readprompt="$1" lbuf_init="$2" rbuf_init="$3"
+integer changeno=$UNDO_CHANGE_NO
 
+{
 # Use anonymous function to make sure special values get restored,
 # even if this function is called as a widget.
 # local +h ensures special parameters stay special.
@@ -39,10 +41,17 @@ local readprompt="$1" lbuf_init="$2" rbuf_init="$3"
     read -k $keys
     stat=$?
   else
+    local NUMERIC
+    unset NUMERIC
     zle recursive-edit -K main
     stat=$?
     (( stat )) || REPLY=$BUFFER
   fi
 }
+} always {
+  # This removes the edits relating to the read from the undo history.
+  # These aren't useful once we get back to the main editing buffer.
+  zle undo $changeno
+}
 
 return $stat
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