Add ^h

Add ^h

#InstallKeybdHook
#UseHook

is_terminal()
{
  ifWinActive,ahk_exe WindowsTerminal.exe
    return 1
  return 0
}

; windows shortcut

  #+a::
    send #a
    return

  #+b::
    send #b
    return

  #+c::
    send #c
    return

  #+d::
    send #d
    return

  #+e::
    send #e
    return

  #+f::
    send #f
    return

  #+g::
    send #g
    return

  #+h::
    send #h
    return

  #+i::
    send #i
    return

  #+j::
    send #j
    return

  #+k::
    send #k
    return

  #+l::
    send #l
    return

  #+m::
    send #m
    return

  #+n::
    send #n
    return

  #+o::
    send #o
    return

  #+p::
    send #p
    return

  #+q::
    send #q
    return

  #+r::
    send #r
    return

  #+s::
    send #s
    return

  #+t::
    send #t
    return

  #+u::
    send #u
    return

  #+v::
    send #v
    return

  #+w::
    send #w
    return

  #+x::
    send #x
    return

  #+y::
    send #y
    return

  #+z::
    send #z
    return

; emacs binding
  ^a::
    if is_terminal() {
      send %A_ThisHotKey%
    } else {
      send {HOME}
    }
    return

  ^b::
    if is_terminal() {
      send %A_ThisHotKey%
    } else {
      send {Left}
    }
    return

  ^d::
    if is_terminal() {
      send %A_ThisHotKey%
    } else {
      send {DEL}
    }
    return

  ^e::
    if is_terminal() {
      send %A_ThisHotKey%
    } else {
      send {END}
    }
    return

  ^f::
    if is_terminal() {
      send %A_ThisHotKey%
    } else {
      send {Right}
    }
    return

  ^h::
    if is_terminal() {
      send %A_ThisHotKey%
    } else {
      send {BackSpace}
    }
    return

  ^n::
    if is_terminal() {
      send %A_ThisHotKey%
    } else {
      send {Down}
    }
    return

  ^p::
    if is_terminal() {
      send %A_ThisHotKey%
    } else {
      send {Up}
    }
    return

; mac-style shortcut

  #a::
    send ^a
    return

  #b::
    send ^b
    return

  #c::
    send ^c
    return

  #d::
    send ^d
    return

  #e::
    send ^e
    return

  #f::
    send ^f
    return

  #g::
    send ^g
    return

  #h::
    send ^h
    return

  #i::
    send ^i
    return

  #j::
    send ^j
    return

  #k::
    send ^k
    return

  #l::
    send ^l
    return

  #m::
    send ^m
    return

  #n::
    send ^n
    return

  #o::
    send ^o
    return

  #p::
    send ^p
    return

  #q::
    send ^q
    return

  #r::
    send ^r
    return

  #s::
    send ^s
    return

  #t::
    send ^t
    return

  #u::
    send ^u
    return

  #v::
    send ^v
    return

  #w::
    send ^w
    return

  #x::
    send ^x
    return

  #y::
    send ^y
    return

  #z::
    send ^z
    return

#InstallKeybdHook
#UseHook

is_terminal()
{
  ifWinActive,ahk_exe WindowsTerminal.exe
    return 1
  return 0
}

; windows shortcut

  #+a::
    send #a
    return

  #+b::
    send #b
    return

  #+c::
    send #c
    return

  #+d::
    send #d
    return

  #+e::
    send #e
    return

  #+f::
    send #f
    return

  #+g::
    send #g
    return

  #+h::
    send #h
    return

  #+i::
    send #i
    return

  #+j::
    send #j
    return

  #+k::
    send #k
    return

  #+l::
    send #l
    return

  #+m::
    send #m
    return

  #+n::
    send #n
    return

  #+o::
    send #o
    return

  #+p::
    send #p
    return

  #+q::
    send #q
    return

  #+r::
    send #r
    return

  #+s::
    send #s
    return

  #+t::
    send #t
    return

  #+u::
    send #u
    return

  #+v::
    send #v
    return

  #+w::
    send #w
    return

  #+x::
    send #x
    return

  #+y::
    send #y
    return

  #+z::
    send #z
    return

; emacs binding
  ^a::
    if is_terminal() {
      send %A_ThisHotKey%
    } else {
      send {HOME}
    }
    return

  ^b::
    if is_terminal() {
      send %A_ThisHotKey%
    } else {
      send {Left}
    }
    return

  ^d::
    if is_terminal() {
      send %A_ThisHotKey%
    } else {
      send {DEL}
    }
    return

  ^e::
    if is_terminal() {
      send %A_ThisHotKey%
    } else {
      send {END}
    }
    return

  ^f::
    if is_terminal() {
      send %A_ThisHotKey%
    } else {
      send {Right}
    }
    return

  ^n::
    if is_terminal() {
      send %A_ThisHotKey%
    } else {
      send {Down}
    }
    return

  ^p::
    if is_terminal() {
      send %A_ThisHotKey%
    } else {
      send {Up}
    }
    return

; mac-style shortcut

  #a::
    send ^a
    return

  #b::
    send ^b
    return

  #c::
    send ^c
    return

  #d::
    send ^d
    return

  #e::
    send ^e
    return

  #f::
    send ^f
    return

  #g::
    send ^g
    return

  #h::
    send ^h
    return

  #i::
    send ^i
    return

  #j::
    send ^j
    return

  #k::
    send ^k
    return

  #l::
    send ^l
    return

  #m::
    send ^m
    return

  #n::
    send ^n
    return

  #o::
    send ^o
    return

  #p::
    send ^p
    return

  #q::
    send ^q
    return

  #r::
    send ^r
    return

  #s::
    send ^s
    return

  #t::
    send ^t
    return

  #u::
    send ^u
    return

  #v::
    send ^v
    return

  #w::
    send ^w
    return

  #x::
    send ^x
    return

  #y::
    send ^y
    return

  #z::
    send ^z
    return


Update EMB_Core_BlockComment.ahk

Update EMB_Core_BlockComment.ahk

