-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcmdconsole.ahk
620 lines (475 loc) · 15.7 KB
/
cmdconsole.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
AUTOEXEC_cmdconsole: ; Workaround for Autohotkey's ugly auto-exec feature. Must be first line.
global g_putty_isMousePressed := false
global g_putty_is_warned_termsize := false
global g_putty_is_esc_comboing := false
global g_putty_hwnd2termsize := {}
; element value is a string, like "9,15", cell is 9 pixels wide, 15 pixels high
global g_cmdc_text_cmdconsole := "CMD Console"
global g_cmdc_text_Cmd100Prompt := "Cmd width 100 Prompt"
global g_cmdc_text_Cmd120Prompt := "Cmd width 120 Prompt"
cmdconsole_InitHotkeys()
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
return ; End of auto-execute section.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
cmdconsole_RightClickTheMainWindow(wait_ctrl_release:=false)
{
; CMD and PuTTY user may need to call this function to simulate right-clicking into
; current application's main window. For CMD and PuTTY, this will provoke
; "paste text" action.
;
; Consider that this function is probably called for AHK hotkey Ctrl+V,
; caller may need to pass in wait_ctrl_release:=true, so that we *wait until*
; Ctrl key is released first, then do send a RightClick.
; If not this waiting, some application may act as if user does a Ctrl+RightClick,
; which behaves differently than a pure RightClick.
; For example, in PuTTY, Ctrl+RightClick calls up a context menu.
Awinid := dev_GetActiveHwnd()
codetext := Trim(Clipboard, "`r`n")
if(!codetext) {
dev_MsgBoxInfo("Clipboard is empty, nothing to paste.")
return
}
lines := dev_StringCountLines(codetext)
if(lines>1) {
is_yes := dev_MsgBoxYesNo(Format("Clipboard text has {} lines, are you sure to paste all of them?", lines))
if(!is_yes)
return
; Due to interruption of the YesNo dialogbox, we should wait
; until the CMD window becomes foreground again.
if(!dev_WinWaitActiveHwnd(Awinid, 1000))
return
}
if(wait_ctrl_release) {
dev_WaitKeyRelease("Ctrl")
}
ControlClick , , ahk_id %Awinid%, , RIGHT
dev_TooltipAutoClear("AmHotkey: Simulated Right-clicking into main window.", 1500)
}
cc_IsCMDorConEmuActive()
{
if IsWinClassActive("ConsoleWindowClass") or IsWinClassActive("VirtualConsoleClass")
return true
else
return false
}
cc_WaitCMDorConEmuActive(timeout_millisec:=500)
{
msec_start := dev_GetTickCount64()
Loop
{
if(cc_IsCMDorConEmuActive())
return true
if( dev_GetTickCount64()-msec_start > timeout_millisec )
return false
dev_Sleep(50)
}
}
cc_PasteTextToCMDWindow(usertext, timeout_millisec:=500)
{
if(not cc_WaitCMDorConEmuActive(timeout_millisec))
{
dev_TooltipAutoClear(Format("cc_WaitCMDorConEmuActive() not success after {} millisec", timeout_millisec))
return false
}
if(not dev_SetClipboardWithTimeout(usertext, 100))
{
dev_MsgBoxWarning("Unexpect: cc_PasteTextToCMDWindow() cannot open Clipboard.")
return false
}
cmdconsole_RightClickTheMainWindow(false)
return true
}
cmd_Backspace10()
{
Send {Backspace 10}
dev_WaitKeyRelease("Backspace")
; Do a keywait to avoid deleting too much after user has released the hotkey.
}
cmd_Del10()
{
Send {Del 10}
dev_WaitKeyRelease("Del")
; Do a keywait to avoid deleting too much after user has released the hotkey.
}
;==============================================================
; CMD window
;==============================================================
SuggestCmdCompletionCharInRegistry()
{
RegRead, theCompletionChar, HKEY_CURRENT_USER, Software\Microsoft\Command Processor, CompletionChar
if theCompletionChar
{
hint = The CompletionChar currently has value %theCompletionChar%
}
else
{
hint = The CompletionChar currently does not exist.
}
MsgBox, % 3+32,, ; Yes/No/Canel + Question-icon
(
Would you like to set a registry item, so that Tab key can be used for filename completion?
[HKEY_CURRENT_USER\Software\Microsoft\Command Processor]
"CompletionChar"=dword:00000009
%hint%
Answering Yes to add, No to delete, Cancel to take no action.
)
IfMsgBox Yes
RegWrite, REG_DWORD, HKEY_CURRENT_USER, Software\Microsoft\Command Processor, CompletionChar, 9
IfMsgBox No
RegDelete, HKEY_CURRENT_USER, Software\Microsoft\Command Processor, CompletionChar
return
}
#If cc_IsCMDorConEmuActive()
; Alt+E , execute command to show %ERRORLEVEL% // Ctrl+E is saved for IPython(move caret to line end)
!E::
SendInput echo `%ERRORLEVEL`%{enter}
; note: 'Send'executes quite slow for a CMD window, and SendInput is fast.
; But very strange! Send and SendInput often randomly loses one or two chars(on chji Win7 etc).
return
!Backspace:: cmd_Backspace10()
!Del:: cmd_Del10()
!m:: cc_LoadChjMacros()
cc_LoadChjMacros()
{
exedir := dev_GetActiveEXE_PathName()[1]
filename := "ChjMacro.txt"
mpath := exedir . "\" . filename
if FileExist(mpath) {
SendInput doskey /macrofile=%mpath%{enter}
}
else {
Msgbox, % "Macro file not found: " . mpath
}
}
!g:: cc_devGMUPATHfront()
cc_devGMUPATHfront()
{
mpath := ":\w\GMU\gmupath-front.bat"
drive_letters := "defgh"
Loop, parse, drive_letters
{
path := A_LoopField . mpath
if FileExist(path) {
SendInput {enter}{enter}{enter}
SendInput %path%{enter}
break
}
}
}
#If ; cc_IsCMDorConEmuActive()
cc_IsPreWin10_CMD_Window()
{
if IsWinClassActive("ConsoleWindowClass") and not StrIsStartsWith(A_OSVersion, "10.0")
return true
else
return false
}
__notused__cc_IsWin10WSL_Window()
{
if( IsWinClassActive("ConsoleWindowClass") and dev_IsExeActive("wsl.exe") )
return true
else
return false
}
#If IsWinClassActive("ConsoleWindowClass")
; -- no matter it is Win7 CMD, Win10 CMD, or Win10 WSL.
^v:: cmdconsole_RightClickTheMainWindow() ; to paste clipboard text
cmdwin_ScrollOneLine(is_up)
{
; WM_VSCROLL=0x115, SB_LINEUP=0, SB_LINEDOWN=1
PostMessage, 0x115 , % is_up?0:1 , 0, , A
}
cmdwin_ScrollOnePage(is_up)
{
; WM_VSCROLL=0x115, SB_PAGEUP=2, SB_PAGEDOWN=3
PostMessage, 0x115 , % is_up?2:3 , 0, , A
}
; Ctrl+up/down to scroll command window back and forward; Paste in command window
^Up:: cmdwin_ScrollOneLine(true)
^Down:: cmdwin_ScrollOneLine(false)
;
^PGUP:: cmdwin_ScrollOnePage(true)
^PGDN:: cmdwin_ScrollOnePage(false)
#If ; IsWinClassActive("ConsoleWindowClass")
#If cc_IsPreWin10_CMD_Window
; Win+Alt+F to suggest writing CompletionChar=9 to registry
#!f:: SuggestCmdCompletionCharInRegistry()
#If
;==============================================================
; 2014-11-07 ConEmu Window
;==============================================================
#IfWinActive ahk_class VirtualConsoleClass
; use F8/F9 to switch to prev/next Tab
F8:: Send ^{F8} ; ConEmu should set Ctrl+F8 to be "Switch next console".
F9:: Send ^{F9} ; ConEmu should set Ctrl+F9 to be "Switch previous console".
#IfWinActive
;==============================================================
; PuTTY
;==============================================================
putty_IsActive()
{
IfWinActive, ahk_class PuTTY
{
return true
}
return false
}
putty_SimuMouseHolddown()
{
if(g_putty_isMousePressed)
return
Send {LButton down}
g_putty_isMousePressed := true
g_putty_is_esc_comboing := false
}
putty_SimuMouseRelease()
{
Send {LButton up}
if(not g_putty_is_esc_comboing)
Send {Esc}
g_putty_isMousePressed := false
}
putty_GetXYUnit(byRef xunit, byRef yunit)
{
; I'm feeling very lucky to be able to implement this function.
; On PuTTY 0.62, once you start dragging its window border, there will be a tiny window pop up
; right above putty window title showing terminal size in character count, the text is like:
; 100x48
; which means there are 100 letters in x-direction and 48 letters in y- direction.
; After you release your mouse, that tiny window is gone.
; Because simulating mouse dragging for user costs quite some time, I decide to
; maintain a map between hwnd and the terminal-size(termsize)
;
WinGet, hwnd, ID, A ; cache active window unique id
Get_ClientAreaPos(hwnd, cx, cy, cw, ch)
cellsize := g_putty_hwnd2termsize[hwnd] ; sample: cellsize="9,15"
if(not cellsize)
{
; Fetch it now.
MouseGetPos, origx, origy
MouseMove, cw/2, 0
Send {LButton down}
MouseMove, 4, 0, , R
; wait for the tiny popup window
SetTitleMatchMode, RegEx
Loop, 5
{
tinywin := WinExist("^[0-9]+x[0-9]+$")
if(tinywin)
{
WinGetTitle, xy_termsize, ahk_id %tinywin%
; tooltip, % "Got putty termsize=" . xy_termsize ; debug
break
}
else
Sleep, 100
}
SetTitleMatchMode, 3 ; restore to default exact match
Send {LButton up}
MouseMove, %origx%, %origy%
if(xy_termsize)
{
outvar := StrSplit(xy_termsize, "x")
xunit := floor( cw/outvar[1] )
yunit := floor( ch/outvar[2] )
cellsize := xunit . "," . yunit
g_putty_hwnd2termsize[hwnd] := cellsize
}
}
if(cellsize)
{
outvar := StrSplit(cellsize, ",")
xunit := outvar[1]
yunit := outvar[2]
; tooltip, % "xunit=" . xunit . " / yunit=" . yunit
return true
}
else
{
xunit := 9 ; use a fixed value
yunit := 15
if(not g_putty_is_warned_termsize)
{
g_putty_is_warned_termsize := true
MsgBox, % msgboxoption_IconExclamation,
, % "Cannot auto-detect PuTTY terminal size; mouse move will not be accurate. Perhaps your PuTTY version is too old(earlier than 0.62)."
}
return false
}
}
putty_GetCaretHeight()
{
return 8
}
putty_MouseAlignFontCell(cx,cy,cw,ch, xunit, yunit
,byRef xcells, byRef ycells, byRef xCell, byRef yCell)
{
; WinGet, hwnd, ID, A ; cache active window hwnd
; WinGetPos, x,y, winwidth, winheight, ahk_id %hwnd%
xcells := floor(cw/xunit)
ycells := floor(ch/yunit)
;msgbox, %xcells% / %ycells%
PB := 1 ; it seems putty client area as a one-pixel non-drawing border
MouseGetPos, mousex, mousey
; mousex/mousey relative to putty window(active window)
;msgbox, %mousex% / %mousey% // %cx% / %cy% // %xunit% / %yunit%
xCell := floor((mousex-cx-PB)/xunit) ; x-direction which cell?
yCell := floor((mousey-cy-PB)/yunit) ; y-direction which cell?
;MsgBox, %xCell% / %yCell%
xAligned := cx+PB + xCell*xunit
yAligned := cy+PB + yCell*yunit
;MsgBox, %xAligned% /// %yAligned%
MouseMoveInActiveWindow(xAligned, yAligned+putty_GetCaretHeight(), 0)
;tooltip, % "newy_offset_by_clientarea=" . yAligned+putty_GetCaretHeight()-cy
}
putty_SimuMouseMove(sdir, count:=1)
{
static g_putty_is_warned_termsize := false
WinGet, hwndPutty, ID, A ; cache active window unique id
Get_ClientAreaPos(hwndPutty, cx, cy, cw, ch)
MouseGetPos, mousex, mousey, hwndUnderMosue
; If mouse pointer is not in the putty window, move it into putty first.
if(hwndUnderMosue!=hwndPutty)
{
MouseMoveInActiveWindow(0.5, 0.5)
}
isok := putty_GetXYUnit(xunit, yunit) ; font width/height in pixels
putty_MouseAlignFontCell(cx,cy,cw,ch, xunit, yunit, xcells,ycells, xcell,ycell)
; output: xcells/ycells/xcell/ycell
if(sdir=="up" and ycell>0)
{
if(count>ycell)
count := ycell
MouseMove, 0, 0-yunit*count, , R
}
else if(sdir=="down" and ycell<ycells)
{
if(count>ycells-ycell)
count := ycells-ycell
MouseMove, 0, yunit*count, , R
ycell += 1
}
else if(sdir=="left" and xcell>0)
{
if(count>xcell)
count := xcell
MouseMove, 0-xunit*count, 0, , R
xcell -= 1
}
else if(sdir=="right" and xcell<xcells)
{
if(count>xcells-xcell)
count := xcells-xcell
MouseMove, xunit*count, 0, , R
xcell += 1
}
g_putty_is_esc_comboing := true
}
cmdconsole_InitHotkeys()
{
; PuTTY use
;
dev_DefineHotkeyWithCondition("CapsLock & Up", "putty_IsActive", "putty_SimuMouseMove", "up")
dev_DefineHotkeyWithCondition("CapsLock & Down", "putty_IsActive", "putty_SimuMouseMove", "down")
dev_DefineHotkeyWithCondition("CapsLock & Left", "putty_IsActive", "putty_SimuMouseMove", "left")
dev_DefineHotkeyWithCondition("CapsLock & Right", "putty_IsActive", "putty_SimuMouseMove", "right")
; dev_DefineHotkeyWithCondition("Esc & Up", "putty_IsActive", "putty_SimuMouseMove", "up")
; dev_DefineHotkeyWithCondition("Esc & Down", "putty_IsActive", "putty_SimuMouseMove", "down")
; dev_DefineHotkeyWithCondition("Esc & Left", "putty_IsActive", "putty_SimuMouseMove", "left")
; dev_DefineHotkeyWithCondition("Esc & Right", "putty_IsActive", "putty_SimuMouseMove", "right")
dev_DefineHotkeyWithCondition("!Up", "putty_IsActive", "putty_SimuMouseMove", "up", 5)
dev_DefineHotkeyWithCondition("!Down", "putty_IsActive", "putty_SimuMouseMove", "down", 5)
dev_DefineHotkeyWithCondition("!Left", "putty_IsActive", "putty_SimuMouseMove", "left", 5)
dev_DefineHotkeyWithCondition("!Right", "putty_IsActive", "putty_SimuMouseMove", "right", 5)
; [2015-03-28] Strange: Using "<!Up" here will invalidate emeditor.ahk's ``!UP:: Send {UP 10}``
; cmdc_SetNewPrompt_AddTrayMenu()
}
cmdc_SetNewPrompt_AddTrayMenu() ; Legacy, not used now
{
; Define submenu item list:
Menu, CmdconsoleSubmenu, add, %g_cmdc_text_Cmd100Prompt%, cmdc_Prompt100
Menu, CmdconsoleSubmenu, add, %g_cmdc_text_Cmd120Prompt%, cmdc_Prompt120
; Attach submenu to main menu
Menu, tray, add, %g_cmdc_text_cmdconsole%, :CmdconsoleSubmenu
}
cmdc_Prompt100()
{
cmdc_SetNewPrompt(100)
}
cmdc_Prompt120()
{
cmdc_SetNewPrompt(120)
}
cmdc_SetNewPrompt(cmd_width)
{
; Activate the most recent CMD window, and then send new `prompt` command to it.
cmdwin := "ahk_class ConsoleWindowClass"
WinActivate, % cmdwin
isok := dev_WinWaitActive_with_timeout(cmdwin)
if(!isok)
{
MsgBox, % "No CMD windows can be activated, so nothing to do."
return
}
else
{
diamond_count := (cmd_width-14)/2
cmd := Format("PROMPT {1}[$t] $p$g", dev_StrRepeat("◆", diamond_count))
dev_TooltipAutoClear("Sending new PROMPT command to recent CMD window...")
SendInput %cmd%{enter}
}
}
#If putty_IsActive()
^v:: cmdconsole_RightClickTheMainWindow(true)
^!-:: ; Ctrl+Alt+-: Clear terminal and clear PuTTY window buffer
Send !{space}t
Send !{space}l
return
^!c:: ; Ctrl+Alt+c: Copy all to clipboard
Send !{space}o
return
; Ctrl+Up/Down: Scroll one line up/down
^Up:: PostMessage, 0x115 , 0, 0, , A
^Down:: PostMessage, 0x115 , 1, 0, , A
; Ctrl+PgUp/PgDn to scroll a page
^PgUp:: PostMessage, 0x115 , 2, 0, , A
^PgDn:: PostMessage, 0x115 , 3, 0, , A
; Ctrl+E , try to show linux error value
^E::
Send echo $?{enter}
return
; Ctrl+Alt+E Run my favorite command in a Bash env(e.g. VMware ESXi shell, FreeBSD Bash)
^!E::
SendInput PS1_ERRC='ERR:$?'{enter}
SendInput PS1="\n[\u @\h (\D{{}`%G-`%m-`%d{}} \t $PS1_ERRC) \[\033[1;31m\w\033[0m\]]\n\[\033[1;37;44m\]$\[\033[0m\] "{enter}
; After unescaping, you get:
; PS1="\n[\u @\h (\D{%G-%m-%d} \t) \[\033[1;31m\w\033[0m\]]\n\[\033[1;37;44m\]$\[\033[0m\] "
SendInput alias ll='ls -l' && alias la='ls -la'{enter}
SendInput {enter}
return
Esc:: putty_SimuMouseHolddown()
Esc up:: putty_SimuMouseRelease()
#If ; putty_IsActive()
;==============================================================
; SysProgs SmarTTY
;==============================================================
smartty_IsActive()
{
WinGet, Awinid, ID, A ; cache active window unique id
; WinGetClass, class, ahk_id %Awinid%
WinGetTitle, title, ahk_id %Awinid%
if ( title ~= "^SmarTTY" ) ; regex match
{
return true
}
return false
}
#If smartty_IsActive()
^v:: SmarttyPasteText()
SmarttyPasteText()
{
; A context menu pops up on {AppsKey}, and the first item is Copy, the second is Paste.
Send {AppsKey}{Down}{Down}{Enter}
}
#If ; smartty_IsActive()