WBStudio.AlignAtChar
;==========================================================================================================================================
; WinBatch Studio Tool
; AlignAtCharacter
;------------------------------------------------------------------------------------------------------------------------------------------
; This WBStudio tool "AlignAtCharacter" aligns the trailing part of text lines
; at the position of the first or last occurrence of a user chosen character string.
;------------------------------------------------------------------------------------------------------------------------------------------
; Example for integration into wsp-user.mnu:
;
;   _AlignAtCharacter ; Align trailing part of text lines at the position of the first or last occurrence of a string.
;       Call ("W:\WBT\WBSTUDIO\WBStudio.AlignAtChar.wbt",0)
;------------------------------------------------------------------------------------------------------------------------------------------
; 20100128: Version 1.01 : Both search directions are allowed, from left or from right.
;                        ; Alignment at user defined position, if possible.
;                        ; Empty alignment string is allowed to enable prepending or appending the filler string.
; 20100127: Version 1.00 : Initial version.
;
; (c)Detlev Dalitz.20100127.20100128.
;==========================================================================================================================================

strScriptName = "WBStudio.AlignAtCharacter"
strMsgTitle = "WinBatch script terminated."
strMsgText = "This WinBatch script " : '"' : strScriptName : '"' : @LF : " is designed only to work from user menu in WinBatch Studio."
Terminate (RtStatus () != 10, strMsgTitle, strMsgText)

strMsgRunning = "Running... " : strScriptName
strMsgReady = "Ready. " : strScriptName

wStatusMsg (strMsgRunning)

intLineNo = wGetLineNo ()
intColNo = wGetColNo ()
If !wGetSelstate ()
   wSelectAll ()
EndIf

; Get selection information.
strSelInfo = wSelInfo ()
intSelStartLine = ItemExtract (1, strSelInfo, @TAB)
; intSelStartCol   = ItemExtract (2, strSelInfo, @TAB)
; intSelStopLine   = ItemExtract (3, strSelInfo, @TAB)
; intSelStopCol    = ItemExtract (4, strSelInfo, @TAB)

wCopy ()
strLines = ClipGet ()
If strLines == "" Then Goto CANCEL


; --- Begin user input ---
strAlign = AskLine (strScriptName, "Alignment at which character or string?" : @LF : @LF : "Enter character or string ...", "=")
intAlignLen = StrLen (strAlign)

strAlignFill = AskLine (strScriptName, "Put surrounding filler." : @LF : @LF : "Enter spaces or what you want ...", " " : strAlign : " ")

strAlignDir = AskLine (strScriptName, "Search direction from which side of the string?" : @LF : "Left or Right?" : @LF : @LF : "Enter direction (L,R) ...", "L")
strAlignDir = StrUpper (StrSub (strAlignDir, 1, 1))
If strAlignDir != "L" && strAlignDir != "R" Then strAlignDir = "L"

intAlignPos = AskLine (strScriptName, "Alignment at which position, if possible?" : @LF : @LF : "Enter pos. Number" : @LF : "(0 is always at end of longest line) ...", "0")
If !IsInt (intAlignPos) Then intAlignPos = 0
;intAlignPos = intAlignPos - 1
If intAlignPos < 0 || intAlignPos > 1 << 16 Then intAlignPos = 0
; --- End user input ---


strLines = StrReplace (strLines, @CRLF, @LF)
strLines = StrReplace (strLines, @TAB, " ")
arrLines = Arrayize (strLines, @LF)
intLines = ArrInfo (arrLines, 1)
arrAlign = ArrDimension (intLines, 2)
ArrInitialize (arrAlign, 0)
intLast = intLines - 1
intAlignMax = 0
strLines = ""

