mirror of
git://git.code.sf.net/p/zsh/code
synced 2025-10-13 11:21:13 +02:00
22854: replace-string-again
This commit is contained in:
parent
d4d6cb5494
commit
966a330649
4 changed files with 63 additions and 35 deletions
|
@ -1,5 +1,9 @@
|
||||||
2006-10-10 Peter Stephenson <pws@csr.com>
|
2006-10-10 Peter Stephenson <pws@csr.com>
|
||||||
|
|
||||||
|
* 22854: Doc/Zsh/contrib.yo, Functions/Zle/replace-string,
|
||||||
|
Functions/Zle/replace-string-again: separate out back end
|
||||||
|
as separate widget to repeat replacement.
|
||||||
|
|
||||||
* 22853: Src/utils.c: unquoted ^ at end of bindkey string
|
* 22853: Src/utils.c: unquoted ^ at end of bindkey string
|
||||||
treated literally.
|
treated literally.
|
||||||
|
|
||||||
|
|
|
@ -1055,8 +1055,10 @@ not used. Hence it is still possible to call tt(executed-named-cmd) and
|
||||||
similar functions while reading a value.
|
similar functions while reading a value.
|
||||||
)
|
)
|
||||||
tindex(replace-string)
|
tindex(replace-string)
|
||||||
|
tindex(replace-string-again)
|
||||||
tindex(replace-pattern)
|
tindex(replace-pattern)
|
||||||
item(tt(replace-string), tt(replace-pattern))(
|
xitem(tt(replace-string), tt(replace-pattern))
|
||||||
|
item(tt(replace-string-again), tt(replace-pattern-again))(
|
||||||
The function tt(replace-string) implements two widgets.
|
The function tt(replace-string) implements two widgets.
|
||||||
If defined under the same name as the function, it prompts for two
|
If defined under the same name as the function, it prompts for two
|
||||||
strings; the first (source) string will be replaced by the second
|
strings; the first (source) string will be replaced by the second
|
||||||
|
@ -1082,6 +1084,13 @@ tt(:zle:replace-string)) to tt(true). In addition, a positive
|
||||||
numeric argument forces the previous values to be offered, a negative or
|
numeric argument forces the previous values to be offered, a negative or
|
||||||
zero argument forces them not to be.
|
zero argument forces them not to be.
|
||||||
|
|
||||||
|
The function tt(replace-string-again) can be used to repeat the
|
||||||
|
previous replacement; no prompting is done. As with tt(replace-string), if
|
||||||
|
the name of the widget contains the word `tt(pattern)', pattern matching
|
||||||
|
is performed, else a literal string replacement. Note that the
|
||||||
|
previous source and replacement text are the same whether pattern or string
|
||||||
|
matching is used.
|
||||||
|
|
||||||
For example, starting from the line:
|
For example, starting from the line:
|
||||||
|
|
||||||
example(print This line contains fan and fond)
|
example(print This line contains fan and fond)
|
||||||
|
|
|
@ -1,11 +1,11 @@
|
||||||
emulate -L zsh
|
emulate -L zsh
|
||||||
setopt extendedglob
|
setopt extendedglob
|
||||||
|
|
||||||
autoload read-from-minibuffer
|
autoload -U read-from-minibuffer replace-string-again
|
||||||
|
|
||||||
local p1="Replace: " p2=" with: "
|
local p1="Replace: " p2=" with: "
|
||||||
local REPLY MATCH MBEGIN MEND curwidget=$WIDGET previous
|
# Saving curwidget is necessary to avoid the widget name being overwritten.
|
||||||
local -a match mbegin mend
|
local REPLY previous curwidget=$WIDGET
|
||||||
|
|
||||||
if (( ${+NUMERIC} )); then
|
if (( ${+NUMERIC} )); then
|
||||||
(( $NUMERIC > 0 )) && previous=1
|
(( $NUMERIC > 0 )) && previous=1
|
||||||
|
@ -20,34 +20,4 @@ read-from-minibuffer "$p1$_replace_string_src$p2" \
|
||||||
${previous:+$_replace_string_rep} || return 1
|
${previous:+$_replace_string_rep} || return 1
|
||||||
_replace_string_rep=$REPLY
|
_replace_string_rep=$REPLY
|
||||||
|
|
||||||
if [[ $curwidget = *pattern* ]]; then
|
replace-string-again $curwidget
|
||||||
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.
|
|
||||||
local rep=$_replace_string_rep
|
|
||||||
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)$~_replace_string_src/${(e)rep2}}
|
|
||||||
RBUFFER=${RBUFFER//(#bm)$~_replace_string_src/${(e)rep2}}
|
|
||||||
else
|
|
||||||
LBUFFER=${LBUFFER//$_replace_string_src/$_replace_string_rep}
|
|
||||||
RBUFFER=${RBUFFER//$_replace_string_src/$_replace_string_rep}
|
|
||||||
fi
|
|
||||||
|
|
45
Functions/Zle/replace-string-again
Normal file
45
Functions/Zle/replace-string-again
Normal file
|
@ -0,0 +1,45 @@
|
||||||
|
# Back end for replace-string; can be called as a widget to repeat
|
||||||
|
# the previous replacement. _replace_string_src and _replace_string_rep
|
||||||
|
# are global.
|
||||||
|
|
||||||
|
# When called from replace-string, we need to use the widget
|
||||||
|
# name passed to decide whether to do pattern matching: the widget
|
||||||
|
# may since have been overwritten.
|
||||||
|
local MATCH MBEGIN MEND curwidget=${1:-$WIDGET}
|
||||||
|
local -a match mbegin mend
|
||||||
|
|
||||||
|
if [[ -z $_replace_string_src ]]; then
|
||||||
|
zle -M No string to replace.
|
||||||
|
fi
|
||||||
|
|
||||||
|
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.
|
||||||
|
local rep=$_replace_string_rep
|
||||||
|
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)$~_replace_string_src/${(e)rep2}}
|
||||||
|
RBUFFER=${RBUFFER//(#bm)$~_replace_string_src/${(e)rep2}}
|
||||||
|
else
|
||||||
|
LBUFFER=${LBUFFER//$_replace_string_src/$_replace_string_rep}
|
||||||
|
RBUFFER=${RBUFFER//$_replace_string_src/$_replace_string_rep}
|
||||||
|
fi
|
Loading…
Reference in a new issue