Substitute selected text in a snippet multiple times

I have a snippet which the selected text needs to be replaced twice.

Let’s say the selected text is HELLO.

The snippet tiddlers below will create:

<$button actions="<<my-macro 'HELLO'>>"> button-text </$button>
                              ^^^^^ 

What would like to have is :

<$button actions="<<my-macro 'HELLO'>>"> HELLO </$button>
                              ^^^^^      ^^^^^

Is there an easy way of doing this?

title: $:/language/Snippets/my-button
tags: $:/tags/TextEditor/Snippet
caption: My button

+======+
title: $:/language/Snippets/my-button/prefix

<$button actions="<<my-macro '
+======+
title: $:/language/Snippets/my-button/suffix

'>>"> button-text </$button>
+======+

Hi,

As far as I know this is not possible with a snippet, but you can do it in a customized editor toolbar button.

Not a complete solution, but you can follow this tutorial to create a new editor button, and use this code inside:

<$action-sendmessage
	$message="tm-edit-text-operation"
	$param="save-selection"
	tiddler="$:/temp/currentSelection"
/>
<$action-sendmessage
	$message="tm-edit-text-operation"
	$param="insert-text"
	text={{{ [[<$button actions="<<my-macro ']] =[{$:/temp/currentSelection}] [['>>"> ]] =[{$:/temp/currentSelection}] [[ </$button>]] +[join[]] }}}
/>

The first <$action-sendmessage> widget saves current selection in a temporary tiddler, then the second <$action-sendmessage> widget replaces the selection with the result of the text attribute filter. Note that the content of the temporary tiddler is used twice in the filter, which implies prefixing it with a = filter run prefix in order to prevent deduplication (a result of Dominant Append).

Fred

2 Likes

Hi Fred,

Used snippet example to keep the question simple. For my use case, an editor toolbar button is preferred - so your solution is what I was actually looking for.

Was unaware of how to get the filter to duplicate an element - thanks for referencing prevent deduplication or another way of saying ‘allow duplicates’. Is what was causing my grief.

1 Like

Pic2go,

I have reflected on your request for sometime. I am glad @tw-FRed provided the answer. However,

  • A snippet can be any code, so it could include a macro call or transclude another tiddler
  • This macro or transclusion can be provided with parameters
  • Within the macro (or procedure) or transcluded tiddler definition you can refer to the same parameter multiple times, thus reusing the parameters value multiple times.

Snippets tend to be static content, where editor toolbar buttons are a set of actions and specifically permit actions using the selected text.