forked from kalekje/LNCHR-pub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLNCHR-Funcs.ahk
186 lines (137 loc) · 4.5 KB
/
LNCHR-Funcs.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#Requires AutoHotkey v2.0+
#Include QuickTips.ahk
; ______________________________________________________________________________ TryRun ___________
TryRun(s) {
try {
run s
}
catch {
QuickTrayTip("failed to run:`n" s, tit:="LNCHR")
}
}
UrlEncode(str, sExcepts := "-_.", enc := "UTF-8")
{
hex := "00", func := "msvcrt\swprintf"
buff := Buffer(StrPut(str, enc)), StrPut(str, buff, enc) ;转码
encoded := ""
Loop {
if (!b := NumGet(buff, A_Index - 1, "UChar"))
break
ch := Chr(b)
; "is alnum" is not used because it is locale dependent.
if (b >= 0x41 && b <= 0x5A ; A-Z
|| b >= 0x61 && b <= 0x7A ; a-z
|| b >= 0x30 && b <= 0x39 ; 0-9
|| InStr(sExcepts, Chr(b), true))
encoded .= Chr(b)
else {
DllCall(func, "Str", hex, "Str", "%%%02X", "UChar", b, "Cdecl")
encoded .= hex
}
}
return encoded
}
; Decode precent encoding
UrlDecode(Url, Enc := "UTF-8")
{
Pos := 1
Loop {
Pos := RegExMatch(Url, "i)(?:%[\da-f]{2})+", &code, Pos++)
If (Pos = 0)
Break
code := code[0]
var := Buffer(StrLen(code) // 3, 0)
code := SubStr(code, 2)
loop Parse code, "`%"
NumPut("UChar", Integer("0x" . A_LoopField), var, A_Index - 1)
Url := StrReplace(Url, "`%" code, StrGet(var, Enc))
}
Return Url
}
run_ReplaceText(replacement, runString){
if InStr(runString, "http") ; if url, do a clean replacement
{
replacement := UrlEncode(replacement)
}
; assume proper formatting for command line stuff
runString := StrReplace(runstring, "REPLACEME", replacement)
tryrun(runString)
}
make_run_ReplaceTexts_func(args*) { ; create a 1 arg function from a list of args given as templates, used to pass to run_Replace_Text
func(rep) {
for index, arg in args
run_ReplaceText(rep, arg)
}
return func
}
; ______________________________________________________________________________ Outlook ___________
GetOutlookCom(){
try
outlookApp := ComObjActive("Outlook.Application")
catch
outlookApp := ComObject("Outlook.Application")
return outlookApp
}
OutlookSearch(searchstr)
{
searchstr := StrReplace(searchstr, "!", " hasattachments:yes")
olApp := GetOutlookCom()
static olSearchScopeAllFolders := 2
olApp.ActiveExplorer.Search(searchstr, olSearchScopeAllFolders) ; Activate the Outlook window
WinActivate("ahk_class rctrl_renwnd32") ; Send Tab to hide the "suggested searches" drop down
ControlSend "{Tab}", "RICHEDIT60W1", "ahk_class rctrl_renwnd32"
return
}
; ______________________________________________________________________________ math JS "Calculate" ___________
#Include JS.ahk
JS := JsRT.Edge() ; instantiate JS obj
JS.Eval(FileRead("mathjs.js")) ; add math js
JS.Eval("const parser = math.parser()") ; create a parser object in JS
Loop Read, 'LNCHR-CalcEqns.txt' { ; load equations
JS.Eval("parser.evaluate('" A_LoopReadLine "')")
}
MakeMathExpr(expr) => "math.format(parser.evaluate('" expr "'), {precision: 5}).toString()"
TryEvalMathExpr(expr) => JS.Eval("try { " . MakeMathExpr(expr) . "; } catch(e) { 'undefined'; }" )
js_math_exp_helper(exp)
{
exp := StrReplace(exp, "**", "^") ; python syntax, ** = ^2
some_number := "(\d|\d\)[)]?)" ; regex to capture a number (followed by an optional ))
exp := RegExReplace(exp, some_number . "j", "$1i") ; replace j with 1i, because I'm an EE (:
exp := RegExReplace(exp, some_number . "sq", "$1^2") ; shortcut for number squard eg: 3sq or (1+2)sq
exp := RegExReplace(exp, some_number . "cu", "$1^3") ; shortcut for number cubed
exp := RegExReplace(exp, some_number . "roo", "$1^0.5") ; shortcut for square root (3+6)roo = 3
return exp
}
run_calc_shortcut_then_return(s){
tryrun(s)
set_lngui_input()
Sleep(200)
WinActivate(lngui.Hwnd)
return
}
lngui_calctext := Map() ; dummy declaration
Calculate(expr)
{
;
exprOG := expr
expr := js_math_exp_helper(expr) ;
if expr == '?' { ; load equations list
run_calc_shortcut_then_return('LNCHR-CalcEqns.txt')
return
} else if expr == "mem" {
run_calc_shortcut_then_return("LNCHR-CalcMemory.txt")
return
}
result := TryEvalMathExpr(expr)
if result != "undefined" {
if InStr(expr, '=') {
FileAppend expr "`n", "LNCHR-CalcEqns.txt" ; store equation in memory
} else {
A_Clipboard := result
FileAppend exprOG "`n", "LNCHR-CalcMemory.txt" ; store expression in memory
}
} else {
result := "..."
}
set_calc_text(result)
}