#Region - TimeStamp
; 2011-08-15 13:41:03   v 1.1
#EndRegion - TimeStamp

#include-once
#include <Array.au3>
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <ListViewConstants.au3>
#include <StaticConstants.au3>
#include <StructureConstants.au3>
#include <WindowsConstants.au3>
#include <File.au3>
#include <GUIListView.au3>
#include <GuiEdit.au3>
Opt("GUIOnEventMode", 1)

Global $ScitePath = RegRead("HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\SciTE.exe", "")
Global $KeyWordPath = StringReplace($ScitePath, "SciTE.exe", "properties\au3.keywords.abbreviations.properties")
Global $MyKeyWordPath = @UserProfileDir & '\au3.keywords.my'
Global $Abbr_Path = @UserProfileDir & '\abbrev.properties'
Global $MyAbbrevPath = @UserProfileDir & '\abbrev.properties.my'
	   If Not FileExists($MyAbbrevPath) Then
		   Local $fh = FileOpen($MyAbbrevPath, 1)
		   FileWriteLine($fh, '; >> My own abbrevs <<')
		   FileClose($fh)
	   EndIf
Global $edit = False
Global $Last_STR = '#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#'
Global $txt, $aTitel[2] = ['Code für neue Abkürzung', 'Code bearbeiten']
Global $aLabel[2] = [ _
'Bitte den Code für die Abkürzung einfügen. Einrückungen bitte mit Tabulator (Im Editfeld durch STRG+TAB).' & @LF & _
		'Für die gewünschte Cursorposition:  |  verwenden. Der Code wird beim Schließen des Fensters übernommen.', _
'Hier den Code bearbeiten. Einrückungen bitte mit Tabulator (Im Editfeld durch STRG+TAB).' & @LF & _
		'Für die gewünschte Cursorposition:  |  verwenden. Der Code wird beim Schließen des Fensters übernommen.']
Global $sAbbrev, $aAbbrevFile, $aAbbrev[1][2]

; == Bei erstmaligem Start ausführen
; == erstes Vorkommen eines Abbrev werden behalten - weitere auskommentiert mit: '#; CHANGED abbrev'
If Not FileExists(@UserProfileDir & '\AbbrevDoubleCheck.done') Then
	MsgBox(64, 'Erststart', 'Einmalige Überprüfung auf doppelte Abbrevs.' & @LF & 'Jetzt starten', 3)
	Local $sToWrite = 'No Double'
	Local $ret = _SetDoubleAsComment()
	If $ret <> '' Then $sToWrite = $ret
	FileWrite(@UserProfileDir & '\AbbrevDoubleCheck.done', $sToWrite)
	MsgBox(64, 'Doppelüberprüfung', 'Das Ergebnis der Überprüfung ist in der Datei:' & @LF & @LF & @UserProfileDir & '\AbbrevDoubleCheck.done' & @LF & @LF & 'dokumentiert. ' & _
	'Das erste Vorkommen eines Abbrev wurde behalten, weitere wurden auskommentiert.' & @LF & 'Um später nochmals eine Überprüfung auf Doppel durchzuführen' & @LF & _
	'(empfehlenswert nach einem Update von SciTE), einfach diese Ergebnisdatei löschen.')
EndIf
; == Ende Double Check

_FileReadToArray($Abbr_Path, $aAbbrevFile)
Local $ret, $aIndex, $aCheck = $aAbbrevFile
ReDim $aAbbrev[$aCheck[0]][2]
For $i = 1 To $aCheck[0]
	If StringRegExp($aCheck[$i], "(\A\w+)=(.+)") Then
		$ret = _SplitOnce($aCheck[$i])
		$aAbbrev[0][0] += 1
		$aAbbrev[$aAbbrev[0][0]][0] = $ret[0]
		$aAbbrev[$aAbbrev[0][0]][1] = $ret[1]
	EndIf