If intAlignLen > 0
   Switch @TRUE
   Case strAlignDir == "L"
      For intLine = 0 To intLast
         intIndex = StrIndex (arrLines[intLine], strAlign, 0, @FWDSCAN)
         If IntIndex > 0
            arrAlign[intLine, 1] = intIndex + intAlignLen
            arrAlign[intLine, 0] = StrLen (ItemExtract (2, StrTrim (Num2Char (1) : StrSub (arrLines[intLine], 1, intIndex - 1)), Num2Char (1))); strTrimR
         EndIf
         intAlignMax = Max (intAlignMax, arrAlign[intLine, 0])
      Next
      Break
   Case strAlignDir == "R"
      For intLine = 0 To intLast
         intIndex = StrIndex (arrLines[intLine], strAlign, 0, @BACKSCAN)
         If IntIndex > 0
            arrAlign[intLine, 1] = intIndex + intAlignLen
            arrAlign[intLine, 0] = StrLen (ItemExtract (2, StrTrim (Num2Char (1) : StrSub (arrLines[intLine], 1, intIndex - 1)), Num2Char (1))); strTrimR
         EndIf
         intAlignMax = Max (intAlignMax, arrAlign[intLine, 0])
      Next
      Break
   EndSwitch

   For intLine = 0 To intLast
      If arrAlign[intLine, 1] > 0
         strL = StrSub (arrLines[intLine], 1, arrAlign[intLine, 0])
         strR = StrTrim (StrSub (arrLines[intLine], arrAlign[intLine, 1], -1))

         If intAlignPos == 0 Then intAlignCurrent = intAlignMax
            Else intAlignCurrent = Max (Min (intAlignPos, intAlignMax), arrAlign[intLine, 0])

         arrLines[intLine] = StrFix (strL, " ", intAlignCurrent) : strAlignFill : strR
      EndIf
      strLines = strLines : @LF : arrLines[intLine]
   Next
Else
   Switch @TRUE
   Case strAlignDir == "L"
      For intLine = 0 To intLast
         strLines = strLines : @LF : strAlignFill : arrLines[intLine]
      Next
      Break
   Case strAlignDir == "R"
      For intLine = 0 To intLast
         strLines = strLines : @LF : arrLines[intLine] : strAlignFill
      Next
      Break
   EndSwitch
EndIf

strLines = StrSub (strLines, 2, -1)
strLines = StrReplace (strLines, @LF, @CRLF)
ClipPut (strLines)
wPaste ()

:CANCEL
wClearSel ()
wGotoLine (intLineNo)
;wGotoLine (intSelStartLine)
wGotoCol (intColNo)
wStatusMsg (strMsgReady)
Exit

;##########################################################################################################################################
; Testcase following ...
URL_DONT_ESCAPE_EXTRA_INFO = 33554432   ; 0x02000000 ; Used only in conjunction with URL_ESCAPE_SPACES_ONLY to prevent the conversion of characters in the query (the portion of the URL following the first # or ? character encountered in the string). This flag should not be used alone, nor combined with URL_ESCAPE_SEGMENT_ONLY.
URL_ESCAPE_PERCENT = 4096       ; 0x00001000 ; Convert any % character found in the segment section of the URL (that section falling between the server specification and the first # or ? character). By default, the % character is not converted to its escape sequence. Other unsafe characters in the segment are also converted normally. Combining this flag with URL_ESCAPE_SEGMENT_ONLY includes those % characters in the query portion of the URL. However, as the URL_ESCAPE_SEGMENT_ONLY flag causes the entire string to be considered the segment, any # or ? characters are also converted. This flag cannot be combined with URL_ESCAPE_SPACES_ONLY.
URL_ESCAPE_SEGMENT_ONLY = 8192       ; 0x00002000 ; Indicates that pszURL contains only that section of the URL following the server component but preceeding the query. All unsafe characters in the string are converted. If a full URL is provided when this flag is set, all unsafe characters in the entire string are converted, including # and ? characters. Combine this flag with URL_ESCAPE_PERCENT to include that character in the conversion. This flag cannot be combined with URL_ESCAPE_SPACES_ONLY or URL_DONT_ESCAPE_EXTRA_INFO.
URL_ESCAPE_SPACES_ONLY = 67108864   ; 0x04000000 ; Convert only space characters to their escape sequences, including those space characters in the query portion of the URL. Other unsafe characters are not converted to their escape sequences. This flag assumes that pszURL does not contain a full URL. It expects only the portions following the server specification. Combine this flag with URL_DONT_ESCAPE_EXTRA_INFO to prevent the conversion of space characters in the query portion of the URL. This flag cannot be combined with URL_ESCAPE_PERCENT or URL_ESCAPE_SEGMENT_ONLY.
..........................................................................................................................................
URL_DONT_SIMPLIFY = 134217728  ; 0x08000000
URL_ESCAPE_UNSAFE = 536870912  ; 0x20000000
URL_INTERNAL_PATH = 8388608    ; 0x00800000
URL_PARTFLAG_KEEPSCHEME = 1          ; 0x00000001
URL_PLUGGABLE_PROTOCOL = 1073741824 ; 0x40000000
URL_UNESCAPE = 268435456  ; 0x10000000
URL_UNESCAPE_HIGH_ANSI_ONLY = 4194304    ; 0x00400000
URL_UNESCAPE_INPLACE = 1048576    ; 0x00100000
;##########################################################################################################################################