I'm having trouble with the join Operator (Or rather, I think that's what's giving me problems)

I often use a couple of “expand” macro, taken from the plugin “expand” by Ben Webber. (I edited them only very slightly) These macros allow me to hide text and show it if a term is clicked.

I saw in this topic recently “How can I get the title of the tiddler created after an excision?” some interesting ideas for creating an EditorToolbar button.
I took inspiration from there to create a button that allows me to quickly create these “expand” macros.
For those who have followed the topic the behavior is very similar, but with a simpler application.

But I only partially succeeded and I can’t figure out where I would have done something different/wrong.

The problem is in this part of the code:

\define block1() <<ex- """
\define block2() """ """TERM""">><<-pand """
\define block3() """ """
\define block4() """>>

\whitespace trim 

...

...

<$wikify name=expand-identifier-wikified text={{$:/EditorToolbar/button/ex-pand/dropdown!!expand-identifier-wikified}}>
<$set name="expand-identifier-wikified" value=<<expand-id>>>
<$button>
<$action-sendmessage
	$message="tm-edit-text-operation"
	$param="wrap-selection"
	prefix={{{ [<block1>] [<expand-id>]  [<block2>] [<expand-id>]  [<block3>]+[join[]] }}}
	suffix={{{ [<block4>] }}}
/>
</$button>
</$set>
</$wikify>

Everything is fine except for the identifier, [<expand-id>],which doesn’t appear at all.

Maybe to @Sobriety or @TW_Tones my mistake will be obvious, since the code from which I took inspiration is almost copied verbatim from yours. :sweat_smile:

  • Where is the error?

Whole tiddler: $ _EditorToolbar_button_ex-pand_dropdownTEST.json (3.5 KB)


P.S.

One more thing, but less important:
When I define \define block2()... is there a way to insert a line break without using <br>?
If I try to insert it using the return key, it stops recognizing the newline text as part of the “block2”

Just to be clear, I mean something like this:
\define block2() """ """TERM""">> Here <br> but without <br>? <<-pand """

-Sam

1 Like

Perhaps I’m missing something, but it doesn’t look like you’ve defined the “expand-id” variable, so there’s nothing to plug into your “prefix” filter.

The only other place I see “expand-id” mentioned is in the set widget <$set name="expand-identifier-wikified" value=<<expand-id>>>, which would create a variable <<expanded-identifier-wikified>> with value <<expand-id>>, if <<expand-id>> were defined elsewhere. (It also overwrites the variable you’d just defined with the wikify widget, but you don’t seem to be using <<expanded-identifier-wikified>> in any case.) Is it possible that you intended

<$set name="expand-id" value=<<expand-identifier-wikified>>>

instead?

If so, you probably don’t need both the $wikify and the $set, since the $set is only providing an alternate name for the variable you’ve just defined with $wikify. And depending on what sort of values you’re putting in $:/EditorToolbar/button/ex-pand/dropdown!!expand-identifier-wikified, you might not need to wikify at all. I’d try simplying both those widgets to

<$let expand-id={{$:/EditorToolbar/button/ex-pand/dropdown!!expand-identifier-wikified}}>
...
</$let>
1 Like

Try this:

\define block1() <<ex- """
\define block2()
""" """TERM""">>
<<-pand """
\end
\define block3() """ """
\define block4() """>>

Fred

1 Like

Yes! Damn I must have read the code 50 times but I never noticed that I had switched the names… Thank you @etardiff!

I tried to fix the code but it still remains a problem, it seems like that:

...
<$wikify name=expand-identifier-wikified text={{$:/EditorToolbar/button/ex-pand/dropdown!!expand-identifier}}>
<$set name="expand-id" value=<<expand-identifier-wikified>>>
<$button>
<$action-sendmessage
	$message="tm-edit-text-operation"
	$param="wrap-selection"
	prefix={{{ [<block1>] [<expand-id>] [<block2>] [<expand-id>] [<block3>]+[join[]] }}}
	suffix={{{ [<block4>] }}}
/>
...

recognizes only one of the expand-id variables.
Indeed as output it gives me:

<<ex- """""" """TERM""">><<-pand """ID""" """selection""">>

