Can a global macro reference a field in the tiddler the macro is defined in?

124 views
Skip to first unread message

S. S.

unread,
Mar 22, 2019, 10:18:14 AM3/22/19
to TiddlyWiki

Within a tiddler that contains a global macro, I want to be able to reference a field within the tiddler where the macro is defined.

If I share the tiddler with the macro and that person renames the tiddler, the reference breaks. I want to stop this happening.

So I thought of having something like a hypothetical tv-macro-origin Variable, so that I can safely reference the tiddler & field the macro is in.

Example - I have a tiddler with fields:
title: showSeparatedText Macro
tags: $:/tags/Macro
separator: |

\define showSeparatedText(in out)
The original tiddler's separator is: {{showSeparatedText Macro!!separator}}
<br>
The hypothetical separator is: {{$(tv-macro-origin)$!!separator}}
<br>
This works: $in$ {{showSeparatedText Macro!!separator}} $out$
<br>
I want this to work: $in$ {{$(tv-macro-origin)$!!separator}} $out$
\end


In another tiddler I put this code:

<$macrocall $name="showSeparatedText" in="A" out="B"/>

The result:

The original tiddler's separator is: |
The hypothetical separator is:
This works: A | B
I want this to work: A B

I was hoping the recent discussion under : Test for existence of macro : could have helped, but it doesn't appear to.

Any pointers on how this can be achieved would be appreciated.

Thanks!


Mark S.

unread,
Mar 22, 2019, 11:24:08 AM3/22/19
to TiddlyWiki
Why not just make "separator" a 3rd parameter with a default of "|" ? What is the advantage or concern with storing the value in a field of the macro tiddler itself?

I think if you want to find the name of the containing tiddler, you will have to have the macro hunt for it before it actually runs the content of the macro. One way to do this would be to give the macro field a highly unique name. For instance mymacroname_separator. That should be easy to search for. A more difficult search would be to look for the string "\define mymacroname(" in every tiddler marked as a global macro. Either way will likely mean some small hit to performance.

-- Mark

Eric Shulman

unread,
Mar 22, 2019, 11:29:31 AM3/22/19
to TiddlyWiki
On Friday, March 22, 2019 at 7:18:14 AM UTC-7, S. S. wrote:
Within a tiddler that contains a global macro, I want to be able to reference a field within the tiddler where the macro is defined.
If I share the tiddler with the macro and that person renames the tiddler, the reference breaks. I want to stop this happening.

Try this:
{{{ [tag[$:/tags/Macro]search[define showSeparatedText]get[separator]] }}}

enjoy,
-e

Jeremy Ruston

unread,
Mar 22, 2019, 11:34:21 AM3/22/19
to tiddl...@googlegroups.com
Cunning! Just to point out that this interesting technique is really too slow for general use because it needs to do a full text search across all tiddlers tagged $:/tags/Macro.

The original question arose because SS was trying to accommodate end users renaming the tiddler containing a global macro. Given the prevalence of named links between tiddlers it is pretty hard to accommodate and so I wouldn't recommend it. (Packaging tiddlers as plugins might remove the temptation for end users to rename them).

Best wishes

Jeremy


--
You received this message because you are subscribed to the Google Groups "TiddlyWiki" group.
To unsubscribe from this group and stop receiving emails from it, send an email to tiddlywiki+...@googlegroups.com.
To post to this group, send email to tiddl...@googlegroups.com.
Visit this group at https://groups.google.com/group/tiddlywiki.
To view this discussion on the web visit https://groups.google.com/d/msgid/tiddlywiki/2252a4fe-be76-40b3-8b5a-c746c3569140%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Mark S.

unread,
Mar 22, 2019, 11:54:14 AM3/22/19
to TiddlyWiki
How does the performance of the "contains" operator compare?

You could use

{{{ [tag[$:/tags/Macro]contains[myidentifier]get[separator]] }}}

where "separator" and "[[My Macro Tiddler]]" are both in the list field of the global macro tiddler.

Then when the user changes the name of the tiddler, assuming they select "Update Tiddler X to Tiddler Y in the tags and list fields of other tiddlers" then the macro tiddler will be automatically updated to allow the name to be found.

Thanks!
-- Mark

Jeremy Ruston

unread,
Mar 22, 2019, 1:51:52 PM3/22/19
to tiddl...@googlegroups.com
Hi Mark


On 22 Mar 2019, at 15:54, 'Mark S.' via TiddlyWiki <tiddl...@googlegroups.com> wrote:

How does the performance of the "contains" operator compare?

The “contains” operator is a good deal faster than “search” because the semantics are much simpler: the former is just a basic string search, while the latter splits the search term into words etc. But it’s still very slow compared to directly accessing the field.

Best wishes

Jeremy.

TonyM

unread,
Mar 22, 2019, 8:17:16 PM3/22/19
to TiddlyWiki
S s

Nessesity is the mother of invention, but sometimes you can avoid a particular nessesity. Perhaps Let them rename the macro but let the macro create a settings tiddler, a system tiddler that stores settings that they do not care about and will not rename. Create the settings tiddler if it does not exist but use its values if it does.

Never the less if a special variable was available to access the title of the tiddler in which the macro resided rather than the current tiddler it would be useful. That would need a tiddlywiki DOM expert to implement.

It would be useful
For your use case
For tiddler s generated from templates that contain macros that inherit the macronames from the tiddler name. (eg new field definition tiddlers creating fieldname macros)
some tricky self referential algorithiums.

Regards
Tony

S. S.

unread,
Mar 23, 2019, 2:09:59 AM3/23/19
to TiddlyWiki
Thanks for all the input!

I thought about this a little more.
The reason for putting the parameter into a field is so that a user can change it.

It is quite possible the macro will be used in many places, so using the $list widget's search or contains operator suggested by Eric & Mark , both interesting solutions that had not occurred to me, might not be a viable idea.

Tony's suggestion to provide for the creation of a settings system tiddler meant making a button, which was fine, as a change of the setting anyway needs user input. And since there is a default separator parameter, this would work well.

However this led me to another idea, which was to have an $edit-box in the macro tiddler to change the separator field. Since the change would be only ONE character typed, the loss of focus on the edit box will be an acceptable compromise.

Now I'm starting to figure out how to limit the input into the edit box to just 1 character.

This does not limit the input:
Change the present ''separator'' here:
<$edit-text field="separator" placeholder="" size="1"/><small> ( Default is `-` )</small><br>
<small>When you delete the present separator, you may have to click again in the box before you can type the new one.</small>

Though the HTML <input> tag has an attribute: maxlength : it is not an attribute of the $edit-text widget as Mark pointed out in the Post last year: Question about maxlength
It may be useful for TiddlyWiki to include maxlength as an attribute of the $edit-text widget.

Perhaps the safe way is to use just the FIRST character in the separator field.

If I ever finish the macro successfully, I may include it on Mohammad's : Yazd: a KISS collection of Tiddlywiki macro :though he may reject it as it is turning out to be a MICK macro
(MICK - Made it Complicated, Knucklehead)

Cheers!


TonyM

unread,
Mar 23, 2019, 6:13:17 AM3/23/19
to TiddlyWiki
S S,

If the settings tiddler is named in the edit text widget it will be created when the person enters the separator character. Perhaps you can then test the length of the field and if its greater than one display "sorry one character only" and the 1st character. This way you do not need a button, the edit text field creates the tiddler/field, then for safety you could design your macro to only use the first.

Rgrads
Tony

S. S.

unread,
Mar 28, 2019, 6:54:43 AM3/28/19
to TiddlyWiki
Still, it would be useful to have a variable <<currentMacro>> that hold the title of the tiddler where the macro definition that is being called, resides.

TonyM

unread,
Mar 28, 2019, 8:08:34 AM3/28/19
to TiddlyWiki
S s
What about
\define mymacro(params)
<$set name=currentMacro value=tiddlername>

macro details

</$set>
\end

I know it needs to be set it in each macro tiddler but it goes someway.

Not withstanding I to would like to be able to access the source as well.

Did you see my practice of field(s) in macro tiddlers of the form macro-macroname with the syntax in the field value? You can then search for tiddler s containing all fields with a prefix of macro- or the field macro-macroname to find the tiddler in which it is defined.

Regards
Tony

Reply all
Reply to author
Forward
0 new messages