Next
Local $aTmp[$aAbbrev[0][0]+1][2]
$aTmp[0][0] = $aAbbrev[0][0]
For $i = 1 To $aTmp[0][0]
	$aTmp[$i][0] = $aAbbrev[$i][0]
	$aTmp[$i][1] = $aAbbrev[$i][1]
Next
$aAbbrev = $aTmp
$aTmp = 0

$Form1 = GUICreate("Abkürzungen verwalten  [" & $Abbr_Path & ']', 800, 620, -1, -1)
GUISetOnEvent($GUI_EVENT_CLOSE, "Form1Close")
$mnu = GUICtrlCreateMenu('Wiederherstellung')
$mnu_restore = GUICtrlCreateMenuItem('Wiederherstellung eigener Abbrevs', $mnu)
GUICtrlSetOnEvent(-1, '_Restore')
$mnuKey = GUICtrlCreateMenu('Keywords')
$mnu_keycreate = GUICtrlCreateMenuItem('Keyword-Datei neu erstellen', $mnuKey)
GUICtrlSetOnEvent(-1, '_KeyCreate')
$iSearch = GUICtrlCreateInput('', 15, 10, 90, 20)
$bSearch = GUICtrlCreateButton('Suchen', 125, 10, 150, 20, $BS_DEFPUSHBUTTON)
GUICtrlSetOnEvent(-1, '_Search')
$bCreate = GUICtrlCreateButton('Hinzufügen', 295, 10, 150, 20)
GUICtrlSetOnEvent(-1, '_Create')
$bEdit = GUICtrlCreateButton('Bearbeiten', 465, 10, 150, 20)
GUICtrlSetOnEvent(-1, '_Edit')
$bDelete = GUICtrlCreateButton('Löschen', 635, 10, 150, 20)
GUICtrlSetOnEvent(-1, '_Delete')
$ListView1 = GUICtrlCreateListView("Abkürzung|Code", 15, 40, 770, 545, BitOR($LVS_SHOWSELALWAYS,$LVS_SINGLESEL,$WS_HSCROLL,$WS_VSCROLL))
GUICtrlSetOnEvent(-1, "ListView1Click")
GUICtrlSetBkColor(-1, 0xF0F8FF)
GUICtrlSetBkColor(-1, $GUI_BKCOLOR_LV_ALTERNATE)
GUICtrlSendMsg(-1, $LVM_SETEXTENDEDLISTVIEWSTYLE, $LVS_EX_ONECLICKACTIVATE, $LVS_EX_ONECLICKACTIVATE)
_GUICtrlListView_SetColumnWidth(-1, 0, 90)
_GUICtrlListView_SetColumnWidth(-1, 1, $LVSCW_AUTOSIZE_USEHEADER)

_LV_Fill()

$Form2 = GUICreate('Code zur Abkürzung', 700, 400, -1, -1)
GUISetOnEvent($GUI_EVENT_CLOSE, "Form2Close")
$Edit1 = GUICtrlCreateEdit('', 10, 10, 680, 380, BitOR($GUI_SS_DEFAULT_EDIT,$ES_READONLY))
GUICtrlSetBkColor(-1, 0x98FB98)

$Form3 = GUICreate('Code für neue Abkürzung', 700, 400, -1, -1)
GUISetOnEvent($GUI_EVENT_CLOSE, "Form3Close")
$Label1 = GUICtrlCreateLabel('Bitte den Code für die Abkürzung einfügen. Einrückungen bitte mit Tabulator (Im Editfeld durch STRG+TAB).' & @LF & _
		'Für die gewünschte Cursorposition:  |  verwenden. Der Code wird beim Schließen des Fensters übernommen.' _
		, 10, 5, 680, 30, $SS_SUNKEN+$SS_CENTER)
GUICtrlSetBkColor(-1, 0x98FB98)
$Edit2 = GUICtrlCreateEdit('', 10, 40, 680, 350, BitOR($WS_HSCROLL,$WS_VSCROLL,$ES_MULTILINE,$ES_WANTRETURN))
GUICtrlSetBkColor(-1, 0x98FB98)

