-
Notifications
You must be signed in to change notification settings - Fork 6
/
commands.ahk
710 lines (594 loc) · 10.8 KB
/
commands.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
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
;; This script provides: command skeletons
#Include commands_util.ahk
kmacro_tt := alloc_tt()
kill_ring_size = 100
;; ---------
;; utilities
;; ---------
safe_cut()
{
ClipBoard =
send("^x")
ClipWait, 0.5 ; confirm that the ClipBoard is updated
}
;; ---------
;; kill_ring
;; ---------
kill_ring_pointer = 0
kill_ring_push()
{ Global
kill_ring_pointer := mod(kill_ring_pointer + 1, kill_ring_size)
kill_ring%kill_ring_pointer% := ClipBoard
}
kill_ring_pop()
{ Global
kill_ring_pointer := mod(kill_ring_pointer + kill_ring_size - 1, kill_ring_size)
ClipBoard := kill_ring%kill_ring_pointer%
}
kill_ring_restore()
{ Global
ClipBoard := kill_ring%kill_ring_pointer%
}
;; --------------
;; alttab command
;; --------------
;; Usage:
;;
;; #If, alt_pressed
;; alt up:: alttab_end()
;; #If,
;; !tab:: alttab_next()
alt_pressed = 0
alttab_next()
{ Global
alt_pressed = 1
command_simple("{alt down}{tab}", 0, 1)
}
alttab_end()
{ Global
alt_pressed = 0
command_simple("{alt up}", 0, 0)
}
;; -----------
;; send itself
;; -----------
;; send itself ARG times (for ASCII keys)
self_insert_command()
{
command_simple(A_ThisHotKey, 1, 1)
}
;; send itself ARG times (for non-ASCII keys)
self_send_command()
{
tmp = {%A_ThisHotKey%}
command_simple(tmp, 0, 1)
}
;; send mouse event itself
mouse_event_command()
{
MouseGetPos, x, y
RegExMatch(A_ThisHotKey, "(\W*)(\w*)( up)?", res)
If res2 = LButton
key = L
Else If res2 = RButton
key = R
Else If res2 = MButton
key = M
Else If res2 = XButton1
key = X1
Else If res2 = XButton2
key = X2
Else
Return
If res3 =
updn = D
Else
updn = U
key = %res1%{Click, %key%, %x%, %y%, 1, %updn%}
command_simple(key, 0, 1)
}
;; digit argument
digit_argument()
{ Global
Local tmp
run_hooks("pre_command_hook")
StringTrimLeft, tmp, A_ThisHotKey, 1
set_digit_argument(arg * 10 + tmp)
run_hooks("post_command_hook")
}
;; ------
;; system
;; ------
;; do nothing
ignore()
{
run_hooks("pre_command_hook")
run_hooks("post_command_hook")
}
set_mark_command()
{
command_simple("set_mark", 0, 0)
}
set_cx_command()
{ Global
run_hooks("pre_command_hook")
set_digit_argument(arg)
set_cx()
run_hooks("post_command_hook")
}
;; send ESC and reset variables
keyboard_quit()
{ Global
run_hooks("pre_command_hook")
If mark
reset_mark()
Else
send("{escape}")
run_hooks("post_command_hook")
}
;; repeat last command again ARG times
repeat()
{ Global
command_simple(last_command, 0, 1)
}
;; ------
;; kmacro
;; ------
;; functions
kmacro_recoding = 0
kmacro_count = 0
kmacro_start()
{ Global
kmacro_recoding = 1
kmacro_count = 0
ToolTip, REC, , , %kmacro_tt%
}
kmacro_after_send_function()
{ Global
If kmacro_recoding
{
kmacro_count++
kmacro%kmacro_count% := last_command
ToolTip, %last_command%, , , %kmacro_tt%
}
}
kmacro_end()
{ Global
kmacro_recoding = 0
ToolTip, , , , %kmacro_tt%
}
kmacro_call()
{ Global
Local varname, x, y
MouseGetPos, x, y
Loop, %kmacro_count%
{
varname := kmacro%A_Index%
Send, %varname%
}
Send, {Click, , %x%, %y%, 0}
}
add_hook("after_send_hook", "kmacro_after_send_function")
;; commands
kmacro_end_macro()
{
command_simple("kmacro_end", 0, 0)
}
kmacro_start_macro()
{
command_simple("kmacro_start", 0, 0)
}
kmacro_call_macro()
{
command_simple("kmacro_call", 0, 1)
}
kmacro_end_or_call_macro()
{ Global
If kmacro_recoding
kmacro_end_macro()
Else
kmacro_call_macro()
}
kmacro_end_and_call_macro()
{ Global
run_hooks("pre_command_hook")
If kmacro_recoding
kmacro_end()
Loop, % arg ? arg : 1
kmacro_call()
run_hooks("post_command_hook")
}
;; -----
;; files
;; -----
;; save (C-s)
save_buffer()
{
command_simple("^s", 0, 0)
}
;; files(_F) > save as(_A)
write_file()
{
command_simple("{alt down}fa{alt up}", 0, 0)
}
;; open file (C-o)
find_file()
{
command_simple("^o", 0, 0)
}
;; launch explorer (Win-e)
dired()
{
command_simple("#e", 0, 0)
}
;; ---------------
;; windows, frames
;; ---------------
;; close window (M-F4)
kill_frame()
{
command_simple("!{F4}", 0, 0)
}
;; delete ARG tabs (C-F4)
delete_window()
{
command_simple("^{f4}", 0, 1)
}
;; new ARG tabs (C-t)
split_window()
{
command_simple("^t", 0, 1)
}
;; forward ARG tabs (C-TAB)
next_window()
{
command_simple("^{tab}", 0, 1)
}
;; backward ARG tabs (C-S-TAB)
previous_window()
{
command_simple("^+{tab}", 0, 1)
}
;; minimize frame (Win-Down)
suspend_frame()
{
command_simple("#{down}", 0, 0)
}
;; --------
;; motion
;; --------
;; forward ARG chars
forward_char()
{
command_motion("{right}", 1)
}
;; backward ARG chars
backward_char()
{
command_motion("{left}", 1)
}
;; forward ARG words (C-right)
forward_word()
{
command_motion("^{right}", 1)
}
;; backward ARG words (C-left)
backward_word()
{
command_motion("^{left}", 1)
}
;; down ARG lines
next_line()
{
command_motion("{down}", 1)
}
;; up ARG lines
previous_line()
{
command_motion("{up}", 1)
}
;; --------------
;; jumping around
;; --------------
;; PgDn ARG times
scroll_down()
{
command_motion("{pgdn}", 1)
}
;; PgUp ARG times
scroll_up()
{
command_motion("{pgup}", 1)
}
;; scroll left ARG times (Alt+PgUp)
scroll_left()
{
command_motion("!{pgup}", 1)
}
;; scroll right ARG times (Alt+PgDn)
scroll_right()
{
command_motion("!{pgdn}", 1)
}
;; Home
move_beginning_of_line()
{
command_motion("{home}", 0)
}
;; End
move_end_of_line()
{
command_motion("{end}", 0)
}
;; bob (C-Home)
beginning_of_buffer()
{
command_motion("^{home}", 9)
}
;; eob (C-End)
end_of_buffer()
{
command_motion("^{end}", 0)
}
;; move to the bob and forward N-1 lines
goto_line()
{
run_hooks("pre_command_hook")
InputBox, line, Goto:, , , 130, 105
If line is number
{
line--
reset_mark()
send("^{home}")
Loop, %line%
send("{down}")
}
run_hooks("post_command_hook")
}
;; ------
;; region
;; ------
;; mark this word
mark_word()
{
command_mark("^{right}{shift down}^{left}{shift up}")
}
;; mark this line
mark_whole_line()
{
command_mark("{home}{shift down}{end}{shift up}")
}
;; mark this buffer
mark_whole_buffer()
{
command_mark("^a")
}
;; copy (C-c)
kill_ring_save()
{
run_hooks("pre_command_hook")
send("^c")
reset_mark()
kill_ring_push()
run_hooks("post_command_hook")
}
;; cut (C-x)
kill_region()
{
command_simple("^x", 1, 0)
kill_ring_push()
}
;; paste ARG times (C-v)
yank()
{
command_simple("^v", 1, 1)
}
;; pop kill_ring ARG times (M-v)
yank_pop()
{
run_hooks("pre_command_hook")
send("^z")
Loop, % arg ? arg : 1
kill_ring_pop()
send("^v")
run_hooks("after_change_hook")
run_hooks("post_command_hook")
}
;; delete ARG chars forward (Del)
delete_char()
{
command_simple("{del}", 1, 1)
}
;; delete ARG chars backward (Bs)
delete_backward_char()
{
command_simple("{bs}", 1, 1)
}
;; delete ARG words "forward"
kill_word()
{
command_abc("", "{shift down}^{right}{shift up}", "^x", 1)
kill_ring_push()
}
;; delete ARG words "backward"
backward_kill_word()
{
command_abc("", "{shift down}^{left}{shift up}", "^x", 1)
kill_ring_push()
}
;; delete this line "forward"
kill_line()
{
command_simple("{shift down}{end}{shift up}^x", 1, 0)
kill_ring_push()
}
;; delete whole line
kill_whole_line()
{
command_simple("{home}{shift down}{end}{right}{shift up}^x", 1, 0)
kill_ring_push()
}
;; ------------------
;; newline and indent
;; ------------------
;; new ARG lines (Ret)
newline()
{
command_simple("{enter}", 1, 1)
}
;; open ARG lines below (Ret, Left)
open_line()
{
command_simple("{enter}{left}", 1, 1)
}
;; indent ARG times (Tab)
indent_for_tab_command()
{
command_simple("{tab}", 1, 1)
}
;; join ARG lines backward
delete_indentation()
{
command_simple("{home}{bs}", 1, 1)
}
;; -------------
;; edit commands
;; -------------
;; undo ARG times (C-z)
undo_only()
{
command_simple("^z", 1, 1)
}
;; redo ARG times (C-y)
redo()
{
command_simple("^y", 1, 1)
}
;; transpose ARG chars
transpose_chars()
{
command_abc("{left}{shift down}{right}{shift up}^x", "{right}", "^v", 1)
kill_ring_restore()
}
;; transpose ARG words
transpose_words()
{
command_abc("{left}^{right}{shift down}^{left}{shift up}^x", "^{right}", "^v", 1)
kill_ring_restore()
}
;; transpose ARG lines
transpose_lines()
{
command_abc("{up}{home}{shift down}{down}{shift up}^x", "{down}", "^v", 1)
kill_ring_restore()
}
;; replace (C-h)
query_replace()
{
command_simple("^h", 0, 0)
}
;; search (C-f)
search_forward()
{
command_simple("^f", 0, 0)
}
;; insert mode (ins)
overwrite_mode()
{
command_simple("{insert}", 0, 0)
}
;; ---------------
;; case conversion
;; ---------------
upcase_region()
{
run_hooks("pre_command_hook")
safe_cut()
StringUpper, Clipboard, Clipboard
send("^v")
run_hooks("after_change_hook")
run_hooks("post_command_hook")
}
downcase_region()
{
run_hooks("pre_command_hook")
safe_cut()
StringLower, Clipboard, Clipboard
send("^v")
run_hooks("after_change_hook")
run_hooks("post_command_hook")
}
upcase_word()
{
run_hooks("pre_command_hook")
send("^{right}{shift down}^{left}{shift up}")
safe_cut()
StringUpper, Clipboard, Clipboard
send("^v")
run_hooks("after_change_hook")
run_hooks("post_command_hook")
}
downcase_word()
{
run_hooks("pre_command_hook")
send("^{right}{shift down}^{left}{shift up}")
safe_cut()
StringLower, Clipboard, Clipboard
send("^v")
run_hooks("after_change_hook")
run_hooks("post_command_hook")
}
capitalize_word()
{
run_hooks("pre_command_hook")
send("{shift down}^{right}{shift up}")
safe_cut()
StringUpper, Clipboard, Clipboard, T
send("^v")
run_hooks("after_change_hook")
run_hooks("post_command_hook")
}
;; ---------------
;; inserting pairs
;; ---------------
;; insert ARG parentheses
insert_parentheses()
{
command_pair("(){left}")
}
;; insert comment (/* `!!' */)
insert_comment()
{
command_pair("/* */{left}{left}{left}")
}
;; continue multiline comment ARG lines (\n * `!!')
indent_new_comment_line()
{
command_simple("{enter} *{space}", 1, 1)
}
;; ------
;; others
;; ------
;; launch cmd.exe
shell()
{
run_hooks("pre_command_hook")
Run, cmd.exe
run_hooks("after_display_transition_hook")
run_hooks("post_command_hook")
}
;; execute shell command (Win-r)
shell_command()
{
command_simple("#r", 0, 0)
}
;; add text properties (basically for MSWord)
facemenu()
{
command_simple("^d", 0, 0)
}
;; help
help()
{
command_simple("{f1}", 0, 0)
}