From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 20978 invoked by alias); 7 Dec 2015 08:22:48 -0000 Mailing-List: contact zsh-users-help@zsh.org; run by ezmlm Precedence: bulk X-No-Archive: yes List-Id: Zsh Users List List-Post: List-Help: X-Seq: 21055 Received: (qmail 15147 invoked from network); 7 Dec 2015 08:22:47 -0000 X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on f.primenet.com.au X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,T_DKIM_INVALID autolearn=ham autolearn_force=no version=3.4.0 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=brasslantern-com.20150623.gappssmtp.com; s=20150623; h=from:message-id:date:in-reply-to:comments:references:to:subject :mime-version:content-type; bh=gCV46MxGMdp10T9NM2Xb2W7IFhvjr5KE9hi9FHXleHY=; b=ykYDqBwebp/NSjcngmSK7y9y0+126fLrtWrAS94fFp+8tfT+angcguJIOxqNagbKdR moRGKbbjeUBld9oz+Fg9ho2mO1S6H8yfAvY6fJy2Ai/p7rkcmtCuIZuU4OgIMnQjmIzb BieTkdHuKs+clg9FAUNVoy0SaY95kHofSTbxVJ4eGUj2Uvnh6qB3p0Gkyep+gtWwNvTJ KzqvcX+Hig5sxN1/0kCx4avvLtfy3Bn45F/YcOUXdWaJak4hWU72zNv1nmqQ5NzezoU9 7BZBaBLweXBxj8aVGZrW1Dq3B9tSudc3YyhPVGlRfT0rGxC/HPJZ0KIOKQob2SSRV5gU 4IqQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:message-id:date:in-reply-to:comments :references:to:subject:mime-version:content-type; bh=gCV46MxGMdp10T9NM2Xb2W7IFhvjr5KE9hi9FHXleHY=; b=i4x9WwmMm88AH8BT6RL4krAXDjzwvmPP0Ht+S1DwUcz4Wr4zv4x2Cepf0qH7wdD1Ge DgUXsYkMVptMOr2w1ZWHx21Y1U8ChXTHhQbLLvffejt2T8XhFFnNYmsqAyGDpyoRmW5s is+FB0MMBq/URsVHHM3+fBuHX5nxTgcLacFyo40eI/PjrxVYGZHb2q/d1RAH3anj1Xir JHbqsmjqynHljiO3On1MLR2xYqepC+fO7tkUeKvxuxCgyMogtwM2en2Bq0aiZqLKkgmN VMo9ThsF6DVD3Uj0udtKbqvFnCrrYNtSEOzdK2XJ6s6ozg7f9jvpuHgTVxssJl5OgKM1 4HNg== X-Gm-Message-State: ALoCoQm+J5p8DKcmgLo18TBfUfe0JU6YqBncpPpEta6/icUWQohisse+YgDfPrRJ/zdIs34IY5XB X-Received: by 10.98.66.93 with SMTP id p90mr41671033pfa.118.1449476565478; Mon, 07 Dec 2015 00:22:45 -0800 (PST) From: Bart Schaefer Message-Id: <151207002253.ZM32539@torch.brasslantern.com> Date: Mon, 7 Dec 2015 00:22:53 -0800 In-Reply-To: Comments: In reply to Mikael Magnusson "Re: Feature request: #a (approximate matching) only for spaces" (Dec 7, 5:44am) References: X-Mailer: OpenZMail Classic (0.9.2 24April2005) To: Zsh Users Subject: Re: Feature request: #a (approximate matching) only for spaces MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii On Dec 7, 5:44am, Mikael Magnusson wrote: } } % print -l ${~s//(#b)(?)/$match[1]*} # make the pattern g*i*t*p*u*r*g*e* Yeah, but that also matches gfooifootfooifoopfooufoorfoogfooefoo, which I'm not sure is what Sebastian wants. The pattern (g*tpurge|gi*tpurge|git*purge|gitp*urge|gitpu*rge|gitpur*ge|gitpurg*e)* is probably closer to what he wants, but it's a bit ugly to generate a pattern like that: setopt histsubstpattern extendedglob s="gitpurge" # string is $#s chars long a=( ( "${(@)${(s::)s}/?/}" ) # array of $#s empty elements x=( ${s:^^a} ) # array of $#s copies of $s i=1 p=( ${x:s/(#b)(#s)(?(#c$[i++]))/$match[1]*} ) # $#s patterns print -lr - (${(j:|:)~p})* # join and enable matching Amazingly, you can write that as a one-liner, if you don't count the necessity of the setopts and declaring the parameters: (${(j:|:)~${i::=1}+${${:-$INPUT"${(@)^${(s::)INPUT}/?/}"}:s/(#b)(#s)(?(#c$[i++]))/$match[1]*}})* I renamed $s to $INPUT to make it easier to pick out from the rest of the chicken scratching. Breaking that down ... 1. ${i::=1} forces $i to reset to 1 before the rest of this expands. 2. The "+" after that says that if $i is set, substitute what follows instead. Since we just forced $i to be set, this always happens. 3. ${:-words} inserts those words as a nested expansion where they otherwise would be a parse error. 4. "${(@)^${(s::)INPUT}/?/}" forms an array of $#INPUT empty elements and also turns on rc_expand_param [the caret after (@)]. 5. Juxtaposing (4) with $INPUT results in $#INPUT copies of $INPUT as an array for the rest of the substitution to act upon. 6. The :s// expression applies to that array such that $[i++] is evaluated for each element. This is unlike ${..//pat/rep} which evaluates pat only once, and thus why we need hist_subst_pattern. 7. Hence (?(#c$[i++])) matches $i characters at array index $i, and we front-anchor that with (#s) and grab a reference to it with (#b), then use it in the replacement as $match[1]. 8. Then (j:|:) combines the resulting $#INPUT patterns and ~ makes active both the | and the * inserted by :s/.../$match[1]* so the whole expansion becomes a single pattern. Theoretically it should work to use this pattern as a subscript of the $history associative array with the (r) or (R) subscript flags to find all matching history events. -- Barton E. Schaefer