How to create a conditionnal shortcut based on the text selected

Hi, I want to create a shortcut to comment my code. If I select some text, the text must be wrapped in /* */ or <!- --> according to the type of the tiddler, and if the selection is empty the whole line where the cursor is must be commented.

I can do the first part but when I tried to put a list widget to test if the saved text is empty or not, my code doesnt work : even though the selection is not empty, it default on the “empty” list widget.

Can someone help ?

Here’s my code :

<$action-sendmessage
$message="tm-edit-text-operation"
$param="save-selection"
tiddler=<<qualify "$:/temp/selection">>
/>

<!-- The selection is not empty -->
<$list filter="[<qualify '$:/temp/selection'>get[text]]">
<$action-sendmessage
    $message="tm-edit-text-operation"
    $param="wrap-selection"
    prefix="<!-- "
    suffix=" -->"
/>
</$list>

<!-- The selection is empty-->
<$list filter="[<qualify '$:/temp/selection'>get[text]else[]]">
<$action-sendmessage
    $message="tm-edit-text-operation"
    $param="wrap-lines"
    prefix="<!-- "
    suffix=" -->"
/>
</$list>
<$action-deletetiddler $tiddler=<<qualify "$:/temp/selection">> />

EDIT: Here’s the final result
$ _showInfo_shortcuts_comment(5).json (874 Bytes)

1 Like

Sounds like a refresh related issue. Try setting the variable tv-action-refresh-policy to true. See https://tiddlywiki.com/#ActionWidget%20Execution%20Modes

1 Like

Yes you were right ! I used

\define tv-action-refresh-policy() always

And it worked properly. There was also an error in my filter, I should have used :

<qualify '$:/temp/selection'>has[text]

I was able to get ride of the \define tv-action-refresh-policy() always by removing the list widget, it seems to prevent the refresh issue :

<$action-sendmessage
$message="tm-edit-text-operation"
$param="save-selection"
tiddler=<<qualify "$:/temp/selection">>
/>
<$action-sendmessage
    $message="tm-edit-text-operation"
    $param={{{[<qualify '$:/temp/selection'>has[text]then[wrap-selection]else[wrap-lines]]}}}
    prefix="<!--"
    suffix="-->"
/>
<$action-deletetiddler $tiddler=<<qualify "$:/temp/selection">> />

Thanks a lot for your help !

1 Like

@telumire - this is lovely! Specially the killer shortcut combination!
Will appear in the next update of TW-Scripts!
Thanks a million!

1 Like

@telumire inspired by your example and by cloning and editing your code, I thought it time to do some editor toolbar buttons with a long sought after feature of mine (Since TWC)

Other than implementing shortcuts here is what I just made.

text-sorter.json (3.7 KB)

It works but inserts unwanted blank lines perhaps new-line={{{ [charcode[13],[10]] }}} is incorrect.

Notes

  • I am not sure how but would love “ctrl-s-downarrow” “ctrl-s-uparrow” shortcuts
  • Another thought occurred to me is, as in your solution, not deleting the $:/temp/selection (I did not qualify mine) and allowing a paste/restore last button.
    • and perhaps even a copy to $:/temp/selection for repeating text slabs, useful using the mouse.

As a general rule actions have refresh issues if:

  • You are writing a field value and reading it in a subsequent action, and
  • the action widgets are wrapped in non-action widgets ($list, $let, etc) and
  • you are not setting tv-action-refresh-policy to always.
1 Like

That’s good to know, I’ll keep that in mind!

Glad you like this ! I’ve updated the first post with an improved version of the shortcut, check it out ! :slight_smile:

2 Likes