-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathRegExHotstring.ahk
69 lines (61 loc) · 1.79 KB
/
RegExHotstring.ahk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#SingleInstance Force
SacHook := InputHook("VI")
; match when pressed
SacHook.KeyOpt("{Space}", "+N")
SacHook.OnKeyDown := KeyDown
SacHook.Start()
Hs := RegExHs()
KeyDown(ih, vk, sc) {
; return when triggered by Hotstrings
if HotstringIsQueued() {
ih.Stop()
Critical false ; Enable immediate thread interruption.
Sleep -1 ; Process any pending messages.
ih.Start()
return
}
; loop through ench strings and find the first match
input := SubStr(ih.Input, 1, StrLen(ih.Input) - 1)
ih.Stop()
loop Hs.Len() {
str := Hs.str_arr[A_Index]
call := Hs.call_arr[A_Index]
if (RegExMatch(input, str, &match)) {
; delete matched string
Send("{BS " (match.Len[0] + 1) "}")
if (call is String) {
Send(RegExReplace(input, str, call))
} else if (call is Func) {
call(match)
} else
throw Error('callback type error `ncallback should be "Func" or "String"')
ih.Start()
return
}
}
ih.Start()
}
; thanks lexikos - https://www.autohotkey.com/boards/viewtopic.php?f=82&t=104538#p464744
; detect if hotstring is triggered
HotstringIsQueued() {
static AHK_HOTSTRING := 1025
msg := Buffer(4 * A_PtrSize + 16)
return DllCall("PeekMessage", "ptr", msg, "ptr", A_ScriptHwnd
, "uint", AHK_HOTSTRING, "uint", AHK_HOTSTRING, "uint", 0)
}
RegExHotstring(Str, Callback) {
Hs.Append(Str, Callback)
}
Class RegExHs {
; stores hotstrings and callbacks
str_arr := Array()
call_arr := Array()
; append new RegExHotstring
Append(Str, CallBack) {
this.str_arr.Push(Str)
this.call_arr.Push(CallBack)
}
Len() {
return this.str_arr.Length
}
}