'$NAME Вставка Перезагрузки процедуры /функции
'         Мой e-mail: edi75@mail.ru
'         Мой ICQ: 237-732-466 


'*********************************************************************************************
Function MinNeNol(X,Y)
'Ближ=?(Рав>0,?(Зап>0,Мин(Зап,Рав),Рав),Зап);
If X>0 then
	If Y>0 then
		If X>Y then
			MinNeNol=Y
			Exit Function
		else
			MinNeNol=X
			Exit Function	
		end if			
	else
		MinNeNol=X
		Exit Function
	end if
else
	MinNeNol=Y
	Exit Function
end if		
End Function
'*********************************************************************************************

Function Variants(stroka,doc,EtoFun)
'*********************************************	
TabInd = vbCrLf   + String(doc.SelStartCol, vbTab)
Variants="Перем Путь,ИмяФайла;РасположениеФайла(Путь,ИмяФайла);ФайлERT=Путь+ИмяФайла;	//НеВключатьВМодуль" 
Variants=Variants+TabInd+"СписокПараметров=СоздатьОбъект(""СписокЗначений"");//НеВключатьВМодуль 	"
'******** узнаем имя функции
Beg= InStr(stroka,"(")
Tek=Mid(stroka,1,Beg-1)
Pr=InStrRev(Tek," ")
ImaFun=Mid(Tek,Pr+1,Beg-Pr-1)
'MsgBox(ImaFun)
'ImaFun="Пример1"
'******** узнаем переменные
Ending= InStr(stroka,")")
Tekst=Mid(stroka,Beg+1,Ending-Beg-1)
'ЭтоПропустить=0;Зап=Найти(Текст,",");Рав=Найти(Текст,"=");
EtoProp=0
Zap=InStr(Tekst,",")
Rav=InStr(Tekst,"=")
' минимальное не равное нулю число
Bliz=MinNeNol(Zap,Rav)
'MsgBox(Bliz)
Do While  Bliz<>0
	If EtoProp=0 then
		Tekst1=Mid(Tekst,1,Bliz-1)			
		Variants=Variants+TabInd+"СписокПараметров.ДобавитьЗначение("+Tekst1+");//НеВключатьВМодуль"
		'MsgBox (Tekst1)
	end if	
	Tekst=Mid(Tekst,Bliz+1,Len(Tekst)-Bliz)	
	'если равество то надо удалить то что после равенства !!!
	' для этого служит переменная ЭтоПропустить
	'ЭтоПропустить=?(Рав=Ближ,1,0);		
	EtoProp=0	
	If Rav=Bliz then
		EtoProp=1			
	end if		
	Zap=InStr(Tekst,",")
	Rav=InStr(Tekst,"=")
	Bliz=MinNeNol(Zap,Rav)	
 Loop
 Tekst=Trim(Tekst)
 If (EtoProp=0) and (Len(Tekst)>0) then 	
 	Variants=Variants+TabInd+"СписокПараметров.ДобавитьЗначение("+Tekst+");//НеВключатьВМодуль"
 	'MsgBox (Tekst)
end if	

