This must be probably stupid, but I am puzzled by this and I cannot find the way to solve it elegantly.
Simply said, I want to enclose a string in parentheses when I use a hotkey.
If I double click in a word, it usually adds spaces to the right of it, doesn't it? And sometimes, if I am selecting the string manually with the mouse, I may accidentally select spaces from the left as well.
So if I want to enclose the number 28 in the following sentence: "I want 28 cakes" I need to have "I want (28) cakes", regardless of whether I select spaces to the left or right of it.
I tried with regex substitution but couldn't make it work for all cases, so here's my final code.
Code: Select all
^>:: ; Copies the source and encloses it in parentheses
ClipBoard :=""
SendInput ^x
ClipWait, .1
Word := Clipboard
Word := SubStr(Word, 1,1) = A_Space ? A_Space . "(" . LTrim(Word) : "(" . Word
Word:=SubStr(Word, 0,1) = A_Space ? RTrim(Word) . ")" . A_Space : Word . ")"
ClipBoard:= Word
SendInput, ^v
Return
So if I have "I want 28 cakes" I need it to be "I want (28) cakes", not "I want ( 28) cakes" or "I want(28) cakes" or any combination, so the spaces in the selection MUST be preserved, just placed in their proper place so I can have a nice sentence with right spacing.
So the thing is, Autohotkey trims any number of spaces to the LEFT of the string. Not just one, but all of them. If I do this:
Code: Select all
Word := SubStr(Word, 1,1) = A_Space ? " (" . LTrim(Word) : "(" . Word ; A lot of spaces to the left of the open parenthesis
So with this selection. it works and this is the result but with this one it does not, I obtain this
Interruptor para establecer/decelerar(28) funciona correctamente.
Any ideas why and how to solve it?
But if the parentheses are on the left, it does not work. Autohotkey trims it, no matter what!