Re: [dev-context] A few questions

2009-08-13 Thread Wolfgang Schuster


Am 07.08.2009 um 14:33 schrieb Aditya Mahajan:

When I started using context, I was surprised that context did not  
have anything for optional arguments. We could have something like


\definesinglearugment\foo[#1]#2{...}

to be equal to

\def\foo{\dosingleargument\dofoo}
\def\dofoo[#1]#2{...}

and

\definesingleempty\foo[#1]#2{...]

to be equal to

\def\foo{\dosingleempty\dofoo}
\def\dofoo[#1]#2{...}

and similar things for \definedoublearugment, \definetripleargument,  
etc. I am not convinced that this is a good thing, but using  
something like this in the core may result in a 10% reduction in the  
code size and might kill a few dodos :)



It's now your turn to extend the macro to more optional arguments.

\def\define
 {\doifnextoptionalelse\dodefine\nodefine}

\def\nodefine{\dodefine[0]}

\def\dodefine[#1]#2%
 {\doifnextoptionalelse{\dododefine[#1]#2}{\nododefine[#1]#2}}

\def\dododefine[#1]#2[#3]#4%
 {\definecomplexorsimple#2
  \setvalue{simple\strippedcsname#2}{\getvalue{complex 
\strippedcsname#2}[#3]}%

  \ifcase0#1\relax
 \setvalue{complex\strippedcsname#2}{#4}%
 \setvalue{simple\strippedcsname #2}{#4}%
  \or\setvalue{complex\strippedcsname#2}[##1]{#4}%
  \or\setvalue{complex\strippedcsname#2}[##1]##2{#4}%
  \or\setvalue{complex\strippedcsname#2}[##1]##2##3{#4}%
  \or\setvalue{complex\strippedcsname#2}[##1]##2##3##4{#4}%
  \or\setvalue{complex\strippedcsname#2}[##1]##2##3##4##5{#4}%
  \or\setvalue{complex\strippedcsname#2}[##1]##2##3##4##5##6{#4}%
  \or\setvalue{complex\strippedcsname#2}[##1]##2##3##4##5##6##6{#4}%
  \or\setvalue{complex\strippedcsname#2}[##1]##2##3##4##5##6##7##8{#4}%
  \or\setvalue{complex\strippedcsname#2} 
[##1]##2##3##4##5##6##7##8##9{#4}%

  \fi}

\def\nododefine[#1]#2#3%
 {\ifx#2\undefined
  \else
\showmessage{systems}{4}{\string#2}%
  \fi
  \ifcase0#1\def#2{#3}%
  \or\def#2##1{#3}%
  \or\def#2##1##2{#3}%
  \or\def#2##1##2##3{#3}%
  \or\def#2##1##2##3##4{#3}%
  \or\def#2##1##2##3##4##5{#3}%
  \or\def#2##1##2##3##4##5##6{#3}%
  \or\def#2##1##2##3##4##5##6##7{#3}%
  \or\def#2##1##2##3##4##5##6##7##8{#3}%
  \or\def#2##1##2##3##4##5##6##7##8##9{#3}%
  \else\def#2{#3}%
  \fi}

\starttext

\define\foo{bar}

\define[2]\bar{bar:#1:#2}

\define[2]\help[me]{help #1 #2}

\foo

\bar{a}{b}

\help{john}

\help[you]{mike}

\stoptext

Wolfgang

___
dev-context mailing list
dev-context@ntg.nl
http://www.ntg.nl/mailman/listinfo/dev-context


Re: [dev-context] A few questions

2009-08-13 Thread Hans Hagen

Wolfgang Schuster wrote:


It's now your turn to extend the macro to more optional arguments.


hm, your challenging me? i must be crazy but here you go

\startluacode
local texsprint, ctxcatcodes = tex.sprint, tex.ctxcatcodes
local format, match, gmatch, rep = string.format, string.match, 
string.gmatch, string.rep

local empty = {
single,
double,
triple,
quadruple,
quintuple,
}
local check = {
first,
second,
third,
fourth,
fifth,
}
function commands.define(str)
-- we could store the defaults in lua and call lua instead but 
why bother

local arg, cmd = match(str,(.*)\\(.-)$)
local a = { }
for s in gmatch(arg,%[(.-)%]) do
a[#a+1] = s
end
local n = tonumber(a[#a])
if n then
a[#a] = nil
else
n = 0
end
texsprint(ctxcatcodes,format(\\unexpanded\\def\\%s,cmd))
if #a  0 then

texsprint(ctxcatcodes,format({\\do%sempty\\do%s},empty[#a],cmd))
texsprint(ctxcatcodes,format(\\def\\do%s,cmd))
for i=1,#a do
texsprint(ctxcatcodes,[#,i,])
end
texsprint(ctxcatcodes,{)
for i=#a,1,-1 do
texsprint(ctxcatcodes,format(\\if%sargument,check[i]))
texsprint(ctxcatcodes,format(\\def\\next{\\dodo%s,cmd))
for j=1,#a-i do
texsprint(ctxcatcodes,format([%s],a[j]))
end
for j=1,i do
texsprint(ctxcatcodes,format([#%s],j))
end
texsprint(ctxcatcodes,})
if i == 1 then
texsprint(ctxcatcodes,rep(\\fi,#a))
else
texsprint(ctxcatcodes,\\else)
end
end
texsprint(ctxcatcodes,\\next})
texsprint(ctxcatcodes,format(\\def\\dodo%s,cmd))
for i=1,#a do
texsprint(ctxcatcodes,[#,i,])
end
end
for i=1,n do
texsprint(ctxcatcodes,#,#a+i)
end
end
\stopluacode

\def\define#1#{\ctxlua{commands.define([[\detokenize{#1}]])}}

\starttext
\define[2]\whatevera{#1+#2}
\whatevera{A}{B}
\define[me][too][2]\whateverb{#1+#2+#3+#4}
\whateverb[A]{B}{C}
\whateverb[A][B]{C}{D}
\stoptext

we might add some \longs here and there


-
  Hans Hagen | PRAGMA ADE
  Ridderstraat 27 | 8061 GH Hasselt | The Netherlands
 tel: 038 477 53 69 | fax: 038 477 53 74 | www.pragma-ade.com
 | www.pragma-pod.nl
-
___
dev-context mailing list
dev-context@ntg.nl
http://www.ntg.nl/mailman/listinfo/dev-context


Re: [dev-context] A few questions

2009-08-07 Thread Wolfgang Schuster


Am 06.08.2009 um 23:42 schrieb Hans Hagen:


1. texdefinition
I like start/stoptexdefinition because it makes the source for  
certain macros more readable
than \def allows me but is there a way to use it for macros without  
parameters?


this works

\starttexdefinition test {}
oeps
\stoptexdefinition

[\test]


not very nice and don't look pleasant to me


we can consider something like:

\starttexdefinition test optional,optional,argument,argument
oeps
\stoptexdefinition

which then becomes

\def\test[#1][#2]#3#4%

etc but i'm only willing to support that if we can stick to  
reasonable definitions


or even

\starttexdefinition test optional:ns,argument:name
oeps
\stoptexdefinition

which then automatically makes you a \getvariable{ns}{key} as well  
as \name


or maybe like xml:

\starttexdefinition test ns:optional,argument
oeps
\stoptexdefinition

with then makes an \getvariable{ns}{first} and so

needs a bit of thinking


i get the feeling we're moving towards latex3 (brr), before this  
happens drop the whole

thing and i don't like \getvariable in this context anyway


2. commalists
Together with texdefinition I use now start/stopprocesscommalist  
(feature request: I need also
start/stopprocessassignmentlist) but I can use the list value  
without the need to define a command

where I specify what should happen with it
 \startprocesscommalist[a,b,c,...]
 the current list value is #1\par
 \stopprocesscommalist
but why is there not something equivalent for the normal  
processcommalist because the start/stop
method is not the best method when you need nested lists, something  
like this would be nice then

(I know \doprocesscommalist is already defined):
 \doprocesscommalist[a,b,c,...]%
   {\processcommalist[x,y,z,...]%
  {do something with #1 and ##1}}


hm, probably possible but not trivial (maybe not even robust) ...  
needs thinking (also a bit slower as we explicitly need a trick  
similar to the one used in dorecurse


when the inner list is used inside of a group the following works

  \long\def\processcommalistwithcontent[#1]#2%
{\def\currentcommalistcommand##1{#2}%
 \processcommalist[#1]\currentcommalistcommand}

  \processcommalistwithcontent[a,b,c] 
{{\processcommalistwithcontent[x,y,z]{This is subelement ##1 of  
element #1.\par}}}


but the extra pair of braces is kind of akward. Working without them  
is not the problem


  \newcount\commalistdepth

  \long\def\processcommalistwithcontent[#1]#2%
{\advance\commalistdepth\plusone
 \long\expandafter\def\csname list:\number\commalistdepth 
\endcsname##1{#2}%
 \def\currentcommalistcommand{\csname list:\number\commalistdepth 
\endcsname}%

 \processcommalist[#1]\currentcommalistcommand
 \advance\commalistdepth\minusone}

  \processcommalistwithcontent[a,b,c] 
{\processcommalistwithcontent[x,y,z]{This is subelement ##1 of element  
#1.\par}}


but as you mentioned above some kind of check (depth counter) is needed.


3. setups
The various start/stopsetups environments another nice feature but  
what's the correct method when
I want my own setup environment, should I adapt the following line  
from core-env to my own command
 \def\startsetups {\xxstartsetups\plusone  \stopsetups }  
\let\stopsetups \relax

and use method from lxml-ini to access/flush the content?


you mean that you want to pass an argument to a setup? (mandate in  
xmlsetup)


yes, more or less what you do with xml in mkiv

  \startxmlsetups ...
  do this in that
  \xmlflush{#1}
  do more things
  \stopxmlsetups

in my games module for the input for chess, go ...

  \startpgnsetups move
  do a few things before the move
  \pgnflush{#1}
  do a few things after the move
  \stoppgnsetups


4. Mixed assignment/comma lists
Are mixed lists of normal comma elements and assignments are  
allowed by ConTeXt’s syntax because
there is a command in the source (\processassignlist) to process  
the non assignment values from
the list? On the other side these values generate a error message  
on the terminal where one can read
there is a missing '=' in the element and this message can't be  
disabled (without hacks).


yeah, a matter of choice ... we can drop the message if users don't  
mind


we shouldn't end up with too many variants and personally i don't  
like the mix (i.e. foo==yes can be used)


if the context way is to avoid comma values and assignment values in  
the same list the message should better remain


Wolfgang

___
dev-context mailing list
dev-context@ntg.nl
http://www.ntg.nl/mailman/listinfo/dev-context


Re: [dev-context] A few questions

2009-08-07 Thread Wolfgang Schuster


Am 07.08.2009 um 12:02 schrieb Hans Hagen:


Wolfgang Schuster wrote:


not very nice and don't look pleasant to me


sure, but a pleasant solution takes me more time -)


that's why i asked, i wasn't able to write a pleasant one myself

i get the feeling we're moving towards latex3 (brr), before this  
happens drop the whole

thing and i don't like \getvariable in this context anyway


there was a presentation about this ltx3 low level syntax and i  
definitely don't want to have something like that; it's more that in  
context we have this system of [optional]{whatever} that we might  
want to support


me neither, the latex3 system is too complicated and one has to  
remember what n, N, x ... means etc.


there are a few interesting things like namespaces for module/macros  
but that's not worth the effort they take



in my games module for the input for chess, go ...
 \startpgnsetups move
 do a few things before the move
 \pgnflush{#1}
 do a few things after the move
 \stoppgnsetups


well, maybe something like

\starttexsetups move
do a few things before the move
\pgnflush{#1}
do a few things after the move
\stoptexsetups

as generic case?


i have to think about this but when i think about the current progress  
on the module i won't come back to this before 2010 :)


Wolfgang

___
dev-context mailing list
dev-context@ntg.nl
http://www.ntg.nl/mailman/listinfo/dev-context


Re: [dev-context] A few questions

2009-08-07 Thread Aditya Mahajan

On Fri, 7 Aug 2009, Hans Hagen wrote:

i get the feeling we're moving towards latex3 (brr), before this happens 
drop the whole

thing and i don't like \getvariable in this context anyway


there was a presentation about this ltx3 low level syntax and i definitely 
don't want to have something like that; it's more that in context we have 
this system of [optional]{whatever} that we might want to support


me neither, the latex3 system is too complicated and one has to remember 
what n, N, x ... means etc.


indeed, and also kind of redundant as the name should denote the usage, i.e. 
imagine that in e.g. lua we did  myfunction_takes_num_and_num


i would not be surprised if it's also quite slow (esp when used in core code) 
and core code is not used by users anyway


there are a few interesting things like namespaces for module/macros but 
that's not worth the effort they take


indeed, but keep in mind that in context we seldom have other interfaces that 
[] and {}, no optoinal * etc


One good thing with LaTeX 3 syntax is that something like

\DeclareDocumentCommand\foo { o o m } will be (from what I understand) 
equivalent to


\def\foo
 {\dodoubleempty\dofoo}

\def\dofoo[#1][#2]#3{...}

When I started using context, I was surprised that context did not have 
anything for optional arguments. We could have something like


\definesinglearugment\foo[#1]#2{...}

to be equal to

\def\foo{\dosingleargument\dofoo}
\def\dofoo[#1]#2{...}

and

\definesingleempty\foo[#1]#2{...]

to be equal to

\def\foo{\dosingleempty\dofoo}
\def\dofoo[#1]#2{...}

and similar things for \definedoublearugment, \definetripleargument, etc. 
I am not convinced that this is a good thing, but using something like 
this in the core may result in a 10% reduction in the code size and might 
kill a few dodos :)


Aditya
___
dev-context mailing list
dev-context@ntg.nl
http://www.ntg.nl/mailman/listinfo/dev-context


Re: [dev-context] A few questions

2009-08-07 Thread Hans Hagen

Aditya Mahajan wrote:

When I started using context, I was surprised that context did not have 
anything for optional arguments. We could have something like


\definesinglearugment\foo[#1]#2{...}

to be equal to

\def\foo{\dosingleargument\dofoo}
\def\dofoo[#1]#2{...}

and

\definesingleempty\foo[#1]#2{...]

to be equal to

\def\foo{\dosingleempty\dofoo}
\def\dofoo[#1]#2{...}

and similar things for \definedoublearugment, \definetripleargument, 
etc. I am not convinced that this is a good thing, but using something 
like this in the core may result in a 10% reduction in the code size and 
might kill a few dodos :)


there is \definecomplexorsimple

anyhow, it does not save commands and does not reduce the code size as 
you still need the intermediates; it might increase the format 
generation time with a few millisec


Hans

-
  Hans Hagen | PRAGMA ADE
  Ridderstraat 27 | 8061 GH Hasselt | The Netherlands
 tel: 038 477 53 69 | fax: 038 477 53 74 | www.pragma-ade.com
 | www.pragma-pod.nl
-
___
dev-context mailing list
dev-context@ntg.nl
http://www.ntg.nl/mailman/listinfo/dev-context


Re: [dev-context] A few questions

2009-08-06 Thread Hans Hagen

Wolfgang Schuster wrote:

Hi Hans,

here are a few questions which I want to ask since a few days.


1. texdefinition

I like start/stoptexdefinition because it makes the source for certain 
macros more readable
than \def allows me but is there a way to use it for macros without 
parameters?


this works

\starttexdefinition test {}
oeps
\stoptexdefinition

[\test]


we can consider something like:

\starttexdefinition test optional,optional,argument,argument
oeps
\stoptexdefinition

which then becomes

\def\test[#1][#2]#3#4%

etc but i'm only willing to support that if we can stick to reasonable 
definitions


or even

\starttexdefinition test optional:ns,argument:name
oeps
\stoptexdefinition

which then automatically makes you a \getvariable{ns}{key} as well as \name

or maybe like xml:

\starttexdefinition test ns:optional,argument
oeps
\stoptexdefinition

with then makes an \getvariable{ns}{first} and so

needs a bit of thinking


2. commalists

Together with texdefinition I use now start/stopprocesscommalist 
(feature request: I need also
start/stopprocessassignmentlist) but I can use the list value without 
the need to define a command

where I specify what should happen with it

  \startprocesscommalist[a,b,c,...]
  the current list value is #1\par
  \stopprocesscommalist

but why is there not something equivalent for the normal 
processcommalist because the start/stop
method is not the best method when you need nested lists, something like 
this would be nice then

(I know \doprocesscommalist is already defined):

  \doprocesscommalist[a,b,c,...]%
{\processcommalist[x,y,z,...]%
   {do something with #1 and ##1}}


hm, probably possible but not trivial (maybe not even robust) ... needs 
thinking (also a bit slower as we explicitly need a trick similar to the 
one used in dorecurse




3. setups

The various start/stopsetups environments another nice feature but 
what's the correct method when
I want my own setup environment, should I adapt the following line from 
core-env to my own command


  \def\startsetups {\xxstartsetups\plusone  \stopsetups } 
\let\stopsetups \relax


and use method from lxml-ini to access/flush the content?


you mean that you want to pass an argument to a setup? (mandate in 
xmlsetup)



4. Mixed assignment/comma lists

Are mixed lists of normal comma elements and assignments are allowed by 
ConTeXt’s syntax because
there is a command in the source (\processassignlist) to process the non 
assignment values from
the list? On the other side these values generate a error message on the 
terminal where one can read
there is a missing '=' in the element and this message can't be disabled 
(without hacks).


yeah, a matter of choice ... we can drop the message if users don't mind

we shouldn't end up with too many variants and personally i don't like 
the mix (i.e. foo==yes can be used)


Hans

-
  Hans Hagen | PRAGMA ADE
  Ridderstraat 27 | 8061 GH Hasselt | The Netherlands
 tel: 038 477 53 69 | fax: 038 477 53 74 | www.pragma-ade.com
 | www.pragma-pod.nl
-
___
dev-context mailing list
dev-context@ntg.nl
http://www.ntg.nl/mailman/listinfo/dev-context