/***********************************************************************************/
/* Highlight the lines between a 'begin' and 'end' RPG Control                     */
/* structure.  For instance, place cursor of line containing IFEQ                  */
/* and will highlite all of the lines to the matching ENDIF                        */
/*                                                                                 */
/*          Copyright: IBM Corporation 1993                                        */
/*                                                                                 */
/***********************************************************************************/
/*                                                                                 */
/* The match command takes one parameter, JUMP. If this parameter is issued, the   */
/* cursor jumps to the matching END without highlighting.                          */
/*                                                                                 */
/***********************************************************************************/

parse  arg opTion                                  /* to get options               */
opTion = translate(opTion)
prefNum    = 1                                     /* to allow highliting          */
if opTion  = 'JUMP' then prefNum = 0

'Extract doctype content'                          /* get document type and current line */
'Extract elements'                                 /* total number of lines        */
'Extract element position'
rowNum = element                                  /* get the starting line number,*/
colNum = position                                 /* column number                */

if (doctype = "RPG") then opcode = 28             /* opcode in column 28 for RPG   */
else opcode = 26                                  /* and column 26 for ILE RPG     */

str2           = Substr(content,opcode,2)                  /* get the contents     */
str3           = Substr(content,opcode,3)                  /* starting from the    */
Str5           = Substr(content,opcode,5)                  /* line number 28       */
specChar       = Substr(content,6,1)                       /* RPG C specs ?        */

keyNum         =  1                                        /* how many keywords ?  */
num            = -1                                        /* just a flag          */
maxLineNo      =  0                                        /* to check wrapping    */
                           
If ((str2='IF' | str2='DO' | str2='WH' | str3='DOU' | str3='DOW' |,
    str3='CAS' | Str5='SELEC' ) & specChar='C') then   num = 1
if (str3 = 'END' & specChar = 'C') then num = 0            /* if END, match others */
                     
if (num = 0 | num = 1)  then                       /* IFxx or ENDxx, continue here */
    do
       if (Substr(content,7,1) = '*') then                            /* comment   */
          do
                Msg 'Cursor is on the Comment line'
                Exit 0
          end
       if (str5= 'CASEQ') then                     /* for CALL statements          */
          do
                Msg ' Cursor is on the calling statement'
                Exit 0
          end
       if (prefNum = 1) then                               /* block the lines      */
          do
                'block clear'                      /* clear if already blocked     */
                'block Set element'                /* set starting of the block    */
          end

       Do until ( keyNum = 0 )
          'Extract element'
          if (maxLineNo < element) then maxLineNo = element
          else
            do
              msg 'matching not found .. wrapping'
              exit 0
            end
          if ((num = 1 & element = elements) | (num = 0 & element =1)) then
              do                                          /* search for the end    */
                    'block clear'                  /* clear if already blocked     */
                    'Find element' rowNum                 /* to handle mismatching */
                    'Set position' colNum                 /* cases-reset the pos.  */
                     if (num=1) then Msg 'matching END not found '
                        else Msg 'matching statement not found'
                     Exit 0
              end

          if (num = 1) then 
                'Next class CONTROL'
          else  'Prev class CONTROL'                    /* upwards or downwards    */
    
          'Extract content'                          /* get the current line       */

           text2 = Substr(content,opcode,2)                    /* for IF, DO, WH.. */
           text3 = Substr(content,opcode,3)                    /* for DOW.., DOU.. */
           text5 = Substr(content,opcode,5)                    /* for SELEC.       */

           if (text2 = 'IF' | text2= 'WH' | text2 = 'DO' | text3 = 'DOW' |,
               text3 = 'DDU' | text3 = 'CAS' |,
               text5 = 'SELEC') then
               do
                    if (text5 \= 'CASEQ') then 
                        do
                           if(num = 1) then
                                 keyNum = keyNum + 1
                           else  keyNum = keyNum -1
                        end
                end
                  
           if (text3 = 'END') then
               do
                    if (text5 \= 'ENDCS') then
                        do
                           if (num = 0) then
                                keyNum = keyNum +1
                           else  keyNum = keyNum -1
                        end
               end
      end
      if (prefNum = 1) then
          do
              'block Set'
              'Find element' rowNum                      /* reset cursor position */
              'Set position' colNum
          end
    end
 else
         Msg 'cursor is not on the proper line'
 Exit 0                                                           /* normal exit  */
