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

Re: nohistsubstpattern and :s//



On Jul 7,  5:10pm, Peter Stephenson wrote:
} Subject: Re: nohistsubstpattern and :s//
}
} On Tue, 7 Jul 2015 08:48:51 -0700
} Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx> wrote:
} > So with histsubstpattern set it looks like $b is expanded before the
} > substitution takes place, but with nohistsubstpattern (the default,
} > which is why I never showed the option change in any of my previous
} > examples) the expansion happens after the substitution.
} 
} Oh, in that case it's perfectly sensible.

Except that's not QUITE what's going on ... if the expansion were simply
being done before the replacement, this wouldn't work:

torch% a=(abc dbe fbg)
torch% setopt histsubstpattern extendedglob
torch% print -R ${a:s/(#b)b(?)/${match}X/}
acX deX fgX
torch% a="$a"
torch% print -R ${a:gs/(#b)b(?)/${match}X/}
acX deX fgX

So what's actually happening (hist.c 2131-2175) is that hist_subst_pattern
causes both the left and right sides to be expanded *during* replacement
[via getmatch() -> igetmatch() in glob.c].

} The only gotcha is to remember that because modifiers operate on raw
} input, which is how they came to exist in the first place, if you
} don't want a $ to be active you need to quote it.

Not exactly the only gotcha.  The after vs. during difference is a the
least worth mentioning, particularly because of this:

torch% print *a*
config.modules.local config.status Makefile stamp-h
torch% x=':gs/(#b)(?)?/X$match/'
torch% setopt histsubstpattern extendedglob
torch% print *a*($x)          
XcXnXiX.XoXuXeX.XoXa XcXnXiX.XtXts XMXkXfXl XsXaXph

Note that I didn't even need to use $~x there, because it's expanded one
time during parsing the glob qualifier and then again during the modifier.
In fact it's actually WRONG to use $~x there:

torch% print *a*($~x)                    
zsh: bad pattern: *a*(:gs/(#b)(?)?/X$match/)

Need to think a while about the right way to document this.



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