EMB_Core_BlockComment(mbsource){
  pair := []
  Matches := []
  i := 1

  ;pattern ignores everything inside quotes (all defined strings)
  pattern = "(?:.|\s)*?"|(\/\*(?:.|\s)*?\*\/)

  ;Match in the mbsource where matches pattern
  i := regexmatch(mbsource, "iO)" . pattern, oMatch, i)

  ;Loop over all i
  while i <> 0 {
    If oMatch.Value(1) {
	  ;msgbox, % oMatch.Value(0) "-->" oMatch.Value(1) "-->" oMatch.Count

      ;Add those that match 2nd criteria (real comments) to match object
      pair := [oMatch.Pos(0), oMatch.Len(0)]

      ;Add pair array to collection of pairs - "Matches" represents all the matches that are not part of existing strings.
      Matches.push(pair)
    }

    ;Next i
    i := oMatch.Pos(0) + oMatch.Len(0)

    ;Get next match from mbsource
    i := regexmatch(mbsource, "iO)" pattern, oMatch, i)
  }

  ;Match11 := Matches[1][1]
  ;Match12 := Matches[1][2]
  ;msgbox, Match11:%Match11% ¦ Match12: %Match12%

  ;if !EMB_Compact_Transpile then
  ;  for each match in matches
  ;    extract and remove match from code
  ;    replace /* with '
  ;    replace `r`n with `r`n'
  ;    replace */ with `r`n
  ;    re-insert new code where match was
  ;  next match
  ;Else
  ;  for each match in matches
  ;    remove match from code
  ;  next match
  ;End If

  ;msgbox, transpile or remove?
  ;msgbox, % !EMB_Compact_Transpile
  if (!EMB_Compact_Transpile) {
    ;Loop over Matches
	loop, % Matches.Length()
    {
	  LoopVar := Matches.Length() - A_Index + 1
      subSrc := p_EMB_Core_BC_removeFromString(mbsource,Matches[LoopVar][1],Matches[LoopVar][2])
		subSrc := RegExReplace(subSrc,"\/\*\s*","'")
		subSrc := StrReplace(subSrc,"`r`n","`r`n'")
		subSrc := RegExReplace(subSrc,"\s*\*\/","`r`n")
      p_EMB_Core_BC_insertIntoString(mbsource,subSrc,matches[LoopVar][1])
    }
  } else {
    LoopVar := Matches.Length() - A_Index + 1
    p_EMB_Core_BC_removeFromString(mbsource,Matches[LoopVar][1],Matches[LoopVar][2])
  }

return mbsource
}

p_EMB_Core_BC_removeFromString(ByRef src,index,length){
  ;Supply function with an index and length of a string to extract
  ;The function will extract and return the string and remove it from the original src string.
  out_String := SubStr(src, index, length)
  src := SubStr(src,1,index - 1) . SubStr(src,index + length + 1)
  return out_String
}

p_EMB_Core_BC_insertIntoString(ByRef src,in_String,index){
  ;inserts a string "in_String" into src before the index supplied.
  ;Mid(s,1,Index-1) & "---" & Mid(s,Index)
  src := SubStr(src,1,index-1) . in_String . SubStr(src,index)
  return
}

EMB_Core_BlockComment(mbsource){
  pair := []
  Matches := []
  i := 1

  ;pattern ignores everything inside quotes (all defined strings)
  pattern = "(?:.|\s)*?"|(\/\*(?:.|\s)*?\*\/)

  ;Match in the mbsource where matches pattern
  i := regexmatch(mbsource, "iO)" . pattern, oMatch, i)

  ;Loop over all i
  while i <> 0 {
    If oMatch.Value(1) {
	  ;msgbox, % oMatch.Value(0) "-->" oMatch.Value(1) "-->" oMatch.Count

      ;Add those that match 2nd criteria (real comments) to match object
      pair := [oMatch.Pos(0), oMatch.Len(0)]

      ;Add pair array to collection of pairs - "Matches" represents all the matches that are not part of existing strings.
      Matches.push(pair)
    }

    ;Next i
    i := oMatch.Pos(0) + oMatch.Len(0)

    ;Get next match from mbsource
    i := regexmatch(mbsource, "iO)" pattern, oMatch, i)
  }

  ;Match11 := Matches[1][1]
  ;Match12 := Matches[1][2]
  ;msgbox, Match11:%Match11% ¦ Match12: %Match12%

  ;if !EMB_Compact_Transpile then
  ;  for each match in matches
  ;    extract and remove match from code
  ;    replace /* with '
  ;    replace `r`n with `r`n'
  ;    replace */ with `r`n
  ;    re-insert new code where match was
  ;  next match
  ;Else
  ;  for each match in matches
  ;    remove match from code
  ;  next match
  ;End If

  ;msgbox, transpile or remove?
  ;msgbox, % !EMB_Compact_Transpile
  if (!EMB_Compact_Transpile) {
    ;Loop over Matches
	loop, % Matches.Length()
    {
	  LoopVar := Matches.Length() - A_Index + 1
      subSrc := p_EMB_Core_BC_removeFromString(mbsource,Matches[LoopVar][1],Matches[LoopVar][2])
		subSrc := RegExReplace(subSrc,"\/\*\s*","'")
		subSrc := StrReplace(subSrc,"`r`n","`r`n'")
		subSrc := RegExReplace(subSrc,"\s*\*\/","`r`n")
      p_EMB_Core_BC_insertIntoString(mbsource,subSrc,matches[LoopVar][1])
    }
  } else {
    p_EMB_Core_BC_removeFromString(mbsource,matches[A_Index][0],matches[A_Index][1])
  }

return mbsource
}

p_EMB_Core_BC_removeFromString(ByRef src,index,length){
  ;Supply function with an index and length of a string to extract
  ;The function will extract and return the string and remove it from the original src string.
  out_String := SubStr(src, index, length)
  src := SubStr(src,1,index - 1) . SubStr(src,index + length + 1)
  return out_String
}

p_EMB_Core_BC_insertIntoString(ByRef src,in_String,index){
  ;inserts a string "in_String" into src before the index supplied.
  ;Mid(s,1,Index-1) & "---" & Mid(s,Index)
  src := SubStr(src,1,index-1) . in_String . SubStr(src,index)
  return
}


Add mouse button hotkeys

Add mouse button hotkeys

; Using Keyboard Numpad as a Mouse -- by deguix
; http://www.autohotkey.com

;START OF CONFIG SECTION

#SingleInstance force
#MaxHotkeysPerInterval 500

; Using the keyboard hook to implement the Numpad hotkeys prevents
; them from interfering with the generation of ANSI characters such
; as .  This is because AutoHotkey generates such characters
; by holding down ALT and sending a series of Numpad keystrokes.
; Hook hotkeys are smart enough to ignore such keystrokes.
#UseHook

MouseSpeed = 1
MouseAccelerationSpeed = 1
MouseMaxSpeed = 5

;Mouse wheel speed is also set on Control Panel. As that
;will affect the normal mouse behavior, the real speed of
;these three below are times the normal mouse wheel speed.
MouseWheelSpeed = 1
MouseWheelAccelerationSpeed = 1
MouseWheelMaxSpeed = 5

;END OF CONFIG SECTION

;This is needed or key presses would faulty send their natural
;actions. Like NumPadDiv would send sometimes "/" to the
;screen.
#InstallKeybdHook

Temp = 0
Temp2 = 0

MouseCurrentAccelerationSpeed = 0
MouseCurrentSpeed = %MouseSpeed%

MouseWheelCurrentAccelerationSpeed = 0
MouseWheelCurrentSpeed = %MouseSpeed%

SetKeyDelay, -1
SetMouseDelay, -1

; CAPS LOCK KEYS