While it should give me:

<<ex- """ID""" """TERM""">><<-pand """ID""" """selection""">>

I also tried to set two different names, but nothing changes:

...
<$wikify name=expand-identifier-wikified text={{$:/EditorToolbar/button/ex-pand/dropdown!!expand-identifier}}>
<$set name="ex-id" value=<<expand-identifier-wikified>>>
<$set name="pand-id" value=<<expand-identifier-wikified>>>
<$button>
<$action-sendmessage
	$message="tm-edit-text-operation"
	$param="wrap-selection"
	prefix={{{ [<block1>] [<ex-id>] [<block2>] [<pand-id>] [<block3>]+[join[]] }}}
	suffix={{{ [<block4>] }}}
/>

... Same behavior: only the last one (pand-id) is handled correctly

I have no idea why.


I’d already tried going newline and then entering \end, but that didn’t work.
But unlike my previous attempts your piece of code works. :joy:

I must be very clumsy, because now that I see that it works I can’t understand how I couldn’t make it work before even though I had the same idea :sweat_smile:

Just think of how before I swapped name with value in the $set widget without realizing it for hours…
Thanks to you too @tw-FRed

Edit:
I understood how I couldn’t make it work.
Instead of:

\define block2()
""" """TERM""">>
<<-pand """
\end

I wrote:

\define block2() """ """TERM""">>
<<-pand """
\end

But this way, without a newline after the “()” only """ """TERM"">" (and it alone) was recognized as the argument of the /define and the rest was not interpreted anymore. Well, I learned a new thing today.


…and I realized that the json I attached before was incomplete:

$ _EditorToolbar_button_ex-pand_dropdown.json (3.7 KB)
$ _EditorToolbar_button_ex-pand.json (438 Bytes)

This behavior is normal and well documented, it’s called Dominant Append (https://tiddlywiki.com/#Dominant%20Append) and its effect is to remove duplicates.

The solution is to use the = filter run prefix on filter runs which can produce duplicates, like this:

prefix={{{ [<block1>] =[<expand-id>] [<block2>] =[<expand-id>] [<block3>]+[join[]] }}}

Fred

Ah that’s great, I just looked and it’s very helpful.
Now everything works, thank you Fred! :grinning_face_with_smiling_eyes:

And if you want to ensure duplicates there is (I believe) no harm applying this to all runs in a filter;

{{{ =[<block1>] =[<expand-id>] =[<block2>] =[<expand-id>] =[<block3>]+[join[]] }}}
  • This helps stop the annoying case of unexpected outputs because you forgot to put a single character = somewhere you should have :frowning_face:
1 Like

I have a doubt: it is possible using <$action-sendmessage $message="tm-edit-text-operation" a line break like this:

(after “HERE” in the example 1 there is a line break, in example 2 there is not)

example 1
<<ex- """New Tiddler""" """TERM""">>
<<-pand """New Tiddler""" 
"""HERE
""">>

example 2
<<ex- """New Tiddler""" """TERM""">>
<<-pand """New Tiddler""" 
"""HERE""">>

  • How can I add it?
\define block1() <<ex- """
\define block2() 
""" """TERM""">>
<<-pand """
\end
\define block3() """ """
\define block4() 
???
""">>
\end
...

...<$action-sendmessage
	$message="tm-edit-text-operation"
	$param="wrap-selection"
	prefix={{{ =[<block1>] =[<expand-id>] =[<block2>] =[<expand-id>] =[<block3>]+[join[]] }}}
	suffix={{{ =[<block4>] }}}
/>

I think I have to edit something where I define block4 (where I put ???). Or perhaps something in the suffix={{{... (?)

Hi @SnapSam,

You can insert a “new line” character before block4 like this:

suffix={{{ =[charcode[10]] =[<block4>] }}}

Fred

Seems like a good approach, but it doesn’t work. Good cue though
Thanks anyway @tw-FRed :grinning:

Oups! I forgot to add +[join[]] to the suffix.
It should read:

suffix={{{ =[charcode[10]] =[<block4>] +[join[]] }}}

Fred

1 Like

Right! I should have learned by now, I should have figured it out. Thank you again!