-
Notifications
You must be signed in to change notification settings - Fork 1
/
UppercaseRevisor.ahk
601 lines (557 loc) · 15.8 KB
/
UppercaseRevisor.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
#InstallKeybdHook
SetKeyDelay, -1 ;Makes the edit instantaneous. Doesn't seem to work though. Anyway, it's fast enough
SetBatchLines, -1
;CONSTANTS:
global bufferSize := 40 ;The number characters plus shift key events to keep track of
global mymargin := 300 ;The minimum number of milliseconds between two keystrokes so that they can be considered definitely in the right order
global shiftPressed := "{Shift Down}"
global shiftReleased := "{Shift Up}"
global uncapitalizedCharacters := "abcdefghijklmnopqrstuvwxyz1234567890-=[]\;',./" ;The characters eligible for substitution for their respective capitalized characters
global capitalizedCharacters := "ABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()_+{}|:""<>?" ;is one character longer due to double quotes being represented as "" rather than "
global newline := "`r"
;variables:
global keyBuffer := Object()
global timestampBuffer := Object()
global index := 0
global successiveInvocationCount := 1
global lastExecutedToggleToken := -1
;hotkeys with noticable effect:
^q::Invoke()
; F12::OutputBuffers()
;outputs a message box with debugging info on the buffers
OutputBuffers()
{
global
i := 0
str := ""
while (i < index)
{
str := % str . keyBuffer[i] . " at " . timestampBuffer[i] . newline
i++
}
MsgBox %str%
}
;main function, see the https://github.com/JeroenBos/UppercaseRevisor/wiki for details
Invoke()
{
global bufferSize
global successiveInvocationCount
pressIndex := -1
releaseIndex := -1
if(not FindShiftPressAndRelease(pressIndex, releaseIndex))
{
return
}
if(releaseIndex - pressIndex = 1)
{
x := false
if(lastExecutedToggleToken = 4)
{
;undoes the capitalization by a previous consecutive invocation
Decapitalize(Mod(releaseIndex + 1, bufferSize))
x := false
}
else if(lastExecutedToggleToken = 5)
{
;undoes the capitalization by a previous consecutive invocation
Decapitalize(Mod(pressIndex - 1 + bufferSize, bufferSize))
x := true
}
else if(IsWithinMarginOf(releaseIndex))
{
x := true
}
else if(IsWithinMarginOf(Mod(pressIndex - 1 + bufferSize, bufferSize)))
{
x := false
}
else if(Mod(releaseIndex + 1, bufferSize) <> index) ;checks existence of key after shift released
{
if(Mod(pressIndex - 1 + bufferSize, bufferSize) <> index) ;checks existence of key before shift pressed
{
;modifies whichever was closest to a shift key press or release
x := timestampBuffer[Mod(releaseIndex + 1, bufferSize)] - timestampBuffer[releaseIndex] < timestampBuffer[pressIndex] - timestampBuffer[Mod(pressIndex - 1 + bufferSize, bufferSize)]
}
else
{
x := true
}
}
else if(Mod(releaseIndex - 1 + bufferSize, bufferSize) <> index) ;checks existence of key before shift pressed
{
x := false
}
else
{
; do nothing, there are no keys other than the shift keys
return
}
;capitalizes either the character before or after, i.e. x, the shift press and release
NoCharacterExecute(x, pressIndex, releaseIndex)
}
else if(releaseIndex - pressIndex = 2)
{
if(lastExecutedToggleToken <> -1)
{
UndoLastToggleSingleCharacterWithinBounds(Mod(pressIndex + 1, bufferSize))
}
ToggleSingleCharacterWithinBounds(Mod(pressIndex + 1, bufferSize))
}
else if(pressIndex < Mod(releaseIndex - 1 - successiveInvocationCount + bufferSize, bufferSize))
{
Decapitalize(Mod(releaseIndex - 1 - successiveInvocationCount + bufferSize, bufferSize))
}
else if(pressIndex = Mod(releaseIndex - 1 - successiveInvocationCount + bufferSize, bufferSize) ;whether all successive capitalized letters have been decapitalized
&& Mod(releaseIndex + 1, bufferIndex) <> index) ;and the shift release wasn't the last action
{
numberOfCharactersToCapitalize := releaseIndex - pressIndex - 1
i := pressIndex + 1
while(numberOfCharactersToCapitalize >= 0)
{
Capitalize(i)
numberOfCharactersToCapitalize--
i++
}
Capitalize(Mod(releaseIndex + 1, bufferSize))
}
else
{
if(Mod(pressIndex + 2 + successiveInvocationCount, bufferIndex) <> index) ;whether there is another succesiveInvocationCount'th character after the shift release
{
Capitalize(Mod(pressIndex + 2 + successiveInvocationCount, bufferSize))
}
}
successiveInvocationCount++
return
}
NoCharacterExecute(x, pressIndex, releaseIndex)
{
if(x)
{
Capitalize(Mod(releaseIndex + 1, bufferSize))
lastExecutedToggleToken := 4
}
else
{
Capitalize(Mod(pressIndex - 1 + bufferSize, bufferSize))
lastExecutedToggleToken := 5
}
}
AppendToBuffer(key)
{
global
keyBuffer[index] := key
timestampBuffer[index] := A_TickCount
index := Mod(index + 1, bufferSize)
if (key <> shiftPressed && key <> shiftReleased)
{
successiveInvocationCount := 0
}
lastExecutedToggleToken := -1
return
}
;removes the last character input from the buffer. Does not remove buffered shift key presses or releases
PopBuffer()
{
global
lastExecutedToggleToken := -1
characterToRemoveIndex := FindLastNonShiftBufferedAction()
if(characterToRemoveIndex = -1)
{
return ; only shift key actions are buffered
}
else
{
index--
BuffersRemoveAt(characterToRemoveIndex)
}
}
FindLastNonShiftBufferedAction()
{
unboundedIndex := index + bufferSize - 1 ;
while index <= unboundedIndex
{
boundedIndex := Mod(unboundedIndex, bufferSize)
if(keyBuffer[boundedIndex] <> shiftPressed && keyBuffer[boundedIndex] <> shiftReleased)
{
return boundedIndex
}
unboundedIndex--
}
return -1
}
;removes the element at the specified index in the buffer (and in the time tick buffer)
BuffersRemoveAt(indexToRemove)
{
global keyBuffer
global timestampBuffer
if(indexToRemove < index)
{
numberOfElementsToMove := index - indexToRemove
}
else
{
numberOfElementsToMove := bufferSize - index + indexToRemove - 1
}
unboundedIndex := indexToRemove
while unboundedIndex <> indexToRemove + numberOfElementsToMove
{
boundedIndex := Mod(unboundedIndex, bufferSize)
boundedNextIndex := Mod(unboundedIndex + 1, bufferSize)
keyBuffer[boundedIndex] := keyBuffer[boundedNextIndex]
timestampBuffer[boundedIndex] := timestampBuffer[boundedNextIndex]
unboundedIndex++
}
}
ClearBuffers()
{
keyBuffer := Object()
timestampBuffer := Object()
index := 0
successiveInvocationCount := 0
lastExecutedToggleToken := -1
}
RemoveCharacters(start, length)
{
global
i := start - 1
while length <> 0
{
bufferedKey := keyBuffer[i]
if(not bufferedKey = shiftPressed && not bufferedKey = shiftReleased)
{
Send {Backspace}
}
i := Mod(i - 1 + bufferSize, bufferSize)
length--
}
return
}
InsertCharacter(character)
{
Send %character%
}
InsertBufferedCharacter(i)
{
InsertCharacter(keyBuffer[i])
}
InsertCharacters(i, count)
{
if(count = 0)
{
return
}
else
{
InsertBufferedCharacter(i)
InsertCharacters(Mod(i + 1, bufferSize), count - 1)
}
}
Capitalize(i) ;i is an index in the buffers
{
global keyBuffer
characterToCapitalize := keyBuffer[i]
substitute := GetCapitalizedForm(keyBuffer[i])
Replace(i, substitute)
}
Decapitalize(i)
{
global keyBuffer
Replace(i, GetDecapitalizedForm(keyBuffer[i]))
}
Replace(i, substitute) ;i is an index in the buffers, substitute is the character to insert there
{
global
if(substitute = "")
{
return
}
debug_mostRecentKey := keyBuffer[index - 1]
toRemoveCount := Mod(index - i + bufferSize, bufferSize) ;the number of characters (including shift presses) to remove to just include removing the character at bufferindex i
if(toRemoveCount = 0)
{
toRemoveCount := bufferSize
}
RemoveCharacters(index, toRemoveCount)
InsertCharacter(substitute)
substituteIndex := Mod(index - toRemoveCount + bufferSize, bufferSize)
currentlyAtSubsituteIndex := keyBuffer[substituteIndex]
keyBuffer[substituteIndex] := substitute ;also changes the character in the buffer, to ensure that any successive modification takes into account the substitute
InsertCharacters(Mod(i + 1, bufferSize), toRemoveCount - 1)
}
FindShiftPressAndRelease(ByRef pressIndex, ByRef releaseIndex)
{
global ; index bufferSize actionBuffer keyBuffer
unboundedIndex := index + bufferSize - 1 ;-1 to start at the last character (since i points to the index where the next character would be placed)
releaseFound := false
pressFound := false
while index <= unboundedIndex
{
boundedIndex := Mod(unboundedIndex, bufferSize)
key := keyBuffer[boundedIndex]
debug_timestamp := timestampBuffer[boundedIndex]
if(key = shiftPressed)
{
if(pressFound)
{
;two consecutive shift pressed found, as it, in is still being pressed
}
else if(releaseFound)
{
;found the correct sequence: now we found a shift press for which we already have found a shift release.
pressIndex := boundedIndex
return true
}
else
{
;we found the latest shift press
pressFound := true
pressIndex := boundedIndex
}
}
else if(key = shiftReleased)
{
if(releaseFound)
{
return false ;two consecutive shift releases found
}
else if(pressFound)
{
;found that during invocation of this programme, shift is down. We'll ignore that shift press and look for a next shift press before the shift release found now
;pressFound = false; EDIT: actually, the implementation isn't adapted to this scenario. Just fail:
return false
}
else
{
;found a shift release, for which we still need to search the corresponding press
releaseFound := true
releaseIndex := boundedIndex
}
}
else
{
;the key represents some other character
}
unboundedIndex--
}
;exhausted all buffered keys, to no avail
return false
}
GetCapitalizedForm(character)
{
global
return GetOtherForm(character, uncapitalizedCharacters, capitalizedCharacters)
}
GetDecapitalizedForm(character)
{
global
return GetOtherForm(character, capitalizedCharacters, uncapitalizedCharacters)
}
GetOtherForm(character, keys, values)
{
i := IndexOf(keys, character)
if(i <> -1)
{
return SubStr(values, i, 1)
}
;the character wasn't there... hmmm... ;actually, intended for spacebar, tab, enter
;return the original character, so that in the end no effect is produced, since it is replaced by itself
return character
}
At(string, index) ;index is one-based, like in any non-self-respecting programming language
{
return SubStr(string, index, 1)
}
IndexOf(string, char)
{
i := 1
length := StrLen(string)
while i <= length
{
if(At(string, i) = char)
{
return i
}
i++
}
return -1
}
;gets whether the key stroke at the specified index is within the allowed temporal margin from the next key stroke, if any
IsWithinMarginOf(i) ;i is the index of the first of the two
{
global mymargin
t1 := timestampBuffer[i]
t2 := timestampBuffer[Mod(i + 1, bufferSize)]
if(not t1 OR not t2)
return false ;then the index of either one is out of range, in which case there is no margin to consider, hence "false"
return mymargin >= t2 - t1
}
ToggleSingleCharacterWithinBounds(i)
{
global
if(lastExecutedToggleToken <= 0) ;can include -1
{
if(IsWithinMarginOf(Mod(i + 1, bufferSize)))
{
Capitalize(Mod(i + 2, bufferSize))
lastExecutedToggleToken := 1
return
}
}
if(lastExecutedToggleToken <= 1)
{
if(IsWithinMarginOf(Mod(i - 2 + bufferSize, bufferSize)))
{
Capitalize(Mod(i - 2 + bufferSize, bufferSize))
lastExecutedToggleToken := 2
return
}
}
if(lastExecutedToggleToken <> 0)
{
Decapitalize(i)
lastExecutedToggleToken := 0
}
return
}
UndoLastToggleSingleCharacterWithinBounds(i) ;i is the character of the single character between the shift press and release
{
global
if(lastExecutedToggleToken = 0)
{
;undo decapitalization of the only character between shifts
Capitalize(i)
}
else if(lastExecutedToggleToken = 1)
{
;undo capitalization of the character after the shift release. Checking margin is redudant, otherwise the lastExecutedToggleToken would have been something different
Decapitalize(Mod(i + 2, bufferSize))
}
else if(lastExecutedToggleToken = 2)
{
;undo capitalization of the character before the shift press. Checking margin is redudant, otherwise the lastExecutedToggleToken would have been something different
Decapitalize(Mod(i - 2 + bufferSize, bufferSize))
}
}
;key strokes that alter the behavior completely without fix:
~*Home:: ; '*' means irrespective of modifier keys
~*End::
~*PGDN::
~*PGUP::
~*UP::
~*Down::
~*Right::
~*Backspace::ClearBuffers()
;key strokes that have special handling
~Space::
~+Space::AppendToBuffer(" ")
~Tab::AppendToBuffer("{Tab}")
~Enter::AppendToBuffer("{Enter}")
~Left::
~+Left::index--
~BackSpace::
~+Backspace::PopBuffer()
;SHIFT: huh: apparently the tilde here matters: ~Shift::MsgBox displays a messagebox on shift down, but Shift::MsgBox displays it on shift up :/ Anyway, with tilde is the desired behavior so just .... yeah... whatever. it works
~Shift::
{
if(keyBuffer[index] <> shiftPressed) ;prevents adding successive shift pressed (in case the shift key is being held down). This requirement is optional, but the buffer could quickly overflow with just shift presses
{
AppendToBuffer(shiftPressed)
}
return
}
~Shift Up::AppendToBuffer(shiftReleased)
;buffer all typed characters
~a::
~b::
~c::
~d::
~e::
~f::
~g::
~h::
~i::
~j::
~k::
~l::
~m::
~n::
~o::
~p::
~q::
~r::
~s::
~t::
~u::
~v::
~w::
~x::
~y::
~z::
~`::
~1::
~2::
~3::
~4::
~5::
~6::
~7::
~8::
~9::
~0::
~-::
~=::
~[::
~]::
~\::
~;::
~'::
~,::
~.::
~/::AppendToBuffer(Substr(A_ThisHotkey, 2))
~+A::
~+B::
~+C::
~+D::
~+E::
~+F::
~+G::
~+H::
~+I::
~+J::
~+K::
~+L::
~+M::
~+N::
~+O::
~+P::
~+Q::
~+R::
~+S::
~+T::
~+U::
~+V::
~+W::
~+X::
~+Y::
~+Z:: AppendToBuffer(Substr(A_ThisHotkey, 3))
~+1:: AppendToBuffer("!")
~+2:: AppendToBuffer("@")
~+3:: AppendToBuffer("#")
~+4:: AppendToBuffer("$")
~+5:: AppendToBuffer("%")
~+6:: AppendToBuffer("^")
~+7:: AppendToBuffer("&")
~+8:: AppendToBuffer("*")
~+9:: AppendToBuffer("(")
~+0:: AppendToBuffer(")")
~+-:: AppendToBuffer("_")
~+=:: AppendToBuffer("+")
~+[:: AppendToBuffer("{")
~+]:: AppendToBuffer("}")
~+\:: AppendToBuffer("|")
~+;:: AppendToBuffer(":")
~+':: AppendToBuffer("""")
~+,:: AppendToBuffer("<")
~+.:: AppendToBuffer(">")
~+/:: AppendToBuffer("?")