about summary refs log tree commit diff
path: root/Functions
diff options
context:
space:
mode:
authorPeter Stephenson <pws@users.sourceforge.net>2003-02-03 11:05:53 +0000
committerPeter Stephenson <pws@users.sourceforge.net>2003-02-03 11:05:53 +0000
commit06902e7f66be368975ca4c58607191cf36a68781 (patch)
tree92287c5ffd8957c4568073cc8cfcb739012d5dae /Functions
parent980b437fcac06d47b37d36e7bbde0150f7d19ad2 (diff)
downloadzsh-06902e7f66be368975ca4c58607191cf36a68781.tar.gz
zsh-06902e7f66be368975ca4c58607191cf36a68781.tar.xz
zsh-06902e7f66be368975ca4c58607191cf36a68781.zip
18174: New read-from-minibuffer and replace-string ZLE functions
Diffstat (limited to 'Functions')
-rw-r--r--Functions/Zle/read-from-minibuffer20
-rw-r--r--Functions/Zle/replace-string45
2 files changed, 65 insertions, 0 deletions
diff --git a/Functions/Zle/read-from-minibuffer b/Functions/Zle/read-from-minibuffer
new file mode 100644
index 000000000..93eec42a5
--- /dev/null
+++ b/Functions/Zle/read-from-minibuffer
@@ -0,0 +1,20 @@
+local savelbuffer=$LBUFFER saverbuffer=$RBUFFER
+local savepredisplay=$PREDISPLAY savepostdisplay=$POSTDISPLAY
+
+LBUFFER=
+RBUFFER=
+PREDISPLAY="$PREDISPLAY$savelbuffer$saverbuffer$POSTDISPLAY
+${1:-? }"
+POSTDISPLAY=
+
+zle recursive-edit
+integer stat=$?
+
+(( stat )) || REPLY=$BUFFER
+
+LBUFFER=$savelbuffer
+RBUFFER=$saverbuffer
+PREDISPLAY=$savepredisplay
+POSTDISPLAY=$savepostdisplay
+
+return $stat
diff --git a/Functions/Zle/replace-string b/Functions/Zle/replace-string
new file mode 100644
index 000000000..2fe0da901
--- /dev/null
+++ b/Functions/Zle/replace-string
@@ -0,0 +1,45 @@
+emulate -L zsh
+setopt extendedglob
+
+autoload read-from-minibuffer
+
+local p1="Replace: " p2="   with: "
+local src rep REPLY MATCH MBEGIN MEND curwidget=$WIDGET
+local -a match mbegin mend
+
+read-from-minibuffer $p1 || return 1
+src=$REPLY
+
+read-from-minibuffer "$p1$src$p2" || return 1
+rep=$REPLY
+
+if [[ $curwidget = *pattern* ]]; then
+    local rep2
+    # The following horror is so that an & preceded by an even
+    # number of backslashes is active, without stripping backslashes,
+    # while preceded by an odd number of backslashes is inactive,
+    # with one backslash being stripped.  A similar logic applies
+    # to \digit.
+    while [[ $rep = (#b)([^\\]#)(\\\\)#(\\|)(\&|\\<->|\\\{<->\})(*) ]]; do
+	if [[ -n $match[3] ]]; then
+	    # Expression is quoted, strip quotes
+	    rep2="${match[1]}${match[2]}${match[4]}"
+	else
+	    rep2+="${match[1]}${match[2]}"
+	    if [[ $match[4] = \& ]]; then
+		rep2+='${MATCH}'
+	    elif [[ $match[4] = \\\{* ]]; then
+		rep2+='${match['${match[4][3,-2]}']}'
+	    else
+		rep2+='${match['${match[4][2,-1]}']}'
+	    fi
+	fi
+	rep=${match[5]}
+    done
+    rep2+=$rep
+    LBUFFER=${LBUFFER//(#bm)$~src/${(e)rep2}}
+    RBUFFER=${RBUFFER//(#bm)$~src/${(e)rep2}}
+else
+    LBUFFER=${LBUFFER//$src/$rep}
+    RBUFFER=${RBUFFER//$src/$rep}
+fi