GUISetState(@SW_SHOW, $Form1)
GUIRegisterMsg($WM_NOTIFY, "MY_WM_COMMAND")

While 1
	Sleep(100)
WEnd

Func Form1Close()
	Exit
EndFunc   ;==>Form1Close

Func Form2Close()
	GUISetState(@SW_HIDE, $Form2)
	GUICtrlSetData($Edit1, '')
EndFunc   ;==>Form2Close

Func Form3Close()
	GUISetState(@SW_HIDE, $Form3)
	If $edit Then
		_WriteEditedAbbrev()
	Else
		_WriteNewAbbrev()
	EndIf
EndFunc   ;==>Form3Close

Func _SetDoubleAsComment()
	_FileReadToArray($Abbr_Path, $aAbbrevFile)
	Local $oDouble = ObjCreate("Scripting.Dictionary"), $sRet = ''
	For $i = 1 To $aAbbrevFile[0]
		If StringRegExp($aAbbrevFile[$i], "(\A\w+)=(.+)") Then
			$KEYWORD = StringLeft($aAbbrevFile[$i], StringInStr($aAbbrevFile[$i], '=', 1, 1) - 1)
			If $oDouble.Exists($KEYWORD) Then
				ContinueLoop
			EndIf
			$aIndex = _CheckOccurences($aAbbrevFile, $KEYWORD)
			$sTmp = ''
			If $aIndex[0] > 1 Then
				For $j = 1 To $aIndex[0]
					$sTmp &= $aIndex[$j] & ','
					If $j > 1 Then
						$aAbbrevFile[$aIndex[$j]] = '#; CHANGED ' & $aAbbrevFile[$aIndex[$j]] ; == Doppel auskommentieren
						$sRet &= 'Zeile ' & $i & ': ' & $aAbbrevFile[$aIndex[$j]] & @CRLF
					EndIf
				Next
				$oDouble.Add($KEYWORD, StringTrimRight($sTmp, 1))
			EndIf
		EndIf
	Next
	If $oDouble.Count > 0 Then _ObjDictList($oDouble, 'Doppel in "abbrev.properties", auskommentiert mit: #; CHANGED', 'Abbrev', 'Zeilen')
	_FileWriteFromArray($Abbr_Path, $aAbbrevFile, 1)
	Return $sRet
EndFunc

Func _CheckOccurences($aArray, $sToCheck, $iStart=1)
	Local $aIndex[1] = [0]
	For $i = $iStart To UBound($aArray) -1
		If StringLeft($aArray[$i], StringLen($sToCheck)+1) = $sToCheck & '=' Then
			$aIndex[0] += 1
			ReDim $aIndex[$aIndex[0] +1]
			$aIndex[$aIndex[0]] = $i
		EndIf
	Next
	Return $aIndex
EndFunc

