about summary refs log tree commit diff
path: root/Functions
diff options
context:
space:
mode:
authorPeter Stephenson <pws@users.sourceforge.net>2010-01-18 12:47:09 +0000
committerPeter Stephenson <pws@users.sourceforge.net>2010-01-18 12:47:09 +0000
commitc508c6e368ac7faef55a16d9c85a2fbdd21232fc (patch)
treec46f72ce11b8485f052698b94485bc95bfae77f3 /Functions
parent8890e6e19ffa0bede585527671987972137009c7 (diff)
downloadzsh-c508c6e368ac7faef55a16d9c85a2fbdd21232fc.tar.gz
zsh-c508c6e368ac7faef55a16d9c85a2fbdd21232fc.tar.xz
zsh-c508c6e368ac7faef55a16d9c85a2fbdd21232fc.zip
27604: substitution using regular expressions
Diffstat (limited to 'Functions')
-rw-r--r--Functions/Misc/.distfiles1
-rw-r--r--Functions/Misc/regexp-replace35
-rw-r--r--Functions/Zle/replace-string-again12
3 files changed, 45 insertions, 3 deletions
diff --git a/Functions/Misc/.distfiles b/Functions/Misc/.distfiles
index b39d09130..d340d93d7 100644
--- a/Functions/Misc/.distfiles
+++ b/Functions/Misc/.distfiles
@@ -10,6 +10,7 @@ is-at-least
 mere
 nslookup
 promptnl
+regexp-replace
 relative
 run-help
 run-help-git
diff --git a/Functions/Misc/regexp-replace b/Functions/Misc/regexp-replace
new file mode 100644
index 000000000..5aaedafba
--- /dev/null
+++ b/Functions/Misc/regexp-replace
@@ -0,0 +1,35 @@
+# Replace all occurrences of a regular expression in a variable.  The
+# variable is modified directly.  Respects the setting of the
+# option RE_MATCH_PCRE.
+#
+# First argument: *name* (not contents) of variable.
+# Second argument: regular expression
+# Third argument: replacement string.  This can contain all forms of
+# $ and backtick substitutions; in particular, $MATCH will be replaced
+# by the portion of the string matched by the regular expression.
+
+integer pcre
+
+[[ -o re_match_pcre ]] && pcre=1
+
+emulate -L zsh
+(( pcre )) && setopt re_match_pcre
+
+# $4 is the string to be matched
+4=${(P)1}
+# $5 is the final string
+5=
+local MATCH MBEGIN MEND
+local -a match mbegin mend
+
+while [[ -n $4 ]]; do
+  if [[ $4 =~ $2 ]]; then
+    5+=${4[1,MBEGIN-1]}${(e)3}
+    4=${4[MEND+1,-1]}
+  else
+    break
+  fi
+done
+5+=$4
+
+eval ${1}=${(q)5}
diff --git a/Functions/Zle/replace-string-again b/Functions/Zle/replace-string-again
index 3d3486437..8f4d23854 100644
--- a/Functions/Zle/replace-string-again
+++ b/Functions/Zle/replace-string-again
@@ -13,7 +13,7 @@ if [[ -z $_replace_string_src ]]; then
   return 1
 fi
 
-if [[ $curwidget = *pattern* ]]; then
+if [[ $curwidget = *(pattern|regex)* ]]; then
     local rep2
     # The following horror is so that an & preceded by an even
     # number of backslashes is active, without stripping backslashes,
@@ -38,8 +38,14 @@ if [[ $curwidget = *pattern* ]]; then
 	rep=${match[5]}
     done
     rep2+=$rep
-    LBUFFER=${LBUFFER//(#bm)$~_replace_string_src/${(e)rep2}}
-    RBUFFER=${RBUFFER//(#bm)$~_replace_string_src/${(e)rep2}}
+    if [[ $curwidget = *regex* ]]; then
+      autoload -U regexp-replace
+      regexp-replace LBUFFER $_replace_string_src $rep2
+      regexp-replace RBUFFER $_replace_string_src $rep2
+    else
+      LBUFFER=${LBUFFER//(#bm)$~_replace_string_src/${(e)rep2}}
+      RBUFFER=${RBUFFER//(#bm)$~_replace_string_src/${(e)rep2}}
+    fi
 else
     LBUFFER=${LBUFFER//$_replace_string_src/$_replace_string_rep}
     RBUFFER=${RBUFFER//$_replace_string_src/$_replace_string_rep}