1
0
Fork 0
mirror of git://git.code.sf.net/p/zsh/code synced 2025-10-24 04:50:27 +02:00

24990: Stephane Chazelas: fix various little quirks in zmv

This commit is contained in:
Peter Stephenson 2008-05-11 11:23:30 +00:00
parent d2b0a6daa5
commit 001e8ca70e
2 changed files with 35 additions and 29 deletions

View file

@ -1,3 +1,8 @@
2008-05-11 Peter Stephenson <p.w.stephenson@ntlworld.com>
* 24990: Stephane Chazelas: Functions/Misc/zmv: fix
various little quirks in zmv.
2008-05-09 Peter Stephenson <pws@csr.com> 2008-05-09 Peter Stephenson <pws@csr.com>
* 24986: Doc/Zsh/prompt.yo, Doc/Zsh/zle.yo, Src/init.c, * 24986: Doc/Zsh/prompt.yo, Doc/Zsh/zle.yo, Src/init.c,

View file

@ -128,12 +128,14 @@ local pat repl errstr fpat hasglobqual opat
typeset -A from to typeset -A from to
integer stat integer stat
myname=${(%):-%N}
while getopts ":o:p:MCLfinqQsvwW" opt; do while getopts ":o:p:MCLfinqQsvwW" opt; do
if [[ $opt = "?" ]]; then if [[ $opt = "?" ]]; then
print -P "%N: unrecognized option: -$OPTARG" >&2 print -r -- "$myname: unrecognized option: -$OPTARG" >&2
return 1 return 1
fi fi
eval "opt_$opt=${(q)OPTARG:--$opt}" eval "opt_$opt=\${OPTARG:--\$opt}"
done done
(( OPTIND > 1 )) && shift $(( OPTIND - 1 )) (( OPTIND > 1 )) && shift $(( OPTIND - 1 ))
@ -143,6 +145,15 @@ done
[[ -n $opt_L ]] && action=ln [[ -n $opt_L ]] && action=ln
[[ -n $opt_p ]] && action=$opt_p [[ -n $opt_p ]] && action=$opt_p
if [[ -z $action ]]; then
action=$myname[-2,-1]
if [[ $action != (cp|mv|ln) ]]; then
print -r "$myname: action $action not recognised: must be cp, mv or ln." >&2
return 1
fi
fi
if (( $# != 2 )); then if (( $# != 2 )); then
print -P "Usage: print -P "Usage:
%N [OPTIONS] oldpattern newpattern %N [OPTIONS] oldpattern newpattern
@ -164,25 +175,8 @@ pat=$1
repl=$2 repl=$2
shift 2 shift 2
if [[ -z $action ]]; then
# We can't necessarily get the name of the function directly, because
# of no_function_argzero stupidity.
tmpf=${TMPPREFIX}zmv$$
print -P %N >$tmpf
myname=$(<$tmpf)
rm -f $tmpf
action=$myname[-2,-1]
if [[ $action != (cp|mv|ln) ]]; then
print "Action $action not recognised: must be cp, mv or ln." >&2
return 1
fi
fi
if [[ -n $opt_s && $action != ln ]]; then if [[ -n $opt_s && $action != ln ]]; then
print -P "%N: invalid option: -s" >&2 print -r -- "$myname: invalid option: -s" >&2
return 1 return 1
fi fi
@ -193,10 +187,10 @@ if [[ -n $opt_w || -n $opt_W ]]; then
# Well, this seems to work. # Well, this seems to work.
# The tricky bit is getting all forms of [...] correct, but as long # The tricky bit is getting all forms of [...] correct, but as long
# as we require inactive bits to be backslashed its not so bad. # as we require inactive bits to be backslashed its not so bad.
find='(#m)(\*\*#[/]|[*?]|\<[0-9]#-[0-9]#\>|\[(\[:[a-z]##:\]|\\\[|\\\]|[^\[\]]##)##\])\##' find='(#m)((\*\*#/|[*?]|<[0-9]#-[0-9]#>|\[(^|)(\]|)(\[:[a-z]##:\]|\\?|[^\]])##\])\##|?\###)'
tmp="${pat//${~find}/$[++cnt]}" tmp="${pat//${~find}/$[++cnt]}"
if [[ $cnt = 0 ]]; then if [[ $cnt = 0 ]]; then
print -P "%N: warning: no wildcards were found in search pattern" >&2 print -r -- "$myname: warning: no wildcards were found in search pattern" >&2
else else
pat="${pat//${~find}/($MATCH)}" pat="${pat//${~find}/($MATCH)}"
fi fi
@ -252,26 +246,33 @@ for f in $files; do
fi fi
[[ -e $f && $f = (#b)${~pat} ]] || continue [[ -e $f && $f = (#b)${~pat} ]] || continue
set -- "$match[@]" set -- "$match[@]"
g=${(e)repl} { {
g=${(Xe)repl}
} 2> /dev/null } always {
if (( TRY_BLOCK_ERROR )); then
print -r -- "$myname: syntax error in replacement" >&2
return 1
fi
}
if [[ -z $g ]]; then if [[ -z $g ]]; then
errs=($errs "\`$f' expanded to an empty string") errs+=("\`$f' expanded to an empty string")
elif [[ $f = $g ]]; then elif [[ $f = $g ]]; then
# don't cause error: more useful just to skip # don't cause error: more useful just to skip
# errs=($errs "$f not altered by substitution") # errs=($errs "$f not altered by substitution")
[[ -n $opt_v ]] && print "$f not altered, ignored" [[ -n $opt_v ]] && print -r -- "$f not altered, ignored"
continue continue
elif [[ -n $from[$g] && ! -d $g ]]; then elif [[ -n $from[$g] && ! -d $g ]]; then
errs=($errs "$f and $from[$g] both map to $g") errs+=("$f and $from[$g] both map to $g")
elif [[ -f $g && -z $opt_f && ! ($f -ef $g && $action = mv) ]]; then elif [[ -f $g && -z $opt_f && ! ($f -ef $g && $action = mv) ]]; then
errs=($errs "file exists: $g") errs+=("file exists: $g")
fi fi
from[$g]=$f from[$g]=$f
to[$f]=$g to[$f]=$g
done done
if (( $#errs )); then if (( $#errs )); then
print -P "%N: error(s) in substitution:" >&2 print -r -- "$myname: error(s) in substitution:" >&2
print -l $errs >&2 print -lr -- $errs >&2
return 1 return 1
fi fi