;==================================================================================================
; Name:      _ObjDictList($oDICT [, $TITLE='Elemente: Objekt Dictionary'])
; Parameter: $oDICT - Handle des Dictionary-Objektes
;            $TITLE - Fenstertitel (optional)
; Return:    Erfolg:   GUI mit ListView
;            Fehler:  -1  @Error=1  Objekt existiert nicht
; Requirements:       #include <GuiConstants.au3>
;                     #include <GuiListView.au3>
;==================================================================================================
Func _ObjDictList(ByRef $oDICT, $TITLE = 'Elemente: Objekt Dictionary', $sKey='Schlüssel', $sValue='Wert')
	If Not IsObj($oDICT) Then Return SetError(1, 0, -1)
	Local $count = $oDICT.Count
	Local $SaveMode = Opt("GUIOnEventMode", 0), $ListGUI, $oDictLV, $btnClose, $msg
	$ListGUI = GUICreate($TITLE, 600, 400, (@DesktopWidth - 600) / 2, (@DesktopHeight - 400) / 2)
	$btnClose = GUICtrlCreateButton('&Ende', 40, 360, 70, 22)
	GUICtrlSetResizing($btnClose, BitOR($GUI_DockRight, $GUI_DockBottom, $GUI_DockSize))
	GUICtrlDelete($oDictLV)
	$oDictLV = GUICtrlCreateListView($sKey & '|' & $sValue, 10, 10, 580, 340, BitOR($LVS_SHOWSELALWAYS, _
			$LVS_EDITLABELS), BitOR($LVS_EX_GRIDLINES, $LVS_EX_HEADERDRAGDROP, $LVS_EX_FULLROWSELECT, $LVS_EX_REGIONAL))
	If $count > 0 Then
		Local $strKey, $colKeys = $oDICT.Keys
		For $strKey In $colKeys
			GUICtrlCreateListViewItem($strKey & '|' & $oDICT.Item($strKey), $oDictLV)
		Next
	Else
		WinSetTitle($ListGUI, '', 'Das Objekt Dictionary enthält keine Elemente!')
	EndIf
	GUISetState(@SW_SHOW, $ListGUI)
	While 1
		$msg = GUIGetMsg(1)
		If $msg[1] = $ListGUI And _
				($msg[0] = $GUI_EVENT_CLOSE Or $msg[0] = $btnClose) Then ExitLoop
	WEnd
	GUIDelete($ListGUI)
	Opt("GUIOnEventMode", $SaveMode)
EndFunc   ;==>_ObjDictList

Func _KeyCreate()
	Local $KEYWORD, $sToWrite = 'au3.keywords.abbrev=', $txtZeile = '', $len
	_FileReadToArray($Abbr_Path, $aAbbrevFile)
	For $i = 1 To $aAbbrevFile[0]
		If StringRegExp($aAbbrevFile[$i], "(\A\w+)=(.+)") Then
			$KEYWORD = StringLeft($aAbbrevFile[$i], StringInStr($aAbbrevFile[$i], '=', 1, 1) - 1)
			$len = StringLen($txtZeile) + 4
			If $len + StringLen($KEYWORD) > 100 Then
				$sToWrite &= $txtZeile & "\" & @CRLF
				$txtZeile = @TAB & $KEYWORD & " "
			Else
				$txtZeile &= $KEYWORD & " "
			EndIf
		EndIf
	Next
	$sToWrite &= $txtZeile
	FileMove($KeyWordPath, $KeyWordPath & '.BAK', 1)
	Local $fh = FileOpen($KeyWordPath, 2)
	FileWrite($fh, $sToWrite)
	FileClose($fh)
EndFunc   ;==>_KeyCreate

Func _Search()
	$txt = GUICtrlRead($iSearch)
	If $txt = '' Then Return
	Local $indx = _LV_FindInSubItem($ListView1, $txt, 0)
	If $indx = -1 Then Return MsgBox(0, 'Suche Abbrev', 'Abbrev: "' & $txt & '" existiert nicht.')
	GUICtrlSetState($ListView1, $GUI_FOCUS)
	_GUICtrlListView_EnsureVisible($ListView1, $indx)
	_GUICtrlListView_SetItemSelected($ListView1, $indx)
	_GUICtrlListView_SetItemFocused($ListView1, $indx)
EndFunc   ;==>_Search

Func _Create()
	$txt = GUICtrlRead($iSearch)
	If $txt = '' Then Return MsgBox(16, 'Fehler', 'Keine Abkürzung eingetragen.')
	Local $indx = _LV_FindInSubItem($ListView1, $txt, 0)
	If $indx > -1 Then Return MsgBox(16, 'Fehler', 'Die Abkürzung existiert bereits.')
	$edit = False
	WinSetTitle($Form3, '', $aTitel[0] & '  [ ' & $txt & ' ]')
	GUICtrlSetData($Label1, $aLabel[0])
	GUISetState(@SW_SHOW, $Form3)
EndFunc

