home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / p2demo21.exe / PEL / ROUTINE.PEL < prev    next >
Text File  |  1995-03-22  |  20KB  |  714 lines

  1. # $Header:   P:\source\wmacros\routine.pev   1.58   22 Mar 1995 13:20:10   PFHDWM0  $
  2. ## $Tabs:4 7$
  3.  
  4. ##############################################################################
  5. #
  6. #       Compuware Corporation
  7. #         31440 Northwestern Highway
  8. #           Farmington Hills, Michigan 48334-2564
  9. #
  10. #   This source code listing contains information that is
  11. #   proprietary to Compuware Corporation and may not be copied
  12. #   duplicated, translated, transmitted, stored, retrieved
  13. #   or in any manner or by any method conveyed or disclosed
  14. #   to a third party or parties without express written
  15. #   permission from Compuware Corporation.
  16. #
  17. #  
  18. ##############################################################################
  19.  
  20. #### $Workfile:   routine.pel  $: function name and string collection
  21.  
  22. ## routines()
  23. #
  24. #  Create a string of all the functions in the current edit buffer.
  25. #  This routines assumes the following format of each function declaration
  26. #
  27. #  For C files:
  28. #     [<type>] <function_name> ( [<parameters>] )
  29. #
  30. #  For PEL files
  31. #     [global|local] function <function_name> ( [<parameters>] ) [{]
  32. #
  33. local dhTemp
  34. global function routines(allBuffers)
  35. {
  36.    local loc, fun_name, dlg_title, sentinel, temp
  37.    local sflags = SEARCH_FORWARD+SEARCH_REGEX+SEARCH_MAXIMAL_MATCH
  38.    local ext    = tolower(path_ext( buffer_filename ));
  39.    local found_routine = 0
  40.    local cursor_loc, srchstring
  41.    
  42.    if ( allBuffers )
  43.       dlg_title = "Global Routines List"
  44.    else
  45.       dlg_title = "Find Routines - " buffer_name
  46.  
  47.    dhTemp = create_list_dialog( dlg_title,  editor_helpfile )
  48.  
  49.    # initialize dBase filename extension list
  50.    if(!templates)
  51.    {
  52.       warning("Extensions not initialized!! Quitting!!" )
  53.       return FALSE
  54.    }
  55.  
  56.    sentinel = current_buffer
  57.  
  58.    process_background( DISABLE_FOREGROUND )
  59.  
  60.    save_position();
  61.    do 
  62.    {
  63.       if (!(and(buffer_flags, BUFFER_SYSTEM))) 
  64.       {
  65.          # check for known filename extension
  66.          temp = Template(ext);
  67.          if ( !( (temp == "c")   \
  68.               || (temp == "basic")  \
  69.               || (temp == "awk")  \
  70.               || (temp == "pascal") \
  71.               || (temp == "cobol") \
  72.               || (temp == "dbase")  \
  73.               || (temp == "clipper87") \
  74.               || (temp == "clipper50") \
  75.               || (temp == "force") ))
  76.          {
  77.             message("Invalid extension for routines.");
  78.             delete_list_dialog(dhTemp);
  79.    
  80.             process_background( END_BACKGROUND )
  81.    
  82.             return;
  83.          }
  84.       
  85.          goto_buffer_top();
  86.    
  87.          srchstring = "^[a-zA-Z0-9_\\-]"
  88.    
  89.          if(temp == "c")
  90.          {
  91.             found_routine = optional_function( "do_c_routines", dhTemp, allBuffers)
  92.             break;
  93.          }
  94.          else if(temp == "cobol")
  95.          {
  96.             found_routine = optional_function( "do_cobol_routines", dhTemp, allBuffers)
  97.             break;
  98.          }
  99.          else
  100.          {
  101.             while (search( srchstring, sflags))
  102.             {
  103.                loc = 0;
  104.    
  105.                goto_bol()
  106.    
  107.                fun_name = ltrim(trim( read_buffer() ));
  108.             
  109.                if (ext == ".pel" || ext == ".awk")
  110.                {
  111.                   if (index( fun_name, "function" ))
  112.                   {
  113.                      if (match(fun_name, "function[ \t]" ))
  114.                      {
  115.                         # remove the words "global|local function" from the front
  116.                         # of the found function name
  117.                         fun_name = substr(fun_name, RSTART+RLENGTH)
  118.                         loc = index( fun_name, "(" );
  119.                      }
  120.                   }
  121.                }
  122.                else if(temp == "pascal")
  123.                {
  124.                   if ( index(fun_name,"function")     \
  125.                     || index(fun_name,"procedure") \
  126.                     || index(fun_name,"FUNCTION")     \
  127.                     || index(fun_name,"PROCEDURE"))
  128.                   {
  129.                      loc = index(fun_name,"(")
  130.                      if ( loc == 0 && loc ~ / / )
  131.                      {
  132.                         loc = length( fun_name ) + 1
  133.                      }
  134.                   }
  135.                }
  136.                else if(temp == "basic")
  137.                {
  138.                   if ( index(fun_name,"FUNCTION"))
  139.                   {
  140.                      if ( fun_name ~ /FUNCTION[ \t]/)
  141.                      {
  142.                         loc = index(fun_name,"(")
  143.                      }
  144.                   }
  145.                }
  146.                else if( (temp == "dbase") \
  147.                      || (temp == "clipper87")  \
  148.                      || (temp == "clipper50")  \
  149.                      || (temp == "force"))
  150.                {
  151.                   if ( index(fun_name,"function")     \
  152.                     || index(fun_name,"procedure") \
  153.                     || index(fun_name,"FUNCTION")     \
  154.                     || index(fun_name,"PROCEDURE"))
  155.                   {
  156.                      if ( substr(fun_name,length(fun_name),1) == ")" )
  157.                         loc = index(fun_name,"(")
  158.                      else
  159.                         loc = length(fun_name)+1
  160.                   }
  161.                }
  162.             
  163.                found_routine += add_routine( loc, fun_name, dhTemp, allBuffers )
  164.    
  165.                sflags = or( sflags, SEARCH_ADVANCE );
  166.    
  167.             }
  168.       
  169.          }
  170.       }
  171.    } while ( allBuffers && next_buffer("", 1, 1) != sentinel)
  172.  
  173.    restore_position(1);
  174.  
  175.    process_background( END_BACKGROUND )
  176.  
  177.    # reset the focus to the current buffer
  178.    current_buffer = sentinel
  179.  
  180.    if (found_routine)
  181.       {
  182.       begin_list_dialog(dhTemp)
  183.       message("Search completed.");
  184.       }
  185.    else
  186.       {
  187.       delete_list_dialog(dhTemp);
  188.       message("No routines found.");
  189.       }
  190. }
  191.  
  192. global function add_routine( loc, fun_name, dhTemp, allBuffers )
  193. {
  194.    local retval = 0
  195.    local i
  196.    local t = 100
  197.    local x = 100
  198.  
  199.    if ( loc )
  200.    {
  201.       fun_name = current_line " : " trim( substr(fun_name, 1, loc-1) );
  202.    
  203.       if ( allBuffers )
  204.          fun_name = buffer_filename " : " fun_name
  205.    
  206.       add_item_to_list( dhTemp, fun_name,   \
  207.                            buffer_filename, current_line, 0, -1 )
  208.    
  209.       retval = 1
  210.    }
  211.  
  212.    # just display message enough to let user know something is going 
  213.    # on.
  214.    i = current_line * 100 / buffer_last_line 
  215.    t = i % 5
  216.    if ( t == 0 && x != i ) 
  217.    {
  218.       message("Searching file %s...( %d %%)", buffer_filename, i)
  219.       x = i
  220.    }
  221.    
  222.    return retval
  223. }
  224.  
  225. global function wroutines()
  226. {
  227.    routines(1)
  228. }
  229.  
  230. local function init_grep_call( str, numSearchCount, allBuffers )
  231. {
  232.    grep_numSearchCount  = numSearchCount
  233.    grep_str             = str
  234.    grep_allBuffers      = allBuffers
  235.  
  236.    if ( allBuffers )
  237.       grep_dlg_title = "Find All List - All Buffers ( \"" str "\" )"
  238.    else if ( and(search_flags, SEARCH_BLOCK) && selection_type() )
  239.       grep_dlg_title = "Find All List - Selected block:" buffer_name "( \"" str "\" )"
  240.    else
  241.       grep_dlg_title = "Find All List - " buffer_name "( \"" str "\" )"
  242.  
  243. }
  244.  
  245. global function background_grep()
  246. {
  247.    local sentinel
  248.  
  249.     if ( and(buffer_flags, BUFFER_SYSTEM) )
  250.        sentinel = next_buffer("", 0, 1)   # skip system buffers
  251.     else
  252.        sentinel = current_buffer
  253.    
  254.    grep_dhTemp = create_list_dialog( grep_dlg_title, editor_helpfile, "grep_list_callback" )
  255.  
  256.    # disable close while searching
  257.    set_dialog_item( grep_dhTemp, IDB_CLOSE_BTN, DAC_DISABLE )
  258.  
  259.    begin_dialog( grep_dhTemp )
  260.  
  261.    do 
  262.    {
  263.       grep_found_match = grep_found_match + perform_grep( grep_str, 
  264.                                                           grep_dhTemp, 
  265.                                                           grep_numSearchCount, 
  266.                                                           grep_allBuffers )
  267.    
  268.    } while ( grep_allBuffers && next_buffer("", 0, 1) != sentinel)
  269. }
  270.  
  271. ## grep()
  272. #
  273. #  given a string and an optional number or string matches, search for all
  274. #  occurrences of the string in the current buffer and display a list of the
  275. #  strings in a menu.
  276. #
  277. global function grep( str, numSearchCount, allBuffers)
  278. {
  279.    local  dlg_title
  280.    local  sentinel
  281.    local  found_string = 0
  282.    local  grep_string  = ""
  283.    local  occurrences
  284.    local  success_str
  285.    
  286.    init_grep()
  287.  
  288.    if ( !str )
  289.         str = prompt_history( "SEARCH", "GREP String: ", "", -1, 1, "grep_dialog" );
  290.    
  291.    if ( !str )
  292.       return;
  293.       
  294.    add_prompt_history( "SEARCH", str );
  295.            
  296.    numSearchCount += 0;       # convert string to int
  297.  
  298.    if ( numSearchCount <= 0 )
  299.       numSearchCount = MAX_SEARCH
  300.  
  301.    grep_string = str
  302.       
  303.    init_grep_call( str, numSearchCount, allBuffers )
  304.  
  305.    save_foreground_context()
  306.  
  307.    if ( process_background(DISABLE_FOREGROUND, function_id("background_grep")) )
  308.       success_str = "Search complete, "
  309.    else
  310.    {
  311.       success_str = "Search interrupted, "
  312.       restore_position( TRUE )
  313.    }
  314.  
  315.    restore_foreground_contexts()
  316.  
  317.    # can't stop once we're done
  318.    set_dialog_item( grep_dhTemp, DI_STOP_BUTTON, DAC_DISABLE )
  319.  
  320.    # but we can close it
  321.    set_dialog_item( grep_dhTemp, IDB_CLOSE_BTN, DAC_ENABLE )
  322.  
  323.  
  324.    # if there is anything in the list, then we've found something
  325.    if ( !(occurrences = query_dialog_item(grep_dhTemp, IDL_LIST, DAC_COUNT_ITEMS)) ) 
  326.    {
  327.       grep_string = ""
  328.       warning( "No matching strings found." )
  329.  
  330.       # if it hasn't already been deleted
  331.       if ( valid_list_dialog( grep_dhTemp ) )
  332.          delete_list_dialog( grep_dhTemp )
  333.    }
  334.    else
  335.       set_dialog_window(   grep_dhTemp, 
  336.                            DWC_STATUSBARTEXT, 
  337.                            success_str occurrences " occurrences found." )
  338.  
  339.    return grep_found_match
  340. }
  341.  
  342. local foreground_context[]
  343. global function save_foreground_context()
  344. {
  345.    foreground_context[ current_buffer ].first  = window_first
  346.    foreground_context[ current_buffer ].line   = current_line
  347.    foreground_context[ current_buffer ].buffer = current_buffer
  348. }
  349.  
  350. local function restore_foreground_contexts()
  351. {
  352.    local old_cur_buf = current_buffer
  353.    local buffer
  354.  
  355.    for ( buffer in foreground_context )
  356.    {
  357.       current_buffer = foreground_context[ buffer ].buffer
  358.       current_line = foreground_context[ buffer ].first
  359.       scroll_window_top()
  360.       current_line = foreground_context[ buffer ].line
  361.    }
  362.  
  363.    delete foreground_context
  364.  
  365.    current_buffer = old_cur_buf
  366. }
  367.  
  368. local function perform_grep(str, dhTemp, numCount, displayFilename)
  369. {
  370.    local  current_column
  371.    local  current_data
  372.    local  sflags = or(search_flags, SEARCH_ONCE_PER_LINE+SEARCH_FORWARD)
  373.    local  found_string = 0
  374.  
  375.    # turn off wrap search so that we only search the file once
  376.    sflags = and(sflags, not(SEARCH_WRAPS) )
  377.    
  378.    # turn off advance so that we can find the string if it starts at 1,1
  379.    sflags = and(sflags, not(SEARCH_ADVANCE) )
  380.  
  381.    save_position();
  382.  
  383.    if (and(search_flags, SEARCH_BLOCK) && selection_type())
  384.       goto_mark(selection_mark_top())
  385.    else
  386.       goto_buffer_top();
  387.  
  388.    while ( numCount-- && search( str, sflags) ) 
  389.    {
  390.       found_string   = 1
  391.       
  392.       # position cursor at beginning of line so that read_buffer gets the 
  393.       # whole line
  394.       #
  395.       goto_bol()
  396.  
  397.       if ( displayFilename )
  398.          current_data   = sprintf( "%s - %5d : %s", buffer_filename, current_line, read_buffer(200) )
  399.       else 
  400.          current_data   = sprintf( "%5d : %s", current_line, read_buffer(200) )
  401.    
  402.       add_item_to_list( dhTemp, current_data,   \
  403.                         buffer_filename, current_line, 0, -1) 
  404.    
  405.       # kludge since search once per line doesn't work
  406.       goto_eol()
  407.  
  408.       # turn advance back on so we find next string
  409.       sflags = or( sflags, SEARCH_ADVANCE )
  410.    
  411.       set_dialog_window(   dhTemp, 
  412.                            DWC_STATUSBARTEXT, 
  413.                            sprintf( "Searching file %s...(%d%%)", 
  414.                                     buffer_filename, 
  415.                                     current_line * 100 / buffer_last_line)  );
  416.    } # end while
  417.    
  418.    restore_position(1);
  419.  
  420.    return found_string
  421. }
  422.  
  423. global function grep_auto( numSearchCount, allBuffers )
  424. {
  425.    grep( symbol_under_cursor(), numSearchCount, allBuffers )
  426. }
  427.  
  428. function wgrep( str, numSearchCount)
  429. {
  430.    local saveFlags
  431.  
  432.    if (!str)
  433.         str = prompt_history("SEARCH", "All buffers GREP String: ", "", -1, 1, "wgrep_dialog" );
  434.  
  435.    if (!str)
  436.       return;
  437.  
  438.    saveFlags    = search_flags
  439.    search_flags = and(saveFlags, not(SEARCH_BLOCK))
  440.    grep( str, numSearchCount, 1)
  441.    search_flags = saveFlags
  442. }
  443.  
  444. global END_BACKGROUND                  = 0
  445. global ENABLE_FOREGROUND               = 1
  446. global DISABLE_FOREGROUND              = 2
  447. global STOP_BACKGROUND                 = 3
  448. global IN_BACKGROUND                   = 4
  449. global BACKGROUND_FOREGROUND_SUSPEND   = 5
  450. global BACKGROUND_FOREGROUND_RESUME    = 6
  451.  
  452. local lastGrepPath         = "*.*"
  453.  
  454. # used by fgrep
  455. local grep_sys_buffer      = 0
  456. local grep_arrayOfFiles[]
  457.  
  458. # used by grep
  459. local grep_numSearchCount  
  460. local grep_allBuffers      
  461.  
  462. # shared by fgrep, grep
  463. local grep_dhTemp
  464. local grep_dlg_title
  465. local grep_found_match
  466. local grep_str
  467.  
  468. local function init_grep()
  469. {
  470.    grep_sys_buffer  = 0
  471.    grep_found_match = FALSE
  472. }
  473.  
  474. local function init_fgrep_call( str, dlg_title, arrayOfFiles )
  475. {
  476.    grep_str          = str
  477.    grep_dlg_title    = dlg_title
  478.    grep_arrayOfFiles = arrayOfFiles
  479. }
  480.  
  481. global function background_fgrep()
  482. {
  483.    local i
  484.    local fn
  485.    local fileToOpen
  486. #   local orig_buffer = current_buffer
  487.    local file_list[]
  488.  
  489.    grep_dhTemp = create_list_dialog( grep_dlg_title, editor_helpfile, "grep_list_callback" )
  490.  
  491.    # disable close button while searching
  492.    set_dialog_item( grep_dhTemp, IDB_CLOSE_BTN, DAC_DISABLE )
  493.  
  494.    begin_list_dialog( grep_dhTemp )
  495.  
  496.    for ( i in grep_arrayOfFiles )
  497.    {
  498.       fileToOpen = grep_arrayOfFiles[i]
  499.  
  500.       # Read the files into an array first. This section shouldn't be 
  501.       # interrupted by a foreground findfirst, so suspend foreground 
  502.       # processing for the duration.
  503.       process_background( BACKGROUND_FOREGROUND_SUSPEND )
  504.          fn = findfirst( fileToOpen )
  505.          while ( fn )
  506.          {
  507.             file_list[ fn ] = TRUE
  508.             fn = findnext()
  509.          }
  510.       process_background( BACKGROUND_FOREGROUND_RESUME )
  511.  
  512.       for ( fn in file_list )
  513.       {
  514.          fn = tolower( buildpath(path_path(fileToOpen) fn) )
  515.  
  516.          set_dialog_window( grep_dhTemp, DWC_STATUSBARTEXT, "Searching file " fn "..." )
  517.             
  518.          # open the buffer as a system buffer
  519.          if ( filemode( fn ) == -1 )
  520.             continue
  521.  
  522.          grep_sys_buffer = current_buffer = create_buffer( fn, 
  523.                                                            fn, 
  524.                                                            BUFFER_SYSTEM + 
  525.                                                            BUFFER_NO_UNDO )
  526.  
  527.          grep_found_match = grep_found_match + perform_grep( grep_str, 
  528.                                                              grep_dhTemp, 
  529.                                                              MAX_SEARCH, 
  530.                                                              1 )
  531.             
  532. #         current_buffer = orig_buffer
  533.  
  534.          delete_buffer(grep_sys_buffer)
  535.  
  536.          grep_sys_buffer = 0
  537.             
  538.       }
  539.       # clear list for next subdir
  540.       delete file_list
  541.  
  542.       # parse the drive:directory from the filename and store in
  543.       # last path.
  544.       #
  545.       lastGrepPath = path_path(fileToOpen) "*.*"
  546.    }
  547. }
  548.  
  549. global function fgrep(str, infileToGrep, recursive)
  550. {
  551.    local ret
  552.    local saveFlags
  553.    local dlg_title
  554.    local InitialPath
  555.    local listOfFiles
  556.    local arrayOfFiles
  557.    local occurrences
  558.    local success_str
  559.  
  560.    init_grep()
  561.  
  562.    if (!str)
  563.         str = prompt_history("SEARCH", "File GREP String: ", "", -1, 1, "fgrep_dialog" )
  564.  
  565.    if (!str)
  566.       return;
  567.  
  568.    #  if infileToOpen is specified use it otherwise use 
  569.    #  use the last path that was used.
  570.    listOfFiles = infileToGrep
  571.  
  572.    if ( !listOfFiles )
  573.       listOfFiles = open_dialog( lastGrepPath, "Select files to GREP" )
  574.  
  575.    if ( listOfFiles )
  576.    {
  577.       add_prompt_history("SEARCH", str)
  578.       add_prompt_history("SRCHFILES", infileToGrep)
  579.  
  580.       saveFlags    = search_flags
  581.       search_flags = and( saveFlags, not(SEARCH_BLOCK) )
  582.  
  583.       dlg_title    = "Find All List - File(s) " infileToGrep " ( \"" str "\" )"
  584.  
  585.       if ( recursive )
  586.          arrayOfFiles = get_recursive_dir_list( listOfFiles )
  587.       else
  588.          split( listOfFiles, arrayOfFiles, ";" )
  589.  
  590.       init_fgrep_call( str, dlg_title, arrayOfFiles )
  591.  
  592.       save_foreground_context()
  593.  
  594.       if ( process_background(DISABLE_FOREGROUND, function_id("background_fgrep")) )
  595.          success_str = "Search complete, "
  596.       else
  597.          success_str = "Search interrupted, "
  598.  
  599.       if ( grep_sys_buffer )
  600.          delete_buffer( grep_sys_buffer )
  601.    
  602.       restore_foreground_contexts()
  603.  
  604.       # can't stop once we're done
  605.       set_dialog_item( grep_dhTemp, DI_STOP_BUTTON, DAC_DISABLE )
  606.  
  607.       # but we can close it.
  608.       set_dialog_item( grep_dhTemp, IDB_CLOSE_BTN, DAC_ENABLE )
  609.  
  610.       if ( message_level < 1 )
  611.          message(" ")
  612.  
  613.       # if there is anything in the list, then we've found something
  614.       if ( !(occurrences = query_dialog_item(grep_dhTemp, IDL_LIST, DAC_COUNT_ITEMS)) ) 
  615.       {
  616.          warning( "No matching strings found." )
  617.  
  618.          # if it hasn't already been deleted
  619.          if ( valid_list_dialog( grep_dhTemp ) )
  620.             delete_list_dialog( grep_dhTemp );
  621.       }
  622.       else
  623.          set_dialog_window(   grep_dhTemp, 
  624.                               DWC_STATUSBARTEXT, 
  625.                               success_str occurrences " occurrences found." )
  626.  
  627.       search_flags = saveFlags
  628.    }
  629. }
  630.  
  631. # add all of the subdirectories of dirToProcess to the dirsToProcess array
  632. local function get_dirs( dirToProcess, dirsToProcess )
  633. {
  634.    local spec
  635.    local subdir
  636.    local root_dir
  637.    local path
  638.    local mode
  639.  
  640.    path = dirToProcess
  641.  
  642.    # if its a dir, look in, not at, that dir
  643.    mode = filemode( path )
  644.    if ( mode != -1 && and(mode, _SUBDIR) )
  645.       {
  646.       spec = "*"
  647.       path = path "/" spec
  648.       }
  649.    else
  650.       {
  651.       spec = path_fname( path ) path_ext( path )
  652.       if ( !spec )
  653.          spec = "*"
  654.       }
  655.  
  656.    # don't include starting dir
  657.    #dirsToProcess[ path ] = TRUE
  658.  
  659.    root_dir = path_path( path )
  660.  
  661.    subdir = findfirst( root_dir "*", ONLY_SUBDIR )
  662.  
  663.    while ( subdir )
  664.       {
  665.       if ( subdir != "." && subdir != ".." )
  666.          dirsToProcess[ root_dir subdir "/" spec ] = TRUE
  667.  
  668.       subdir = findnext()
  669.       }
  670. }
  671.  
  672. global function get_recursive_dir_list( infileToGrep )
  673. {
  674.    local paths[]
  675.    local index
  676.    local dirsToProcess[]
  677.    local dirsProcessed[]
  678.    local dir_list[]
  679.    local dir
  680.    local count
  681.    local i
  682.  
  683.    split( infileToGrep, paths, ";" )
  684.  
  685.    # form a list of the directories to recurse through
  686.    for ( index in paths )
  687.       dirsToProcess[ paths[index] ] = TRUE
  688.  
  689.    # process all of the directories, adding each subdirectory
  690.    # to the list of directories to process as we go
  691.    while ( dirsToProcess )
  692.       {
  693.       count = 0
  694.       for ( dir in dirsToProcess )
  695.          {
  696.          count++
  697.          get_dirs( dir, dirsToProcess )
  698.          dirsProcessed[ dir ] = TRUE
  699.          delete dirsToProcess[ dir ]
  700.          }
  701.  
  702.       if ( !count )
  703.          break
  704.       }
  705.  
  706.    # put the result in an array indexed by number
  707.    i = 1
  708.    for ( dir in dirsProcessed )
  709.       dir_list[ i++ ] = dir
  710.  
  711.    return dir_list
  712. }
  713.  
  714.