; Left hand
Hotkey, CapsLock & W, MouseLeftUp
Hotkey, CapsLock & E, MouseUp
Hotkey, CapsLock & R, MouseRightUp
Hotkey, CapsLock & S, MouseLeft
Hotkey, CapsLock & D, MouseDown
Hotkey, CapsLock & F, MouseRight
Hotkey, CapsLock & X, MouseLeftDown
Hotkey, CapsLock & C, MouseDown
Hotkey, CapsLock & V, MouseRightDown

; Right hand
Hotkey, CapsLock & U, MouseLeftUp
Hotkey, CapsLock & I, MouseUp
Hotkey, CapsLock & O, MouseRightUp
Hotkey, CapsLock & J, MouseLeft
Hotkey, CapsLock & K, MouseDown
Hotkey, CapsLock & L, MouseRight
Hotkey, CapsLock & M, MouseLeftDown
Hotkey, CapsLock & `, , MouseDown
Hotkey, CapsLock & `. , MouseRightDown

; Numeric
Hotkey, CapsLock & 7, MouseLeftUp
Hotkey, CapsLock & 8, MouseUp
Hotkey, CapsLock & 9, MouseRightUp
Hotkey, CapsLock & 4, MouseLeft
Hotkey, CapsLock & 5, MouseDown
Hotkey, CapsLock & 6, MouseRight
Hotkey, CapsLock & 1, MouseLeftDown
Hotkey, CapsLock & 2, MouseDown
Hotkey, CapsLock & 3, MouseRightDown

; Arrows
Hotkey, CapsLock & Up, MouseUp
Hotkey, CapsLock & Down, MouseDown
Hotkey, CapsLock & Left, MouseLeft
Hotkey, CapsLock & Right, MouseRight

; LEFT BUTTON KEYS

; Left hand
Hotkey, ~LButton & W, MouseLeftUp
Hotkey, ~LButton & E, MouseUp
Hotkey, ~LButton & R, MouseRightUp
Hotkey, ~LButton & S, MouseLeft
Hotkey, ~LButton & D, MouseDown
Hotkey, ~LButton & F, MouseRight
Hotkey, ~LButton & X, MouseLeftDown
Hotkey, ~LButton & C, MouseDown
Hotkey, ~LButton & V, MouseRightDown

; Right hand
Hotkey, ~LButton & U, MouseLeftUp
Hotkey, ~LButton & I, MouseUp
Hotkey, ~LButton & O, MouseRightUp
Hotkey, ~LButton & J, MouseLeft
Hotkey, ~LButton & K, MouseDown
Hotkey, ~LButton & L, MouseRight
Hotkey, ~LButton & M, MouseLeftDown
Hotkey, ~LButton & `, , MouseDown
Hotkey, ~LButton & `. , MouseRightDown

; Numeric
Hotkey, ~LButton & 7, MouseLeftUp
Hotkey, ~LButton & 8, MouseUp
Hotkey, ~LButton & 9, MouseRightUp
Hotkey, ~LButton & 4, MouseLeft
Hotkey, ~LButton & 5, MouseDown
Hotkey, ~LButton & 6, MouseRight
Hotkey, ~LButton & 1, MouseLeftDown
Hotkey, ~LButton & 2, MouseDown
Hotkey, ~LButton & 3, MouseRightDown

; Arrows
Hotkey, ~LButton & Up, MouseUp
Hotkey, ~LButton & Down, MouseDown
Hotkey, ~LButton & Left, MouseLeft
Hotkey, ~LButton & Right, MouseRight

; RIGHT BUTTON KEYS

; Left hand
Hotkey, ~RButton & W, MouseLeftUp
Hotkey, ~RButton & E, MouseUp
Hotkey, ~RButton & R, MouseRightUp
Hotkey, ~RButton & S, MouseLeft
Hotkey, ~RButton & D, MouseDown
Hotkey, ~RButton & F, MouseRight
Hotkey, ~RButton & X, MouseLeftDown
Hotkey, ~RButton & C, MouseDown
Hotkey, ~RButton & V, MouseRightDown

; Right hand
Hotkey, ~RButton & U, MouseLeftUp
Hotkey, ~RButton & I, MouseUp
Hotkey, ~RButton & O, MouseRightUp
Hotkey, ~RButton & J, MouseLeft
Hotkey, ~RButton & K, MouseDown
Hotkey, ~RButton & L, MouseRight
Hotkey, ~RButton & M, MouseLeftDown
Hotkey, ~RButton & `, , MouseDown
Hotkey, ~RButton & `. , MouseRightDown

; Numeric
Hotkey, ~RButton & 7, MouseLeftUp
Hotkey, ~RButton & 8, MouseUp
Hotkey, ~RButton & 9, MouseRightUp
Hotkey, ~RButton & 4, MouseLeft
Hotkey, ~RButton & 5, MouseDown
Hotkey, ~RButton & 6, MouseRight
Hotkey, ~RButton & 1, MouseLeftDown
Hotkey, ~RButton & 2, MouseDown
Hotkey, ~RButton & 3, MouseRightDown

; Arrows
Hotkey, ~RButton & Up, MouseUp
Hotkey, ~RButton & Down, MouseDown
Hotkey, ~RButton & Left, MouseLeft
Hotkey, ~RButton & Right, MouseRight

; MIDDLE BUTTON KEYS

; Left hand
Hotkey, ~MButton & W, MouseLeftUp
Hotkey, ~MButton & E, MouseUp
Hotkey, ~MButton & R, MouseRightUp
Hotkey, ~MButton & S, MouseLeft
Hotkey, ~MButton & D, MouseDown
Hotkey, ~MButton & F, MouseRight
Hotkey, ~MButton & X, MouseLeftDown
Hotkey, ~MButton & C, MouseDown
Hotkey, ~MButton & V, MouseRightDown

; Right hand
Hotkey, ~MButton & U, MouseLeftUp
Hotkey, ~MButton & I, MouseUp
Hotkey, ~MButton & O, MouseRightUp
Hotkey, ~MButton & J, MouseLeft
Hotkey, ~MButton & K, MouseDown
Hotkey, ~MButton & L, MouseRight
Hotkey, ~MButton & M, MouseLeftDown
Hotkey, ~MButton & `, , MouseDown
Hotkey, ~MButton & `. , MouseRightDown

; Numeric
Hotkey, ~MButton & 7, MouseLeftUp
Hotkey, ~MButton & 8, MouseUp
Hotkey, ~MButton & 9, MouseRightUp
Hotkey, ~MButton & 4, MouseLeft
Hotkey, ~MButton & 5, MouseDown
Hotkey, ~MButton & 6, MouseRight
Hotkey, ~MButton & 1, MouseLeftDown
Hotkey, ~MButton & 2, MouseDown
Hotkey, ~MButton & 3, MouseRightDown

; Arrows
Hotkey, ~MButton & Up, MouseUp
Hotkey, ~MButton & Down, MouseDown
Hotkey, ~MButton & Left, MouseLeft
Hotkey, ~MButton & Right, MouseRight

