ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
* Re: How to get parameters in lua
@ 2014-09-20 12:45 Christoph Reller
  2014-09-21 11:04 ` Hans Hagen
  0 siblings, 1 reply; 4+ messages in thread
From: Christoph Reller @ 2014-09-20 12:45 UTC (permalink / raw)
  To: ntg-context


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

On 9/19/2014 9:30 AM, Christoph Reller wrote:
> > Hi,
> >
> > \installcommandhandler nicely provides (among others) \define<name> and
> > \setup<name> commands. Thus it is easy to provide a key with
> > \setup<name>[key=value] and to read the value of such a key with
> > \<name>parameter{key}.
> >
> > My question is: How do I read the value of a key defined with
> > \setup...[key=value] on the lua side? If I use
> >    context.<name>parameter("key")
> > then the value of key is printed directly. I want to be able to read
> > that value and use it in lua with something like
> >    myvariable = context.?whatfunction?("key")
> >
> > Here is some example code:
> >
> > \unprotect
> >
> > \startluacode
> >    function myfun(parameters,content)
> >      context("From lua: Parameters: ")
> >      context(parameters)
> >      context.elemparameter("gkey") -- directly prints the parameter
> >      context.par()
> >    end
> > \stopluacode
> >
> > \installnamespace{elem}
> > \installcommandhandler \????elem {elem} \????elem
> > \appendtoks
> >    \setuevalue{\currentelem}{\elem_cmd{\currentelem}}%
> > \to \everydefineelem
> >
> > \unexpanded\def\elem_cmd#environment{%
> >    \edef\currentelem{#environment}%
> >    \dosingleempty\elem_cmd_parameters}
> >
> > \def\elem_cmd_parameters[#parameters]#content{%
> >    \setupcurrentelem[#parameters]
> >    From \TeX\: Parameters: key=\elemparameter{key},
> > gkey=\elemparameter{gkey}\par
> >
> >
\ctxlua{myfun(\!!bs#parameters\!!es,\!!bs\normalunexpanded{#content}\!!es)}
> > }
> >
> > \defineelem[base]
> > \setupelem[gkey=gval]
> >
> > \protect
> >
> > \starttext
> >
> > \base[key=val]{Hello}
> >
> > \stoptext
> >
> > Thank you for any feedback,

On Fri, Sep 19, 2014 at 12:00 PM, Hans Hagen wrote:
> something like this ... keep in mind that there is no grouping in lua
>
> % macros=mkvi
>
> \unprotect
>
> \installnamespace{elem}
>
> \installcommandhandler \????elem {elem} \????elem
>
> \startluacode
>      userdata.elem = { data = { [""] = { } } }
>
>      function userdata.elem.define(namespace,parent)
>          print("define",namespace,parent)
>          userdata.elem.data[namespace] = { }
>          if namespace ~= parent then
>
>
table.setmetatableindex(userdata.elem.data[namespace],userdata.elem.data[parent])
>          end
>      end
>
>      function userdata.elem.setup(namespace,key,gkey)
>          print("setup",namespace,key,gkey)
>          userdata.elem.data[namespace].key = key
>          userdata.elem.data[namespace].gkey = gkey
>      end
>
>      function userdata.myfun(namespace,one,two)
>          context.par()
>          context("elem: one=%s",userdata.elem.data[namespace][one])
>          context.par()
>          context("elem: two=%s",userdata.elem.data[namespace][two])
>          context.par()
>      end
> \stopluacode
>
> \appendtoks
>      \setuevalue{\currentelem}{\elem_cmd{\currentelem}}%
>      \ctxlua{userdata.elem.define("\currentelem","\currentelemparent")}%
> \to \everydefineelem
>
> \appendtoks
>
>
\ctxlua{userdata.elem.setup("\currentelem","\elemparameter{key}","\elemparameter{gkey}")}%
> \to \everysetupelem
>
> \unexpanded\def\elem_cmd#environment%
>    {\edef\currentelem{#environment}%
>     \dosingleempty\elem_cmd_parameters}
>
> \def\elem_cmd_parameters[#parameters]#content%
>    {\setupelem[\currentelem][#parameters]
>     From \TeX\: Parameters: key=\elemparameter{key},
> gkey=\elemparameter{gkey}\par
>     \ctxlua{userdata.myfun("\currentelem","key","gkey")}}
>
> \defineelem[base]
> \setupelem[gkey=gval]
>
> \protect
>
> \starttext
>
> \base[key=val]{Hello}
>
> \stoptext
>
> A simpler alternative is:
>
> % macros=mkvi
>
> \unprotect
>
> \installnamespace{elem}
>
> \installcommandhandler \????elem {elem} \????elem
>
> \startluacode
>      userdata.elem = { data = {  } }
>
>      function userdata.elem.sync(key,gkey)
>          userdata.elem.data.key = key
>          userdata.elem.data.gkey = gkey
>      end
>
>      function userdata.myfun(one,two)
>          context.par()
>          context("elem: one=%s",userdata.elem.data[one])
>          context.par()
>          context("elem: two=%s",userdata.elem.data[two])
>          context.par()
>      end
> \stopluacode
>
> \appendtoks
>      \setuevalue{\currentelem}{\elem_cmd{\currentelem}}%
> \to \everydefineelem
>
> \unexpanded\def\elem_cmd#environment%
>    {\edef\currentelem{#environment}%
>     \dosingleempty\elem_cmd_parameters}
>
> \unexpanded\def\elem_sync
>
>
{\ctxlua{userdata.elem.sync("\elemparameter{key}","\elemparameter{gkey}")}}
>
> \def\elem_cmd_parameters[#parameters]#content%
>    {\setupcurrentelem[#parameters]
>     \elem_sync
>     From \TeX\: Parameters: key=\elemparameter{key},
> gkey=\elemparameter{gkey}\par
>     \ctxlua{userdata.myfun("key","gkey")}}
>
> \defineelem[base]
> \setupelem[gkey=gval]
>
> \protect
>
> \starttext
>
> \base[key=val]{Hello}
>
> \stoptext

Thank you Hans!

Two general questions arise (if your permit):

1. Where are the key-value pairs stored?
In your first solution you handle the key setting by explicitly calling lua
on every \setup<name> call.  It seems that all key-value pairs (that are
set with \installcommandhandler-generated \setup<name>) are stored on the
TeX side and not on the lua side.  Is this correct?

2. Calling \ctxlua from lua.
I was tempted to add another level of indirection to your second (the
"synchronize") solution.  I.e: in the lua code (in a separate lua file to
avoid the do ... end introduced by \startluacode ... \stopluacode) I have:

n = ""
v = ""
function getkey(name,value)
   n = name
   v = value
end
function myfun(parameters,content)
   context([[\ctxlua{getkey("gkey","\elemparameter{gkey}")}]])
   context(" name=")
   context(n)
   context(", value=")
   context(v)
end

The above does not work because the value obtained "lags behind", i.e.
initially v will be "", on the second call v will have the value of the
first \setupelem[gkey=gval], and so on.  I guess this lag is due to the way
TeX and lua communicate to each other?

A small question: What do you mean by "there is no grouping in lua"?

Thank you anyway for your valuable input.

Regards, Christoph

[-- Attachment #1.2: Type: text/html, Size: 9756 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] 4+ messages in thread

* Re: How to get parameters in lua
  2014-09-20 12:45 How to get parameters in lua Christoph Reller
@ 2014-09-21 11:04 ` Hans Hagen
  0 siblings, 0 replies; 4+ messages in thread
