Escape expansion in Smart Selection using `Send text...`

14 views
Skip to first unread message

Robbie Gates

unread,
Jun 28, 2024, 8:30:47 PM (4 days ago) Jun 28
to iterm2-discuss
Hi All,
  I'm getting "not the behaviour I expect" when I have a Smart Selection using `Send text...` action (maybe it's a bug, maybe my expectations are wrong).

Basically, I have a Smart Selection which does a `Send text...` of `\1` which I use to "click to copy" various things. I noticed however that when I used it to copy

echo '\t\n'

it was sent as 

echo '\t'

- the \n has been suppressed. A bit of digging later, and I I think it's because of the  `computeParameterForActionDict` expansion order, which is called by `contextMenuActionSendText`. Sorry I haven't run it up in the Xcode debugger [thought I'd check it's not intended or unfixable before digging more]. I have a debuglog.txt showing the `\n` has been expanded near there, I think it suppressed later in the text entry.


    for (int i = 0; i < 9; i++) {

        NSString *repl = @"";

        if (i < components.count) {

            repl = [self parameterValue:[components objectAtIndex:i]

                       encodedForAction:action];

        }

        parameter = [parameter stringByReplacingBackreference:i withString:repl ?: @""];

    }


    NSString *workingDirectory = [scope path];

    NSString *hostname = [scope hostname];

    NSString *username = [scope username];


    parameter = [parameter stringByReplacingEscapedChar:'d' withString:workingDirectory ?: @"."];

    parameter = [parameter stringByReplacingEscapedChar:'h' withString:hostname];

    parameter = [parameter stringByReplacingEscapedChar:'u' withString:username];

    parameter = [parameter stringByReplacingEscapedChar:'n' withString:@"\n"];

    parameter = [parameter stringByReplacingEscapedChar:'\\' withString:@"\\"];


which performs the string escaping of the `Send text...` parameter _after_ expanding the `\1` match group. Naively, I would want it the other way around - the special characters are expanded only when literally present in the format string in the `Parameter` column.

In testing, I see that `\d` for example is also expanded when in the *captured* text.

The "Use interpolated strings for parameters (affects all rules)" checkbox looks attractive, but "affects all rules" is a problem for me (I have other smart selections that use `\d` to run commands in the right place).

Sooooo ....
a) is this considered a bug, and would a patch to switching the order of the blocks above be well received ? If so, would you want it guarded by a "Advanced Settings" flag (I guess it's plausible that people want / are relying on current behaviour?).
b) I guess I could imagine a per rule checkbox managing this behaviour - that'd be more work but if that's the direction you want I'll have a crack.
c) A coarse workaround for me would be a "Send text with limited interpolation..." action, but that feels pretty bespoke and weird ... I guess "Send parameter as text..." might be plausible?

Lmk if you'd rather a bug on gitlab, and I'm happy to whack some code together if you lmk which direction is preferable (I'll just need to get Xcode working properly on my machine again :-) ).

 - Robbie

p.s. as always - thanks for iTerm2 - it's :amaze:

George Nachman

unread,
Jul 1, 2024, 2:18:24 AM (2 days ago) Jul 1
to iterm2-...@googlegroups.com
This code is kind of a mess, as you discovered. Changing it will also break existing workflows, which I hate to do. Interpolated strings are meant to be the way out of this quagmire because they are not a mess and it gives everyone the option to break their workflows when it suits them :).

Replacing \d with \(path) should be straightforward, unless you rely on the rather quirky existing behavior of using “." in place of a missing value (which I should offer a workaround for—and doing so is not hard).

I think the path of least agony is to make switching to interpolated strings easier for you and everyone else. For example, a Python script that performs the migration would be reasonably easy to write. Would that be a welcome solution?

--
You received this message because you are subscribed to the Google Groups "iterm2-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to iterm2-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/iterm2-discuss/1a03ca89-225e-46b3-8737-02c4bfae6a89n%40googlegroups.com.

Robbie Gates

unread,
Jul 2, 2024, 7:15:57 AM (22 hours ago) Jul 2
to iterm2-discuss
Hi George,
  I didn't realise the interpolated strings thing was a complete replacement - thanks. I had not realised I could use `\(path)` for `\d`. I don't have that many rules, so it was easy enough to migrate them all once I knew the replacement for \d - I don't need a script. So any way, I'm all good now - no fixes needed.

How could I have helped myself earlier?

I don't remember exactly what else I explored that I didn't cover above, but I suspect I clicked on the ? next to the "Use interpolated strings" checkbox, found https://iterm2.com/documentation-smart-selection.html - I'm not sure if I clicked on `interpolated string` under Actions there - if I did, I probably got to the description of contexts and thought that the bit at the top of the dialog (describing `\(matches[I])` was the complete context in this case. I think that if the second paragraph under Actions in the Smart Selection documentation there had a brief description of the context of the interpolated string for smart selection it might have helped. Once I was in the "Scripting Fundamentals" doc I figured I'd taken a wrong turn.

could say more, but I guess you can't put a link there, and there's a limit to the amount of text that can go there. Maybe even just adding "See documentation for additional available context" would have made me look harder.

thanks again for unblocking me, as well as for iTerm2.

 - Robbie

Reply all
Reply to author
Forward
0 new messages