WBStudio.WspopupMnu.CleanUp
;==========================================================================================================================================
; WBStudio Utility for use within WinBatch Studio debug mode.
;==========================================================================================================================================
;
; "CleanUp" for WSPOPUP.MNU - Read/Write -
;
; This script does some cleaning procedures on the special menu title lines
; directly on wspopup.mnu - virtually in WBStudio editor - user must save changes.
;
; It works directly on the file wspopup.mnu - virtually in WBStudio editor -
; but user must decide to save changes afterwards.
;
;..........................................................................................................................................
;
; >>> The running script can be terminated by pressing the Shift key. <<<
;
; Adapt the filepath to your environment and other options to your need (see "Options - Tweak Section").
;
;..........................................................................................................................................
; Version History:
; 20090712. Changed StrCat() to colon ":" concatenator and beautified the code.
; 20051215. Initial version.
;
; Detlev Dalitz.20051215.20090712.
;==========================================================================================================================================

;------------------------------------------------------------------------------------------------------------------------------------------
Terminate (RtStatus () != 10, IntControl (1004, 0, 0, 0, 0), "Terminated. Script runs only in WinBatch Studio debug mode.")
;------------------------------------------------------------------------------------------------------------------------------------------


;------------------------------------------------------------------------------------------------------------------------------------------
; ****** Options - Tweak Section ****** adapt it to your environment and your needs ******
;
sWorkFile = DirHome () : "Wspopup.mnu"
;
bDoCleanUp = @FALSE                         ; Do not touch any lines, just log status.
;bDoCleanUp = @TRUE                         ; Do the cleanup process.
;
; Experimental option FirstColumnPadding gives better overview in editor when editing lines manually.
bItm1_FirstColumnPadding = @FALSE           ; No padding of Item1.
;bItm1_FirstColumnPadding  = @TRUE          ; Pad Item1 with spaces to have the comment char start at column n.
;
iItm1_FirstColumnPadSize = 60               ; Activate padding by bItm1_FirstColumnPadding = @TRUE.
;
bItm3_SpBeforeRoundBrcktOpen = @FALSE       ; No Space before opening (.  "("
bItm3_SpBehindRoundBrcktOpen = @FALSE       ; No Space behind opening (.  "("
bItm3_SpBeforeRoundBrcktClose = @FALSE      ; No Space before closing ).  ")"
bItm3_SpBehindRoundBrcktClose = @FALSE      ; No Space behind closing ).  ")"
;
;bItm3_SpBeforeRoundBrcktOpen  = @TRUE      ; Space before opening (.     " ("
;bItm3_SpBehindRoundBrcktOpen  = @TRUE      ; Space behind opening (.     "( "
;bItm3_SpBeforeRoundBrcktClose = @TRUE      ; Space before closing ).     " )"
;bItm3_SpBehindRoundBrcktClose = @TRUE      ; Space behind closing ).     ") "
;
;bItm3_SpBeforeBrcktOpen  = @FALSE          ; No Space before opening [.  "["
bItm3_SpBehindBrcktOpen = @FALSE            ; No Space behind opening [.  "["
bItm3_SpBeforeBrcktClose = @FALSE           ; No Space before closing ].  "]"
bItm3_SpBehindBrcktClose = @FALSE           ; No Space behind closing ].  "]"
;
bItm3_SpBeforeBrcktOpen = @TRUE             ; Space before opening [.     " ["
;bItm3_SpBehindBrcktOpen  = @TRUE           ; Space behind opening [.     "[ "
;bItm3_SpBeforeBrcktClose = @TRUE           ; Space before closing ].     " ]"
;bItm3_SpBehindBrcktClose = @TRUE           ; Space behind closing ].     "] "
;
bItm3_SpBetweenBrcktsOpen = @FALSE          ; No space between round and normal bracket opening.    "(["
;bItm3_SpBetweenBrcktsOpen  = @TRUE         ; Space between round and normal bracket opening.       "( ["
bItm3_SpBetweenBrcktsClose = @FALSE         ; No space between round and normal bracket closing.    "])"
;bItm3_SpBetweenBrcktsClose = @TRUE         ; Space between round and normal bracket closing.       "] )"
;
bItm3_SpBetweenRoundBrckts = @FALSE         ; No space in between ().    "()"
;bItm3_SpBetweenRoundBrckts = @TRUE         ; One space in between ().     "( )"
;
;bItm3_SpBehindComma = @FALSE               ; No space behind comma.      ","
bItm3_SpBehindComma = @TRUE                 ; Space behind comma.         ", "
;
;------------------------------------------------------------------------------------------------------------------------------------------


