Is there a way to make an append to line text-operation?

Is there a way to make an append to each line text-operation for transforming lines into entries of a dictionary-tiddler?

Certainly! Here are a couple of approaches, depending on your needs:

\function transform.line() [search-replace[ ],[: ]]

<$let
	tiddler="Sample text"
	text={{{ [<tiddler>get[text]] }}}
	lbr="
"
>
<$button>
	Convert to dictionary
<$action-setfield $tiddler=<<tiddler>> $field=text
	$value={{{ [<text>splitregexp[\n]] :map[transform.line[]] +[join<lbr>] }}} />
</$button>
</$let>

I wasn’t sure what sort of transformation you’re looking for, so I wrote a simple function to make the first word of each line the index name. You could alternately prefix something, or do other more complicated transformations.

You could alternately take advantage of <$action-setfield $index=... /> to write each line as an index in a new tiddler.

\define key() entry $(#)$

<$let
	input={{Sample text}}
	target="MyTarget"
>
<$button>
	Convert to dictionary
<$list filter="[<input>splitregexp[\n]]" counter="#">
	<$action-setfield $tiddler=<<target>> $index=<<key>> $value={{!!title}} />
</$list>
</$button>
</$let>

This makes it a bit easier to set the names of the indexes.

By default, this will produce a type: application/json tiddler rather than a data dictionary (type: application/x-tiddler-dictionary). They work the same way from a filter perspective, but if you’d rather have a data dictionary for legibility or ease of manual editing, you’ll need to either set the target tiddler’s type to “application/x-tiddler-dictionary” in advance, or add an extra $action-setfield to do it (before the $list).

You could also use $action-setmultiplefields (with $indexes) in place of the $list/$action-setfield combination, which might be easiest if you have separate lists of dictionary keys and dictionary definitions to pair up.

3 Likes

@JanJo I think @etardiff has described this well without too much information from you. Especialy where the key is in the text.

There are many ways to append text and prepend in tiddlywiki including the append operator, addprefix and various approaches using substitution. As emilys example mentions if you used the index it effectivly prepends the line.

When handling lines another key approach is splitting it into lines in the first place.

'[[Tiddlername]get[text]splitregexp[\n]]`

Hi and thank you @etardiff …
I was thinking about the following scenarionwhich is quite common for me:
I ad 15 issues to an existing x-tiddler-dictionary .
I would like to select them and then add the doublepoint with a click. For prepending like * bullets this is a simple edit-text action.

I think it’s almost definitely possible to make a editor toolbar button that would do it, and as you said, I’d start by cloning something like the bullets button. If you could give me a sample of your text and the kind of output you want (where : should go, in particular) I can probably offer more guidance.

Hi @etardiff
After the “key” like:

elephant
banana
…
→
elephant:
banana:

Oh, that’s a lot simpler than I was thinking! Sorry for over-complicating on you.

Here’s a package for you:

dictionary index button.json (1.7 KB)

And here’s what I did:

  • I started out by cloning $:/core/ui/EditorToolbar/list-bullet, which I found via the $:/tags/EditorToolbar tag.
  • The editor toolbar buttons use variants on <$action-sendmessage $message="tm-edit-text-operation" ..., as laid out here.
    • The list-bullet button uses $param="prefix-lines". It’d be ideal if we had a corresponding suffix-lines… but we don’t seem to.
    • Instead, we’ll need to use a combination:
      • save-selection, to save to a temporary tiddler), and
      • replace-selection, to perform some filter operations on the text we just saved, and then replace the selected text with the modified version. (This is where get[text]splitregexp[\n] comes in again.)
      • Here’s what the bulk of our new button looks like:
\define temp() $:/temp/make-key

<$let
	lbr="
">
<$action-sendmessage
	$message="tm-edit-text-operation"
	$param="save-selection"
	tiddler=<<temp>>
/>
<$action-sendmessage
	$message="tm-edit-text-operation"
	$param="replace-selection"
	text={{{ [<temp>get[text]splitregexp[\n]] +[addsuffix[: ]] +[join<lbr>] }}}
/>
<$action-deletetiddler $tiddler=<<temp>> />
</$let>
  • You’ll also want to edit the caption, condition, description, icon, and shortcuts fields.
    • make sure to add [<targetTiddler>type[application/x-tiddler-dictionary]] to the condition field, so it will show up in a dictionary tiddler
    • note down the name you used in the shortcuts field: mine is ((dictionary-key))
  • Last, make the corresponding shortcut config tiddler to set up your chosen hotkey.
    • The title needs to be $:/config/shortcuts/ + the shortcuts value you just chose, so mine is $:/config/shortcuts/dictionary-key.
    • The text field has the key combo you want to use. Feel free to reconfigure to suit your usage.

And that should be it!

2 Likes