From: Stephane Chazelas <Stephane_Chazelas@yahoo.fr>
To: Zsh hackers list <zsh-workers@sunsite.dk>
Subject: [PATCH] zmv (Was: b='${a//"/}' and ${(e)b})
Date: Sat, 10 May 2008 22:02:06 +0100 [thread overview]
Message-ID: <20080510210206.GC5560@sc.homeunix.net> (raw)
In-Reply-To: <20080510123843.GA5560@sc.homeunix.net>
[-- Attachment #1: Type: text/plain, Size: 1302 bytes --]
While I was looking at zmv:
I read:
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
We could use:
action=${(%):-%N}
(which looks like a very complicated smiley)
Also, for tempfiles, there's
tmpf==(print -P %N)
Also, I read:
print -P "%N: unrecognized option: -$OPTARG" >&2
If $OPTARG is "%", that fails
Maybe it would make sense to have a %P as a printf format for
prompt expansions:
printf '%P: unrecognized option: -%s\n' %N "$OPTARG"
Of course, the above can be written:
print -nP %N >&2
print -r ": unrecognized option: -$OPTARG" >&2
or
print -r "${(%):-%N}: unrecognized option: -$OPTARG" >&2
though
Also, I've seen some problems with the: $find variable:
$ zmv -wn '[a\\]' '$f'
zmv: warning: no wildcards were found in search pattern
$ zmv -wn 'a#' '$f'
zmv: warning: no wildcards were found in search pattern
Please find a patch attached.
Please test as I'm not really confident that I didn't introduce
a few pairs of half-dozens bugs ;)
--
Stéphane
[-- Attachment #2: diff --]
[-- Type: text/plain, Size: 3471 bytes --]
Index: zmv
===================================================================
RCS file: /cvsroot/zsh/zsh/Functions/Misc/zmv,v
retrieving revision 1.13
diff -u -r1.13 zmv
--- zmv 13 Apr 2008 16:11:54 -0000 1.13
+++ zmv 10 May 2008 20:59:45 -0000
@@ -128,12 +128,14 @@
typeset -A from to
integer stat
+myname=${(%):-%N}
+
while getopts ":o:p:MCLfinqQsvwW" opt; do
if [[ $opt = "?" ]]; then
- print -P "%N: unrecognized option: -$OPTARG" >&2
+ print -r -- "$myname: unrecognized option: -$OPTARG" >&2
return 1
fi
- eval "opt_$opt=${(q)OPTARG:--$opt}"
+ eval "opt_$opt=\${OPTARG:--\$opt}"
done
(( OPTIND > 1 )) && shift $(( OPTIND - 1 ))
@@ -143,6 +145,15 @@
[[ -n $opt_L ]] && action=ln
[[ -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
print -P "Usage:
%N [OPTIONS] oldpattern newpattern
@@ -164,25 +175,8 @@
repl=$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
- print -P "%N: invalid option: -s" >&2
+ print -r -- "$myname: invalid option: -s" >&2
return 1
fi
@@ -193,10 +187,10 @@
# Well, this seems to work.
# The tricky bit is getting all forms of [...] correct, but as long
# 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]}"
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
pat="${pat//${~find}/($MATCH)}"
fi
@@ -252,26 +246,33 @@
fi
[[ -e $f && $f = (#b)${~pat} ]] || continue
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
- errs=($errs "\`$f' expanded to an empty string")
+ errs+=("\`$f' expanded to an empty string")
elif [[ $f = $g ]]; then
# don't cause error: more useful just to skip
# 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
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
- errs=($errs "file exists: $g")
+ errs+=("file exists: $g")
fi
from[$g]=$f
to[$f]=$g
done
if (( $#errs )); then
- print -P "%N: error(s) in substitution:" >&2
- print -l $errs >&2
+ print -r -- "$myname: error(s) in substitution:" >&2
+ print -lr -- $errs >&2
return 1
fi
prev parent reply other threads:[~2008-05-10 21:02 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2008-05-10 12:38 b='${a//"/}' and ${(e)b} Stephane Chazelas
2008-05-10 17:12 ` Bart Schaefer
2008-05-11 16:10 ` Stephane Chazelas
2008-05-10 21:02 ` Stephane Chazelas [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20080510210206.GC5560@sc.homeunix.net \
--to=stephane_chazelas@yahoo.fr \
--cc=zsh-workers@sunsite.dk \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://git.vuxu.org/mirror/zsh/
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).