clever default value for a parameter

86 views
Skip to first unread message

Jean-Pierre Rivière

unread,
Dec 1, 2020, 3:09:38 PM12/1/20
to TiddlyWiki
I am writing a macro with three parameters. If omitted, the third shall be equal to the first one. This cannot be hardwired of course! So I need to code it. With a filter.

As far as I have seen, only the "regexp" filter can be used.

Here is my code. I try to replace "excluding" argument by "rejecting" variable in order to get things done. The "NO" and "YES" are juste here to help going quick with the results.

\define itemlist2(tag:"thème", field:"theme", excluding)
<$set name="rejecting" filter="[$excluding$]+[regexp[^.]then[YES $excluding$]else[NO $tag$]]">
tag=$tag$, field=$field$, excluding=$excluding$, rejecting=<<rejecting>>.<br>
</$set>
\end

In a tiddler named "choix des critères du thème 5" I have the following code:

<$set name=beg value="choix des critères du ">
<$set name=theme2 filter="[{!!title}removeprefix<beg>]">
<$wikify name=theme text=<<theme2>>>
<$macrocall $name=itemlist2 tag=<<theme-label>> field=criterion excluding=<<theme>>/>
</$wikify></$wikify>
</$set></$set>

and I get 

tag=tableaux, field=criterion, excluding=thème 5, rejecting=NO tableaux.

instead of

tag=tableaux, field=criterion, excluding=thème 5, rejecting=YES thème 5

Note as a side question that I had to use theme2 and theme to get the pure text "thème 5" because theme2 would be a link!!! Why so ???

I have tried other regexp in my code, with eventually a negative logic. At first, I would have thought that

regexp[^$]

would target only an empty string. but the overall result is the same. In fact, my problem is really a problem of filtering empty strings.



Eric Shulman

unread,
Dec 1, 2020, 3:24:39 PM12/1/20
to TiddlyWiki
On Tuesday, December 1, 2020 at 12:09:38 PM UTC-8 jn.pierr...@gmail.com wrote:
I am writing a macro with three parameters. If omitted, the third shall be equal to the first one. This cannot be hardwired of course! So I need to code it. With a filter.
\define itemlist2(tag:"thème", field:"theme", excluding)
<$set name="rejecting" filter="[$excluding$]+[regexp[^.]then[YES $excluding$]else[NO $tag$]]">
tag=$tag$, field=$field$, excluding=$excluding$, rejecting=<<rejecting>>.<br>
</$set>
\end

Try using the !match[...] filter, like this:
\define itemlist2(tag:"thème", field:"theme", excluding)
<$set name="rejecting" filter="[[$excluding$]!match[]then[$excluding$]else[$tag$]]">
tag=$tag$, field=$field$, excluding=$excluding$, rejecting=<<rejecting>>.<br>
</$set>
\end

-e

Jean-Pierre Rivière

unread,
Dec 2, 2020, 5:36:18 AM12/2/20
to TiddlyWiki
@Eric. Tahnk you! It does the trick but... not so much as I wanted. see the output I get:

unwanted-link.png

"thème 5" is indeed the tittle of an existing tiddler.

What I am looking for is only the text "thème 5", not the link to "thème 5".

Now, I know how to use $wikify to acieve that goal with the current linking result. But what I don't understand is why I got this linking in the first place!

my code (same as yours):

\define itemlist2(tag:"thème", field:"theme", excluding)
<$set name="rejecting" filter="[[$excluding$]!match[]then[$excluding$]else[$tag$]]">
tag=$tag$, field=$field$, excluding=$excluding$, rejecting=<<rejecting>>.
</$set>
\end

and my using that code (to get the picture above) in tiddler "choix des critères du thème 5"

<$set name=beg value="choix des critères du ">
<$set name=theme2 filter="[{!!title}removeprefix<beg>]">
<$wikify name=theme3 text="<<theme2>>">
<$wikify name="theme-label" text="""<$transclude tiddler=<<theme3>> field=theme/>""">
beg=<<beg>> theme2=<<theme2>> theme3=<<<theme3>> theme-label=<<theme-label>><br>
<$macrocall $name=itemlist2 tag=<<theme-label>> field=criterion excluding=<<theme3>>/>
</$wikify></$wikify>
</$set></$set>

And I get also this line:

unwanted-link2.png

(where I neither understand why theme2 is a link and theme3 is void)

Jean-Pierre Rivière

unread,
Dec 2, 2020, 6:14:19 AM12/2/20
to TiddlyWiki
a typo in my code : theme3=<<<theme3>> instead of theme3=<<theme3>>
with that fix, I can see theme3 as a simple text, "thème 5" in the result line beginning by bag=

as for the code with removeprefix, it works OK in an other tiddler. But here, it keeps being dizzy. the tiddler types are all set to tw5. any idea to spot the cause of this?

Jean-Pierre Rivière

unread,
Dec 2, 2020, 6:23:40 AM12/2/20
to TiddlyWiki
Further investigating: the difference lies in the title and what you finally get. If you're left with a single word, then you get a simple text. If you have any space, then it's a link.

thus the following code 

<$set name="last" filter="[{!!title}removeprefix[jingle ]]"><<last>></$set>

will produce a "bells" simple text in the "jingle bells" tiddler BUT a "bells song" link in a "jingle bells song" tiddler.

Saq Imtiaz

unread,
Dec 2, 2020, 6:45:38 AM12/2/20
to TiddlyWiki
<$set name="last" filter="[{!!title}removeprefix[jingle ]]" select="0"><<last>></$set>  

When assigning the value of a set widget via a filter, it is important to note that the value is a title list and not a title.
In a title list, tiddler titles with spaces are enclosed in double brackets.

Please see the section Filtered List Variable Assignment

Filtered List Variable Assignment

This form of the set variable widget evaluates the filter and assigns the result to the variable as a space-separated list (using double square brackets for titles containing spaces).

<$set name="myVariable" filter="[tag[HelloThere]]"> <$text text=<<myVariable>>/> </$set>Filtered Item Variable Assignment

 NEW IN: 5.1.14 This form of the set variable widget evaluates the filter and assigns the specified result to the variable as a single item (ie, not using double square brackets for titles containing spaces).

<$set name="myVariable" filter="[tag[HelloThere]]" select="0"> <$text text=<<myVariable>>/> </$set>  

Reply all
Reply to author
Forward
0 new messages