-
Notifications
You must be signed in to change notification settings - Fork 1
/
Typora.ahk
228 lines (205 loc) · 4.19 KB
/
Typora.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#Warn all
#NoEnv
; restrict to Typora windows.
#IfWinActive ahk_exe Typora.exe
; reload script. used for development
^F1::Reload
; used to toggle all hotkeys
; F1::Suspend
; fire hotstrings even when preceding character is non-space.
#Hotstring ?
; inserts align marker and line break
::==::
SendInput, &=%A_Space%
return
; creates new math environment with a \begin{aligned}
::$$$::
SendInput, $${Enter}
Sleep, 100
SendInput, \begin{{}aligned{}}{Enter}\end{{}aligned{}}{Home}{Enter}{Up}
return
; inserts \begin{aligned} and matching end tag
::\bal::
SendInput, \begin{{}aligned{}}{Enter}\end{{}aligned{}}{Home}{Enter}{Up}
return
; inserts \begin{cases} and matching end tag
::\cases::
SendInput, \begin{{}cases{}}{Enter}\end{{}cases{}}{Home}{Enter}{Up}
return
; inserts \begin{bmatrix} and matching end tag
::\bmat::
SendInput, \begin{{}bmatrix{}}{Enter}\end{{}bmatrix{}}{Home}{Enter}{Up}
return
; inserts {}{} and places cursor inside first argument
::\2a::
SendInput, {{}{}}{{}{}}{Left 3}
return
; inserts \boldsymbol{}
::\bm::
SendInput, \boldsymbol{{}{}}{Left}
return
; inserts \hat{}
::\ha::
SendInput, \hat{{}{}}{Left}
return
; inserts \operatorname*{}
::\on::
SendInput, \operatorname*{{}{}}{Left}
return
; inserts \text{}
::\t::
SendInput, \text{{}{}}{Left}
return
; inserts \textit{}
::\ti::
SendInput, \textit{{}{}}{Left}
return
; inserts \textsf{}
::\ts::
SendInput, \textsf{{}{}}{Left}
return
; inserts \frac{}{}
::\fr::
SendInput, \frac {{}{}}{{}{}}{Left 3}
return
; inserts Res operator with subscript
::\res::
SendInput, \operatorname*{{}Res{}}_{{}z={}}{Left}
return
; takes the word immediately left of the \beg and wraps it in \begin and \end.
::\beg::
old := ClipboardAll
clipboard := ""
SendInput, +{Home}^c
ClipWait, 1
if ErrorLevel {
clipboard := old
return
}
SendInput, {Right}
words := StrSplit(Trim(clipboard), A_Space)
clipboard := old
c := Trim(words[words.MaxIndex()], " `r`n`t")
n := StrLen(c)
if (n > 0) {
SendInput, +{Left %n%}\begin{{}%c%{}}{Enter}{Enter}\end{{}%c%{}}{Home}{Up}
}
return
EscapeBackslash(c) {
if (c == "{" || c == "}") {
return "\" . c
} else {
return c
}
}
; wraps from the left { to \paren in parentheses
::\wr::
old := ClipboardAll
clipboard := ""
SendInput, +{Home}^c
ClipWait, 1
if ErrorLevel {
clipboard := old
return
}
SendInput, {Right}
t := clipboard
; limit to left $ in case we are in an inline equation
dollar := InStr(t, "$", false, 0)
t := SubStr(t, dollar + 1)
tl := StrLen(t)
d1 := SubStr(t, tl-1, 1)
d2 := SubStr(t, tl, 1)
l := InStr(t, "{", false, 0)
r := InStr(t, "}", false, 0)
while (r > l) {
;MsgBox, L %l% R %r%
r := InStr(t, "}", false, -(tl - l + 1))
l := InStr(t, "{", false, -(tl - l + 1))
}
;MsgBox, L %l% R %r%
lefts := StrLen(t) - l
; use clipboard to avoid Typora automatically inserting ()
Critical ; prevent other threads from interrupting our clipboard restore
clipboard := "\left" . EscapeBackslash(d1) . SubStr(t, l+1, -2) . "\right" . EscapeBackslash(d2)
SendInput, +{Left %lefts%}^v
Sleep, 400
clipboard := old
return
; inserts \left and \right on the two preceding characters, escaping braces
::\lr::
old := ClipboardAll
clipboard := ""
SendInput, +{Left 2}^c
ClipWait, 1
if ErrorLevel {
clipboard := old
return
}
SendInput, {Right}
t := Trim(clipboard)
clipboard := old
if (StrLen(t) != 2) {
return
}
l := SubStr(t, 1, 1)
r := SubStr(t, 2, 1)
e1 := ""
e2 := ""
if (l == "{") {
e1 := "\"
}
if (r == "}") {
e2 := "\"
}
lefts := 6 + StrLen(e2) + 1
SendInput, {Left 2}\left%e1%{Right} \right%e2%{Left %lefts%}
return
; smart tab. moves after next { or (, or failing that moves after next ) or }
$Tab::
old := ClipboardAll
clipboard := ""
SendInput, +{END}^c
;Critical
ClipWait, 0.5
if ErrorLevel {
clipboard := old
;SendInput, {Tab}
return
}
SendInput, {LEFT}
c := clipboard
clipboard := old
M := StrLen(c) + 10
if (M == 10) {
SendInput {Right}
return
}
n1 := InStr(c, "{")
n2 := InStr(c, "(")
if (n1 == 0) {
n1 := M
}
if (n2 == 0) {
n2 := M
}
n := Min(n1, n2)
if (n == M) {
n1 := InStr(c, "}")
n2 := InStr(c, ")")
if (n1 == 0) {
n1 := M
}
if (n2 == 0) {
n2 := M
}
n := Min(n1, n2)
}
; MsgBox % "c >" . c . "< n: " . n
if (n != M) {
SendInput {RIGHT %n%}
} else {
SendInput {End}
}
; MsgBox % c
return