From: Hans Hagen @ 2014-09-21 11:04 UTC (permalink / raw)
  To: ntg-context

On 9/20/2014 2:45 PM, Christoph Reller wrote:
> Two general questions arise (if your permit):
>
> 1. Where are the key-value pairs stored?
> In your first solution you handle the key setting by explicitly calling
> lua on every \setup<name> call.  It seems that all key-value pairs (that
> are set with \installcommandhandler-generated \setup<name>) are stored
> on the TeX side and not on the lua side.  Is this correct?

yes and getting the macro meaning at the lua end is not efficient as it 
is tokenized

> 2. Calling \ctxlua from lua.
> I was tempted to add another level of indirection to your second (the
> "synchronize") solution.  I.e: in the lua code (in a separate lua file
> to avoid the do ... end introduced by \startluacode .... \stopluacode) I
> have:
>
> n = ""
> v = ""
> function getkey(name,value)
>     n = name
>     v = value
> end
> function myfun(parameters,content)
>     context([[\ctxlua{getkey("gkey","\elemparameter{gkey}")}]])
>     context(" name=")
>     context(n)
>     context(", value=")
>     context(v)
> end
>
> The above does not work because the value obtained "lags behind", i.e.
> initially v will be "", on the second call v will have the value of the
> first \setupelem[gkey=gval], and so on.  I guess this lag is due to the
> way TeX and lua communicate to each other?