; EXTRA BUTTON 1 KEYS

; Left hand
Hotkey, ~XButton1 & W, MouseLeftUp
Hotkey, ~XButton1 & E, MouseUp
Hotkey, ~XButton1 & R, MouseRightUp
Hotkey, ~XButton1 & S, MouseLeft
Hotkey, ~XButton1 & D, MouseDown
Hotkey, ~XButton1 & F, MouseRight
Hotkey, ~XButton1 & X, MouseLeftDown
Hotkey, ~XButton1 & C, MouseDown
Hotkey, ~XButton1 & V, MouseRightDown

; Right hand
Hotkey, ~XButton1 & U, MouseLeftUp
Hotkey, ~XButton1 & I, MouseUp
Hotkey, ~XButton1 & O, MouseRightUp
Hotkey, ~XButton1 & J, MouseLeft
Hotkey, ~XButton1 & K, MouseDown
Hotkey, ~XButton1 & L, MouseRight
Hotkey, ~XButton1 & M, MouseLeftDown
Hotkey, ~XButton1 & `, , MouseDown
Hotkey, ~XButton1 & `. , MouseRightDown

; Numeric
Hotkey, ~XButton1 & 7, MouseLeftUp
Hotkey, ~XButton1 & 8, MouseUp
Hotkey, ~XButton1 & 9, MouseRightUp
Hotkey, ~XButton1 & 4, MouseLeft
Hotkey, ~XButton1 & 5, MouseDown
Hotkey, ~XButton1 & 6, MouseRight
Hotkey, ~XButton1 & 1, MouseLeftDown
Hotkey, ~XButton1 & 2, MouseDown
Hotkey, ~XButton1 & 3, MouseRightDown

; Arrows
Hotkey, ~XButton1 & Up, MouseUp
Hotkey, ~XButton1 & Down, MouseDown
Hotkey, ~XButton1 & Left, MouseLeft
Hotkey, ~XButton1 & Right, MouseRight

; EXTRA BUTTON 2 KEYS

; Left hand
Hotkey, ~XButton2 & W, MouseLeftUp
Hotkey, ~XButton2 & E, MouseUp
Hotkey, ~XButton2 & R, MouseRightUp
Hotkey, ~XButton2 & S, MouseLeft
Hotkey, ~XButton2 & D, MouseDown
Hotkey, ~XButton2 & F, MouseRight
Hotkey, ~XButton2 & X, MouseLeftDown
Hotkey, ~XButton2 & C, MouseDown
Hotkey, ~XButton2 & V, MouseRightDown

; Right hand
Hotkey, ~XButton2 & U, MouseLeftUp
Hotkey, ~XButton2 & I, MouseUp
Hotkey, ~XButton2 & O, MouseRightUp
Hotkey, ~XButton2 & J, MouseLeft
Hotkey, ~XButton2 & K, MouseDown
Hotkey, ~XButton2 & L, MouseRight
Hotkey, ~XButton2 & M, MouseLeftDown
Hotkey, ~XButton2 & `, , MouseDown
Hotkey, ~XButton2 & `. , MouseRightDown

; Numeric
Hotkey, ~XButton2 & 7, MouseLeftUp
Hotkey, ~XButton2 & 8, MouseUp
Hotkey, ~XButton2 & 9, MouseRightUp
Hotkey, ~XButton2 & 4, MouseLeft
Hotkey, ~XButton2 & 5, MouseDown
Hotkey, ~XButton2 & 6, MouseRight
Hotkey, ~XButton2 & 1, MouseLeftDown
Hotkey, ~XButton2 & 2, MouseDown
Hotkey, ~XButton2 & 3, MouseRightDown

; Arrows
Hotkey, ~XButton2 & Up, MouseUp
Hotkey, ~XButton2 & Down, MouseDown
Hotkey, ~XButton2 & Left, MouseLeft
Hotkey, ~XButton2 & Right, MouseRight

return

Move( x, y ) {
	MouseMove, x, y, 0, R
}

MouseUp:
	Move( 0, -1 )
	return
MouseDown:
	Move( 0, 1 )
	return
MouseLeft:
	Move( -1, 0 )
	return
MouseRight:
	Move( 1, 0 )
	return
MouseLeftUp:
	Move( -1, -1 )
	return
MouseRightUp:
	Move( 1, -1 )
	return
MouseLeftDown:
	Move( -1, 1 )
	return
MouseRightDown:
	Move( 1, 1 )
	return

If Action <> 0
{
	IfNotInString, A_ThisLabel, %Action%
	{
		MouseCurrentAccelerationSpeed = 0
		MouseCurrentSpeed = %MouseSpeed%
	}
}
StringReplace, Action, A_ThisLabel, *

ButtonAccelerationStart:
If MouseAccelerationSpeed >= 1
{
	If MouseMaxSpeed > %MouseCurrentSpeed%
	{
		Temp = 0.001
		Temp *= %MouseAccelerationSpeed%
		MouseCurrentAccelerationSpeed += %Temp%
		MouseCurrentSpeed += %MouseCurrentAccelerationSpeed%
	}
}

MouseCurrentSpeedToDirection = 1

MouseCurrentSpeedToSide = 0

MouseCurrentSpeedToDirection *= %MouseCurrentSpeed%
MouseCurrentSpeedToSide *= %MouseCurrentSpeed%

Temp = 0

If Action = MouseUp
{
	MouseCurrentSpeedToDirection *= -1
	MouseMove, %MouseCurrentSpeedToSide%, %MouseCurrentSpeedToDirection%, 0, R
}
else if Action = MouseDown
{
	MouseCurrentSpeedToSide *= -1
	MouseMove, %MouseCurrentSpeedToSide%, %MouseCurrentSpeedToDirection%, 0, R
}
else if Action = MouseLeft
{
	MouseCurrentSpeedToSide *= -1
	MouseCurrentSpeedToDirection *= -1

	MouseMove, %MouseCurrentSpeedToDirection%, %MouseCurrentSpeedToSide%, 0, R
}
else if Action = MouseRight
{
	MouseMove, %MouseCurrentSpeedToDirection%, %MouseCurrentSpeedToSide%, 0, R
}
else if Action = MouseLeftUp
{
	Temp = %MouseCurrentSpeedToDirection%
	Temp -= %MouseCurrentSpeedToSide%
	Temp *= -1
	Temp2 = %MouseCurrentSpeedToDirection%
	Temp2 += %MouseCurrentSpeedToSide%
	Temp2 *= -1
	MouseMove, %Temp%, %Temp2%, 0, R
}
else if Action = MouseRightUp
{
	Temp = %MouseCurrentSpeedToDirection%
	Temp += %MouseCurrentSpeedToSide%
	Temp2 = %MouseCurrentSpeedToDirection%
	Temp2 -= %MouseCurrentSpeedToSide%
	Temp2 *= -1
	MouseMove, %Temp%, %Temp2%, 0, R
}
else if Action = MouseLeftDown
{
	Temp = %MouseCurrentSpeedToDirection%
	Temp += %MouseCurrentSpeedToSide%
	Temp *= -1
	Temp2 = %MouseCurrentSpeedToDirection%
	Temp2 -= %MouseCurrentSpeedToSide%
	MouseMove, %Temp%, %Temp2%, 0, R
}
else if Action = MouseRightDown
{
	Temp = %MouseCurrentSpeedToDirection%
	Temp -= %MouseCurrentSpeedToSide%
	Temp2 *= -1
	Temp2 = %MouseCurrentSpeedToDirection%
	Temp2 += %MouseCurrentSpeedToSide%
	MouseMove, %Temp%, %Temp2%, 0, R
}

