#Region - TimeStamp
; 2013-03-10 23:02:35   v 0.2
#EndRegion - TimeStamp

#include "SciTE_Constants.au3"

#cs  The actions understood by SciTE are:
askfilename: 								Return the name of the file being edited.
askproperty:<key> 							Return the value of a property.
close: 										Close the current file.
closing: 									Director is closing - SciTE closes if it was started by the director.
currentmacro:<string> 						Set the current macro to name.
cwd: 										Change the working directory.
enumproperties:dyn|local|user|base|embed 	Enumerate all the properties in the argument set.
exportashtml:<path> 						Save the document in HTML format as the indicated file.
exportasrtf:<path> 							Save the document in RTF format as the indicated file.
exportaspdf:<path> 							Save the document in PDF format as the indicated file.
exportaslatex:<path> 						Save the document in LaTeX format as the indicated file.
exportasxml:<path> 							Save the document in XML format as the indicated file.
extender:<command> 							Call the extension interface with the given command.
find:<string> 								Search for a string, select and show it.
goto:<lineNumber>[,<columnNumber>] 			Move caret to a particular line and make it visible.
											If there is a column number then select the word at that column number or move the caret there if no word is present.
identity:<hwndDirector> 					Sets the director window handle to which SciTE sends messages. The argument is in decimal.
insert:<value> 								Display the value in the editor pane replacing the selection.
loadsession:<path>	 						Load a session as given by the indicated file.
macrocommand:<command> 						Execute a macro command. See the SciTE source code for the syntax of the command argument.
macroenable:<enable> 						If enable, display menu commands in SciTE for recording and playing macros.
macrolist:<list> 							Display a list for the user to choose from.
menucommand:<cmd> 							Execute a menu command based on numeric ID.
open:<path> 								Open the indicated file.
output:<value> 								Display the value in the output pane replacing the selection.
property:<key>=<value> 						Set a property to a value.
quit: 										Shut down SciTE.
reloadproperties: 							Reload properties from files.
replaceall:<search>\000<replace> 			Replace all instances of he search string in the document with the replace string.
saveas:<path> 								Save the document as the indicated file.
savesession:<path> 							Save a session as given by the indicated file.
#ce

Global $SciTECmd
GUIRegisterMsg(74, "MY_WM_COPYDATA")  ; $WM_COPYDATA = 74


