zsh-workers
 help / color / mirror / code / Atom feed
* MBEGIN, MEND in #m when pattern has "|"
@ 2016-06-02 17:41 Sebastian Gniazdowski
  2016-06-02 22:11 ` Bart Schaefer
  0 siblings, 1 reply; 5+ messages in thread
From: Sebastian Gniazdowski @ 2016-06-02 17:41 UTC (permalink / raw)
  To: Zsh hackers list

Hello,
below is a code that uses indexes set by (#m) or (#b). Turns out the
indexes are set only when using (#b). For (#m) they are set when there
is no "|" in the pattern. Why?

Output is:
==========================

Text is: vim ~/.zshrc s, searching for: vim|s
|-1 0 fg=red,bold|
 ~/.z|-1 0 fg=red,bold|
hrc |-1 0 fg=red,bold|

Text is: vim ~/.zshrc s, searching for: vim|s
|0 3 fg=red,bold|
|8 9 fg=red,bold|
|13 14 fg=red,bold|

Code:
==========================

#!/usr/bin/env zsh

nl=$'\n'
text="vim ~/.zshrc s"
colsearch_pattern="vim|s"

harray=( "${(f)${text//(#mi)${~colsearch_pattern}/|$(( MBEGIN - 1 ))
$(( MEND )) fg=red,bold|$nl}%$nl*}" )
echo "Text is: $text, searching for: $colsearch_pattern"
print -rl -- "${harray[@]}"

print

harray=( "${(f)${(S)text//*(#bi)(${~colsearch_pattern})/|$(( mbegin[1]
- 1 )) $(( mend[1] )) fg=red,bold|$nl}%$nl*}" )
echo "Text is: $text, searching for: $colsearch_pattern"
print -rl -- "${harray[@]}"


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: MBEGIN, MEND in #m when pattern has "|"
  2016-06-02 17:41 MBEGIN, MEND in #m when pattern has "|" Sebastian Gniazdowski
@ 2016-06-02 22:11 ` Bart Schaefer
  2016-06-03  4:47   ` Sebastian Gniazdowski
  0 siblings, 1 reply; 5+ messages in thread
From: Bart Schaefer @ 2016-06-02 22:11 UTC (permalink / raw)
  To: Zsh hackers list

On Jun 2,  7:41pm, Sebastian Gniazdowski wrote:
}
} below is a code that uses indexes set by (#m) or (#b). Turns out the
} indexes are set only when using (#b). For (#m) they are set when there
} is no "|" in the pattern. Why?

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?

This probably should have been documented better.


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: MBEGIN, MEND in #m when pattern has "|"
  2016-06-02 22:11 ` Bart Schaefer
@ 2016-06-03  4:47   ` Sebastian Gniazdowski
  2016-06-04  5:58     ` Bart Schaefer
  0 siblings, 1 reply; 5+ messages in thread
From: Sebastian Gniazdowski @ 2016-06-03  4:47 UTC (permalink / raw)
  To: Bart Schaefer; +Cc: Zsh hackers list

On 3 June 2016 at 00:11, Bart Schaefer <schaefer@brasslantern.com> 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.

Best regards,
Sebastian Gniazdowski


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: MBEGIN, MEND in #m when pattern has "|"
  2016-06-03  4:47   ` Sebastian Gniazdowski
@ 2016-06-04  5:58     ` Bart Schaefer
  2016-06-04  6:41       ` Sebastian Gniazdowski
  0 siblings, 1 reply; 5+ messages in thread
From: Bart Schaefer @ 2016-06-04  5:58 UTC (permalink / raw)
  To: Zsh hackers list

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@brasslantern.com> 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 "|".


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: MBEGIN, MEND in #m when pattern has "|"
  2016-06-04  5:58     ` Bart Schaefer
@ 2016-06-04  6:41       ` Sebastian Gniazdowski
  0 siblings, 0 replies; 5+ messages in thread
From: Sebastian Gniazdowski @ 2016-06-04  6:41 UTC (permalink / raw)
  To: Bart Schaefer; +Cc: Zsh hackers list

On 4 June 2016 at 07:58, Bart Schaefer <schaefer@brasslantern.com> wrote:
> 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 "|".

This helps, thanks! In my other project I was using (), probably
because I suspected that alternative requires it.

Best regards,
Sebastian Gniazdowski


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2016-06-04  6:42 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-06-02 17:41 MBEGIN, MEND in #m when pattern has "|" Sebastian Gniazdowski
2016-06-02 22:11 ` Bart Schaefer
2016-06-03  4:47   ` Sebastian Gniazdowski
2016-06-04  5:58     ` Bart Schaefer
2016-06-04  6:41       ` Sebastian Gniazdowski

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).