WBStudio.WspopupMnu.MakeCall
;==========================================================================================================================================
; WBStudio Utility for use within WinBatch Studio debug mode.
;==========================================================================================================================================
;
; "MakeCall" alias "Call Unifier" for WSPOPUP.MNU - Read/Write -
;
; This script creates 'Call(zxc,"...")' statements from special title lines
; with the same spelling as the related keyword in the line before.
;
; It works directly on the file wspopup.mnu - virtually in WBStudio editor -
; but user must decide to save changes afterwards.
;
; This tool is part of the WBStudio.WspopupMnu service utilities.
; 1. WBStudio.WspopupMnu.SpellCheck.wbt
; 2. WBStudio.WspopupMnu.MakeCall.wbt
; 3. WBStudio.WspopupMnu.CleanUp.wbt
; 4. ...
;..........................................................................................................................................
;
; >>> 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"
;
iLeftMargin = 4    ; First column to start with code is col 5, because col 1-4 are reserved for menu structure.
;
;iLeftMargin = 51  ; Experimental.
; Nice alignment with comments in column 60 when used in combination
; with "FirstColumnPadding" by "WBStudio.WspopupMnu.CleanUp" tool.
;
sMask_Call = `Call(zxc,"{1}")` ; Should not be changed.
;
;------------------------------------------------------------------------------------------------------------------------------------------


;------------------------------------------------------------------------------------------------------------------------------------------
; Set Editor AutoIndent OFF.
iAutoIndent = RegQueryDword (@REGCURRENT, "Software\Wilson WindowWare\WinBatch Studio\Settings\File types\WIL Files[Autoindent]")
If iAutoIndent
   ;SendKey("!vo^{TAB}!a{ENTER}") ; Function wViewOptions() does not fit in this case.
   SendKey ("!vo!a~") ; Function wViewOptions() does not fit in this case.
   TimeDelay (1)
   iAutoIndent = RegQueryDword (@REGCURRENT, "Software\Wilson WindowWare\WinBatch Studio\Settings\File types\WIL Files[Autoindent]")
   Terminate (iAutoIndent, IntControl (1004, 0, 0, 0, 0), "Terminated. Cannot set registry value [Autoindent]=off.")
EndIf
;------------------------------------------------------------------------------------------------------------------------------------------
wFileOpen (sWorkFile)
wTopOfFile ()
iLineCount = wLineCount ()
;------------------------------------------------------------------------------------------------------------------------------------------
SendKey ("!wh") ; Tile horizontal MDI windows in WBStudio editor. ; Function wWinTile() does not fit in this case.
;------------------------------------------------------------------------------------------------------------------------------------------
; 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
;------------------------------------------------------------------------------------------------------------------------------------------
iLeftMargin = Min (Max (4, iLeftMargin), 132)
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 ()
   wClearSel ()                           ; Release selection.

   ;..........................................................................................................................................
   ; Work on Item2
   ;..........................................................................................................................................
   ; Fetch Item2 Keyword from selection buffer.
   ; Assuming that there is no quirkie value in there.
   ; A "cleaning" has to be done before! See description at top of this file.
   sPopLine2 = ItemExtract (2, sPopLine, "^")

   ; Prepare the Call statement.
   sCall = StrReplace (sMask_Call, "{1}", sPopLine2)
   sCall = StrFill (" ", iLeftMargin) : sCall

   ;..........................................................................................................................................
   ; Lookup into following line and modify line.
   ;..........................................................................................................................................
   iLine = wGetLineNo ()                  ; Current line. Our homebase for the following activities.

   wGotoLine (iLine + 1)                  ; Following line. Already Call statement? If so, delete it.
   wGotoCol (1)
   wStartSel ()
   wEnd ()
   wEndSel ()
   sPopLine = wGetWord ()
   If StrIndexWild (sPopLine, "Call*(*)", 1)
      wDelete ()                          ; Delete Selection.
      wDelete ()                          ; Delete EOL.
   EndIf

   wGotoLine (iLine + 1)                  ; Following line. Menu title or a codeline?
   wGotoCol (1)
   wStartSel ()
   wEnd ()
   wEndSel ()
   sPopLine = wGetWord ()
   wClearSel ()
   If (StrSub (sPopLine, 1, 4) != "    ") ; If not a codeline (codeline has min. 4 leading spaces).
      wGotoLine (iLine + 1)
      wGotoCol (1)
      wInsLine (sCall)                    ; Insert Call statement.
   EndIf

   ; Note: Possible commentline with first nonblank ";" is not handled here.
   ;..........................................................................................................................................

   sLineNum = StrFixLeft (iLine, " ", 6)
   iLineCount = wLineCount ()
   sStatusMsg = iLineCount : "/" : sLineNum : "  |" : sPopLine
   wStatusMsg (sStatusMsg)

   If IsKeyDown (@SHIFT) Then Break
EndWhile

wTopOfFile ()

:CANCEL
;------------------------------------------------------------------------------------------------------------------------------------------
RegSetDword (@REGCURRENT, "Software\Wilson WindowWare\WinBatch Studio\Settings\File types\WIL Files[Autoindent]", iAutoIndent)
;------------------------------------------------------------------------------------------------------------------------------------------
wWinMaximize ()  ; SendKey("!-x") ; Maximize MDI window in WBStudio editor.
;------------------------------------------------------------------------------------------------------------------------------------------
Exit
;------------------------------------------------------------------------------------------------------------------------------------------