For the time being, it will be a script that converts AHK v1.1 code to AHK v1.1 code that is more forwards compatible, i.e. more AHK v2-ready.
I will eventually make it able to convert from AHK v1.1 to AHK v2, however I need AHK v2 to be more finalised before it's worth finishing/publishing stage 2.
Note: Currently this handles:
- var = string -> var := "string"
- var = num -> var := num
It can also identify where continuation sections begin/end.
I will add in further 'var = ' to 'var := ' support, and also various conversions such as StringReplace to StrReplace.
I would suggest that you compare any before/after code with a utility such as WinMerge to confirm that any changes were the right ones, and I would recommend backing up your original code.
I may also add in some code, which can be turned on/off to:
- confirm each line matches certain standards (including: ensure commands have/don't have commas between the command name and the first parameter)
- convert some parameters to force expression mode, e.g. 'Cmd, %var%' to 'Cmd, % var'
- clean up NumPut e.g. ensure 4 parameters
- clean up DllCall lines (this could be quite challenging)
- indent code (I finally got some working code for this yesterday, that correctly converted an entire script)
Some things may be trickier, and so I may provide no or limited support for them, e.g. converting StringSplit to StrSplit, or handling 'return' lines with multiple parameters, or handling the removal of SetFormat. [I will return with a list of the more difficult conversions that would need to be done manually.]
I may also provide support to convert code so that it can use my 'AHK v2 functions for AHK v1' library.
I may also provide support for converting existing functions to variant functions, e.g. SubStr lines to 'JEE_SubStr' lines, providing an AHK v2-style 'SubStr' in AHK v1. Perhaps I will call that particular function one of: 'AHKv2_SubStr' / 'Ahk2_SubStr' / 'SubStr2' / 'JEE_SubStr2', I'm not sure, although I tend to prefer that any nonstandard function have a prefix of some sort to avoid confusion with standard functions.
However, some of these things may present difficulties, so there may be limited modes that convert some matching lines (safer conversions), but that instead notify you of potential other matching lines (riskier conversions) (perhaps by appending comments to those lines).
Btw I've already done all the serious work for this, it's more getting it ready for public viewing that is the issue. Also, of course I need AHK v2 to be more finalised, I've already had to do a lot of 'unconverting' and major rewrites to conversion scripts as the AHK v2 specs have changed, so I've decided to put AHK v1 -> AHK v2 conversion on hold, and focus on AHK v1 command style to AHK v1 expression style, and related script checking and script tidying.
;AHK v1 command style to AHK v1 expression style
;make AHK v1 more ready for conversion
;vText := JEE_GetSelectedText()
;ControlGet, vText, Selected,, Edit1, A
vPath := A_ScriptDir "\AutoHotkey.ahk"
FileRead, vText, % vPath
vTextOrig := vText
vOutput := ""
vText := StrReplace(vText, "`r`n", "`n")
oArrayOrig := StrSplit(vText, "`n")
oArrayNew := StrSplit(vText, "`n")
vIsConSec := 0
vLastCodeLine := 0
vConSecPfx := "[CONSEC] "
vConSecSfx := " `;[CONSEC]"
vConSecPfx := ""
vConSecSfx := ""
Loop, % oArrayNew.Length()
vTemp := oArrayNew[A_Index]
if (SubStr(vTemp, 1, 1) = ";")
vCode := "", vComments := vTemp
else if (vPos := InStr(vTemp, " `;"))
vCode := SubStr(vTemp, 1, vPos-1), vComments := SubStr(vTemp, vPos)
vCode := vTemp, vComments := ""
vCode2 := LTrim(vCode)
vWhitespace := SubStr(vCode, 1, StrLen(vCode)-StrLen(vCode2))
;HANDLE CONTINUATION SECTIONS
if (SubStr(vCode2, 1, 1) = "(") && !InStr(vCode2, ")")
vIsConSec := 1
if !vLastCodeLine || !(vLastCodeLine < A_Index)
MsgBox, % "error: " vLastCodeLine
Loop, % A_Index - vLastCodeLine
oArrayNew[vLastCodeLine-1+A_Index] := vConSecPfx oArrayOrig[vLastCodeLine-1+A_Index] vConSecSfx
if vIsConSec && !(vConSecPfx vConSecSfx = "")
oArrayNew[A_Index] := vConSecPfx vTemp vConSecSfx
if (SubStr(vCode2, 1, 1) = ")")
vIsConSec := 0
vCode2Orig := vCode2
;var = string -> var := "string"
if RegExMatch(vCode2, "^[A-Za-z]+ = [A-Za-z]+$")
vCode2 := RegExReplace(vCode2, "^([A-Za-z]+) = ([A-Za-z]+)$", "$1 := " Chr(34) "$2" Chr(34))
;var = num -> var := num
else if RegExMatch(vCode2, "^[A-Za-z]+ = \d+$")
vCode2 := RegExReplace(vCode2, "^([A-Za-z]+) = (\d+)$", "$1 := $2")
if !(vCode2 = vCode2Orig)
oArrayNew[A_Index] := vWhitespace vCode2 vComments
if !(vCode2 = "")
vLastCodeLine := A_Index
Loop, % oArrayNew.Length()
vOutput .= (A_Index=1?"":"`r`n") oArrayNew[A_Index]
;Clipboard := vOutput
;JEE_WinMergeCompareStrings("BEFORE`r`n" vTextOrig, "AFTER`r`n" vOutput)