Splat operators in TiddlyWiki macro definition, equivalent to *args in Python

47 views
Skip to first unread message

Tejasvi S Tomar

unread,
Sep 11, 2020, 3:18:15 AM9/11/20
to TiddlyWiki
Is there a way to specify an arbitrary number of parameters in the macro definition? My use case is to pass the tiddler name without using quotes. More specifically, in http://tobibeer.github.io/tw5-plugins/#inc, I want be able to transclude by <<{ Tiddler name>> which currently requires quoting, like <<{ "Tiddler name">>.

Eric Shulman

unread,
Sep 11, 2020, 3:48:06 AM9/11/20
to TiddlyWiki
On Friday, September 11, 2020 at 12:18:15 AM UTC-7, Tejasvi S Tomar wrote:
Is there a way to specify an arbitrary number of parameters in the macro definition? My use case is to pass the tiddler name without using quotes. More specifically, in http://tobibeer.github.io/tw5-plugins/#inc, I want be able to transclude by <<{ Tiddler name>> which currently requires quoting, like <<{ "Tiddler name">>.

When you pass arguments to a macro, the spaces define separate arguments.  Thus:
<<someMacro this is a test>>
has 4 arguments, "this", "is", "a", and "test" 

However, if you want to treat them all as one big argument, you can re-combine them in the macro itself, like this:
\define someMacro(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9)
<$vars arg={{{ [[$arg1$ $arg2$ $arg3$ $arg4$ $arg5$ $arg6$ $arg7$ $arg8$ $arg9$]trim[]] }}}>
...
\end
The $vars concatenates all the individual args into one long space-separated string and strips off any excess trailing spaces if the number of args passed in was less than 9 

-e

TW Tones

unread,
Sep 11, 2020, 3:56:35 AM9/11/20
to TiddlyWiki
Tejasvi,

In someways the answer is no, but there are plenty of alternatives.

Perhaps if your arbitrary number is still below a limit
\define mymacro(p1 p2 p3 p4 p5 p6 p7 p8 p9)
The parameters will be populated in the order they are given (as per Erics post that got here before me)

The other is to design you macro so you call it once for each parameter (if possible) 

If your list of parameters is already delimited in some way you can pass them in a single parameter and split them in the macro you called, 
  • eg with the split operator.
  • If they are titles obeying the rules "enlist" will turn a "list" of titles in each title (Deals with comas, spaces and [[tiddler names]]
  • Using """your list """ the triple quotes supports lists containing quotes.
However one trick is not to pass the parameter at all. Inside a macro you can refer to $(variablename)$ to use substitution and most probably the usual <<variablename>>

In many ways it depends in what form your arbitrary set of items is already in. Perhaps tell us.

Regards
Tony
Reply all
Reply to author
Forward
0 new messages