ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
* need help with macro
@ 2012-09-23 13:33 Philipp Gesang
  2012-09-23 14:00 ` Wolfgang Schuster
  0 siblings, 1 reply; 3+ messages in thread
From: Philipp Gesang @ 2012-09-23 13:33 UTC (permalink / raw)
  To: ConTeXt ML


[-- Attachment #1.1: Type: text/plain, Size: 1799 bytes --]

Hi all,

I have a macro “mogrify” that eats three tokens of a string and
treats them differently from the rest of the string. I can’t
however use it inside more complex macros. Is there a way out or
an alternative using macros? (No Lua.) Other formatting macros
work fine in the same place.

Any help will be greatly appreciated
Philipp

············· Example Code ············································
% macros=mkvi

\unprotect

\def\mogrify#content{%
  \begingroup
    \let\stopper\relax
    \def\get_first_three##1##2##3##4\stopper{%
      \def\first_three{##1##2##3}%
      \def\rest{##4}%
    }%
    \get_first_three#content\stopper%
    \colored[red]{\first_three}%
    \colored[green]{\rest}%
    \endgraf
  \endgroup%
}

\installnamespace {ww}
\installcommandhandler \????ww {ww} \????ww

\appendtoks
  \setuevalue{\currentww}{\wont_work[\currentww]}
\to \everydefineww

\unexpanded\def\wont_work[#id]{%
  \edef\current_wont_work{#id}%
  \dosingleempty\wont_work_indeed%
}

\def\wont_work_indeed[#setups]#content{%
  \iffirstargument\setupcurrentww[#setups]\fi
  %% here is the problem:
  \doifsomething{\wwparameter{param}}{\mogrify{\wwparameter{param}}}%
  \endgraf
  \framed{#content}%
}

\defineww[wontwork]

\protect

\starttext

\mogrify{foobar}\par%% the macro does work in isolation

\wontwork[param=whatever]{will it work?}
\wontwork[param=]        {this, however, does work}

\stoptext
·······································································


-- 
()  ascii ribbon campaign - against html e-mail
/\  www.asciiribbon.org   - against proprietary attachments

[-- Attachment #1.2: Type: application/pgp-signature, Size: 198 bytes --]

[-- Attachment #2: Type: text/plain, Size: 485 bytes --]

___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://tex.aanhet.net
archive  : http://foundry.supelec.fr/projects/contextrev/
wiki     : http://contextgarden.net
___________________________________________________________________________________

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

* Re: need help with macro
  2012-09-23 13:33 need help with macro Philipp Gesang
@ 2012-09-23 14:00 ` Wolfgang Schuster
  2012-09-23 14:28   ` Philipp Gesang
  0 siblings, 1 reply; 3+ messages in thread
From: Wolfgang Schuster @ 2012-09-23 14:00 UTC (permalink / raw)
  To: mailing list for ConTeXt users


Am 23.09.2012 um 15:33 schrieb Philipp Gesang <gesang@stud.uni-heidelberg.de>:

> Hi all,
> 
> I have a macro “mogrify” that eats three tokens of a string and
> treats them differently from the rest of the string. I can’t
> however use it inside more complex macros. Is there a way out or
> an alternative using macros? (No Lua.) Other formatting macros
> work fine in the same place.
> 
> Any help will be greatly appreciated
> Philipp
> 
> ············· Example Code ············································
> % macros=mkvi
> 
> \unprotect
> 
> \def\mogrify#content{%
>  \begingroup
>    \let\stopper\relax
>    \def\get_first_three##1##2##3##4\stopper{%
>      \def\first_three{##1##2##3}%
>      \def\rest{##4}%
>    }%
>    \get_first_three#content\stopper%
>    \colored[red]{\first_three}%
>    \colored[green]{\rest}%
>    \endgraf
>  \endgroup%
> }
> 
> \installnamespace {ww}
> \installcommandhandler \????ww {ww} \????ww
> 
> \appendtoks
>  \setuevalue{\currentww}{\wont_work[\currentww]}
> \to \everydefineww
> 
> \unexpanded\def\wont_work[#id]{%
>  \edef\current_wont_work{#id}%
>  \dosingleempty\wont_work_indeed%
> }
> 
> \def\wont_work_indeed[#setups]#content{%
>  \iffirstargument\setupcurrentww[#setups]\fi
>  %% here is the problem:
>  \doifsomething{\wwparameter{param}}{\mogrify{\wwparameter{param}}}%
>  \endgraf
>  \framed{#content}%
> }
> 
> \defineww[wontwork]
> 
> \protect
> 
> \starttext
> 
> \mogrify{foobar}\par%% the macro does work in isolation
> 
> \wontwork[param=whatever]{will it work?}
> \wontwork[param=]        {this, however, does work}
> 
> \stoptext

It’s a expansion problem, you can either expand the content in the definition of \mogrify (as seen below)
or you make the \mogrify command unexpanded and you expand the content of the \wwparameter
(i.e. \doifsomething{…}{\normalexpanded{\mogrify{…}}})

\unprotect

\def\mogrify_scan#first#second#third#rest\relax
  {\def\m_mogrify_three{#first#second#third}%
   \def\m_mogrify_rest {#rest}}

\def\mogrify#content%
  {\begingroup
   \edef\m_mogrify_content{#content}%
   \expandafter\mogrify_scan\m_mogrify_content\relax
   \colored  [red]{\m_mogrify_three}%
   \colored[green]{\m_mogrify_rest }%
   \endgraf
  \endgroup}

\installnamespace              {ww}
\installcommandhandler \????ww {ww} \????ww

\appendtoks
  \setuevalue{\currentww}{\wont_work[\currentww]}
\to \everydefineww

\unexpanded\def\wont_work[#id]%
  {\edef\currentww{#id}%
   \dosingleempty\wont_work_indeed}

\def\wont_work_indeed[#setups]#content%
  {\iffirstargument\setupcurrentww[#setups]\fi
   \doifsomething{\wwparameter{param}}{\mogrify{\wwparameter{param}}}%
   \endgraf
   \framed{#content}}

\defineww[wontwork]

\protect

\starttext

\mogrify{foobar}\par%% the macro does work in isolation

\wontwork[param=whatever]{will it work?}
\wontwork[param=]        {this, however, does work}

\stoptext

Wolfgang

___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://tex.aanhet.net
archive  : http://foundry.supelec.fr/projects/contextrev/
wiki     : http://contextgarden.net
___________________________________________________________________________________


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

* Re: need help with macro
  2012-09-23 14:00 ` Wolfgang Schuster
@ 2012-09-23 14:28   ` Philipp Gesang
  0 siblings, 0 replies; 3+ messages in thread
From: Philipp Gesang @ 2012-09-23 14:28 UTC (permalink / raw)
  To: mailing list for ConTeXt users


[-- Attachment #1.1: Type: text/plain, Size: 2280 bytes --]

···<date: 2012-09-23, Sunday>···<from: Wolfgang Schuster>···

> It’s a expansion problem, you can either expand the content in the definition of \mogrify (as seen below)
> or you make the \mogrify command unexpanded and you expand the content of the \wwparameter
> (i.e. \doifsomething{…}{\normalexpanded{\mogrify{…}}})

Thanks a lot, I rewrote my macros according to your the first
suggestion and it works like a breeze.

Best regards
Philipp


> 
> \unprotect
> 
> \def\mogrify_scan#first#second#third#rest\relax
>   {\def\m_mogrify_three{#first#second#third}%
>    \def\m_mogrify_rest {#rest}}
> 
> \def\mogrify#content%
>   {\begingroup
>    \edef\m_mogrify_content{#content}%
>    \expandafter\mogrify_scan\m_mogrify_content\relax
>    \colored  [red]{\m_mogrify_three}%
>    \colored[green]{\m_mogrify_rest }%
>    \endgraf
>   \endgroup}
> 
> \installnamespace              {ww}
> \installcommandhandler \????ww {ww} \????ww
> 
> \appendtoks
>   \setuevalue{\currentww}{\wont_work[\currentww]}
> \to \everydefineww
> 
> \unexpanded\def\wont_work[#id]%
>   {\edef\currentww{#id}%
>    \dosingleempty\wont_work_indeed}
> 
> \def\wont_work_indeed[#setups]#content%
>   {\iffirstargument\setupcurrentww[#setups]\fi
>    \doifsomething{\wwparameter{param}}{\mogrify{\wwparameter{param}}}%
>    \endgraf
>    \framed{#content}}
> 
> \defineww[wontwork]
> 
> \protect
> 
> \starttext
> 
> \mogrify{foobar}\par%% the macro does work in isolation
> 
> \wontwork[param=whatever]{will it work?}
> \wontwork[param=]        {this, however, does work}
> 
> \stoptext
> 
> Wolfgang
> 
> ___________________________________________________________________________________
> If your question is of interest to others as well, please add an entry to the Wiki!
> 
> maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
> webpage  : http://www.pragma-ade.nl / http://tex.aanhet.net
> archive  : http://foundry.supelec.fr/projects/contextrev/
> wiki     : http://contextgarden.net
> ___________________________________________________________________________________

-- 
()  ascii ribbon campaign - against html e-mail
/\  www.asciiribbon.org   - against proprietary attachments

[-- Attachment #1.2: Type: application/pgp-signature, Size: 198 bytes --]

[-- Attachment #2: Type: text/plain, Size: 485 bytes --]

___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://tex.aanhet.net
archive  : http://foundry.supelec.fr/projects/contextrev/
wiki     : http://contextgarden.net
___________________________________________________________________________________

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

end of thread, other threads:[~2012-09-23 14:28 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-09-23 13:33 need help with macro Philipp Gesang
2012-09-23 14:00 ` Wolfgang Schuster
2012-09-23 14:28   ` Philipp Gesang

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).