Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

RegEx Powered Dynamic Hotstrings


  • Please log in to reply
224 replies to this topic
polyethene
  • Administrators
  • 5517 posts
  • Last active: Nov 15 2014 07:55 AM
  • Joined: 26 Oct 2012

Dynamically add regular expression type hotstrings.

Example:

hotstrings("now#", "%A_Now%")
hotstrings("(B|b)tw", "%$1%y the way")
hotstrings("(\d+)\/(\d+)%", "percent") ; try 4/50%
Return

percent:
p := Round($1 / $2 * 100)
Send, %p%`%
Return
Download

hughman
  • Members
  • 192 posts
  • Last active: Jun 21 2014 06:07 AM
  • Joined: 11 Feb 2007

Dynamically add regular expression type hotstrings.


Thinks.I'll have a try.

rjwilmsi
  • Members
  • 30 posts
  • Last active: Nov 21 2012 12:40 PM
  • Joined: 18 May 2005
Hmm, this seems interesting, but I can't get it to work.
I've pasted the function and examples into my autohotkey.ini file and reloaded it. I then expected that if I typed Btw in notepad it would be converted to By the way, and if I typed btw it would be converted to by the way. Am I totally missing the point? Does this script not effectively add regex support to hotstrings?

polyethene
  • Administrators
  • 5517 posts
  • Last active: Nov 15 2014 07:55 AM
  • Joined: 26 Oct 2012

Does this script not effectively add regex support to hotstrings?

It does, that was probably a bad example. Try hs("gr[ea]y", "{#}CCC") - it replaces 'grey' or 'gray' with its hex colour code.

ahklerner
  • Members
  • 1386 posts
  • Last active: Oct 08 2014 10:29 AM
  • Joined: 26 Jun 2006
Used it here
http://www.autohotke...pic.php?t=18110

talkasab
  • Members
  • 2 posts
  • Last active: Apr 19 2008 07:01 PM
  • Joined: 28 May 2007
I like the idea of the dynamic hotstrings and I like the idea of regular expressions in them. But, as other people have described, I'd like to find a way to make dynamic hotstrings that behave more like regular, default hotstrings in that they wait for an non-word character before firing the replacement text.

For example, when defined using the hs() function above, the "btw" entry will expand to "by the way" without waiting for a space or comma afterward. (Which makes it impossible to define multiple hotstrings that use the same root.)

For example, I'd "lb" to expand to "pound", but "lbrt" to expand to liberate, and I haven't figured out yet how to do that using the hs() function above.

(The reason I don't want to use the #Hostring directive or syntax is that I'd like these hotstrings to be dynamically created as part of a compiled script.)

Thanks,
T

polyethene
  • Administrators
  • 5517 posts
  • Last active: Nov 15 2014 07:55 AM
  • Joined: 26 Oct 2012
That is an important feature that's currently lacking. I've been meaning to update this script for a long time. I'll try work on it soon and release version 2.

engunneer
  • Moderators
  • 9162 posts
  • Last active: Sep 12 2014 10:36 PM
  • Joined: 30 Aug 2005
please try
hs("(B|b)tw\W", "%$1%y the way")


talkasab
  • Members
  • 2 posts
  • Last active: Apr 19 2008 07:01 PM
  • Joined: 28 May 2007

please try

hs("(B|b)tw\W", "%$1%y the way")

Does that work for you? Because that doesn't get expanded at all for me. (FWIW, I also thought about using regular expressions to try to test for end-of-word characters, but couldn't get it to work.)

T

polyethene
  • Administrators
  • 5517 posts
  • Last active: Nov 15 2014 07:55 AM
  • Joined: 26 Oct 2012
That would work if you typed something like 'btw.' or 'btw!', but these aren't nice solutions so I'll continue working on an update.

engunneer
  • Moderators
  • 9162 posts
  • Last active: Sep 12 2014 10:36 PM
  • Joined: 30 Aug 2005
I didn't have a chance to try it. I also didn't have time to look up the regex, maybe \s was the right one?

rjwilmsi
  • Members
  • 30 posts
  • Last active: Nov 21 2012 12:40 PM
  • Joined: 18 May 2005
Hi all,
I've been using this for a while now but have got stuck by one issue:

say i define an entry like this (I have this problem with other examples):

hs("ann(a|e)" , "Ann%$1%")

now this correctly changes:
anna -> Anna
anne -> Anne

however, spaces don't seem to be counted, so these are also incorrectly changed;
ann a -> aAnna
ann e -> aAnne

So it would seem that spaces are ignored by the regex matching? Am I right and can anybody tell me how to fix it?

Thanks

rjwilmsi
  • Members
  • 30 posts
  • Last active: Nov 21 2012 12:40 PM
  • Joined: 18 May 2005
Please, anybody? I haven't yet resolved this.

tonne
  • Members
  • 1654 posts
  • Last active: May 06 2014 06:22 PM
  • Joined: 06 Jun 2006
Try this:

hs(c, a = "") {
	static d, b, q
	global $
	v = `n,€,~$,Numpad,BS[color=red],Space[/color]
	StringSplit, v, v, `,
	If !d {
		Loop, 104
			Hotkey, % Chr(42 * !!RegExMatch(t := Chr(32 + (x := A_Index))
				, "[a-z]")) . v3 . (x > 94 ? v4 . x - 95 : t), _hs
		Hotkey, %v3%%v5%, _hs
		[color=red]d := 1[/color]
	}
	If a
		Return, !!b .= c . v2 . a . v1
	If c = %v3%%v5%
		Return, !!q := SubStr(q, 1, -1)
[color=red]	If c = %v3%%v6%
		Return, !!q := SubStr(q, 1, -1)[/color]
	If GetKeyState("Shift", "P") or GetKeyState("Capslock", "T")
		StringUpper, c, c
	Else StringLower, c, c
	q .= SubStr(c, StrLen(v3) + !!InStr(c, v4) * StrLen(v4) + 1 + !!InStr(c, "*"))
	Loop, Parse, b, %v1%
	{
		If (c := RegExMatch(q, SubStr(t := A_LoopField, 1
			, InStr(t, v2) - 1) . "$", $)) and t {
			f += RegExMatch(t, ".+" . v2 . "(.+)", l)
			SendInput, % "{BS " . StrLen($) . "}"
			Transform, l, Deref, %l1%
			If IsLabel(l)
				GoSub, %l%
			Else SendInput, %l%
		}
	}
	If f
		q =
}

[color=red]~$space:: [/color]
_hs:
hs(A_ThisHotkey)
Return


rjwilmsi
  • Members
  • 30 posts
  • Last active: Nov 21 2012 12:40 PM
  • Joined: 18 May 2005
Thanks, that's perfect!

For others who may be interested, it's possible to exclude returns and tabs in a similar fashion:

hs(c, a = "") {
   static d, b, q
   global $
   v = `n,€,~$,Numpad,BS,Space[color=red],Tab,Return[/color]
   StringSplit, v, v, `,
   If !d {
      Loop, 104
         Hotkey, % Chr(42 * !!RegExMatch(t := Chr(32 + (x := A_Index))
            , "[a-z]")) . v3 . (x > 94 ? v4 . x - 95 : t), _hs
      Hotkey, %v3%%v5%, _hs
      d := 1
   }
   If a
      Return, !!b .= c . v2 . a . v1
   If c = %v3%%v5%
      Return, !!q := SubStr(q, 1, -1)
   If c = %v3%%v6%
      Return, !!q := SubStr(q, 1, -1)
  [color=red] If c = %v3%%v7%
      Return, !!q := SubStr(q, 1, -1)
   If c = %v3%%v8%
      Return, !!q := SubStr(q, 1, -1)[/color]
   If GetKeyState("Shift", "P") or GetKeyState("Capslock", "T")
      StringUpper, c, c
   Else StringLower, c, c
   q .= SubStr(c, StrLen(v3) + !!InStr(c, v4) * StrLen(v4) + 1 + !!InStr(c, "*"))
   Loop, Parse, b, %v1%
   {
      If (c := RegExMatch(q, SubStr(t := A_LoopField, 1
         , InStr(t, v2) - 1) . "$", $)) and t {
         f += RegExMatch(t, ".+" . v2 . "(.+)", l)
         SendInput, % "{BS " . StrLen($) . "}"
         Transform, l, Deref, %l1%
         If IsLabel(l)
            GoSub, %l%
         Else SendInput, %l%
      }
   }
   If f
      q =
}

~$space::
[color=red]~$return::
~$tab::[/color]
_hs:
hs(A_ThisHotkey)
Return