indeed, all printed to tex is injected into the input after the lua call 
ends

> A small question: What do you mean by "there is no grouping in lua"?

\bgroup
   ... some assignment at the tex end
   ... followed by setting it in lua
\bgroup

... forgotten at the tex end
... still set at the lua end

> Thank you anyway for your valuable input.
>
> Regards, Christoph
>
>
> ___________________________________________________________________________________
> 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
> ___________________________________________________________________________________
>


-- 

-----------------------------------------------------------------
                                           Hans Hagen | PRAGMA ADE
               Ridderstraat 27 | 8061 GH Hasselt | The Netherlands
     tel: 038 477 53 69 | voip: 087 875 68 74 | www.pragma-ade.com
                                              | www.pragma-pod.nl
-----------------------------------------------------------------
___________________________________________________________________________________
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] 4+ messages in thread

* Re: How to get parameters in lua
  2014-09-19  7:30 Christoph Reller
@ 2014-09-19  8:03 ` Hans Hagen
  0 siblings, 0 replies; 4+ messages in thread
From: Hans Hagen @ 2014-09-19  8:03 UTC (permalink / raw)
  To: ntg-context

On 9/19/2014 9:30 AM, Christoph Reller wrote:
> Hi,
>
> \installcommandhandler nicely provides (among others) \define<name> and
> \setup<name> commands. Thus it is easy to provide a key with
> \setup...[key=value] and to read the value of such a key with
> \<name>parameter{key}.
>
> My question is: How do I read the value of a key defined with
> \setup...[key=value] on the lua side? If I use
>    context.<name>parameter("key")
> then the value of key is printed directly. I want to be able to read
> that value and use it in lua with something like
>    myvariable = context.?whatfunction?("key")
>
> Here is some example code:
>
> \unprotect
>
> \startluacode
>    function myfun(parameters,content)
>      context("From lua: Parameters: ")
>      context(parameters)
>      context.elemparameter("gkey") -- directly prints the parameter
>      context.par()
>    end
> \stopluacode
>
> \installnamespace{elem}
> \installcommandhandler \????elem {elem} \????elem
> \appendtoks
>    \setuevalue{\currentelem}{\elem_cmd{\currentelem}}%
> \to \everydefineelem
>
> \unexpanded\def\elem_cmd#environment{%
>    \edef\currentelem{#environment}%
>    \dosingleempty\elem_cmd_parameters}
>
> \def\elem_cmd_parameters[#parameters]#content{%
>    \setupcurrentelem[#parameters]
>    From \TeX\: Parameters: key=\elemparameter{key},
> gkey=\elemparameter{gkey}\par
>
> \ctxlua{myfun(\!!bs#parameters\!!es,\!!bs\normalunexpanded{#content}\!!es)}
> }
>
> \defineelem[base]
> \setupelem[gkey=gval]
>
> \protect
>
> \starttext
>
> \base[key=val]{Hello}
>
> \stoptext
>
> Thank you for any feedback,

something like this ... keep in mind that there is no grouping in lua

% macros=mkvi

\unprotect

\installnamespace{elem}

\installcommandhandler \????elem {elem} \????elem

\startluacode
     userdata.elem = { data = { [""] = { } } }

     function userdata.elem.define(namespace,parent)
         print("define",namespace,parent)
         userdata.elem.data[namespace] = { }
         if namespace ~= parent then
 
table.setmetatableindex(userdata.elem.data[namespace],userdata.elem.data[parent])
         end
     end

     function userdata.elem.setup(namespace,key,gkey)
         print("setup",namespace,key,gkey)
         userdata.elem.data[namespace].key = key
         userdata.elem.data[namespace].gkey = gkey
     end

     function userdata.myfun(namespace,one,two)
         context.par()
         context("elem: one=%s",userdata.elem.data[namespace][one])
         context.par()
         context("elem: two=%s",userdata.elem.data[namespace][two])
         context.par()
     end
\stopluacode

\appendtoks
     \setuevalue{\currentelem}{\elem_cmd{\currentelem}}%
     \ctxlua{userdata.elem.define("\currentelem","\currentelemparent")}%
\to \everydefineelem

\appendtoks
 
\ctxlua{userdata.elem.setup("\currentelem","\elemparameter{key}","\elemparameter{gkey}")}%
\to \everysetupelem

\unexpanded\def\elem_cmd#environment%
   {\edef\currentelem{#environment}%
    \dosingleempty\elem_cmd_parameters}

\def\elem_cmd_parameters[#parameters]#content%
   {\setupelem[\currentelem][#parameters]
    From \TeX\: Parameters: key=\elemparameter{key}, 
gkey=\elemparameter{gkey}\par
    \ctxlua{userdata.myfun("\currentelem","key","gkey")}}

\defineelem[base]
\setupelem[gkey=gval]

\protect

\starttext

\base[key=val]{Hello}

\stoptext

A simpler alternative is:

% macros=mkvi

\unprotect

\installnamespace{elem}

\installcommandhandler \????elem {elem} \????elem

\startluacode
     userdata.elem = { data = {  } }

     function userdata.elem.sync(key,gkey)
         userdata.elem.data.key = key
         userdata.elem.data.gkey = gkey
     end

     function userdata.myfun(one,two)
         context.par()
         context("elem: one=%s",userdata.elem.data[one])
         context.par()
         context("elem: two=%s",userdata.elem.data[two])
         context.par()
     end
\stopluacode

\appendtoks
     \setuevalue{\currentelem}{\elem_cmd{\currentelem}}%
\to \everydefineelem

\unexpanded\def\elem_cmd#environment%
   {\edef\currentelem{#environment}%
    \dosingleempty\elem_cmd_parameters}

\unexpanded\def\elem_sync
 
{\ctxlua{userdata.elem.sync("\elemparameter{key}","\elemparameter{gkey}")}}

\def\elem_cmd_parameters[#parameters]#content%
   {\setupcurrentelem[#parameters]
    \elem_sync
    From \TeX\: Parameters: key=\elemparameter{key}, 
gkey=\elemparameter{gkey}\par
    \ctxlua{userdata.myfun("key","gkey")}}

\defineelem[base]
\setupelem[gkey=gval]

\protect

\starttext

\base[key=val]{Hello}

\stoptext


-----------------------------------------------------------------
                                           Hans Hagen | PRAGMA ADE
               Ridderstraat 27 | 8061 GH Hasselt | The Netherlands
     tel: 038 477 53 69 | voip: 087 875 68 74 | www.pragma-ade.com
                                              | www.pragma-pod.nl
-----------------------------------------------------------------
___________________________________________________________________________________
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] 4+ messages in thread

* How to get parameters in lua
@ 2014-09-19  7:30 Christoph Reller
  2014-09-19  8:03 ` Hans Hagen
  0 siblings, 1 reply; 4+ messages in thread
