Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Regex in trigger #146

Closed
Kwelity opened this issue Dec 18, 2019 · 9 comments
Closed

Regex in trigger #146

Kwelity opened this issue Dec 18, 2019 · 9 comments

Comments

@Kwelity
Copy link

Kwelity commented Dec 18, 2019

Hi,

It would be cool to use regular expression for trigger, something like this:

- trigger: "^[fF]oo"
  replace: "bar"

Thanks for an awesome project :)

@federico-terzi
Copy link
Collaborator

Hey @Kwelity

Sorry for the late reply, it's been a super busy week!
Thanks for the feedback, it would certainly be a good feature to have. Due to the way espanso works, implementing full regex support would be problematic though.

Besides the UPPER/lower case insensitivity, which use-cases would benefit from it in your opinion?
If we individuate a set of cases, we could think about implementing a subset of regex to solve them.

Thanks for your help :)

@Kwelity
Copy link
Author

Kwelity commented Dec 18, 2019

Hum, my most "pressing" feature request would be simplify multi trigger with regex, I think I saw another issue about multi trigger. This would solve it just fine.

What I could imagine doing if we had full regex support (I do understand that this is not a trivial feature and might be to demanding in term of performance) would be to capture pattern in a trigger that are then used in replace.

Something like:

- trigger: "^:foo(\w*):
  replace: "\1"

so that :foobar: would evaluate to bar and :foostuff: to stuff

even better with script where group could be used as parameters:

- trigger: ":xo(\d)"
  replace: "{{output}}"
  vars:
   - name: output
     type: shell
     params:
       cmd: printf 'xo%.s' {1..$1}

Ok, this one I improvised on the spot, I'm not really sure how it would work, maybe pass the captured group as arguments.
But it would give for example: :xo5 -> xoxoxoxoxo

@federico-terzi
Copy link
Collaborator

Hey @Kwelity,

I agree that full regex support would be ideal. Unfortunately, due to the "realtime" nature of espanso, this is very difficult to achieve. That said, it seems like you want something along the lines of #118, which is one of the most requested features (hopefully coming soon).

I'll think about a way to implement regex support anyway, thank you for the help :)

@Kwelity
Copy link
Author

Kwelity commented Dec 22, 2019

@federico-terzi sure thing, I understand, I'll keep an eyes on the issue you mentioned.
If you'd like, you can close this issue.

@khaveesh
Copy link

This would be a really cool feature. I use regex in Typinator app to correct double capitalized words. Example - DAsh to Dash. This is something that I regularly encounter when I hold the Shift key for slightly too long. Wish you would reopen this issue.

@federico-terzi
Copy link
Collaborator

@khaveesh Unfortunately, as I said, it's very unlikely that full regex support will make its way into espanso due to performance reasons (some users run up to 50k matches, can you imagine checking every one of them after each character is pressed? that would either be a performance disaster or require implementing a full parallel regex engine such as hyperscan which is out of scope at the moment).

That said, if your goal is to correct double capitalized words, you can open an issue specifically for that :)

@chrisgrieser
Copy link

This would be a really cool feature. I use regex in Typinator app to correct double capitalized words. Example - DAsh to Dash. This is something that I regularly encounter when I hold the Shift key for slightly too long. Wish you would reopen this issue.

In case anyone finds this via Google like me, Espanso by now has Regex triggers and the double capitalization fixing can be implemented with this:

matches:
  - regex: "\\b(?P<one>[A-Z])(?P<two>[A-Z])(?P<three>[a-z])"
    replace: "{{CapitalCased}}"
    vars:
      - name: CapitalCased
        type: shell
        params:
          cmd: "echo $ESPANSO_ONE$(echo $ESPANSO_TWO | tr [A-Z] [a-z])$ESPANSO_THREE"
          shell: bash

@ernstki
Copy link

ernstki commented Jan 6, 2023

@chrisgrieser How are you getting around the 30-character limitation mentioned in the docs?

The maximum length of a "regex match" is set to 30 characters, including the captured named groups.

Edit: I guess it's not a limitation anymore? Looks like I just had a typo in my regex, or rather, I forgot to quadruple-backslash a literal backslash. 🤦 Never mind, it just dawned on me that the trigger is limited to 30 characters, as in Espanso only monitors up to input 30 characters when looking for a match. The regex pattern itself doesn't have that limitation. This is quite clear from the documentation, if you read it carefully. Duh!

@chrisgrieser
Copy link

I don't, as you can see, the regex trigger in my snippet is only 3 characters long 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants