Macro with variable number of arguments

197 views
Skip to first unread message

MaxGyver

unread,
Apr 19, 2020, 11:05:31 AM4/19/20
to TiddlyWiki
Hi all,

I often use the kbd tag for showing (keyboard) keys in my TiddlyWiki, like that:

<kbd>Ctrl</kbd>+<kbd>Shift</kbd>+<kbd>A</kbd>

Typing this a lot is no fun, so I made a macro:

\define key(text: "Shift") <kbd>$text$</kbd>

It can be used this way:

<<key Ctrl>>

Now I could call this macro two or three times for creating a key combination. But it's even easier with additional macros:

\define combo2(key1: "Ctrl", key2: "x") <kbd>$key1$</kbd>+<kbd>$key2$</kbd>

\define combo3(key1: "Ctrl", key2: "Shift", key3: "x") <kbd>$key1$</kbd>+<kbd>$key2$</kbd>+<kbd>$key3$</kbd>

So I can write:

<<combo3 Ctrl Shift A>>

which generates the key combination of my example at top.

Just for curiosity:
Is is possible to create a single macro that works for either one, two or three keys?

Best regards,
Max
Message has been deleted

Mat

unread,
Apr 19, 2020, 11:13:56 AM4/19/20
to tiddl...@googlegroups.com
[pah! I edited and happened to repost and ... ah, whatever here ya go]

 
Macros themselves don't require that all arguments are actually used. If all are needed depends only on the logic that you create inside the macro. 

\define foo(bar, baz) $bar$ and $baz$

<<foo YO>> -------> YO and

However be careful because the first argument that you put in the macrocall is interpreted as the first parameter. So you must either specify which parameter:

<<foo bar:"YO">> ---------> and YO

(in this particular case, because there are no spaces in YO, it could actually be merely bar:YO )

or give an empty argument

<<foo "" YO>>  ---------> and YO


<:-)

Eric Shulman

unread,
Apr 19, 2020, 11:24:54 AM4/19/20
to tiddl...@googlegroups.com
On Sunday, April 19, 2020 at 8:05:31 AM UTC-7, MaxGyver wrote:
I often use the kbd tag for showing (keyboard) keys in my TiddlyWiki, like that:
\define combo3(key1: "Ctrl", key2: "Shift", key3: "x") <kbd>$key1$</kbd>+<kbd>$key2$</kbd>+<kbd>$key3$</kbd>
So I can write:
<<combo3 Ctrl Shift A>>
Is is possible to create a single macro that works for either one, two or three keys? 

This will do what you want:
\define key(key1,key2,key3)
\whitespace trim
<kbd>$key1$</kbd>
<$reveal text="$key2$" type="nomatch" default="">+<kbd>$key2$</
kbd></$reveal>
<$reveal text="$key3$" type="nomatch" default="">+<kbd>$key3$</kbd></$reveal>
\end

With the above macro, you can then write any of the following
<<key Ctrl>>
<
<key Ctrl Shift>>
<
<key Ctrl Shift A>>

Note: The <$reveal> widget usually takes a "state" parameter, which is the name of a tiddler containing a text field with the value to compare.  By omitting the "state" parameter, and using the "default" parameter instead, you can use $reveal to compare two literal values, without needs a tiddler in which to store a state value.

enjoy,
-e

MaxGyver

unread,
Apr 19, 2020, 11:42:17 AM4/19/20
to TiddlyWiki
@Mat: Thanks for your reply! Yes, I know that I can pass empty strings as arguments but I don't want to end up with empty keys. And I had no idea how to skip a key if an empty string was given.

@Eric: Awesome! This works perfectly. Thank you!

TonyM

unread,
Apr 19, 2020, 8:41:12 PM4/19/20
to TiddlyWiki
Max,

Personally I have a different approach to this; and it is more general in use. Paste this onto Tiddlywiki.com
  • If you are only making use of space delimiters anyway, do it yourself in a single parameter
  • This below defaults to space delimiter and + separator as you wish
  • By using comma you can do the same for multiple words
  • By using "/" you can make a system tiddler easier to read

\define keys(string delim:" " sep:"+")
<$list filter="[[$string$]split[$delim$]first[]]"><kbd><<currentTiddler>></kbd></$list>
<$list filter="[[$string$]split[$delim$]rest[]]"> $sep$ <kbd><<currentTiddler>></kbd></$list>
\end

