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

Re: MBEGIN, MEND in #m when pattern has "|"



On Jun 3,  6:47am, Sebastian Gniazdowski wrote:
} Subject: Re: MBEGIN, MEND in #m when pattern has "|"
}
} On 3 June 2016 at 00:11, Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx> wrote:
} > MATCH MBEGIN and MEND are set only if there is a single possible match
} > (more precisely, when the string can be matched with only a combination
} > of strcmp() and simple wildcards).  In //pat/repl/, there are multiple
} > possible match positions; which begin and end do you want?
} 
} The ones that are substituted, i.e. if #m knows that it will replace
} "vim", it should give indexes for that substring, and if it knows it
} will replace "s", then should give indexes for it, etc.

On closer inspection, your problem isn't what I thought it was.  Note
this bit of the doc:

m
     Set references to the match data for the entire string matched;
     this is similar to backreferencing and does not work in filename
     generation.  The flag must be in effect at the end of the pattern,
     i.e. not local to a group.

When you do

    (#mi)this|that

The #m ends at the "|", and so is not "in effect at the end of the
pattern" which is after "that".  So if you change your expression to be
one of

    harray=( "${(f)${text//(#mi)(${~colsearch_pattern})/|$(( MBEGIN - 1 )) $((
MEND )) fg=red,bold|$nl}%$nl*}" )

[added parens around the alternates] or

    harray=( "${(f)${text//(#i)${~colsearch_pattern}(#m)/|$(( MBEGIN - 1 )) $((
MEND )) fg=red,bold|$nl}%$nl*}" )

[moved (#m) to the end] then $MBEGIN et al. work as you desire.  I suspect
you actually want the first with the parens because otherwise the (#i) also
ends at the "|".



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