'********  завершим конструкцию
Variants=Variants+TabInd+"Результат=глВыполнитьМодуль(ФайлERT,"""+ImaFun+""",СписокПараметров);//НеВключатьВМодуль"
'EtoFun
If EtoFun=1 then
	Variants=Variants+TabInd+"Возврат Результат;//НеВключатьВМодуль	"
Else
	Variants=Variants+TabInd+"Возврат ;	//НеВключатьВМодуль	"
end if			
Variants=Variants+TabInd
'Variants=Variants+""
End Function' Variants 
'*********************************************************************************************
Sub VstavimVTekush()
	set doc = CommonScripts.GetTextDocIfOpened(0)
	if doc is Nothing then Exit Sub

	set PositionInModule =  CommonScripts.GetDocumentPosition(doc)

	ModuleText = split(doc.Text, vbCrLf)	
	for i = doc.SelStartLine to 0 step -1
		sText1=ModuleText(i)
		sText = UCase(sText1)
		estProz=0
		if Instr(sText,"ПРОЦЕДУРА") = 1  then
			'MsgBox(sText1)						
			'вставим после определения процедуры 
			doc.range(i,len(sText1),i,len(sText1)) = "//НеВключатьВМодуль"
			
			txt=Variants(sText1,doc,0)						
			for j = i+1 to UBound(ModuleText)			
				sText = Trim(UCase(ModuleText(j)))
				if Instr(sText,"ПЕРЕМ ") <>1  then
					doc.range(j,0,j,0) = txt			
					'MsgBox(txt)
					'doc.MoveCaret i, 0
					Exit For
				end if
			next			
			Exit For
		end if
		if  Instr(sText,"ФУНКЦИЯ") = 1 then
			'MsgBox(sText1)
			'вставим после определения функции  
			doc.range(i,len(sText1),i,len(sText1)) = "//НеВключатьВМодуль"
			
			txt=Variants(sText1,doc,1)			
			for j = i+1 to UBound(ModuleText)			
				sText = Trim(UCase(ModuleText(j)))
				if Instr(sText,"ПЕРЕМ ") <>1  then
					doc.range(j,0,j,0) = txt			
					'MsgBox(txt)
					'doc.MoveCaret i, 0
					Exit For
				end if
			next
			Exit For
		end if
	next
End Sub ' VstavimVTekush
'*********************************************************************************************
Sub DelVTekush()
	set doc = CommonScripts.GetTextDocIfOpened(0)
	if doc is Nothing then Exit Sub
	set PositionInModule =  CommonScripts.GetDocumentPosition(doc)

	ModuleText = split(doc.Text, vbCrLf)
	for i = doc.SelStartLine to 0 step -1
		sText1=ModuleText(i)
		sText = UCase(sText1)		
		if Instr(sText,"ПРОЦЕДУРА") = 1 or Instr(sText,"ФУНКЦИЯ") = 1 then
			'doc.MoveCaret i, 0
			sText1=Replace(sText1,"//НеВключатьВМодуль","") 			
			doc.range(i,0,i,Len(sText)) = sText1
			' выясним где конец и начнем чистить с конца 
			'msgbox(i+1)
			'msgbox(UBound(ModuleText))
			for j = i+1 to UBound(ModuleText)
				sText1=ModuleText(j)
				sText = Trim(UCase(sText1))				
				if Instr(sText,"КОНЕЦПРОЦЕДУРЫ") = 1 or Instr(sText,"КОНЕЦФУНКЦИИ") = 1 then
					k=j
					'msgbox(k)
					Exit For
				end if	
			next
			
			'msgbox(k)
			'  начнем чистить с конца 
			for j = k-1 to  i+1 step -1				
				sText = UCase(ModuleText(j))			
				if Instr(sText,"//НЕВКЛЮЧАТЬВМОДУЛЬ") <> 0   then				
				sText2=ModuleText(j+1)
				'msgbox(sText2)				
				doc.range(j,0,j+1,len(sText2)) = doc.range(j+1,0,j+1,len(sText2))
				'doc.save
				end if									
			next			
			Exit For
		end if
	next
End Sub ' DelVTekush

'
' Процедура инициализации скрипта
'
Sub Init(dummy) ' Фиктивный параметр, чтобы процедура не попадала в макросы
    Set c = Nothing
    On Error Resume Next
    Set c = CreateObject("OpenConf.CommonServices")
    On Error GoTo 0
    If c Is Nothing Then
        Message "Не могу создать объект OpenConf.CommonServices", mRedErr
        Message "Скрипт " & SelfScript.Name & " не загружен", mInformation
        Scripts.UnLoad SelfScript.Name
		Exit Sub
    End If
    c.SetConfig(Configurator)
	SelfScript.AddNamedItem "CommonScripts", c, False
End Sub
 
Init 0 ' При загрузке скрипта выполняем инициализацию