home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Professional / OS2PRO194.ISO / os2 / prgramer / rexx / 4macrs / smartc.rex < prev   
OS/2 REXX Batch file  |  1993-05-27  |  10KB  |  411 lines

  1. /* extended smart c template editing for SourceLink32 */
  2. /* 
  3.    Creation        93/03/12 by  Thomas Korfhage, tel&fax ++49 6446 2931
  4.                                                  CIS: 100271,16
  5.    Description
  6.    This macro has 4 entrys.
  7.    
  8.    If there is an argument it might be either UP, DOWN or FCTYPE.
  9.    
  10.    UP     - goto prev. function header. (match { at pos 1)
  11.    DOWN   - goto next  function header. (match { at pos 1)
  12.    FCTYPE - asume the actual line is a complete function declaration.
  13.             Take this and insert it under the first FTY eyecatcher comment
  14.             found in file and append a ';'.
  15.  
  16.    any other argument will be ignored and the macro will do a C-template
  17.    edition. It will expand one or more letters to a C-template
  18.    i -> if; e -> else; f -> for; w -> while; d -> do.while; b or { -> braces
  19.    
  20.    if the letter(s) are lower case a short template will be created
  21.       e.g.   i ->  if ()
  22.       
  23.    if the letter(s) are upper case the result is a full template (with braces)
  24.       e.g.   I ->  if () {
  25.                    }
  26.     all text following the letter(s) on the same line goes into the parens.
  27.     
  28.     (cl, ce, h are unchanged from the original SLINK2 SMARTC.REX)
  29.     
  30.    If any text is selected, the user will be asked for the type of template
  31.    before the template will be created arround it.
  32.    The selected text then becomes the body of the block.
  33.    Normaly only full lines should be selected.
  34.    
  35.    The created template are adapted to my style.
  36.    Indentation will be 1 tab.
  37. */
  38. 'S_CLEAR_MACRO_OUTPUT'
  39. if ARG(1,'E')then do
  40.    ARG ExtFunc
  41.    if Wordpos( ExtFunc, 'UP DOWN FCTYPE') \= 0 then do
  42.       'S_DISABLE_WINDOW'
  43.       Signal Value ExtFunc
  44.    end 
  45.    exit
  46. end   
  47. 'S_GET_SELECTED_SIZE' 'Len'
  48. If Len <= 1 THEN SIGNAL SMART_UNSEL
  49. 'S_PROMPT Create block over selected code (i-e-f-w-d-b-{), CCode'
  50. 'S_DISABLE_WINDOW'
  51. 'S_CUT_SEL_TO_PASTE'
  52. 'S_GET_LINE_NUM' 'SaveLineNum'
  53. 'S_GET_COL_NUM' 'SaveColNum'
  54. 'S_INDENT'
  55. SELECT
  56.    WHEN abbrev( 'if', CCode, 1 ) THEN
  57.    do
  58.    'S_INSERT_LINE' 'if () {'
  59.    NrBS = 3 
  60.    end
  61.  
  62.    WHEN abbrev( 'while', CCode, 1 ) THEN 
  63.    do
  64.   'S_INSERT_LINE' 'while () {'
  65.    NrBS = 3 
  66.    end
  67.  
  68.    WHEN abbrev( 'for', CCode, 1 ) THEN
  69.    do
  70.   'S_INSERT_LINE' 'for (;;) {'
  71.    NrBS = 5 
  72.    end
  73.   
  74.    WHEN (WCode = '{') | abbrev( 'b', CCode, 1 ) THEN
  75.    do
  76.   'S_INSERT_LINE' '{'
  77.    NrBS = 0
  78.    end
  79.    
  80.    WHEN abbrev( 'do', CCode, 1 ) THEN
  81.    do
  82.   'S_INSERT_LINE' 'do {'
  83.   'S_INDENT'
  84.   'S_INSERT_LINE' '} while (#) ;'
  85.   call insertpaste
  86.   'S_SEARCH_FWD' 1 0 '} while (#) ;'
  87.   'S_END_OF_LINE'
  88.   'S_PREV_CHAR'
  89.   'S_PREV_CHAR'
  90.   'S_PREV_CHAR'
  91.   'S_PREV_CHAR'
  92.   'S_DEL_CHAR'
  93.   'S_ENABLE_WINDOW'
  94.    EXIT
  95.    end
  96.  
  97.    OTHERWISE
  98.    'S_INSERT_PASTE'
  99.    'S_ENABLE_WINDOW'
  100.    EXIT
  101. end
  102. 'S_INDENT'
  103. 'S_INSERT_LINE' '}'
  104. call insertpaste
  105. 'S_GOTO_LINE' SaveLineNum
  106. 'S_GOTO_COL' SaveColNum
  107. 'S_END_OF_LINE'
  108. DO I=1 TO NrBS
  109. 'S_PREV_CHAR'
  110. end
  111. 'S_ENABLE_WINDOW'
  112. EXIT
  113.  
  114. SMART_UNSEL:
  115. 'S_GET_CURR_WORD' 'WCode'
  116. CCode = WCode
  117. CCode = translate( WCode)
  118. FULLCode = WCode == CCode
  119. MoreInLine = 0
  120.  
  121. SELECT
  122.    
  123.    WHEN abbrev( 'IF', CCode, 1 ) THEN
  124.       do
  125.       call isMoreInLine
  126.       MoreInLine = RESULT
  127.       call insertcode( "if ()" )
  128.       'S_PREV_CHAR'
  129.       end
  130.  
  131.    WHEN abbrev( 'ELSE', CCode, 1 ) THEN
  132.       do
  133.       call insertcode( "else" )
  134.       if FULLCode == 1 then do
  135.          'S_END_OF_LINE'
  136.          ADDSTR = '  ' 
  137.          'S_INSERT_LINE' ADDSTR     
  138.          'S_INDENT' 
  139.          'S_INSERT_STRING' ADDSTR     
  140.          'S_END_OF_LINE'  
  141.       end
  142.       else do
  143.          'S_END_OF_LINE'
  144.          ADDSTR = ' ' 
  145.          'S_INSERT_STRING' ADDSTR  
  146.       end 
  147.       end
  148.  
  149.    WHEN abbrev( 'WHILE', CCode, 1 ) THEN 
  150.       do
  151.       call isMoreInLine
  152.       MoreInLine = RESULT
  153.       call insertcode( "while ()" )
  154.       'S_PREV_CHAR'
  155.       end
  156.       
  157.    WHEN abbrev( 'SWITCH', CCode, 1 ) THEN 
  158.       do
  159.       call isMoreInLine
  160.       MoreInLine = RESULT
  161.       call insertcode( "switch ()" )
  162.       'S_PREV_CHAR'
  163.       end
  164.  
  165.    WHEN abbrev( 'CASE', CCode, 1 ) THEN
  166.       do
  167.       call insertcode( "case :" )
  168.       'S_PREV_CHAR'
  169.       end
  170.  
  171.    WHEN abbrev( 'FOR', CCode, 1 ) THEN
  172.       do
  173.       call insertcode( "for( ; ; )" )
  174.       'S_PREV_CHAR'
  175.       'S_PREV_CHAR'
  176.       'S_PREV_CHAR'
  177.       'S_PREV_CHAR'
  178.       'S_PREV_CHAR'
  179.       end
  180.  
  181.    WHEN abbrev( 'DO', CCode, 1 ) THEN 
  182.       do
  183.       call insertline( "do" )          /* do             */
  184.       ADDSTR = '{'  
  185.       'S_INSERT_LINE' ADDSTR           /* {              */
  186.       'S_INDENT'
  187.       ADDSTR = ' '  
  188.       'S_INSERT_LINE' ADDSTR           /* blank line     */
  189.       'S_INDENT'
  190.       'S_PREV_CHAR'
  191.       ADDSTR = '} while( )'  
  192.       'S_INSERT_LINE' ADDSTR           /* } while ()     */
  193.       'S_PREV_LINE'
  194.       'S_PREV_LINE'
  195.       'S_INDENT'
  196.       ADDSTR = ' ' 
  197.       'S_INSERT_STRING' ADDSTR  
  198.       end
  199.       
  200.    WHEN WCode = 'b' | WCode = '{' THEN 
  201.       do
  202.       call insertline( "{" )           
  203.       ADDSTR = '  '  
  204.       'S_INSERT_LINE' ADDSTR     
  205.       'S_INDENT' 
  206.       'S_PREV_CHAR'
  207.       ADDSTR = '}'  
  208.       'S_INSERT_STRING' ADDSTR      
  209.       'S_PREV_LINE'
  210.       'S_END_OF_LINE'  
  211.       end
  212.  
  213.  
  214.    WHEN WCode = 'cl' THEN
  215.       do
  216.       'S_PROMPT' 'Enter Comment,' 'Comment' 
  217.       NEWCODE = '/* ' || Comment '*/'
  218.       call insertcode( NEWCODE ) 
  219.       'S_INSERT_LINE' ' '
  220.       'S_INDENT'   
  221.       end
  222.  
  223.    WHEN WCode = 'ce' THEN
  224.       do
  225.       'S_PROMPT' 'Enter Comment,' 'Comment' 
  226.       'S_SELECT_WORD'
  227.       'S_DEL_SELECTED'
  228.       StrLen = LENGTH( Comment )
  229.       Justify = 'LEFT'
  230.  
  231.       IF Justify = 'RIGHT'  THEN 
  232.          IF ( StrLen >= 70 ) THEN
  233.             CommentCol = 1
  234.          ELSE
  235.             CommentCol = 70 - StrLen + 1
  236.       ELSE
  237.          CommentCol = 45
  238.  
  239.       'S_END_OF_LINE'
  240.       IF rc \= 0 THEN DO
  241.          SAY 'S_ Error = ' rc
  242.          EXIT
  243.          END
  244.  
  245.       'S_GET_COL_NUM' 'CURRCOL'
  246.       IF rc \= 0 THEN DO
  247.          SAY 'S_ Error = ' rc
  248.          EXIT
  249.          END
  250.  
  251.       IF CurrCol < CommentCol THEN
  252.          DO
  253.          'S_GOTO_COL' CommentCol
  254.          'S_INSERT_STRING' '// ' || Comment
  255.          END
  256.       ELSE
  257.          DO
  258.          'S_PREV_LINE'
  259.          'S_END_OF_LINE'
  260.          ForceNewLine = ' '
  261.          'S_INSERT_LINE' ForceNewLine
  262.          NewComment = COPIES(' ', CommentCol - 1 ) || '// ' || Comment 
  263.          'S_INSERT_STRING' NewComment
  264.          END
  265.  
  266.       end
  267.       
  268.    WHEN WCode = 'h' THEN
  269.       do
  270.       'S_SELECT_WORD'
  271.       'S_DEL_SELECTED'
  272.       'S_PROMPT' 'Enter Function Comments,' 'NEWFILENAME'
  273.       
  274.       comment = '/'|| copies('*',67)
  275.       'S_INSERT_LINE' comment
  276.  
  277.       'S_INSERT_LINE' '*'
  278.       'S_INSERT_LINE' '*  ' || NEWFILENAME 
  279.       'S_INSERT_LINE' '*'
  280.       comment = copies('*',67) || '/'
  281.       'S_INSERT_LINE' comment
  282.  
  283.       end
  284.  
  285.       OTHERWISE
  286.       NOP
  287. end 
  288. exit     
  289.  
  290. insertcode: procedure expose MoreInLine FULLCode
  291.       parse arg CodeToAdd
  292.       'S_SELECT_WORD'
  293.       'S_DEL_SELECTED'
  294.       'S_INSERT_STRING' CodeToAdd
  295.       if MoreInLine == 1 then do
  296.          'S_PREV_CHAR'
  297.          'S_DEL_CHAR'
  298.          'S_DEL_CHAR'
  299.          'S_END_OF_LINE'
  300.          do forever
  301.             'S_PREV_CHAR'
  302.             'S_GET_CURR_CHAR' 'curchar'
  303.             if (curchar \= " " & curchar \= ";") then leave
  304.             'S_DEL_CHAR'
  305.          end   
  306.          'S_NEXT_CHAR'
  307.          'S_INSERT_STRING' ')'
  308.       end
  309.       if FULLCode == 1 then do
  310.          'S_GET_LINE_NUM' 'SaveLineNum'
  311.          'S_GET_COL_NUM' 'SaveColNum'
  312.          'S_INSERT_LINE' ' {'
  313.          'S_INDENT'
  314.          'S_INSERT_STRING }'
  315.          'S_GOTO_LINE' SaveLineNum
  316.          'S_GOTO_COL' SaveColNum
  317.       end   
  318.       return   
  319.  
  320. insertline: procedure
  321.       parse arg CodeToAdd
  322.       'S_SELECT_WORD'
  323.       'S_DEL_SELECTED'
  324.       'S_INSERT_LINE' CodeToAdd
  325.       'S_INDENT'
  326.       return   
  327.  
  328. insertpaste: procedure
  329.    'S_PREV_LINE'
  330.    'S_GET_LINE_NUM' 'ZAnum'
  331.    'S_INSERT_PASTE'
  332.    'S_GET_LINE_NUM' 'ZEnum'
  333.    'S_GOTO_LINE' ZAnum
  334.    
  335.    AddTab = '9'x
  336.    do I=ZAnum+1 to ZEnum by 1
  337.       'S_BEG_OF_LINE'
  338.       'S_INSERT_STRING' AddTab
  339.       'S_NEXT_LINE'
  340.    end
  341.    return
  342.    
  343. isMoreInLine: procedure
  344.    'S_GET_COL_NUM' 'Col'
  345.    'S_GET_LINE_LEN' 'LLen'
  346.    return LLen > (Col+2)
  347.  
  348. UP:   /* search back line with brace at 1 pos */
  349. 'S_GET_LINE_NUM' 'SaveLineNum'
  350. 'S_GET_COL_NUM' 'SaveColNum'
  351. 'S_SEARCH_BACK' 1 '{'
  352. do forever
  353.    if RC = 0 then do 
  354.       'S_GET_COL_NUM' 'ColNum'
  355.       if ColNum <= 2 then do
  356.          'S_ENABLE_WINDOW'
  357.          exit
  358.       end
  359.       else  
  360.          'S_SEARCH_NEXT'
  361.    end
  362.    else
  363.       leave 
  364. end
  365. 'S_GOTO_LINE' SaveLineNum
  366. 'S_GOTO_COL' SaveColNum
  367. 'S_ENABLE_WINDOW'
  368. exit
  369.  
  370. DOWN: /* search forward for line with brace at 1 pos */
  371. 'S_GET_LINE_NUM' 'SaveLineNum'
  372. 'S_GET_COL_NUM' 'SaveColNum'
  373. 'S_SEARCH_FWD' 1 0 '{'
  374. do forever
  375.    if RC = 0 then do 
  376.       'S_GET_COL_NUM' 'ColNum'
  377.       if ColNum <= 2 then do
  378.          'S_ENABLE_WINDOW'
  379.          exit
  380.       end
  381.       else  
  382.          'S_SEARCH_NEXT'
  383.    end
  384.    else
  385.       leave 
  386. end
  387. 'S_GOTO_LINE' SaveLineNum
  388. 'S_GOTO_COL' SaveColNum
  389. 'S_ENABLE_WINDOW'
  390. exit
  391.  
  392. /* Get the function header an put it unter the FTY - comment on top of file */
  393. /* Current position has to be the line containing the function declaration  */
  394. /* For my needs only 1 line is ok. Change the code for more lines */
  395. FCTYPE: 
  396. 'S_GET_LINE_NUM' 'SaveLineNum'
  397. 'S_GET_COL_NUM' 'SaveColNum'
  398. 'S_SELECT_LINE'
  399. 'S_GET_SELECTED_TEXT' 'FtyLine'
  400. 'S_SEARCH_FWD' 1 1 '/*FTY*/'
  401. if RC = 0 then do 
  402.    'S_NEXT_LINE'
  403.    'S_BEG_OF_LINE'
  404.    'S_INSERT_STRING' INSERT(';',FtyLine,LENGTH(FtyLine)-2)
  405. end
  406. 'S_GOTO_LINE' SaveLineNum+1
  407. 'S_GOTO_COL' SaveColNum
  408. 'S_ENABLE_WINDOW'
  409. exit
  410.  
  411.