' автор - trad                                                                               
' Умеет: 
' 1. добавлять/убирать в начале выделенного блока символы: | и //#
' 2.Сдвигать влево/вправо текст выделенного блока за символом | (для форматирования запросов)
' 
' ------------------------------------------------------------------
'Возвращает количество одинаковых символов (Space или Tab) в начале строки
'out-параметр firstChar - первый символ в строке
Function SpaceCount(str, firstChar)
	firstChar = Left(str, 1)
	count = 0
	For i = 1 To Len(str)
		char = Mid(str, i, 1)
		If char = firstChar and (char = vbTab or char = " ") Then
			count = count + 1
		Else
			Exit For
		End If
	Next
	SpaceCount = count
End Function

' ------------------------------------------------------------------
Sub FormatBlock(Symbol, InBeginLine)

	If Windows.ActiveWnd is Nothing Then
		Exit Sub
	End If

	Set doc = Windows.ActiveWnd.Document
	If doc = docWorkBook Then Set doc = doc.Page(1)
	If doc.Type = docText Then
		arrLines = Split(doc.Range(doc.SelStartLine, 0, doc.SelEndLine, doc.SelEndCol), vbNewLine)

		If InBeginLine = 0 Then
			'определение размера минимального общего отступа
			pos = -1
			curChar = ""
			char = ""
			For i = 0 To UBound(arrLines)
				line = arrLines(i)
				If line <> vbNullString or i < UBound(arrLines) Then
					cnt = SpaceCount(line, char)

					If char <> curChar and pos > -1 Then pos = 0 : Exit For
					If pos > cnt or pos = -1 Then pos = cnt
					If pos = 0 Then Exit For

					curChar = char
				End If
			Next
		Else
			pos = 0
		End If

		'вставка символа
		For i = 0 To UBound(arrLines)
			line = arrLines(i)
			If line <> vbNullString or i < UBound(arrLines) Then
				arrLines(i) = Left(line, pos) & Symbol & Mid(line, pos+1)
			End If
		Next

		doc.Range(doc.SelStartLine, 0, doc.SelEndLine, doc.SelEndCol) = Join(arrLines, vbNewLine)
	End If

End Sub

' ------------------------------------------------------------------
Sub UnFormatBlock(Symbol)

	If Windows.ActiveWnd is Nothing Then
		Exit Sub
	End If

	Set doc = Windows.ActiveWnd.Document
	If doc = docWorkBook Then Set doc = doc.Page(1)
	If doc.Type = docText Then
		arrLines = Split(doc.Range(doc.SelStartLine, 0, doc.SelEndLine, doc.SelEndCol), vbNewLine)

		'вырезание символа
		For i = 0 To UBound(arrLines)
			line = arrLines(i)
			If line <> vbNullString or i < UBound(arrLines) Then
				For pos = 1 To Len(line)
					char = Mid(line, pos, 1)
					If char <> vbTab and char <> " " Then Exit For
				Next
				If Mid(line, pos, Len(Symbol)) = Symbol Then
					pos2 = pos + Len(Symbol)
				Else
					pos2 = pos
				End If
				arrLines(i) = Left(line, pos-1) & Mid(line, pos2)
			End If
		Next

		doc.Range(doc.SelStartLine, 0, doc.SelEndLine, doc.SelEndCol) = Join(arrLines, vbNewLine)
	End If

End Sub

' ------------------------------------------------------------------
Sub MoveBlock(Direction, SpaceChar)

	If Windows.ActiveWnd is Nothing Then
		Exit Sub
	End If

	Set doc = Windows.ActiveWnd.Document
	If doc = docWorkBook Then Set doc = doc.Page(1)
	If doc.Type = docText Then
		arrLines = Split(doc.Range(doc.SelStartLine, 0, doc.SelEndLine, doc.SelEndCol), vbNewLine)

		For i = 0 To UBound(arrLines)
			If Direction = 1 Then 'Влево
				arrLines(i) = Replace(arrLines(i), "|" & SpaceChar, "|", 1, 1)
			Else 'Вправо
				arrLines(i) = Replace(arrLines(i), "|", "|" & SpaceChar, 1, 1)
			End If
		Next

		doc.Range(doc.SelStartLine, 0, doc.SelEndLine, doc.SelEndCol) = Join(arrLines, vbNewLine)
	End If

End Sub

'---------------------------------------
Sub FormatSharp 'hotkey: ctrl+3
	FormatBlock "//#", 0
End Sub
Sub FormatVertical 'hotkey: ctrl+\
	FormatBlock "|", 0
End Sub
'---------------------------------------
Sub UnFormatSharp 'hotkey: ctrl+shift+3
	UnFormatBlock "//#"
End Sub
Sub UnFormatVertical 'hotkey: ctrl+shift+\
	UnFormatBlock "|"
End Sub
'---------------------------------------
Sub MoveBlockLeftSpace 'hotkey: ctrl+[
	MoveBlock 1, " "
End Sub
Sub MoveBlockRightSpace 'hotkey: ctrl+]
	MoveBlock 2, " "
End Sub
'---------------------------------------
Sub MoveBlockLeftTab 'hotkey: ctrl+,
	MoveBlock 1, vbTab
End Sub
Sub MoveBlockRightTab 'hotkey: ctrl+.
	MoveBlock 2, vbTab
End Sub