Func _Edit()
	$txt = GUICtrlRead($iSearch)
	If $txt = '' Then Return MsgBox(16, 'Fehler', 'Keine Abkürzung ausgewählt.')
	$edit = True
	WinSetTitle($Form3, '', $aTitel[1] & '  [ ' & $txt & ' ]')
	GUICtrlSetData($Label1, $aLabel[1])
	$sAbbrev = _GUICtrlListView_GetItemText($ListView1, _LV_FindInSubItem($ListView1, $txt, 0), 1)
	GUICtrlSetData($Edit2, _Abbrev2Code($sAbbrev))
	GUISetState(@SW_SHOW, $Form3)
EndFunc   ;==>_Edit

Func _Restore()
	Local $aMyAbbrev, $split_f, $index
	_FileReadToArray($MyAbbrevPath, $aMyAbbrev)
	For $i = 2 To UBound($aMyAbbrev) - 1
		$split_f = _SplitOnce($aMyAbbrev[$i])
		If @error Then ContinueLoop
		$index = _ArraySearch($aAbbrev, $split_f[0], 1)
		If Not @error Then ; == gefunden, Abbrev ändern > in Array: $aAbbrev
			$aAbbrev[$index][1] = $split_f[1]
			WinSetTitle($Form1, '', 'Restore Abbrev: ' & $split_f[0])
		Else ; == Abbrev existiert nicht ==> Erstellen  > in Array: $aAbbrev
			$aAbbrev[0][0] += 1
			ReDim $aAbbrev[$aAbbrev[0][0] +1][2]
			$aAbbrev[$aAbbrev[0][0]][0] = $split_f[0]
			$aAbbrev[$aAbbrev[0][0]][1] = $split_f[1]
			WinSetTitle($Form1, '', 'Restore Abbrev: ' & $split_f[0])
		EndIf
		; == jetzt noch in $aAbbrevFile ändern zum Zurückschreiben in die Abbrev-Datei
		$index = -1
		For $j = 1 To $aAbbrevFile[0]
			If StringLeft($aAbbrevFile[$j], StringLen($split_f[0] +1)) = $split_f[0] & '=' Then
				$index = $j
				ExitLoop
			Else
				ContinueLoop
			EndIf
		Next
		If $index > -1 Then ; == gefunden, Abbrev ändern > in Array: $aAbbrevFile
			$aAbbrevFile[$index] = $aMyAbbrev[$i]
		Else
			ReDim $aAbbrevFile[$aAbbrevFile[0] + 1]
			$aAbbrevFile[0] += 1
			$aAbbrevFile[$aAbbrevFile[0]] = $aMyAbbrev[$i]  ; == Abbrev am Ende anfügen
			For $j = $aAbbrevFile[$aAbbrevFile[0]] To 1 Step -1
				If $aAbbrevFile[$j] = $Last_STR Then ExitLoop
			Next
			_ArraySwap($aAbbrev[UBound($aAbbrev) - 1], $aAbbrev[$j])  ; == Eintrag verschieben vor die End-Linie
		EndIf
	Next
	; == bestehende Abbrev-Datei sichern und Abbrev-Datei neu schreiben
	FileMove($Abbr_Path, $Abbr_Path & '.BAK', 1)
	_FileWriteFromArray($Abbr_Path, $aAbbrevFile, 1)
	; == Schlüsselwörter ergänzen
	If FileExists($MyKeyWordPath) Then
		Local $aMyKeyWord
		_FileReadToArray($MyKeyWordPath, $aMyKeyWord)
		For $i = 1 To UBound($aMyKeyWord) - 1
			WinSetTitle($Form1, '', 'Restore Keyword: ' & $aMyKeyWord[$i])
			Sleep(20)
			_KeyWordSet($aMyKeyWord[$i], 1)
		Next
	EndIf
	WinSetTitle($Form1, '', 'Restore')
	MsgBox(64, 'Restore', 'Restore beendet.', 1.5)
	WinSetTitle($Form1, '', "Abkürzungen verwalten  [" & $Abbr_Path & ']')
	_GUICtrlListView_DeleteAllItems(GUICtrlGetHandle($ListView1))
	_LV_Fill()
	Reload_Config()
