; Necessary includes #include #include ; A littile delay for smooth working Opt("SendKeyDownDelay",20 ) ; Declare enter key as a const Global Const $VK_RETURN = 0x0D ; Start SciTE editor Local $Process = Run("C:\Program Files\AutoIt3\SciTE\SciTE.exe") ; Wait for SciTE to active WinWait("[CLASS:SciTEWindow]") ; Get the handle of SciTE window Local $Handle = WinGetHandle("[CLASS:SciTEWindow]") ; Here is our hotkey assignment. Thanks for Yashied. _HotKey_Assign($VK_RETURN, "EnterPress", 0, $Handle) ; Looping while SciTE closes While 1 Sleep(10) If Not ProcessExists($Process) Then Exit EndIf WEnd ; Main Function starts here ; #FUNCTION# ==================================================================================================================== ; Name ..........: EnterPress ; Description ...: This is the main function ; Syntax ........: EnterPress() ; Parameters ....: No parametes ; Return values .: None ; Author ........: kcvinu ; Modified ......: 04-04-2015 ; Remarks .......: ; Related .......: ; Link ..........: ; Example .......: No ; =============================================================================================================================== Func EnterPress() ;------------------------------------------------------------------------------------------------------------------ Global $SciTE_Title ; Title of SciTE window Global $SciTE_Text ; Total text in SciTE code editor Global $CurrentLineNumber ; Line number where cursor rests Global $LineCount ; Total line numbers Global $CurrentColNumber ; Col number where cursor rests Global $CurrentLine_Text ; Text of current line number Global $CLT_Length ; Length of current line Global $IsSymbol ; Boolean variable for checking any "#, ; , _" symbols in the beginning Global $SpaceStriped_CLT ; Space stripped from both side of current line text Global $FirstWord ; First word of the current line Global $LastThen ; If a "Then" key word is in the last are a current line Global $FW_col ; Col number of first word Global $EW_col ; Col number of end keywords like"EndIf/WEnd" etc Global $CLT_Array ; An array to split whole text with @LF, Means each line will be splitted Global $NxtLineTxt ; Text of next line from cursor Global $NWC_Status = True ; Boolean, if an end keyword is in next line, it is false Global $EndWord ; End keywords like "EndIf/WEnd" etc. Global $NoNeedToPaste = False ; Boolean, if first word is not a keywork like "If/While", then it is false ;---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Global $KeyWordList[7][7] = [['If', 'EndIf'], ['While', 'WEnd'], ['For', 'Next'], ['Do', 'Until'], ['Select', 'EndSelect'], ['Func', 'EndFunc'], ['Switch', 'EndSwitch']] ;---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ; Getting total line number from SciTE code window $LineCount = ControlCommand($Handle, "S", 350, "GetLineCount", " ") ; Getting text from SciTE code window $SciTE_Text = ControlGetText($Handle, "S", 350) ; Getting current line number from SciTE code window $CurrentLineNumber = ControlCommand($Handle, "S", 350, "GetCurrentLine", " ") ; Getting current col number from SciTE code window $CurrentColNumber = ControlCommand($Handle, "S", 350, "GetCurrentCol", " ") ;----------------------------------------------------------------------------------------------- ; Split the text into lines and put each line text in this array $CLT_Array = StringSplit($SciTE_Text, @LF) ; Getting the current line text from the array $CurrentLine_Text = $CLT_Array[$CurrentLineNumber] ;------------------------------------------------------------------------------------------------------------ ; If any errors are there, then current line text is empty If @error Then $CurrentLine_Text = "" ; getting the length of current line text $CLT_Length = StringLen($CurrentLine_Text) ; Stripping the left side space from current line text $SpaceStriped_CLT = StringStripWS($CurrentLine_Text, 1) ; Stripping the right side space from current line text $SpaceStriped_CLT = StringStripWS($SpaceStriped_CLT, 2) ; Checking if there is any symbols in the beginning $IsSymbol = StringRegExp($SpaceStriped_CLT, "^#|^_|^;") ; Getting the first word,only if it is a 'if' or 'func' etc.. (StringMatchAndGet is my function) $FirstWord = StringMatchAndGet($SpaceStriped_CLT, "^[iI]f|^[fF]unc|^[fF]or|^[wW]hile|^[sS]witch|^[dD]o|^[sS]elect") ; Check if there is a "Then" is present $LastThen = StringMatchAndGet($SpaceStriped_CLT, "Then$") ; Getting col number of first word $FW_col = StringInStr($CurrentLine_Text, $FirstWord) ;----------------------------------------------------------------------------------------------------------------------------- ; If first word is a keyword like "If/While" If $FirstWord <> "" Then For $j = 0 To 6 ; Find the appropriate end keyword from $KeyWordList If $KeyWordList[$j][0] = $FirstWord Then $EndWord = $KeyWordList[$j][1] EndIf Next ; If the first word is not a keyword ElseIf $FirstWord = "" Then ; Then no need to paste any end key words $NoNeedToPaste = True EndIf ;------------------------------------------------------------------------------------------------------------------- ; Calling the ColFinder function ColFinder() ;------------------------------------------------------------------------------------------------------------------- ; If cursor is in the middile area of the line If $CLT_Length > $CurrentColNumber Then ; Just act like a normal enter key press ControlSend($Handle, "S", 350, "{ENTER}") ; If there is no text in SciTE ElseIf $SciTE_Text = "" Then ; Just act like a normal enter key press ControlSend($Handle, "S", 350, "{ENTER}") ; If no need to paste any keywords, That means first word is empty ElseIf $NoNeedToPaste = True Then ; Just act like a normal enter key press ControlSend($Handle, "S", 350, "{ENTER}") ; If the beginning of the line is a symbol like "#, ; , _" ElseIf $IsSymbol = 1 Then ; Just act like a normal enter key press ControlSend($Handle, "S", 350, "{ENTER}") ; If there is no text in current line ElseIf $CurrentLine_Text = "" Then ; Just act like a normal enter key press ControlSend($Handle, "S", 350, "{ENTER}") ; If first word is 'If' and no 'EndIf' in next line ElseIf $FirstWord = "If" And $NWC_Status = True Then ; Check for the presence of a 'Then' If $LastThen = "" Then ; If there is not a 'Then', then paste it ControlSend($Handle, "S", 350, " Then{SPACE}{ENTER 2}{BS}EndIf{SPACE}{UP}") ElseIf $LastThen = "Then" Then ; Else do paste the 'EndIf' ControlSend($Handle, "S", 350, "{ENTER 2}{BS}EndIf{SPACE}{UP}") EndIf ; If first word is 'func' and no 'Endfunc' in next line ElseIf $FirstWord = "Func" Or $FirstWord = "func" And $NWC_Status = True Then ; Check for the presence of a '()' If StringRight($SpaceStriped_CLT,2) = "()" Then ; If so, just paste the 'EndFunc' ControlSend($Handle, "S", 350, "{ENTER 2}{BS}EndFunc{SPACE}{UP}") Else ; Paste the Endfunc followed by '()' ControlSend($Handle, "S", 350, "(){SPACE}{ENTER 2}{BS}EndFunc{SPACE}{UP}") EndIf ; If first word is 'For' And no 'Next' In Next line ElseIf $FirstWord = "For" Or $FirstWord = "for" And $NWC_Status = True Then ; Paste a 'Next' ControlSend($Handle, "S", 350, "{ENTER 2}{BS}Next{SPACE}{UP}") ; If first word is 'While' And no 'WEnd' In Next line ElseIf $FirstWord = "While" Or $FirstWord = "while" And $NWC_Status = True Then ; Paste a 'WEnd' ControlSend($Handle, "S", 350, "{ENTER 2}{BS}WEnd{SPACE}{UP}") ; If first word is 'Do' And no 'Until' In Next line ElseIf $FirstWord = "Do" Or $FirstWord = "do" And $NWC_Status = True Then ; Paste an 'Until' ControlSend($Handle, "S", 350, "{ENTER 2}{BS}Until{SPACE}{UP}") ; If first word is 'Switch' And no 'EndSwitch' In Next line ElseIf $FirstWord = "Switch" Or $FirstWord = "switch" And $NWC_Status = True Then ; Paste a 'Case' and 'EndSwitch' ControlSend($Handle, "S", 350, "{ENTER}Case{SPACE}{ENTER}{BS 2}EndSwitch{SPACE}{UP}{SPACE}") ; If first word is 'Select' And no 'EndSelect' In Next line ElseIf $FirstWord = "Select" Or $FirstWord = "select" And $NWC_Status = True Then ; Paste an 'EndSelect' ControlSend($Handle, "S", 350, "{ENTER 2}{BS}EndSelect{SPACE}{UP}") Else ; Else, Just act like a normal enter key press ControlSend($Handle, "S", 350, "{ENTER}") EndIf EndFunc ;==>EnterPress ;================================================================================================================================ ; #FUNCTION# ==================================================================================================================== ; Name ..........: StringMatchAndGet ; Description ...: A simple function for finding words with regular expression ; Syntax ........: StringMatchAndGet($String, $Pattern) ; Parameters ....: $String - A string to use ; $Pattern - A pattern to find any word. ; Return values .: None ; Author ........: kcvinu ; Modified ......: ; Remarks .......: ; Related .......: ; Link ..........: ; Example .......: No ; =============================================================================================================================== Func StringMatchAndGet($String, $Pattern) Local $Result Local $MatchArray = StringRegExp($String, $Pattern, 1) If @error Then $MatchArray = " " Else $Result = $MatchArray[0] EndIf Return $Result EndFunc ;==>StringMatchAndGet ; #FUNCTION# ==================================================================================================================== ; Name ..........: ColFinder ; Description ...: A function to find the col number of an end keyword like "EndIf" or "WEnd" ; Syntax ........: ColFinder() ; Parameters ....: None ; Return values .: None ; Author ........: kcvinu ; Modified ......: ; Remarks .......: ; Related .......: ; Link ..........: ; Example .......: No ; =============================================================================================================================== Func ColFinder() ; This is the col number of the first word $FW_col = StringInStr($CurrentLine_Text, $FirstWord) If $SciTE_Text = "" Then $NWC_Status = False ElseIf $CLT_Array[0] = 1 Then $NWC_Status = True ; If there is more lines under the current line ElseIf $LineCount > $CurrentLineNumber Then ; Loop through each line For $i = $CurrentLineNumber + 1 To $CLT_Array[0] ; Getting next line $NxtLineTxt = $CLT_Array[$i] ; If there is an appropriate end keyword there If StringInStr($NxtLineTxt, $EndWord) = $FW_col Then ; No need to paste an end keyword $NWC_Status = False ExitLoop EndIf Next EndIf EndFunc ;==>ColFinder