<<keys "Ctrl Shift A B C D E F G">>
<<keys "Ctrl-Alt Del">>
<<keys "Control Panel, Keyboard Shortcuts" delim:"," ">" >>
<<keys "$:/editions/tw5.com/download-empty" delim:"/" " ">>
<<keys "$:/editions/tw5.com/download-empty" delim:"/" "/">>

Need any more features?

Regards
Tony

TonyM

unread,
Apr 19, 2020, 10:58:53 PM4/19/20
to TiddlyWiki
Updated

New Macro name and a button one as well

\define kbd(string delim:" " sep:"+")

<$list filter="[[$string$]split[$delim$]first[]]"><kbd><<currentTiddler>></kbd></$list>
<$list filter="[[$string$]split[$delim$]rest[]]"> $sep$ <kbd><<currentTiddler>></kbd></$list>
\end
\define dummy-button(string delim:" " sep:" ")
<$list filter="[[$string$]split[$delim$]first[]]"><$button tooltip="demonstration button only"><<currentTiddler>></$button></$list>
<$list filter="[[$string$]split[$delim$]rest[]]"> $sep$ <$button tooltip="demonstration button only"><<currentTiddler>></$button></$list>
\end

<<kbd "Ctrl Shift A B C D E F G">> <<dummy-button "OK">>


<<kbd "Ctrl-Alt Del">>
<<kbd "Control Panel, Keyboard Shortcuts" delim:"," ">" >>
<<kbd "$:/editions/tw5.com/download-empty" delim:"/" " ">>
<<kbd "$:/editions/tw5.com/download-empty" delim:"/" "/">>


<<dummy-button "Ctrl Shift A B C D E F G">>
<<dummy-button "Ctrl-Alt Del">>
<<dummy-button "Control Panel, Keyboard Shortcuts" delim:"," ">" >>
<<dummy-button "$:/editions/tw5.com/download-empty" delim:"/" " ">>
<<dummy-button "$:/editions/tw5.com/download-empty" delim:"/" "/">>

Mohammad

unread,
Apr 20, 2020, 12:07:36 AM4/20/20
to TiddlyWiki
Great solution Tony!
Added to TW-Scripts.

TonyM

unread,
Apr 20, 2020, 12:10:09 AM4/20/20
to TiddlyWiki
Mohammad,

I was thinking of you when writing this, perhaps adding your Shiraz colour options. Feel free to pillage :)

Regards
Tony

Mohammad

unread,
Apr 20, 2020, 12:14:17 AM4/20/20
to TiddlyWiki
Thanks Tony!
I make some modification on these normally!

MaxGyver

unread,
Apr 20, 2020, 5:24:14 AM4/20/20
to TiddlyWiki
Thanks, Tony!

I'm wondering how many people on earth really understand the TiddlyWiki macro language :-)

Why do you use currentTiddler as variable?

TonyM

unread,
Apr 20, 2020, 7:10:58 AM4/20/20
to TiddlyWiki
Max

An increasing number know it well, everyone in this thread are arguably experts. Its beauty is its flexibility. In reality macros are a very simple extensions of wiki text and widgets. Although its strength is in filters but these are very powerful and needs new concepts for many.

Those who have taken this journey and the gurus are paving the way for the next generation through support and development.

Enjoy the virtuose circle.

Regards
Tony

TonyM

unread,
Apr 20, 2020, 7:17:58 AM4/20/20
to TiddlyWiki
Max

I write as many of my macros I can using current tiddler so they can be used in different ways and depend on less parameters.

When you generate a list in a filter they are considered tiddler titles and use current Tiddler unless you set variable=varname.

Macros based on current tiddler are often valid in others lists.

Regards
Tony

Mat

unread,
Apr 20, 2020, 9:47:16 AM4/20/20
to TiddlyWiki
MaxGyver wrote:
Why do you use currentTiddler as variable?

See docs.

<:-) 

TonyM

unread,
Apr 20, 2020, 10:15:03 PM4/20/20
to TiddlyWiki
I will add to mats reference a less known storyTiddler https://tiddlywiki.com/prerelease/#storyTiddler%20Variable

Regards
Tony
Reply all
Reply to author
Forward
0 new messages