SetTimer, ButtonAccelerationEnd, 10
return

ButtonAccelerationEnd:
GetKeyState, kstate, %Action%, P
if kstate = D
	Goto ButtonAccelerationStart

SetTimer, ButtonAccelerationEnd, off
MouseCurrentAccelerationSpeed = 0
MouseCurrentSpeed = %MouseSpeed%
Action = 0
return

; Using Keyboard Numpad as a Mouse -- by deguix
; http://www.autohotkey.com

;START OF CONFIG SECTION

#SingleInstance force
#MaxHotkeysPerInterval 500

; Using the keyboard hook to implement the Numpad hotkeys prevents
; them from interfering with the generation of ANSI characters such
; as .  This is because AutoHotkey generates such characters
; by holding down ALT and sending a series of Numpad keystrokes.
; Hook hotkeys are smart enough to ignore such keystrokes.
#UseHook

MouseSpeed = 1
MouseAccelerationSpeed = 1
MouseMaxSpeed = 5

;Mouse wheel speed is also set on Control Panel. As that
;will affect the normal mouse behavior, the real speed of
;these three below are times the normal mouse wheel speed.
MouseWheelSpeed = 1
MouseWheelAccelerationSpeed = 1
MouseWheelMaxSpeed = 5

;END OF CONFIG SECTION

;This is needed or key presses would faulty send their natural
;actions. Like NumPadDiv would send sometimes "/" to the
;screen.
#InstallKeybdHook

Temp = 0
Temp2 = 0

MouseCurrentAccelerationSpeed = 0
MouseCurrentSpeed = %MouseSpeed%

MouseWheelCurrentAccelerationSpeed = 0
MouseWheelCurrentSpeed = %MouseSpeed%

SetKeyDelay, -1
SetMouseDelay, -1

; Left hand
Hotkey, CapsLock & W, MouseLeftUp
Hotkey, CapsLock & E, MouseUp
Hotkey, CapsLock & R, MouseRightUp
Hotkey, CapsLock & S, MouseLeft
Hotkey, CapsLock & D, MouseDown
Hotkey, CapsLock & F, MouseRight
Hotkey, CapsLock & X, MouseLeftDown
Hotkey, CapsLock & C, MouseDown
Hotkey, CapsLock & V, MouseRightDown

; Right hand
Hotkey, CapsLock & U, MouseLeftUp
Hotkey, CapsLock & I, MouseUp
Hotkey, CapsLock & O, MouseRightUp
Hotkey, CapsLock & J, MouseLeft
Hotkey, CapsLock & K, MouseDown
Hotkey, CapsLock & L, MouseRight
Hotkey, CapsLock & M, MouseLeftDown
Hotkey, CapsLock & `, , MouseDown
Hotkey, CapsLock & `. , MouseRightDown

; Numeric
Hotkey, CapsLock & 7, MouseLeftUp
Hotkey, CapsLock & 8, MouseUp
Hotkey, CapsLock & 9, MouseRightUp
Hotkey, CapsLock & 4, MouseLeft
Hotkey, CapsLock & 5, MouseDown
Hotkey, CapsLock & 6, MouseRight
Hotkey, CapsLock & 1, MouseLeftDown
Hotkey, CapsLock & 2, MouseDown
Hotkey, CapsLock & 3, MouseRightDown

; Arrows
Hotkey, CapsLock & Up, MouseUp
Hotkey, CapsLock & Down, MouseDown
Hotkey, CapsLock & Left, MouseLeft
Hotkey, CapsLock & Right, MouseRight

return

Move( x, y ) {
	MouseMove, x, y, 0, R
}

MouseUp:
	Move( 0, -1 )
	return
MouseDown:
	Move( 0, 1 )
	return
MouseLeft:
	Move( -1, 0 )
	return
MouseRight:
	Move( 1, 0 )
	return
MouseLeftUp:
	Move( -1, -1 )
	return
MouseRightUp:
	Move( 1, -1 )
	return
MouseLeftDown:
	Move( -1, 1 )
	return
MouseRightDown:
	Move( 1, 1 )
	return

If Action <> 0
{
	IfNotInString, A_ThisLabel, %Action%
	{
		MouseCurrentAccelerationSpeed = 0
		MouseCurrentSpeed = %MouseSpeed%
	}
}
StringReplace, Action, A_ThisLabel, *

ButtonAccelerationStart:
If MouseAccelerationSpeed >= 1
{
	If MouseMaxSpeed > %MouseCurrentSpeed%
	{
		Temp = 0.001
		Temp *= %MouseAccelerationSpeed%
		MouseCurrentAccelerationSpeed += %Temp%
		MouseCurrentSpeed += %MouseCurrentAccelerationSpeed%
	}
}

MouseCurrentSpeedToDirection = 1

MouseCurrentSpeedToSide = 0

MouseCurrentSpeedToDirection *= %MouseCurrentSpeed%
MouseCurrentSpeedToSide *= %MouseCurrentSpeed%

Temp = 0