;------------------------------------------------------------------------------------------------------------------------------------------
wFileOpen (sWorkFile)
wTopOfFile ()
iLineCount = wLineCount ()
;------------------------------------------------------------------------------------------------------------------------------------------
SendKey ("!wh") ; Tile horizontal MDI windows in WBStudio editor. ; Function wWinTile() does not fit here.
;------------------------------------------------------------------------------------------------------------------------------------------
sHeader = TimeYmdHms () : " - WSPOPUP.MNU - CleanUp" : @LF
sExitMsg = "Ready."
;------------------------------------------------------------------------------------------------------------------------------------------
; Search within WSPOPUP.MNU for menu title lines.
wf_sPattern = "^.*\^.*\^.*\^.*$" ; Pattern for menu title lines.
wf_iForward = @TRUE
wf_iMatchCase = @TRUE
wf_iRegExp = @TRUE
wf_iWrap = @FALSE
;------------------------------------------------------------------------------------------------------------------------------------------
iCountTouched = 0
sOut = ""
While @TRUE
   wClearSel ()
   wf_iResult = wFind (wf_sPattern, wf_iForward, wf_iMatchCase, wf_iRegExp, wf_iWrap)
   If !wf_iResult Then Break

   ; Fill selection buffer with hit line from wspopup.mnu.
   sPopLine = wGetWord ()

   iLine = wGetLineNo ()

   If !bDoCleanUp Then Goto BLOCK_END

   ;..........................................................................................................................................
   ; Work on Item1
   ;..........................................................................................................................................
   ; Fetch Item1 menu title from selection buffer.
   sPopLine1 = ItemExtract (1, sPopLine, "^")

   ; Delete spaces at end of Item1.
   sPopLine1 = ItemExtract (1, sPopLine1, ";")
   sPopLine1 = StrClean (StrTrim (@LF : sPopLine1), @LF, "", @TRUE, 1) ; Eliminate trailing blanks.

   ; Add spaces to Item1 to shift start of comment to column n.
   sComment = " ;"   ; Wspopup traditional floating comment, one space plus comment char.
   If bItm1_FirstColumnPadding Then sPopLine1 = StrFix (sPopLine1, " ", iItm1_FirstColumnPadSize - 2) : sComment
      Else sPopLine1 = sPopLine1 : sComment

      ; Store back Item1 into selection buffer.
   sPopLine = ItemReplace (sPopLine1, 1, sPopLine, "^")

   ;..........................................................................................................................................
   ; Work on Item2
   ;..........................................................................................................................................
   ; Fetch Item2 Keyword from selection buffer.
   sPopLine2 = ItemExtract (2, sPopLine, "^")

   ; Trim blanks.
   sPopLine2 = StrTrim (sPopLine2)

   ; Lowercase if there is one point character in Item2.
   If StrIndex (sPopLine2, ".", 1, @FWDSCAN) Then sPopLine2 = StrLower (sPopLine2)

   ; Store back Item2 into selection buffer.
   sPopLine = ItemReplace (sPopLine2, 2, sPopLine, "^")

   ;..........................................................................................................................................
   ; Work on Item3
   ;..........................................................................................................................................
   ; Fetch Item3 Parameters from selection buffer.
   sPopLine3 = ItemExtract (3, sPopLine, "^")

   ; Trim.
   sPopLine3 = StrTrim (sPopLine3)

   ; If there is something before the parameter list.
   ; ... and if there is a point in Item2.
   sPre = ItemExtract (1, sPopLine3, "(")
   sPre = StrTrim (sPre)
   If (sPre > "")
      sPopLine2 = ItemExtract (2, sPopLine, "^")
      If !StrIndex (sPopLine2, ".", 1, @FWDSCAN) Then sPre = " " : sPre : " "
   EndIf
   sPopLine3 = ItemReplace (sPre, 1, sPopLine3, "(")

   ; Comma.
   If bItm3_SpBehindComma Then sPopLine3 = StrReplace (sPopLine3, ",", ", ")
      Else sPopLine3 = StrReplace (sPopLine3, ", ", ",")

   ; Spaces.
   If bItm3_SpBeforeBrcktOpen Then sPopLine3 = StrReplace (sPopLine3, "[", " [")
      Else sPopLine3 = StrReplace (sPopLine3, " [", "[")
   If bItm3_SpBehindBrcktOpen Then sPopLine3 = StrReplace (sPopLine3, "[", "[ ")
      Else sPopLine3 = StrReplace (sPopLine3, "[ ", "[")
   If bItm3_SpBeforeBrcktClose Then sPopLine3 = StrReplace (sPopLine3, "]", " ]")
      Else sPopLine3 = StrReplace (sPopLine3, " ]", "]")
   If bItm3_SpBehindBrcktClose Then sPopLine3 = StrReplace (sPopLine3, "]", "] ")
      Else sPopLine3 = StrReplace (sPopLine3, "] ", "]")

   If bItm3_SpBeforeRoundBrcktOpen Then sPopLine3 = StrReplace (sPopLine3, "(", " (")
      Else sPopLine3 = StrReplace (sPopLine3, " (", "(")
   If bItm3_SpBehindRoundBrcktOpen Then sPopLine3 = StrReplace (sPopLine3, "(", "( ")
      Else sPopLine3 = StrReplace (sPopLine3, "( ", "(")
   If bItm3_SpBeforeRoundBrcktClose Then sPopLine3 = StrReplace (sPopLine3, ")", " )")
      Else sPopLine3 = StrReplace (sPopLine3, " )", ")")
   If bItm3_SpBehindRoundBrcktClose Then sPopLine3 = StrReplace (sPopLine3, ")", ") ")
      Else sPopLine3 = StrReplace (sPopLine3, ") ", ")")

   ; Replace double spaces with one space.
   While StrIndex (sPopLine3, "  ", 1, @FWDSCAN)
      sPopLine3 = StrReplace (sPopLine3, "  ", " ")
   EndWhile

   If bItm3_SpBetweenBrcktsOpen Then sPopLine3 = StrReplace (sPopLine3, "([", "( [")
      Else sPopLine3 = StrReplace (sPopLine3, "( [", "([")
   If bItm3_SpBetweenBrcktsClose Then sPopLine3 = StrReplace (sPopLine3, "])", "] )")
      Else sPopLine3 = StrReplace (sPopLine3, "] )", "])")

   If bItm3_SpBetweenRoundBrckts Then sPopLine3 = StrReplace (sPopLine3, "()", "( )")
      Else sPopLine3 = StrReplace (sPopLine3, "( )", "()")


   ; Make unique name.
   ;If StrIndex(sPopLine3,"partial",1,@FWDSCAN) Then BreakPoint
   sPopLine3 = StrReplace (sPopLine3, "partial filename", "partial-filename")
   sPopLine3 = StrReplace (sPopLine3, "dialog_handle", "dialog-handle")
   sPopLine3 = StrReplace (sPopLine3, "newwinname", "new-windowname")
   sPopLine3 = StrReplace (sPopLine3, "parent windowname", "parent-windowname")
   sPopLine3 = StrReplace (sPopLine3, "partial Child winname", "partial-child-windowname")
   sPopLine3 = StrReplace (sPopLine3, "partial parent winname", "partial-parent-windowname")
   sPopLine3 = StrReplace (sPopLine3, "partial window name", "partial-windowname")
   sPopLine3 = StrReplace (sPopLine3, "partial_window_name", "partial-windowname")
   sPopLine3 = StrReplace (sPopLine3, "partialChildwindowname", "partial-child-windowname")
   sPopLine3 = StrReplace (sPopLine3, "partialChildwinname", "partial-child-windowname")
   sPopLine3 = StrReplace (sPopLine3, "partialname", "partial-windowname")
   sPopLine3 = StrReplace (sPopLine3, "partialparentwindowname", "partial-parent-windowname")
   sPopLine3 = StrReplace (sPopLine3, "partialwinname", "partial-windowname")
   sPopLine3 = StrReplace (sPopLine3, "partial-winname", "partial-windowname")
   sPopLine3 = StrReplace (sPopLine3, "program name", "programmname")
   sPopLine3 = StrReplace (sPopLine3, "program-name", "programmname")


   ; Store back Item3 into selection buffer.
   sPopLine = ItemReplace (sPopLine3, 3, sPopLine, "^")

   ;..........................................................................................................................................
   ; Work on Item4
   ;..........................................................................................................................................
   ; Fetch Item4 desription from selection buffer.
   sPopLine4 = ItemExtract (4, sPopLine, "^")

   ; Trim blanks.
   sPopLine4 = StrTrim (sPopLine4)

   ; If first word in Item4 is the same as Item2 functionname then prepend the word "The" to save lowercase prefix of functionname.
   sFirst = ItemExtract (1, sPopLine4, " ")
   sPopLine2 = ItemExtract (2, sPopLine, "^")
   If (StriCmp (sFirst, sPopLine2) == 0)
      sPopLine4 = ItemReplace (sPopLine2, 1, sPopLine4, " ") ; Get spell checked name from Item2.
      sPopLine4 = ItemInsert ("The", 0, sPopLine4, " ")
   EndIf

   ; Set first char to uppercase.
   sPopLine4 = StrUpper (StrSub (sPopLine4, 1, 1)) : StrSub (sPopLine4, 2, -1)

   ; Replace double spaces with one space.
   While StrIndex (sPopLine4, "  ", 1, @FWDSCAN)
      sPopLine4 = StrReplace (sPopLine4, "  ", " ")
   EndWhile

   ; Add a point character if there is none to close the sentence.
   If (StrSub (sPopLine4, StrLen (sPopLine4), 1) != ".") Then sPopLine4 = sPopLine4 : "."

   ; Make unique name.
   sPopLine4 = StrReplace (sPopLine4, "dialog_handle", "dialog-handle")
   sPopLine4 = StrReplace (sPopLine4, "newwinname", "new windowname")
   sPopLine4 = StrReplace (sPopLine4, "partial Child winname", "partial child windowname")
   sPopLine4 = StrReplace (sPopLine4, "partial parent winname", "partial parent windowname")
   sPopLine4 = StrReplace (sPopLine4, "partial window name", "partial windowname")
   sPopLine4 = StrReplace (sPopLine4, "partialChildwindowname", "partial child windowname")
   sPopLine4 = StrReplace (sPopLine4, "partialChildwinname", "partial child windowname")
   sPopLine4 = StrReplace (sPopLine4, "partialparentwindowname", "partial parent windowname")
   sPopLine4 = StrReplace (sPopLine4, "partialwinname", "partial windowname")
   sPopLine4 = StrReplace (sPopLine4, "program name", "programmname")
   sPopLine4 = StrReplace (sPopLine4, "program-name", "programmname")

   ; Store back Item4 into selection buffer.
   sPopLine = ItemReplace (sPopLine4, 4, sPopLine, "^")

   ;..........................................................................................................................................
   ; Write back.
   ;..........................................................................................................................................
   ; Write back selection buffer into the file.
   wInsString (sPopLine)

   iCountTouched = iCountTouched + 1

   :BLOCK_END

   sLineNum = StrFixLeft (iLine, " ", 6)
   sPopLine = sLineNum : "  |" : sPopLine
   sOut = ItemInsert (sPopLine, -1, sOut, @LF)

   sStatusMsg = iLineCount : "/" : sPopLine
   wStatusMsg (sStatusMsg)

   If IsKeyDown (@SHIFT)
      sExitMsg = "*** Aborted. ***"
      Break
   EndIf
EndWhile

wTopOfFile ()
;------------------------------------------------------------------------------------------------------------------------------------------
If sOut != ""
   sOut = ItemInsert ("", -1, sOut, @LF)
   sOut = ItemInsert (TimeYmdHms () : " - Line " : iLineCount : "/" : iLine, -1, sOut, @LF)
   sOut = ItemInsert (" - Lines touched: " : iCountTouched, -1, sOut, @LF)
   sOut = ItemInsert (sExitMsg, -1, sOut, @LF) ; Add Footer.
   sOut = ItemInsert (sHeader, 0, sOut, @LF) ; Add Header.
   sOut = StrReplace (sOut, @LF, @CRLF)
EndIf
;------------------------------------------------------------------------------------------------------------------------------------------

;------------------------------------------------------------------------------------------------------------------------------------------
:CANCEL
wWinMaximize ()  ; SendKey("!-x") ; Maximize MDI window in WBStudio editor.
;------------------------------------------------------------------------------------------------------------------------------------------
If sOut == "" Then sOut = "*** No output. ***"
ClipPut (sOut)
wFileNew ()
wPaste ()
Exit
;------------------------------------------------------------------------------------------------------------------------------------------