; vim.exe should be in the %path% variable or in the same directory as vimedit.exe

#SingleInstance force

Hotkey, XButton1 & v, EditSelect
Hotkey, XButton1 & s, EditSelectSql
Hotkey, XButton1 & p, EditSelectPython
Hotkey, XButton1 & z, EditSelectTest

EditSelect:
    winId := WinExist("A")
    Clipboard =
    Send ^{ins}
    ; If no text is selected then select the current line
    if Clipboard = 
    {
        Send {home}
        Send +{end}
        Send ^{ins}
    }
	; if this isn't in home folder I get permissions issues
    fileName := "c:\users\ikeane\es" . A_YYYY . A_MM . A_DD . A_Hour . A_Min . A_Sec . A_MSec
    FileDelete, %fileName%
    FileAppend, %Clipboard%, %fileName%
	; noudf turns off undofiles
    runwait, "c:\Program Files (x86)\Vim\vim82\gvim.exe" -c "set noudf" %fileName%, c:\
    FileRead, contents, %fileName%
    StringRight, ending, contents, 2
    if ending = `r`n
        StringTrimRight, contents, contents, 2 ; remove last crlf from clipboard
    Clipboard := contents 
    WinActivate ahk_id %winId% 
    Send +{ins}
    contents = 
    FileDelete, %fileName%
return

EditSelectTest:
    winId := WinExist("A")
    Send ^{ins}
    fileName := "c:\users\ikeane\es" . A_YYYY . A_MM . A_DD . A_Hour . A_Min . A_Sec . A_MSec
    FileDelete, %fileName%
    FileAppend, %Clipboard%, %fileName%
    runwait, "c:\Program Files (x86)\Vim\vim82\gvim.exe" -c "set noudf" %fileName%, c:\
    FileRead, contents, %fileName%
    StringRight, ending, contents, 2
    if ending = `r`n
        StringTrimRight, contents, contents, 2 ; remove last crlf from clipboard
    Clipboard := contents 
    WinActivate ahk_id %winId% 
    Send +{ins}
    contents = 
    FileDelete, %fileName%
return

EditSelectSql:
    winId := WinExist("A")
    Clipboard =
    Send ^{ins}
    ; If no text is selected then select the current line
    if Clipboard = 
    {
        Send {home}
        Send +{end}
        Send ^{ins}
    }
	; if this isn't in home folder I get permissions issues
    fileName := "c:\users\ikeane\es" . A_YYYY . A_MM . A_DD . A_Hour . A_Min . A_Sec . A_MSec
    FileDelete, %fileName%
    FileAppend, %Clipboard%, %fileName%
	; noudf turns off undofiles
    runwait, "c:\Program Files (x86)\Vim\vim82\gvim.exe" -c "set noudf syntax=sql" %fileName%, c:\
    FileRead, contents, %fileName%
    StringRight, ending, contents, 2
    if ending = `r`n
        StringTrimRight, contents, contents, 2 ; remove last crlf from clipboard
    Clipboard := contents 
    WinActivate ahk_id %winId% 
    Send +{ins}
    contents = 
    FileDelete, %fileName%
return

EditSelectPython:
    winId := WinExist("A")
    Clipboard =
    Send ^{ins}
    ; If no text is selected then select the current line
    if Clipboard = 
    {
        Send {home}
        Send +{end}
        Send ^{ins}
    }
	; if this isn't in home folder I get permissions issues
    fileName := "c:\users\ikeane\es" . A_YYYY . A_MM . A_DD . A_Hour . A_Min . A_Sec . A_MSec
    FileDelete, %fileName%
    FileAppend, %Clipboard%, %fileName%
	; noudf turns off undofiles
    runwait, "c:\Program Files (x86)\Vim\vim82\gvim.exe" -c "set noudf syntax=Python" %fileName%, c:\
    FileRead, contents, %fileName%
    StringRight, ending, contents, 2
    if ending = `r`n
        StringTrimRight, contents, contents, 2 ; remove last crlf from clipboard
    Clipboard := contents 
    WinActivate ahk_id %winId% 
    Send +{ins}
    contents = 
    FileDelete, %fileName%
return