If Action = MouseUp
{
	MouseCurrentSpeedToDirection *= -1
	MouseMove, %MouseCurrentSpeedToSide%, %MouseCurrentSpeedToDirection%, 0, R
}
else if Action = MouseDown
{
	MouseCurrentSpeedToSide *= -1
	MouseMove, %MouseCurrentSpeedToSide%, %MouseCurrentSpeedToDirection%, 0, R
}
else if Action = MouseLeft
{
	MouseCurrentSpeedToSide *= -1
	MouseCurrentSpeedToDirection *= -1

	MouseMove, %MouseCurrentSpeedToDirection%, %MouseCurrentSpeedToSide%, 0, R
}
else if Action = MouseRight
{
	MouseMove, %MouseCurrentSpeedToDirection%, %MouseCurrentSpeedToSide%, 0, R
}
else if Action = MouseLeftUp
{
	Temp = %MouseCurrentSpeedToDirection%
	Temp -= %MouseCurrentSpeedToSide%
	Temp *= -1
	Temp2 = %MouseCurrentSpeedToDirection%
	Temp2 += %MouseCurrentSpeedToSide%
	Temp2 *= -1
	MouseMove, %Temp%, %Temp2%, 0, R
}
else if Action = MouseRightUp
{
	Temp = %MouseCurrentSpeedToDirection%
	Temp += %MouseCurrentSpeedToSide%
	Temp2 = %MouseCurrentSpeedToDirection%
	Temp2 -= %MouseCurrentSpeedToSide%
	Temp2 *= -1
	MouseMove, %Temp%, %Temp2%, 0, R
}
else if Action = MouseLeftDown
{
	Temp = %MouseCurrentSpeedToDirection%
	Temp += %MouseCurrentSpeedToSide%
	Temp *= -1
	Temp2 = %MouseCurrentSpeedToDirection%
	Temp2 -= %MouseCurrentSpeedToSide%
	MouseMove, %Temp%, %Temp2%, 0, R
}
else if Action = MouseRightDown
{
	Temp = %MouseCurrentSpeedToDirection%
	Temp -= %MouseCurrentSpeedToSide%
	Temp2 *= -1
	Temp2 = %MouseCurrentSpeedToDirection%
	Temp2 += %MouseCurrentSpeedToSide%
	MouseMove, %Temp%, %Temp2%, 0, R
}

SetTimer, ButtonAccelerationEnd, 10
return

ButtonAccelerationEnd:
GetKeyState, kstate, %Action%, P
if kstate = D
	Goto ButtonAccelerationStart

SetTimer, ButtonAccelerationEnd, off
MouseCurrentAccelerationSpeed = 0
MouseCurrentSpeed = %MouseSpeed%
Action = 0
return


Updated dosbox.ahk

Updated dosbox.ahk

RunWait dosbox-x
ExitApp

LWin::F13
LAlt::F14
RAlt::F15

LWin::F13
LAlt::F14
RAlt::F15
RunWait "C:\dosbox\dosbox-x.exe", C:\dosbox
ExitApp


hide/show

hide/show

/*
  command functions : command_[a-z]+
  @Author  : johngrib82@gmail.com
  @Created : 2015. NOV. 25
*/

/*
  maximize active window
*/
command_max(args) {
  WinMaximize, A
}
/*
  minimize active window
*/
command_min(args) {
  WinMinimize, A
}

/*
  send ctrl + s
*/
command_w(args) {
  Send, ^s
}

/*
  adjust window invisibility 0 ~ 255
*/
command_trans(args){
  if(args.HasKey(2))
    option := args[2]
  else
    option := 35

  WinSet, Transparent, %option% , A
}

/*
  reset window inivisibility
*/
command_notrans(args){
  WinSet, Transparent, OFF, A
}

/*
  kill process (active window)
*/
command_kill(args){
  WinKill, A
}

/*
  get color at mouse pointer
*/
command_color(args){
  MouseGetPos, x, y
  PixelGetColor, rgb, x, y, RGB
  StringTrimLeft, rgb, rgb, 2
  InputBox, color, color, , , 300, 110, , , , , %rgb%
  Return
}

/*
  shows list of memorized windows
*/
command_mark(args){
  msg := ""
  loop 10
  {
    num := A_Index - 1
    win_id := MARK[num]
    WinGetTitle, title, ahk_id %win_id%
    msg .= num . " : " . title . "`n"
  }
  MsgBox %msg%
}

/*
  edit script
*/
command_edit(args){
  Edit
}

/*
  show key log
*/
command_log(args){
  KeyHistory
}

/*
  show list of hot keys
*/
command_hotkey(args){
  ListHotkeys
}

/*
  get title text of active window
*/
command_title(args){
  WinGetActiveTitle, title
  InputBox, title, TITLE, , , 300, 110, , , , , %title%
}

/*
  get time/date string
*/
command_time(args){

  if(args.HasKey(2) and args[2] = "help"){
    Run, https://www.autohotkey.com/docs/commands/FormatTime.htm#Date_Formats_case_sensitive
    return
  }

  format_str := { "": "", "date" :  "yyyy.MM.d", "now" : "hh:mm:ss"}

  if(args.HasKey(2) and format_str.HasKey(args[2]))
    form := format_str[args[2]]
  else
    form := args[2]

  FormatTime, TimeString, ,%form%
  InputBox, time, time, , , 300, 110, , , , , %TimeString%
}

/*
  show variables list
*/
command_list(args){
  ListVars
}

/*
  open everything
*/
command_run(args){
  txt := args[2]
  run %txt%
}

/*
  open keywords
  Canonical Names of Control Panel Items
  @link https://msdn.microsoft.com/en-us/library/ee330741%28VS.85%29.aspx
*/
command_op(args){

  sect := CFG.get_sect("OP_ADDRESS")

  if(sect.HasKey(args[2])){
    value := sect[args[2]]
    page := args[3]
    sentence := Trim(value . " " . page)
    Run, %sentence%
  }
}
command_open(args){
  command_op(args)
}

/*
  show clipboard register
*/
command_reg(args){
  rs := CLIP.get_list()
  MsgBox % rs
}

/*
  adjust invisivility of status bar
*/
command_stattrans(args){

  if(not RegExMatch(args[2], "^\d{1,3}$"))
    return

  if(args[2] < 0)
    args[2] := 0
  else if(args[2] > 255)
    args[2] := 255

  CFG.get_sect("NORMAL")["trans"] := args[2]
  CFG.get_sect("INSERT")["trans"] := args[2]
  CFG.get_sect("CAPSLOCK")["trans"] := args[2]

  CFG.fix_ini_value("NORMAL", "trans", args[2] )
  CFG.fix_ini_value("INSERT", "trans", args[2] )
  CFG.fix_ini_value("CAPSLOCK", "trans", args[2] )
}

/*
command_hide(args) {
  activeWin := WinExist("A")
  invisWins .= (invisWins ? "|" : "") . activeWin
  WinHide ahk_id %activeWin%
  GroupActivate All
}

command_show(args) {
  Loop Parse, invisWins, |
    WinShow ahk_id %A_LoopField%

  invisWins := ""
}
*/


Use mintty_options in run-wsl-file.exe and vim.exe

#NoTrayIcon
#NoEnv

