Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: histsubstpattern in zmv



On 01/06/2023 19:35 Stephane Chazelas <stephane@xxxxxxxxxxxx> wrote:
> The csh-style ${var:s/foo/bar} is often more legible and easy
> to cumulate than the Korn-style equivalent ${var/foo/bar}
>..
> But that needs set -o histsubstpattern and in zmv, emulate -LR
> zsh resets options.
> 
> Would it be possibe to enable it in zmv along with extendedglob
> which is already enabled there, or if not allow the user to enable it,
> or maybe even better introduced some :+s/pattern/repl/
> :-s/string/repl/ or :S/pattern/replacement/ so we can use both
> at the same time, like ${f:gs/***/3-stars/:gS/???/any-3-chars} ?

I can't see any good reason not to enable it.  The range of
likely cases where this would cause problems is rather limited ---
I'd probably instinctively quote pattern characters anyway.  But
if anyone thinks we should consider something more sophisticated
we can.

pws

diff --git a/Doc/Zsh/contrib.yo b/Doc/Zsh/contrib.yo
index 96de5aa9b..60553210c 100644
--- a/Doc/Zsh/contrib.yo
+++ b/Doc/Zsh/contrib.yo
@@ -4618,12 +4618,16 @@ example(zmv '(*).lis' '$1.txt')
 renames `tt(foo.lis)' to `tt(foo.txt)', `tt(my.old.stuff.lis)' to
 `tt(my.old.stuff.txt)', and so on.
 
-The pattern is always treated as an tt(EXTENDED_GLOB) pattern.  Any file
-whose name is not changed by the substitution is simply ignored.  Any
-error (a substitution resulted in an empty string, two substitutions gave
-the same result, the destination was an existing regular file and tt(-f)
-was not given) causes the entire function to abort without doing
-anything.
+The pattern is always treated as an tt(EXTENDED_GLOB) pattern.
+Furthermore, the tt(HIST_SUBST_PATTERN) option is enabled in order
+to allow the use of patterns in history-style substituions, i.e.
+tt(${f:s/)var(pat)tt(/)var(rep)tt(/}).
+
+Any file whose name is not changed by the substitution is simply
+ignored.  Any error (a substitution resulted in an empty string, two
+substitutions gave the same result, the destination was an existing
+regular file and tt(-f) was not given) causes the entire function to
+abort without doing anything.
 
 In addition to pattern replacement, the variable tt($f) can be referred
 to in the second (replacement) argument.  This makes it possible to
diff --git a/Functions/Misc/zmv b/Functions/Misc/zmv
index 269fe5ba5..58b0327cb 100644
--- a/Functions/Misc/zmv
+++ b/Functions/Misc/zmv
@@ -123,7 +123,7 @@
 #   paranoiacs like the author will probably put them there anyway.
 
 emulate -RL zsh
-setopt extendedglob
+setopt extendedglob histsubstpattern
 
 local f g args match mbegin mend files action myname tmpf opt exec
 local opt_f opt_i opt_n opt_q opt_Q opt_s opt_M opt_C opt_L




Messages sorted by: Reverse Date, Date, Thread, Author