EndFunc   ;==>_Restore

Func _LV_FindInSubItem($hWnd, $2Find, $SubIndex)
	Local $out = -1
	For $i = 0 To _GUICtrlListView_GetItemCount($hWnd) - 1
		If _GUICtrlListView_GetItemText($hWnd, $i, $SubIndex) = $2Find Then $out = $i
	Next
	Return $out
EndFunc   ;==>_LV_FindInSubItem

Func ListView1Click()
	Local $indx = _GUICtrlListView_GetHotItem($ListView1)
	GUICtrlSetData($iSearch, _GUICtrlListView_GetItemText($ListView1, $indx, 0))
	GUICtrlSetData($Edit1, _Abbrev2Code(_GUICtrlListView_GetItemText($ListView1, $indx, 1)))
	WinSetTitle($Form2, '', 'Code zur Abkürzung   [ ' & GUICtrlRead($iSearch) & ' ]')
	GUISetState(@SW_SHOW, $Form2)
EndFunc   ;==>ListView1Click

Func _LV_Fill()
	GUISetState(@SW_LOCK, $Form1)
	Local $CtrlID
	For $i = 1 To $aAbbrev[0][0]
		$CtrlID = GUICtrlCreateListViewItem('|', $ListView1)
		GUICtrlSetBkColor($CtrlID, 0xE6E8FA)
		_GUICtrlListView_SetItemText($ListView1, $i-1, $aAbbrev[$i][0], 0)
		_GUICtrlListView_SetItemText($ListView1, $i-1, $aAbbrev[$i][1], 1)
	Next
	GUISetState(@SW_UNLOCK, $Form1)
EndFunc   ;==>_LV_Fill

Func _SplitOnce($STRING, $DELIM = '=')
	If Not StringInStr($STRING, $DELIM) Then Return SetError(1, 0, 1)
	Local $out[2]
	Local $len = StringLen($STRING)
	Local $pos = StringInStr($STRING, $DELIM, 1, 1)
	$out[0] = StringLeft($STRING, $pos - 1)
	$out[1] = StringRight($STRING, $len - $pos)
	Return $out
EndFunc   ;==>_SplitOnce

Func _Abbrev2Code($ABBREV)
	Local $out = ''
	$var = StringSplit($ABBREV, '\n', 1)
	For $i = 1 To UBound($var) - 1
		If StringInStr($var[$i], '\t', 1) Then $var[$i] = StringReplace($var[$i], '\t', @TAB)
		If $i = UBound($var) - 1 Then
			$out &= $var[$i]
		Else
			$out &= $var[$i] & @CRLF
		EndIf
	Next
	Return $out
EndFunc   ;==>_Abbrev2Code

Func _Code2Abbrev()
	Local $out = '', $count = _GUICtrlEdit_GetLineCount($Edit2), $line
	If @error Then Return SetError(1)
	For $i = 0 To $count - 1
		$line = _GUICtrlEdit_GetLine($Edit2, $i)
		If StringInStr($line, @TAB, 1) Then $line = StringReplace($line, @TAB, '\t')
		If $i = $count - 1 Then
			$out &= $line
		Else
			$out &= $line & '\n'
		EndIf
	Next
	Return $out
EndFunc   ;==>_Code2Abbrev

