From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 16264 invoked by alias); 4 Dec 2015 20:57:01 -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: 21045 Received: (qmail 7896 invoked from network); 4 Dec 2015 20:57:01 -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=0.2 required=5.0 tests=BAYES_00,DKIM_ADSP_CUSTOM_MED, FREEMAIL_FROM,FSL_HELO_BARE_IP_2,NML_ADSP_CUSTOM_MED,RCVD_NUMERIC_HELO autolearn=no autolearn_force=no version=3.4.0 X-Injected-Via-Gmane: http://gmane.org/ To: zsh-users@zsh.org From: Scott Frazer Subject: Re: Interactive search on the command line? Date: Fri, 4 Dec 2015 15:56:45 -0500 Message-ID: <5661FE0D.7000604@gmail.com> References: <20151126080400.GA20074@linux.vnet.ibm.com> <565B723B.70900@gmail.com> <20151130031915.GD2504@tarsus.local2> <20151203233926.GF1955@tarsus.local2> <151203165950.ZM13362@torch.brasslantern.com> Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit X-Complaints-To: usenet@ger.gmane.org X-Gmane-NNTP-Posting-Host: 173.38.117.73 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:38.0) Gecko/20100101 Thunderbird/38.4.0 In-Reply-To: <151203165950.ZM13362@torch.brasslantern.com> On 12/3/15 7:59 PM, Bart Schaefer wrote: > On Dec 3, 11:39pm, Daniel Shahaf wrote: > } > } So, you could do something like this (using the (b::) subscript flag as > } well): > } > } % () { > } local haystack="$1" > } local needle="a" > } integer idx > } while (( idx = ${haystack[(ib:idx+1:)${needle}]} )) > } (( idx <= $#haystack )) > } do > } print -r - $haystack[idx,idx+$#needle-1] > } done > } } foobar > } a > } % > > This example might be a little clearer if we show the index where the > match was found and use a match pattern longer than one character: > > % () { > local haystack=$1 needle=$2 > integer idx > while idx=${haystack[(ib:idx+1:)$needle]} > (( idx <= $#haystack )) > do > print -r - $idx $haystack[idx,idx+$#needle-1] > done > } foobar 'o??' > 2 oob > 3 oba > % > > (I also removed all redundant quotes and braces, since the original > would only work in native zsh emulation anyway.) > Oh, it's a reverse lookup instead of walking through the entire BUFFER. Neat! Code updated. Scott