;===============================================================================
; Function Name....: _GetCurrentFile
; Description......: Gibt den Pfad der aktuell im Editor geöffneten Datei zurück
; Parameter(s).....: keine
; Return Value(s)..: Dateipfad
; Author(s)........: BugFix ( bugfix@autoit.de )
;===============================================================================
Func _GetCurrentFile()
	SendSciTE_Command("askfilename:", 1)
	Return StringReplace(StringTrimLeft($SciTECmd,StringInStr($SciTECmd, ':', 1, 3)), '\\', '\')
EndFunc

;===============================================================================
; Function Name....: _GetProperty
; Description......: Gibt den Wert für eine Property zurück
; Parameter(s).....: Property
; Return Value(s)..: Wert der Property
; Docu.............: http://www.scintilla.org/SciTEDoc.html od. "SciTE-Hilfe" --> "SciTE Documentation"
; Author(s)........: BugFix ( bugfix@autoit.de )
;===============================================================================
Func _GetProperty($_sProperty)
	SendSciTE_Command("askproperty:" & $_sProperty, 1)
	Return StringTrimLeft($SciTECmd,StringInStr($SciTECmd, ':', 1, 4))
EndFunc

;===============================================================================
; Function Name....: _CloseCurrentFile
; Description......: Schließt die aktuell offene Datei im Editor
; Parameter(s).....: keine
; Return Value(s)..: keine
; Author(s)........: BugFix ( bugfix@autoit.de )
;===============================================================================
Func _CloseCurrentFile()
	SendSciTE_Command("close:")
EndFunc

;===============================================================================
; Function Name....: _FindString
; Description......: Sucht nach einem übergebenen String und selektiert ihn, wenn
; .................: gefunden (erstes Vorkommen)
; Parameter(s).....: Suchstring
; Return Value(s)..: keine
; Author(s)........: BugFix ( bugfix@autoit.de )
;===============================================================================
Func _FindString($_sString)
	SendSciTE_Command("find:" & $_sString)
EndFunc

;===============================================================================
; Function Name....: _GoToLineCol
; Description......: Setzt den Cursor auf die übergebene Zeilen und (optional)
; .................: Spaltenposition. Zeile: 1-basiert, Spalte: 0-basiert
; .................: Ist an dieser Position ein Wort, wird es selektiert
; Parameter(s).....: Zeile, optional Spalte
; Return Value(s)..: keine
; Author(s)........: BugFix ( bugfix@autoit.de )
;===============================================================================
Func _GoToLineCol($_iLine, $_iCol=-1)
	Local $iCol = ""
	If $_iCol > -1 Then $iCol = "," & $_iCol
	SendSciTE_Command("goto:" & $_iLine & $iCol)
EndFunc

;===============================================================================
; Function Name....: _InsertValue
; Description......: Fügt an aktueller Cursorposition od. Selektion den übergebenen
; .................: wert ein
; Parameter(s).....: Einfügestring
; Return Value(s)..: keine
; Author(s)........: BugFix ( bugfix@autoit.de )
;===============================================================================
Func _InsertValue($_vValue)
	SendSciTE_Command("insert:" & $_vValue)
EndFunc

;===============================================================================
; Function Name....: _MenuCmd
; Description......: Führt einen Menübefehl aus
; Parameter(s).....: Command-ID
; Requirement(s)...: SciTE_Constants.au3
; Return Value(s)..: keine
; Docu.............: http://www.scintilla.org/CommandValues.html
; Author(s)........: BugFix ( bugfix@autoit.de )
;===============================================================================
Func _MenuCmd($_CmdID)
	SendSciTE_Command("menucommand:" & $_CmdID)
EndFunc

;===============================================================================
; Function Name....: _FileOpen
; Description......: Öffnet eine Datei im Editor
; Parameter(s).....: Dateipfad
; Return Value(s)..: keine
; Author(s)........: BugFix ( bugfix@autoit.de )
;===============================================================================
Func _FileOpen($_sPath)
	SendSciTE_Command("open:" & StringReplace($_sPath, '\', '\\'))
EndFunc

;===============================================================================
; Function Name....: _OutputToConsole
; Description......: Gibt einen Wert in die Konsole aus
; Parameter(s).....: auszugebender Wert, Zeilenumbruch mit "\n", Tab mit "\t"
; Return Value(s)..: keine
; Author(s)........: BugFix ( bugfix@autoit.de )
;===============================================================================
Func _OutputToConsole($_vValue)
	SendSciTE_Command("output:" & $_vValue)
EndFunc

;===============================================================================
; Function Name....: _SetProperty
; Description......: Setzt den Wert einer Property
; Parameter(s).....: Property und Wert
; Return Value(s)..: keine
; Author(s)........: BugFix ( bugfix@autoit.de )
;===============================================================================
Func _SetProperty($_sProperty, $_sValue)
	SendSciTE_Command("property:" & $_sProperty & "=" & $_sValue)
EndFunc

;===============================================================================
; Function Name....: _ReloadProperties
; Description......: Lädt die Properties neu
; Parameter(s).....: keine
; Return Value(s)..: keine
; Author(s)........: BugFix ( bugfix@autoit.de )
;===============================================================================
Func _ReloadProperties()
	SendSciTE_Command("reloadproperties:")
EndFunc

;===============================================================================
; Function Name....: _ReplaceAll
; Description......: Ersetzt alle Vorkommen eines Suchbegriffs
; Parameter(s).....: Suchwort und Ersetzung
; Return Value(s)..: keine
; Author(s)........: BugFix ( bugfix@autoit.de )
;===============================================================================
Func _ReplaceAll($_sSearch, $_sReplace)
	SendSciTE_Command("replaceall:" & $_sSearch & "\000" & $_sReplace)
EndFunc

;===============================================================================
; Function Name....: _FileSaveAs
; Description......: Datei unter angegebenem Pfad speichern
; Parameter(s).....: Dateipfad
; Return Value(s)..: keine
; Author(s)........: BugFix ( bugfix@autoit.de )
;===============================================================================
Func _FileSaveAs($_sPath)
	SendSciTE_Command("saveas:" & StringReplace($_sPath, '\', '\\'))
EndFunc

; by Jos
Func SendSciTE_Command($sCmd, $Wait_For_Return_Info = 0)
    Local $WM_COPYDATA = 74
    Local $WM_GETTEXT = 0x000D
    Local $WM_GETTEXTLENGTH = 0x000E224
    Local Const $SCI_GETLINE = 2153
    Local $Scite_hwnd = WinGetHandle("DirectorExtension") ; Get SciTE DIrector Handle
    Local $My_Hwnd = GUICreate("AutoIt3-SciTE interface") ; Create GUI to receive SciTE info
    Local $My_Dec_Hwnd = Dec(StringTrimLeft($My_Hwnd, 2)) ; Convert my Gui Handle to decimal
    $sCmd = ":" & $My_Dec_Hwnd & ":" & $sCmd              ; Add dec my gui handle to commandline to tell SciTE where to send the return info
    Local $CmdStruct = DllStructCreate('Char[' & StringLen($sCmd) + 1 & ']')
    DllStructSetData($CmdStruct, 1, $sCmd)
    Local $COPYDATA = DllStructCreate('Ptr;DWord;Ptr')
    DllStructSetData($COPYDATA, 1, 1)
    DllStructSetData($COPYDATA, 2, StringLen($sCmd) + 1)
    DllStructSetData($COPYDATA, 3, DllStructGetPtr($CmdStruct))
    DllCall('User32.dll', 'None', 'SendMessage', 'HWnd', $Scite_hwnd, _
            'Int', $WM_COPYDATA, 'HWnd', $My_Hwnd, _
            'Ptr', DllStructGetPtr($COPYDATA))
    GUIDelete($My_Hwnd)
EndFunc   ;==>SendSciTE_Command

Func MY_WM_COPYDATA($hWnd, $msg, $wParam, $lParam)
	Local $COPYDATA = DllStructCreate('Ptr;DWord;Ptr', $lParam)
	Local $SciTECmdLen = DllStructGetData($COPYDATA, 2)
	Local $CmdStruct = DllStructCreate('Char[255]', DllStructGetData($COPYDATA, 3))
	$SciTECmd = StringLeft(DllStructGetData($CmdStruct, 1), $SciTECmdLen)
EndFunc   ;==>MY_WM_COPYDATA