Func _WriteNewAbbrev()
	If GUICtrlRead($Edit2) = '' Then Return
	Local $abbr = _Code2Abbrev()
	If $abbr = $sAbbrev Then Return
	Local $fh = FileOpen($MyAbbrevPath, 1)
	FileWriteLine($fh, $txt & '=' & $abbr)
	FileClose($fh)
	$aAbbrev[0][0] += 1
	ReDim $aAbbrev[$aAbbrev[0][0] +1][2]
	$aAbbrevFile[0] += 1
	ReDim $aAbbrevFile[$aAbbrevFile[0] +1]
	$aAbbrev[$aAbbrev[0][0]][0] = $txt
	$aAbbrev[$aAbbrev[0][0]][1] = $abbr
	$aAbbrevFile[$aAbbrevFile[0]] = $txt & '=' & $abbr
	For $i = $aAbbrevFile[0] To 1 Step -1
		If $aAbbrevFile[$i] = $Last_STR Then ExitLoop
	Next
	_ArraySwap($aAbbrevFile[$aAbbrevFile[0]], $aAbbrevFile[$i])
	FileMove($Abbr_Path, $Abbr_Path & '.BAK', 1)
	_FileWriteFromArray($Abbr_Path, $aAbbrevFile, 1)
	_KeyWordSet($txt)
	$fh = FileOpen($MyKeyWordPath, 1)
	FileWrite($fh, $txt & @CRLF)
	FileClose($fh)
	_GUICtrlListView_DeleteAllItems(GUICtrlGetHandle($ListView1))
	GUICtrlSetData($Edit2, '')
	_LV_Fill()
	Local $idx = _LV_FindInSubItem($ListView1, $txt, 0)
	GUICtrlSetState($ListView1, $GUI_FOCUS)
	_GUICtrlListView_EnsureVisible($ListView1, $idx)
	_GUICtrlListView_SetItemSelected($ListView1, $idx)
	Reload_Config()
EndFunc   ;==>_WriteNewAbbrev

Func _WriteEditedAbbrev()
	If GUICtrlRead($Edit2) = '' Then Return
	Local $abbr = _Code2Abbrev()
	If $abbr = $sAbbrev Then Return
	Local $index = _ArraySearch($aAbbrev, $txt)
	$aAbbrev[$index][1] = $abbr
	For $i = $aAbbrevFile[$aAbbrevFile[0]] To 1 Step -1
		If StringLeft($aAbbrevFile[$i], StringLen($txt +1)) = $txt & '=' Then
			$aAbbrevFile[$i] = $txt & '=' & $abbr
			ExitLoop
		EndIf
	Next
	FileMove($Abbr_Path, $Abbr_Path & '.BAK', 1)
	_FileWriteFromArray($Abbr_Path, $aAbbrevFile, 1)
	Local $fh = FileOpen($MyAbbrevPath, 0)
	Local $content = FileRead($fh)
	FileClose($fh)
	If Not StringRegExp($content, '\r\n' & $txt & '=') Then
		$fh = FileOpen($MyAbbrevPath, 1)
		FileWriteLine($fh, $txt & '=' & $abbr)
		FileClose($fh)
	Else
		$fh = FileOpen($MyAbbrevPath, 0)
		Local $line, $row = 2
		While 1
			$line = FileReadLine($fh, $row)
			If @error = -1 Then ExitLoop
			$split_f = _SplitOnce($line)
			If $txt = $split_f[0] Then ExitLoop
			$row += 1
		WEnd
		FileClose($fh)
		_FileWriteToLine($MyAbbrevPath, $row, $txt & '=' & $abbr, 1)
	EndIf
	Local $line = _GetLine() - 1
	Local $idx = _LV_FindInSubItem($ListView1, $txt, 0)
	$aAbbrevFile[$line] = $txt & '=' & $abbr
	_GUICtrlListView_DeleteAllItems(GUICtrlGetHandle($ListView1))
	GUICtrlSetData($Edit2, '')
	_LV_Fill()
	_FileWriteToLine($Abbr_Path, $line, $txt & '=' & $abbr, 1)
	GUICtrlSetState($ListView1, $GUI_FOCUS)
	_GUICtrlListView_EnsureVisible($ListView1, $idx)
	_GUICtrlListView_SetItemSelected($ListView1, $idx)
	Reload_Config()
EndFunc   ;==>_WriteEditedAbbrev