; Read ini file {{{1
ini_file = %A_ScriptDir%\etc\wsl-terminal.conf
IniRead, title, %ini_file%, config, title, "        "
IniRead, shell, %ini_file%, config, shell, "bash"
IniRead, use_cbwin, %ini_file%, config, use_cbwin, 0
IniRead, use_tmux, %ini_file%, config, use_tmux, 0
IniRead, mintty_options, %ini_file%, config, mintty_options,
IniRead, icon, %ini_file%, config, icon,
IniRead, distro_guid, %ini_file%, config, distro_guid,
IniRead, keep_wsl_running, %ini_file%, config, keep_wsl_running, 0

if (mintty_options == "ERROR") {
    mintty_options =
}

; Prepare mintty_base and wslbridge_base {{{1
icon_option = -i "%A_ScriptFullPath%"
if (icon != "" && FileExist(icon)) {
    icon_option = -i "%icon%"
}

distro_option := ""
if (distro_guid != "ERROR") {
    distro_option = --distro-guid %distro_guid%
}

mintty_base = "%A_ScriptDir%\bin\mintty" --wsl --rootfs=// --configdir "%A_ScriptDir%\etc" %icon_option%
wslbridge_base = -e /bin/wslbridge %distro_option% -e SHELL="%shell%" -e LANG

; Run as run-wsl-file or any editor {{{1
SplitPath, A_ScriptName, , , , exe_name
if (exe_name == "run-wsl-file") {
    arg = %1%

    if (arg == "") {
        MsgBox, Open .sh/.py/.pl/... with %exe_name%.exe to run it in WSL.
        ExitApp
    }

    SplitPath, arg, filename, dir
    SetWorkingDir, %dir%

    Run, %mintty_base% %mintty_options% -t "%arg%" %wslbridge_base% -t ./"%filename%"
    ExitApp
} else if (exe_name != "open-wsl" && exe_name != "cmd") {
    argc = %0%
    filepath := ""
    filename := ""
    options := ""

    if (argc > 0) {
        filepath := %argc%

        Loop, % argc - 1 {
            options .= " " %A_Index%
        }

        SplitPath, filepath, filename, dir
        SetWorkingDir, %dir%

        if (InStr(filename, " ")) {
            filename = "%filename%"
        }
    }

    Run, %mintty_base% %mintty_options% -t "%filepath%" %wslbridge_base% -t "%exe_name%" %options% %filename%
    ExitApp
}

; Parse arguments {{{1
argc = %0%
args := []
Loop, %argc% {
    args.Insert(%A_Index%)
}

activate_window := False
change_directory := ""
distro := ""
login_shell := False
wslbridge_options := ""
user_command := ""

i := 0
while (i++ < argc) {
    c := args[i]
    if (c == "-a") {
        activate_window := True
    } else if (c == "-C") {
        if (argc < ++i) {
            MsgBox, 0x10, , Require directory arg.
            ExitApp, 1
        }

        change_directory := args[i]
    } else if (c == "-W") {
        if (argc < ++i) {
            MsgBox, 0x10, , Require directory arg.
            ExitApp, 1
        }

        SetWorkingDir, % args[i]
    } else if (c == "-l") {
        login_shell := True
    } else if (c == "-d") {
        if (argc < ++i) {
            MsgBox, 0x10, , Require distro name arg.
            ExitApp, 1
        }

        distro := args[i]
    } else if (c == "-b") {
        if (argc < ++i) {
            MsgBox, 0x10, , Require additional wslbridge options arg.
            ExitApp, 1
        }

        wslbridge_options := args[i]
    } else if (c == "-B") {
        if (argc < ++i) {
            MsgBox, 0x10, , Require additional mintty options arg.
            ExitApp, 1
        }

        mintty_options .= " " args[i]
    } else if (c == "-c") {
        if (argc < ++i) {
            MsgBox, 0x10, , Require command arg.
            ExitApp, 1
        }

        user_command := args[i]
    } else if (c == "-e") {
        if (argc < ++i) {
            MsgBox, 0x10, , Require command arg.
            ExitApp, 1
        }

        user_command := shell " -c """ args[i]
        while (++i <= argc) {
            user_command .= " " args[i]
        }
        user_command .= """"
        StringReplace, user_command, user_command, `", `\`", All
    } else if (c == "-h") {
        help =
        (
        Usage: open-wsl [OPTION]...
          -a: activate an existing wsl-terminal window.
              if use_tmux=1, attach the running tmux session.
          -l: start terminal in your home directory (doesn't work with tmux).
          -c "command": run command (e.g. -c "echo a b; echo c; cat").
          -e commands: run commands (e.g. -e echo a b; echo c; cat).
          -C dir: change directory to a WSL dir (e.g. /home/username).
          -W dir: change directory to a Windows dir (e.g. c:\Users\username).
          -d distro: switch distros.
          -b "options": pass additional options to wslbridge.
          -B "options": pass additional options to mintty.
          -h: show help.
        )
        MsgBox, %help%
        ExitApp
    }
}

if (user_command != "") {
    if (change_directory != "") {
        wslbridge_options = %wslbridge_options% -C "%change_directory%"
    }

    Run, %mintty_base% %mintty_options% -t "%user_command%" %wslbridge_base% %wslbridge_options% -t %shell% -c "%user_command%"
    ExitApp
}

; Find bash.exe {{{1
bash_exe = %A_WinDir%\sysnative\bash.exe
if (!FileExist(bash_exe)) {
    bash_exe = %A_WinDir%\system32\bash.exe
} if (!FileExist(bash_exe)) {
    MsgBox, 0x10, , WSL(Windows Subsystem for Linux) must be installed.
    ExitApp, 1
}

; Switch distro {{{1
if (distro != "") {
    Run, % StrReplace(bash_exe, "bash.exe", "wslconfig.exe") " /s " distro
}

; Build command line {{{1
cmd =
opts = %wslbridge_options% -t

if (activate_window && WinExist(title)) {
} else if (!use_tmux) {
    if (login_shell) {
        cmd = %shell% -l
        if (change_directory == "") {
            change_directory = ~
        }
    } else {
        cmd = %shell%
    }
} else {
    if (WinExist(title)) {
        Run, "%bash_exe%" -c 'tmux new-window -c "$PWD"', , Hide
    } else {
        cmd = %shell%
        opts = %opts% -e USE_TMUX=1

        if (activate_window) {
            opts = %opts% -e ATTACH_ONLY=1
        }
    }
}

if (change_directory != "") {
    opts = %opts% -C "%change_directory%"
}

if (cmd != "") {
    Run, %mintty_base% %mintty_options% -t "%title%" %wslbridge_base% %opts% %cmd%
}

; Activate window {{{1
Loop, 5 {
    WinActivate, %title%
    if (WinActive(title)) {
        break
    }

    Sleep, 50
}

if (use_cbwin) {
    Process, Exist, outbash.exe

    if (ErrorLevel == 0) {
        Run, "%A_ScriptDir%\bin\outbash" --outbash-session -c 'exec sleep 10000000d', , Hide
    }
} else if (keep_wsl_running) {
    Process, Exist, sleep

    if (ErrorLevel == 0) {
        Run, "%bash_exe%" -c 'exec sleep 10000000d', , Hide
    }
}

#NoTrayIcon
#NoEnv

; Read ini file {{{1
ini_file = %A_ScriptDir%\etc\wsl-terminal.conf
IniRead, title, %ini_file%, config, title, "        "
IniRead, shell, %ini_file%, config, shell, "bash"
IniRead, use_cbwin, %ini_file%, config, use_cbwin, 0
IniRead, use_tmux, %ini_file%, config, use_tmux, 0
IniRead, mintty_options, %ini_file%, config, mintty_options,
IniRead, icon, %ini_file%, config, icon,
IniRead, distro_guid, %ini_file%, config, distro_guid,
IniRead, keep_wsl_running, %ini_file%, config, keep_wsl_running, 0

if (mintty_options == "ERROR") {
    mintty_options =
}

; Prepare mintty_base and wslbridge_base {{{1
icon_option = -i "%A_ScriptFullPath%"
if (icon != "" && FileExist(icon)) {
    icon_option = -i "%icon%"
}

distro_option := ""
if (distro_guid != "ERROR") {
    distro_option = --distro-guid %distro_guid%
}

mintty_base = "%A_ScriptDir%\bin\mintty" --wsl --rootfs=// --configdir "%A_ScriptDir%\etc" %icon_option%
wslbridge_base = -e /bin/wslbridge %distro_option% -e SHELL="%shell%" -e LANG

; Run as run-wsl-file or any editor {{{1
SplitPath, A_ScriptName, , , , exe_name
if (exe_name == "run-wsl-file") {
    arg = %1%

    if (arg == "") {
        MsgBox, Open .sh/.py/.pl/... with %exe_name%.exe to run it in WSL.
        ExitApp
    }

    SplitPath, arg, filename, dir
    SetWorkingDir, %dir%

    Run, %mintty_base% -t "%arg%" %wslbridge_base% -t ./"%filename%"
    ExitApp
} else if (exe_name != "open-wsl" && exe_name != "cmd") {
    argc = %0%
    filepath := ""
    filename := ""
    options := ""

    if (argc > 0) {
        filepath := %argc%

        Loop, % argc - 1 {
            options .= " " %A_Index%
        }

        SplitPath, filepath, filename, dir
        SetWorkingDir, %dir%

        if (InStr(filename, " ")) {
            filename = "%filename%"
        }
    }

    Run, %mintty_base% -t "%filepath%" %wslbridge_base% -t "%exe_name%" %options% %filename%
    ExitApp
}

; Parse arguments {{{1
argc = %0%
args := []
Loop, %argc% {
    args.Insert(%A_Index%)
}

activate_window := False
change_directory := ""
distro := ""
login_shell := False
wslbridge_options := ""
user_command := ""

i := 0
while (i++ < argc) {
    c := args[i]
    if (c == "-a") {
        activate_window := True
    } else if (c == "-C") {
        if (argc < ++i) {
            MsgBox, 0x10, , Require directory arg.
            ExitApp, 1
        }

        change_directory := args[i]
    } else if (c == "-W") {
        if (argc < ++i) {
            MsgBox, 0x10, , Require directory arg.
            ExitApp, 1
        }

        SetWorkingDir, % args[i]
    } else if (c == "-l") {
        login_shell := True
    } else if (c == "-d") {
        if (argc < ++i) {
            MsgBox, 0x10, , Require distro name arg.
            ExitApp, 1
        }

        distro := args[i]
    } else if (c == "-b") {
        if (argc < ++i) {
            MsgBox, 0x10, , Require additional wslbridge options arg.
            ExitApp, 1
        }

        wslbridge_options := args[i]
    } else if (c == "-B") {
        if (argc < ++i) {
            MsgBox, 0x10, , Require additional mintty options arg.
            ExitApp, 1
        }

        mintty_options .= " " args[i]
    } else if (c == "-c") {
        if (argc < ++i) {
            MsgBox, 0x10, , Require command arg.
            ExitApp, 1
        }

        user_command := args[i]
    } else if (c == "-e") {
        if (argc < ++i) {
            MsgBox, 0x10, , Require command arg.
            ExitApp, 1
        }

        user_command := shell " -c """ args[i]
        while (++i <= argc) {
            user_command .= " " args[i]
        }
        user_command .= """"
        StringReplace, user_command, user_command, `", `\`", All
    } else if (c == "-h") {
        help =
        (
        Usage: open-wsl [OPTION]...
          -a: activate an existing wsl-terminal window.
              if use_tmux=1, attach the running tmux session.
          -l: start terminal in your home directory (doesn't work with tmux).
          -c "command": run command (e.g. -c "echo a b; echo c; cat").
          -e commands: run commands (e.g. -e echo a b; echo c; cat).
          -C dir: change directory to a WSL dir (e.g. /home/username).
          -W dir: change directory to a Windows dir (e.g. c:\Users\username).
          -d distro: switch distros.
          -b "options": pass additional options to wslbridge.
          -B "options": pass additional options to mintty.
          -h: show help.
        )
        MsgBox, %help%
        ExitApp
    }
}

if (user_command != "") {
    if (change_directory != "") {
        wslbridge_options = %wslbridge_options% -C "%change_directory%"
    }

    Run, %mintty_base% %mintty_options% -t "%user_command%" %wslbridge_base% %wslbridge_options% -t %shell% -c "%user_command%"
    ExitApp
}

; Find bash.exe {{{1
bash_exe = %A_WinDir%\sysnative\bash.exe
if (!FileExist(bash_exe)) {
    bash_exe = %A_WinDir%\system32\bash.exe
} if (!FileExist(bash_exe)) {
    MsgBox, 0x10, , WSL(Windows Subsystem for Linux) must be installed.
    ExitApp, 1
}

; Switch distro {{{1
if (distro != "") {
    Run, % StrReplace(bash_exe, "bash.exe", "wslconfig.exe") " /s " distro
}

; Build command line {{{1
cmd =
opts = %wslbridge_options% -t

if (activate_window && WinExist(title)) {
} else if (!use_tmux) {
    if (login_shell) {
        cmd = %shell% -l
        if (change_directory == "") {
            change_directory = ~
        }
    } else {
        cmd = %shell%
    }
} else {
    if (WinExist(title)) {
        Run, "%bash_exe%" -c 'tmux new-window -c "$PWD"', , Hide
    } else {
        cmd = %shell%
        opts = %opts% -e USE_TMUX=1

        if (activate_window) {
            opts = %opts% -e ATTACH_ONLY=1
        }
    }
}

if (change_directory != "") {
    opts = %opts% -C "%change_directory%"
}

if (cmd != "") {
    Run, %mintty_base% %mintty_options% -t "%title%" %wslbridge_base% %opts% %cmd%
}

; Activate window {{{1
Loop, 5 {
    WinActivate, %title%
    if (WinActive(title)) {
        break
    }

    Sleep, 50
}

if (use_cbwin) {
    Process, Exist, outbash.exe

    if (ErrorLevel == 0) {
        Run, "%A_ScriptDir%\bin\outbash" --outbash-session -c 'exec sleep 10000000d', , Hide
    }
} else if (keep_wsl_running) {
    Process, Exist, sleep

    if (ErrorLevel == 0) {
        Run, "%bash_exe%" -c 'exec sleep 10000000d', , Hide
    }
}