From: Christoph Reller @ 2014-09-19  7:30 UTC (permalink / raw)
  To: ntg-context


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

Hi,

\installcommandhandler nicely provides (among others) \define<name> and
\setup<name> commands. Thus it is easy to provide a key with
\setup...[key=value] and to read the value of such a key with
\<name>parameter{key}.

My question is: How do I read the value of a key defined with
\setup...[key=value] on the lua side? If I use
  context.<name>parameter("key")
then the value of key is printed directly. I want to be able to read that
value and use it in lua with something like
  myvariable = context.?whatfunction?("key")

Here is some example code:

\unprotect

\startluacode
  function myfun(parameters,content)
    context("From lua: Parameters: ")
    context(parameters)
    context.elemparameter("gkey") -- directly prints the parameter
    context.par()
  end
\stopluacode

\installnamespace{elem}
\installcommandhandler \????elem {elem} \????elem
\appendtoks
  \setuevalue{\currentelem}{\elem_cmd{\currentelem}}%
\to \everydefineelem

\unexpanded\def\elem_cmd#environment{%
  \edef\currentelem{#environment}%
  \dosingleempty\elem_cmd_parameters}

\def\elem_cmd_parameters[#parameters]#content{%
  \setupcurrentelem[#parameters]
  From \TeX\: Parameters: key=\elemparameter{key},
gkey=\elemparameter{gkey}\par

\ctxlua{myfun(\!!bs#parameters\!!es,\!!bs\normalunexpanded{#content}\!!es)}
}

\defineelem[base]
\setupelem[gkey=gval]

\protect

\starttext

\base[key=val]{Hello}

\stoptext

Thank you for any feedback,

Christoph Reller

[-- Attachment #1.2: Type: text/html, Size: 2138 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] 4+ messages in thread

end of thread, other threads:[~2014-09-21 11:04 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-09-20 12:45 How to get parameters in lua Christoph Reller
2014-09-21 11:04 ` Hans Hagen
  -- strict thread matches above, loose matches on Subject: below --
2014-09-19  7:30 Christoph Reller
2014-09-19  8:03 ` Hans Hagen

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