Func _Delete()
	$txt = GUICtrlRead($iSearch)
	If MsgBox(262180, 'Achtung', 'Soll die Abkürzung ' & @LF & '>> ' & $txt & ' <<' & @LF & ' wirklich gelöscht werden?') = 7 Then Return
	Local $idx = _GetLine() - 1
	$aAbbrevFile[$idx] = ''
	_GUICtrlListView_DeleteAllItems(GUICtrlGetHandle($ListView1))
	GUICtrlSetData($iSearch, '')
	Local $index = _ArraySearch($aAbbrev, $txt, 1)
	_ArrayDelete($aAbbrev, $index)
	_LV_Fill()
	_FileWriteToLine($Abbr_Path, $idx, '', 1)
	$fh = FileOpen($MyAbbrevPath, 0)
	Local $line, $row = 2
	While 1
		$line = FileReadLine($fh, $row)
		If @error = -1 Then ExitLoop
		$split_f = _SplitOnce($line)
		If @error Then ContinueLoop
		If $txt = $split_f[0] Then ExitLoop
		$row += 1
	WEnd
	FileClose($fh)
	_FileWriteToLine($MyAbbrevPath, $row, '', 1)
	Local $keys = FileRead($KeyWordPath)
	$keys = StringReplace($keys, $txt & ' ', '', 1, 1)
	Local $fh = FileOpen($KeyWordPath, 2)
	FileWrite($fh, $keys)
	FileClose($fh)
	$keys = FileRead($MyKeyWordPath)
	$keys = StringReplace($keys, $txt & @CRLF, '', 1, 1)
	$fh = FileOpen($MyKeyWordPath, 2)
	FileWrite($fh, $keys)
	FileClose($fh)
	Reload_Config()
EndFunc   ;==>_Delete

Func _GetLine()
	Local $var
	For $i = 1 To UBound($aAbbrevFile) - 1
		$var = _SplitOnce($aAbbrevFile[$i], '=')
		If @error Then ContinueLoop
		If $var[0] = $txt Then Return $i + 1
	Next
EndFunc   ;==>_GetLine

Func _KeyWordSet($KEYWORD, $CHECK = 0)
	Local $txtZeile, $len
	If $CHECK Then
		Local $fh = FileOpen($KeyWordPath, 0)
		Local $read = FileRead($fh)
		FileClose($fh)
		If StringInStr($read, $KEYWORD & ' ', 1) Then Return
	EndIf
	$zeile = _FileCountLines($KeyWordPath)
	$txtZeile = FileReadLine($KeyWordPath, $zeile)
	$len = StringLen($txtZeile) + 4
	If $len + StringLen($KEYWORD) > 100 Then
		_FileWriteToLine($KeyWordPath, $zeile, $txtZeile & "\" & @CRLF, 1)
		_FileWriteToLine($KeyWordPath, $zeile + 1, @TAB & $KEYWORD & " ", 1)
	Else
		_FileWriteToLine($KeyWordPath, $zeile, $txtZeile & $KEYWORD & " ", 1)
	EndIf
EndFunc   ;==>_KeyWordSet

Func MY_WM_COMMAND($hWnd, $Msg, $wParam, $lParam)
	Switch $wParam
		Case $ListView1
			Local $tagNMHDR = DllStructCreate("int;int;int", $lParam)
			If @error Then Return $GUI_RUNDEFMSG
			If DllStructGetData($tagNMHDR, 3) = $NM_CLICK Then ListView1Click()
	EndSwitch
	Return $GUI_RUNDEFMSG
EndFunc   ;==>MY_WM_COMMAND

; Thanks to Jos for following functions:
Func Reload_Config()
    ;Send SciTE Director my GUI handle so it will report info back from SciTE
    SendSciTE_Command("reloadproperties:")
EndFunc   ;==>Reload_Config
; Send command to SciTE
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
;~     ConsoleWrite('SciTE Command  --> ' & $sCmd & @LF)
    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
