macro variable valuation

110 views
Skip to first unread message

Jean-Pierre Rivière

unread,
Apr 6, 2021, 9:19:36 AM4/6/21
to TiddlyWiki
I wanted to check if having space within a dictionary name or an index name was a go or a no-go for tw. So I wrote a macro to check it up (BTW, the answer is: no, it doesn't matter).

OK, now for what I want to say here, the macro will only print its arguments as is and as a wiki link.


\define spaceDemo(dico index)
!!! space demo for "$index$" in "$dico$" [[$dico$]] (not [[$dico$|$index$]])
\end

and now the invocation was

<<spaceDemo dico:"mon autre dico" index:"ma citation">>
<<spaceDemo dico:"mon autre dico" index:"ma-citation">>
<<spaceDemo dico:"mon-autre-dico" index:"ma citation">>
<<spaceDemo dico:"mon-autre-dico" index:"ma-citation">>

and I got what I thought I would get, like

space demo for "ma citation" in "mon autre dico" mon autre dico (not mon autre dico)

witg tge first link pointing to "mon autre dico" and the second to "ma citation".

But then I added two fields within thi tidder calling the macro:
* a "dico" field containing "mon autre dico"
* an "index" field containing "ma citation"

and I called the macro thus:

<<spaceDemo dico:{{!!dico}} index:{{!!index}}>>

and then, surprise!, I got:

space demo for "ma citation" in "mon autre dico" {{!!dico}} (not {{!!dico}})

with the first link to the litteral "{{!dico}}" and the second to "{{!!index}}".

I would have thought the argument transmitted would have been interpreted before calling the macro. And why then this differences of treatment? What is the mechanism responsible of this?

Practical interest, BTW: with" [[$arg$]]" you can see 
how really was written the argument "arg" when the macro was called.

Regards,






clutterstack

unread,
Apr 6, 2021, 10:35:12 AM4/6/21
to TiddlyWiki

Hi,

You've troubleshot it pretty well yourself, and you've run into a big difference between WikiText and the kind of programming you might be used to. To quote the docs, "Macros are in fact just parameterised variables". Things don't get evaluated everywhere we put them; generally they get substituted, and only at particular points do they get evaluated. It takes some adjustment to get used to.

The way I look at it, it's not so much that  "{{!!dico}}" and "{{!!index}}" are *not* wikified (evaluated) within the double square bracket. It's more that TW *does* wikify the non-linked ones to render the tiddler.

The text widget is a useful way of checking whether your macro outputs something that TiddlyWiki will wikify into the result you want.

Try

<$text text=<<spaceDemo dico:{{!!dico}} index:{{!!index}}>> />

to see what it really looks like when TiddlyWiki goes to wikify it for rendering:

space demo for "{{!!index}}" in "{{!!dico}}" [[{{!!dico}}]] (not [[{{!!dico}}|{{!!index}}]])

As you've noticed,  a text reference like that doesn't work inside the double square brackets of the link shorthand. If you type that out into a tiddler, you'll get the same result as if you run the macro.

The most obvious way I can think of to get the output you expect is to write out the link widget:

\define spaceDemo2(dico index)
space demo for "$index$" in "$dico$" <$link to=$dico$/> (not
<$link to=$index$>$dico$</$link>)
\end


Best,
Chris

Mark S.

unread,
Apr 6, 2021, 10:39:02 AM4/6/21
to TiddlyWiki
Macros with << >> syntax do simple text substitution. They look like "functions", but they are really just text substituters (new word).

Sometimes AFTER the substitution things will get rendered. So in your expression

!!! space demo for "$index$" in "$dico$" [[$dico$]] (not [[$dico$|$index$]])

only the first $dico$ gets rendered. That happens AFTER the substitution. The second and 3rd $dico$ are inserted literally, because they are inside more wikitext (the links).

Widgets, on the other hand, convert parameters before handling them. So you might get the result you expect with:

<$macrocall $name=spaceDemo dico={{!!dico}} index={{!!index}} />

Jean-Pierre Rivière

unread,
Apr 6, 2021, 11:49:39 AM4/6/21
to TiddlyWiki
Thank you Mark, this is most clear and straightforward.

So I've introduced an auxmacro to be sure what I get is what I want:

\define auxdemo(dico, index) <$macrocall $name=spacedemo dico=$dico$ index=$index$/>

ÇThat way, it will be easier to write my macro. No more ugly surprise. And yes it is usefull' because sometimes you cannot use the macrocall widget if one of your parameter needs to be a macrocall widget too! In fact, this and the impossibility to use a macro as parameter of a macro are annoying in the long run, a grammatical distraction from your programming goal to achieve one's useful purpose.


Jean-Pierre Rivière

unread,
Apr 6, 2021, 11:57:15 AM4/6/21
to TiddlyWiki
I tried the text widget thus:

* DEMO: <$text text="""<<demo {{!!dico}} {{!!index}}>>"""/>
* MACRO: <$text text="""<$macrocall $name=demo {{!!dico}} {{!!index}}/>"""/>

and I got:
  • DEMO: <<demo {{!!dico}} {{!!index}}>>
  • MACRO: <$macrocall $name=demo {{!!dico}} {{!!index}}/>
I thought that the macrocall parameter were to interpretated before the actual calling of the widget (that's what I understood from Mark S.) and was awaiting for an other result for the MACRO line.
  • DEMO: <<demo {{!!dico}} {{!!index}}>>
  • MACRO: <$macrocall $name=demo {{!!dico}} {{!!index}}/>

Mark S.

unread,
Apr 6, 2021, 12:37:26 PM4/6/21
to TiddlyWiki
On Tuesday, April 6, 2021 at 8:57:15 AM UTC-7 jn.pierr...@gmail.com wrote:
I tried the text widget thus:

* DEMO: <$text text="""<<demo {{!!dico}} {{!!index}}>>"""/>
* MACRO: <$text text="""<$macrocall $name=demo {{!!dico}} {{!!index}}/>"""/>


You wrapped the arguments in strings, so now it interprets your input as strings.  The only time that wrapping things in strings gets rendered is if you use the wikify widget. So you might play with

<$wikify text="""<<demo {{!!dico}} {{!!index}}>>""" name=rendered >
<$text text=<<rendered>>/>
</$wikify>

Note that wikify has a lot of options for output. So you might not even need the text widget if you render as text.

<$wikify text="""<<demo {{!!dico}} {{!!index}}>>""" name=rendered output="text">
<<rendered>>
</$wikify>

 

Jean-Pierre Rivière

unread,
Apr 6, 2021, 2:59:14 PM4/6/21
to TiddlyWiki
If I use

DEMO: <$text text=<<demo {{!!dico}} {{!!index}}>>/>

then I get the body of tho demo macro' with the parameters ($dico$ and $index$) replaced by the expressions sent as arguments (not their evaluation). It could be somewhat handy but it was not what was looked for.

for the MACRO line, I can't do anything else. I can't use a widget within a widget.

As for wikify, I play with it a lot, but I was unable to get what I wanted in complex situation. Surely a good hint to indicate that trying a new approach could be the way to go.

Now, I have just devised a way to have a source tiddler to be trancluded as source and being interpreted: The "code" tiddler has the code AND it is typed as plain text (this is REQUIRED). I'm doing this:

First, the tiddler where is the text to be seen
--------------------------------
!! source code
<code>
<$text text={{code}}/>
</code>
!! output ({{dictionary!!metal}})

<$wikify text={{code}} name=ex><<ex>></$wikify>

!! here a copycat of the code (hand made, no transclusion)

; value of {{!!index}} to be read in <$link to={{!!dico}}>{{!!dico}}</$link>
: field: (<$transclude tiddler={{!!dico}} field={{!!index}}/>)
: index: (<$transclude tiddler={{!!dico}} index={{!!index}}/>)
-----------------------------
with filelds "dico" set to "dictionary" and "index" to "metal".

now the tiddler with the source code (type: simple text)
------------------------------------
; value of {{!!index}} to be read in <$link to={{!!dico}}>{{!!dico}}</$link>
: field: (<$transclude tiddler={{!!dico}} field={{!!index}}/>)
: index: (<$transclude tiddler={{!!dico}} index={{!!index}}/>)
------------------------------------
But this is not perfect. The wiki interpretation is lacking some levels. For instance, the transcluded code does net interpret the wiki-style definition and do not produce a link, but the copycat does. but tghe fetching of the field and of the index works OK for both codes.

Jean-Pierre Rivière

unread,
Apr 6, 2021, 7:02:09 PM4/6/21
to TiddlyWiki
I've been able to have the link with my latest idea. But it adds an other tiddler, tgat I call codexe. this tiddler has a tw5 type and just transclude the code tiddler. Then my user tiddler transclude codexe.

Some code to make things clearer.

* tiddler codexe:
<$transclude tiddler=code mode=block/>

* tiddler user (modified bits):
<$wikify text={{codexe}} name=ex>
<<ex>>
</$wikify>

Jean-Pierre Rivière

unread,
Apr 7, 2021, 4:12:31 AM4/7/21
to TiddlyWiki
I've checked the view widget. For tge display, it has the same effect as the text widget today if used with "text" format.

And if used with "htmlwikified" format, it gives me the html source of what I want to display. Add a wikify widget, and that's it ! (No need for my "codexe" tiddler).

The code:

!! source code
<code><$view tiddler=code format=text></code>
!! output
<$wikify name=sample
text="<$view tiddler=code format=htmlwikified mode=block/>">
<<sample>></$wikify>

I should post a new message to have a clean way to expose the problem and the way to resolve it. It might help somebody.
Reply all
Reply to author
Forward
0 new messages