From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.io/gmane.comp.tex.context/88728 Path: news.gmane.org!not-for-mail From: Christoph Reller Newsgroups: gmane.comp.tex.context Subject: Re: How to get parameters in lua Date: Sat, 20 Sep 2014 14:45:18 +0200 Message-ID: Reply-To: mailing list for ConTeXt users NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="===============0767924761==" X-Trace: ger.gmane.org 1411217164 32189 80.91.229.3 (20 Sep 2014 12:46:04 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Sat, 20 Sep 2014 12:46:04 +0000 (UTC) To: ntg-context Original-X-From: ntg-context-bounces@ntg.nl Sat Sep 20 14:46:00 2014 Return-path: Envelope-to: gctc-ntg-context-518@m.gmane.org Original-Received: from balder.ntg.nl ([5.39.185.229]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1XVK33-0006Jj-1B for gctc-ntg-context-518@m.gmane.org; Sat, 20 Sep 2014 14:45:57 +0200 Original-Received: from localhost (localhost [127.0.0.1]) by balder.ntg.nl (Postfix) with ESMTP id 450F510220 for ; Sat, 20 Sep 2014 14:45:56 +0200 (CEST) X-Virus-Scanned: Debian amavisd-new at balder.ntg.nl Original-Received: from balder.ntg.nl ([127.0.0.1]) by localhost (balder.ntg.nl [127.0.0.1]) (amavisd-new, port 10024) with LMTP id Cb-Zl7lHAwOB for ; Sat, 20 Sep 2014 14:45:56 +0200 (CEST) Original-Received: from balder.ntg.nl (localhost [IPv6:::1]) by balder.ntg.nl (Postfix) with ESMTP id 1181810223 for ; Sat, 20 Sep 2014 14:45:47 +0200 (CEST) Original-Received: from localhost (localhost [127.0.0.1]) by balder.ntg.nl (Postfix) with ESMTP id 50BC6101E7 for ; Sat, 20 Sep 2014 14:45:43 +0200 (CEST) X-Virus-Scanned: Debian amavisd-new at balder.ntg.nl Original-Received: from balder.ntg.nl ([127.0.0.1]) by localhost (balder.ntg.nl [127.0.0.1]) (amavisd-new, port 10024) with LMTP id HY5h-c3XK8Kr for ; Sat, 20 Sep 2014 14:45:41 +0200 (CEST) Original-Received: from filter1-utr.mf.surf.net (filter1-utr.mf.surf.net [195.169.124.152]) by balder.ntg.nl (Postfix) with ESMTP id B80FE101E1 for ; Sat, 20 Sep 2014 14:45:41 +0200 (CEST) Original-Received: from mail-ig0-x231.google.com (mail-ig0-x231.google.com [IPv6:2607:f8b0:4001:c05::231]) by filter1-utr.mf.surf.net (8.14.3/8.14.3/Debian-9.4) with ESMTP id s8KCjVFv011510 (version=TLSv1/SSLv3 cipher=RC4-SHA bits=128 verify=NOT) for ; Sat, 20 Sep 2014 14:45:32 +0200 Original-Received: by mail-ig0-f177.google.com with SMTP id h3so523177igd.16 for ; Sat, 20 Sep 2014 05:45:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:from:date:message-id:subject:to:content-type; bh=pOD2bdVrVIdcKFFuN253GB/4wWrM7dCVB8qjNgOB/NA=; b=wSUT5OkjaJWxWsh7w8ShrC807n08/cLIRDd7o64HqUgNNOWSGF+l1WRk0kL3xNJ0UA X5Lv01cLV/fF8TJ/kquEl/ug62OOhFeqjHKrgc/AEcWUp6+weHXmYROPjIz2Yw0nW2qY W/oJ1usH94UjJlzfvAAQrHDAevKzKvhz/8KVj89uw6FRSpG/gOWVr/A7xUYn+0kIcX4+ 80Togp3mlQS0VCEhRDArNm9lDp1mNZtxd/IP/RvAxH1Hz5EIVniI/NDGx1/7MNdIdkkK 7ZinQinQZtbbDznIAHZfrJvO5nGgv0JfNSgLW2V9aBZXc9MLgqTJ8pgSRf5KcBGNwCd1 syrA== X-Received: by 10.50.43.233 with SMTP id z9mr2699948igl.41.1411217139040; Sat, 20 Sep 2014 05:45:39 -0700 (PDT) Original-Received: by 10.107.40.2 with HTTP; Sat, 20 Sep 2014 05:45:18 -0700 (PDT) X-Bayes-Prob: 0.0001 (Score 0, tokens from: ntg-context@ntg.nl, base:default, @@RPTN) X-CanIt-Geo: ip=2607:f8b0:4001:c05::231; country=US X-CanItPRO-Stream: uu:ntg-context@ntg.nl (inherits from uu:default, base:default) X-Canit-Stats-ID: 07MRoJwmy - de89f070ef40 - 20140920 (trained as not-spam) X-Scanned-By: CanIt (www . roaringpenguin . com) X-BeenThere: ntg-context@ntg.nl X-Mailman-Version: 2.1.14 Precedence: list List-Id: mailing list for ConTeXt users List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: ntg-context-bounces@ntg.nl Original-Sender: ntg-context-bounces@ntg.nl Xref: news.gmane.org gmane.comp.tex.context:88728 Archived-At: --===============0767924761== Content-Type: multipart/alternative; boundary=089e0122774846077b05037e98f4 --089e0122774846077b05037e98f4 Content-Type: text/plain; charset=UTF-8 On 9/19/2014 9:30 AM, Christoph Reller wrote: > > Hi, > > > > \installcommandhandler nicely provides (among others) \define and > > \setup commands. Thus it is easy to provide a key with > > \setup[key=value] and to read the value of such a key with > > \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.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 call. It seems that all key-value pairs (that are set with \installcommandhandler-generated \setup) 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 --089e0122774846077b05037e98f4 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
On 9/19/2014 9:30 AM, Christoph Reller wrote:
> > Hi,
> >
> > \installcommandhan= dler nicely provides (among others) \define<name> and
> = > \setup<name> commands. Thus it is easy to provide a key with
> > \setup<name>[key=3Dvalue] and to read the value of s= uch a key with
> > \<name>parameter{key}.
&= gt; >
> > My question is: How do I read the value of a k= ey defined with
> > \setup...[key=3Dvalue] on the lua side?= If I use
> > =C2=A0 =C2=A0context.<name>parameter(&q= uot;key")
> > then the value of key is printed directl= y. I want to be able to read
> > that value and use it in l= ua with something like
> > =C2=A0 =C2=A0myvariable =3D cont= ext.?whatfunction?("key")
> >
> >= Here is some example code:
> >
> > \unprot= ect
> >
> > \startluacode
> &g= t; =C2=A0 =C2=A0function myfun(parameters,content)
> > =C2= =A0 =C2=A0 =C2=A0context("From lua: Parameters: ")
>= > =C2=A0 =C2=A0 =C2=A0context(parameters)
> > =C2=A0 = =C2=A0 =C2=A0context.elemparameter("gkey") -- directly prints the= parameter
> > =C2=A0 =C2=A0 =C2=A0context.par()
= > > =C2=A0 =C2=A0end
> > \stopluacode
> = >
> > \installnamespace{elem}
> > \insta= llcommandhandler \????elem {elem} \????elem
> > \appendtoks=
> > =C2=A0 =C2=A0\setuevalue{\currentelem}{\elem_cmd{\curr= entelem}}%
> > \to \everydefineelem
> >
> > \unexpanded\def\elem_cmd#environment{%
> >= =C2=A0 =C2=A0\edef\currentelem{#environment}%
> > =C2=A0 = =C2=A0\dosingleempty\elem_cmd_parameters}
> >
>= ; > \def\elem_cmd_parameters[#parameters]#content{%
> > = =C2=A0 =C2=A0\setupcurrentelem[#parameters]
> > =C2=A0 =C2= =A0From \TeX\: Parameters: key=3D\elemparameter{key},
> > g= key=3D\elemparameter{gkey}\par
> >
> > \ctx= lua{myfun(\!!bs#parameters\!!es,\!!bs\normalunexpanded{#content}\!!es)}
> > }
> >
> > \defineelem[base= ]
> > \setupelem[gkey=3Dgval]
> >
> > \protect
> >
> > \starttext
> >
> > \base[key=3Dval]{Hello}
> = >
> > \stoptext
> >
> > = Thank you for any feedback,

On Fri, Sep 19, 2014 a= t 12:00 PM, Hans Hagen wrote:
> something like this ... keep i= n mind that there is no grouping in lua
>=C2=A0
>= % macros=3Dmkvi
>=C2=A0
> \unprotect
&= gt;=C2=A0
> \installnamespace{elem}
>=C2=A0
=
> \installcommandhandler \????elem {elem} \????elem
>= =C2=A0
> \startluacode
> =C2=A0 =C2=A0 =C2=A0user= data.elem =3D { data =3D { [""] =3D { } } }
>=C2=A0<= /div>
> =C2=A0 =C2=A0 =C2=A0function userdata.elem.define(namespace,= parent)
> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0print("define= ",namespace,parent)
> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0u= serdata.elem.data[namespace] =3D { }
> =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0if namespace ~=3D parent then
>=C2=A0
&= gt; table.setmetatableindex(userdata.elem.data[namespace],userdata.elem.dat= a[parent])
> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0end
&= gt; =C2=A0 =C2=A0 =C2=A0end
>=C2=A0
> =C2=A0 =C2= =A0 =C2=A0function userdata.elem.setup(namespace,key,gkey)
> = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0print("setup",namespace,key,gke= y)
> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0userdata.elem.data[name= space].key =3D key
> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0userdat= a.elem.data[namespace].gkey =3D gkey
> =C2=A0 =C2=A0 =C2=A0end=
>=C2=A0
> =C2=A0 =C2=A0 =C2=A0function userdata.= myfun(namespace,one,two)
> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0c= ontext.par()
> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0context("= ;elem: one=3D%s",userdata.elem.data[namespace][one])
> = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0context.par()
> =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0context("elem: two=3D%s",userdata.elem.da= ta[namespace][two])
> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0contex= t.par()
> =C2=A0 =C2=A0 =C2=A0end
> \stopluacode<= /div>
>=C2=A0
> \appendtoks
> =C2=A0 =C2= =A0 =C2=A0\setuevalue{\currentelem}{\elem_cmd{\currentelem}}%
>= ; =C2=A0 =C2=A0 =C2=A0\ctxlua{userdata.elem.define("\currentelem"= ,"\currentelemparent")}%
> \to \everydefineelem
>=C2=A0
> \appendtoks
>=C2=A0
= > \ctxlua{userdata.elem.setup("\currentelem","\elemparame= ter{key}","\elemparameter{gkey}")}%
> \to \ever= ysetupelem
>=C2=A0
> \unexpanded\def\elem_cmd#env= ironment%
> =C2=A0 =C2=A0{\edef\currentelem{#environment}%
> =C2=A0 =C2=A0 \dosingleempty\elem_cmd_parameters}
>= ;=C2=A0
> \def\elem_cmd_parameters[#parameters]#content%
=
> =C2=A0 =C2=A0{\setupelem[\currentelem][#parameters]
>= ; =C2=A0 =C2=A0 From \TeX\: Parameters: key=3D\elemparameter{key},
> gkey=3D\elemparameter{gkey}\par
> =C2=A0 =C2=A0 \ctxlua= {userdata.myfun("\currentelem","key","gkey")}= }
>=C2=A0
> \defineelem[base]
> \set= upelem[gkey=3Dgval]
>=C2=A0
> \protect
= >=C2=A0
> \starttext
>=C2=A0
> \b= ase[key=3Dval]{Hello}
>=C2=A0
> \stoptext
>=C2=A0
> A simpler alternative is:
>=C2=A0=
> % macros=3Dmkvi
>=C2=A0
> \unprot= ect
>=C2=A0
> \installnamespace{elem}
&= gt;=C2=A0
> \installcommandhandler \????elem {elem} \????elem<= /div>
>=C2=A0
> \startluacode
> =C2=A0 =C2= =A0 =C2=A0userdata.elem =3D { data =3D { =C2=A0} }
>=C2=A0
> =C2=A0 =C2=A0 =C2=A0function userdata.elem.sync(key,gkey)
=
> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0userdata.elem.data.key =3D key<= /div>
> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0userdata.elem.data.gkey = =3D gkey
> =C2=A0 =C2=A0 =C2=A0end
>=C2=A0
<= div>> =C2=A0 =C2=A0 =C2=A0function userdata.myfun(one,two)
>= ; =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0context.par()
> =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0context("elem: one=3D%s",userdata.elem.da= ta[one])
> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0context.par()
> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0context("elem: two=3D%s&= quot;,userdata.elem.data[two])
> =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0context.par()
> =C2=A0 =C2=A0 =C2=A0end
> \= stopluacode
>=C2=A0
> \appendtoks
> = =C2=A0 =C2=A0 =C2=A0\setuevalue{\currentelem}{\elem_cmd{\currentelem}}%
> \to \everydefineelem
>=C2=A0
> \unexp= anded\def\elem_cmd#environment%
> =C2=A0 =C2=A0{\edef\currente= lem{#environment}%
> =C2=A0 =C2=A0 \dosingleempty\elem_cmd_par= ameters}
>=C2=A0
> \unexpanded\def\elem_sync
>=C2=A0
> {\ctxlua{userdata.elem.sync("\elempara= meter{key}","\elemparameter{gkey}")}}
>=C2=A0
> \def\elem_cmd_parameters[#parameters]#content%
>= =C2=A0 =C2=A0{\setupcurrentelem[#parameters]
> =C2=A0 =C2=A0 = \elem_sync
> =C2=A0 =C2=A0 From \TeX\: Parameters: key=3D\elem= parameter{key},
> gkey=3D\elemparameter{gkey}\par
&g= t; =C2=A0 =C2=A0 \ctxlua{userdata.myfun("key","gkey")}}=
>=C2=A0
> \defineelem[base]
> \setu= pelem[gkey=3Dgval]
>=C2=A0
> \protect
&= gt;=C2=A0
> \starttext
>=C2=A0
> \ba= se[key=3Dval]{Hello}
>=C2=A0
> \stoptext

Thank you Hans!

Two general quest= ions arise (if your permit):

1. Where are the key-= value pairs stored?
In your first solution you handle the key set= ting by explicitly calling lua on every \setup<name> call. =C2=A0It s= eems that all key-value pairs (that are set with \installcommandhandler-gen= erated \setup<name>) are stored on the TeX side and not on the lua si= de. =C2=A0Is this correct?

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

n =3D ""<= /div>
v =3D ""
function getkey(name,value)
=C2=A0 =C2=A0n =3D name
=C2=A0 =C2=A0v =3D value
end<= /div>
function myfun(parameters,content)
=C2=A0 =C2=A0context= ([[\ctxlua{getkey("gkey","\elemparameter{gkey}")}]])
=C2=A0 =C2=A0context(" name=3D")
=C2=A0 =C2=A0c= ontext(n)
=C2=A0 =C2=A0context(", value=3D")
= =C2=A0 =C2=A0context(v)
end

The above do= es not work because the value obtained "lags behind", i.e. initia= lly v will be "", on the second call v will have the value of the= first \setupelem[gkey=3Dgval], and so on. =C2=A0I 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.
<= div>
Regards, Christoph
--089e0122774846077b05037e98f4-- --===============0767924761== Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline ___________________________________________________________________________________ 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 ___________________________________________________________________________________ --===============0767924761==--