home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 5 Edit
/
05-Edit.zip
/
p2demo21.exe
/
PEL
/
ROUTINE.PEL
< prev
next >
Wrap
Text File
|
1995-03-22
|
20KB
|
714 lines
# $Header: P:\source\wmacros\routine.pev 1.58 22 Mar 1995 13:20:10 PFHDWM0 $
## $Tabs:4 7$
##############################################################################
#
# Compuware Corporation
# 31440 Northwestern Highway
# Farmington Hills, Michigan 48334-2564
#
# This source code listing contains information that is
# proprietary to Compuware Corporation and may not be copied
# duplicated, translated, transmitted, stored, retrieved
# or in any manner or by any method conveyed or disclosed
# to a third party or parties without express written
# permission from Compuware Corporation.
#
#
##############################################################################
#### $Workfile: routine.pel $: function name and string collection
## routines()
#
# Create a string of all the functions in the current edit buffer.
# This routines assumes the following format of each function declaration
#
# For C files:
# [<type>] <function_name> ( [<parameters>] )
#
# For PEL files
# [global|local] function <function_name> ( [<parameters>] ) [{]
#
local dhTemp
global function routines(allBuffers)
{
local loc, fun_name, dlg_title, sentinel, temp
local sflags = SEARCH_FORWARD+SEARCH_REGEX+SEARCH_MAXIMAL_MATCH
local ext = tolower(path_ext( buffer_filename ));
local found_routine = 0
local cursor_loc, srchstring
if ( allBuffers )
dlg_title = "Global Routines List"
else
dlg_title = "Find Routines - " buffer_name
dhTemp = create_list_dialog( dlg_title, editor_helpfile )
# initialize dBase filename extension list
if(!templates)
{
warning("Extensions not initialized!! Quitting!!" )
return FALSE
}
sentinel = current_buffer
process_background( DISABLE_FOREGROUND )
save_position();
do
{
if (!(and(buffer_flags, BUFFER_SYSTEM)))
{
# check for known filename extension
temp = Template(ext);
if ( !( (temp == "c") \
|| (temp == "basic") \
|| (temp == "awk") \
|| (temp == "pascal") \
|| (temp == "cobol") \
|| (temp == "dbase") \
|| (temp == "clipper87") \
|| (temp == "clipper50") \
|| (temp == "force") ))
{
message("Invalid extension for routines.");
delete_list_dialog(dhTemp);
process_background( END_BACKGROUND )
return;
}
goto_buffer_top();
srchstring = "^[a-zA-Z0-9_\\-]"
if(temp == "c")
{
found_routine = optional_function( "do_c_routines", dhTemp, allBuffers)
break;
}
else if(temp == "cobol")
{
found_routine = optional_function( "do_cobol_routines", dhTemp, allBuffers)
break;
}
else
{
while (search( srchstring, sflags))
{
loc = 0;
goto_bol()
fun_name = ltrim(trim( read_buffer() ));
if (ext == ".pel" || ext == ".awk")
{
if (index( fun_name, "function" ))
{
if (match(fun_name, "function[ \t]" ))
{
# remove the words "global|local function" from the front
# of the found function name
fun_name = substr(fun_name, RSTART+RLENGTH)
loc = index( fun_name, "(" );
}
}
}
else if(temp == "pascal")
{
if ( index(fun_name,"function") \
|| index(fun_name,"procedure") \
|| index(fun_name,"FUNCTION") \
|| index(fun_name,"PROCEDURE"))
{
loc = index(fun_name,"(")
if ( loc == 0 && loc ~ / / )
{
loc = length( fun_name ) + 1
}
}
}
else if(temp == "basic")
{
if ( index(fun_name,"FUNCTION"))
{
if ( fun_name ~ /FUNCTION[ \t]/)
{
loc = index(fun_name,"(")
}
}
}
else if( (temp == "dbase") \
|| (temp == "clipper87") \
|| (temp == "clipper50") \
|| (temp == "force"))
{
if ( index(fun_name,"function") \
|| index(fun_name,"procedure") \
|| index(fun_name,"FUNCTION") \
|| index(fun_name,"PROCEDURE"))
{
if ( substr(fun_name,length(fun_name),1) == ")" )
loc = index(fun_name,"(")
else
loc = length(fun_name)+1
}
}
found_routine += add_routine( loc, fun_name, dhTemp, allBuffers )
sflags = or( sflags, SEARCH_ADVANCE );
}
}
}
} while ( allBuffers && next_buffer("", 1, 1) != sentinel)
restore_position(1);
process_background( END_BACKGROUND )
# reset the focus to the current buffer
current_buffer = sentinel
if (found_routine)
{
begin_list_dialog(dhTemp)
message("Search completed.");
}
else
{
delete_list_dialog(dhTemp);
message("No routines found.");
}
}
global function add_routine( loc, fun_name, dhTemp, allBuffers )
{
local retval = 0
local i
local t = 100
local x = 100
if ( loc )
{
fun_name = current_line " : " trim( substr(fun_name, 1, loc-1) );
if ( allBuffers )
fun_name = buffer_filename " : " fun_name
add_item_to_list( dhTemp, fun_name, \
buffer_filename, current_line, 0, -1 )
retval = 1
}
# just display message enough to let user know something is going
# on.
i = current_line * 100 / buffer_last_line
t = i % 5
if ( t == 0 && x != i )
{
message("Searching file %s...( %d %%)", buffer_filename, i)
x = i
}
return retval
}
global function wroutines()
{
routines(1)
}
local function init_grep_call( str, numSearchCount, allBuffers )
{
grep_numSearchCount = numSearchCount
grep_str = str
grep_allBuffers = allBuffers
if ( allBuffers )
grep_dlg_title = "Find All List - All Buffers ( \"" str "\" )"
else if ( and(search_flags, SEARCH_BLOCK) && selection_type() )
grep_dlg_title = "Find All List - Selected block:" buffer_name "( \"" str "\" )"
else
grep_dlg_title = "Find All List - " buffer_name "( \"" str "\" )"
}
global function background_grep()
{
local sentinel
if ( and(buffer_flags, BUFFER_SYSTEM) )
sentinel = next_buffer("", 0, 1) # skip system buffers
else
sentinel = current_buffer
grep_dhTemp = create_list_dialog( grep_dlg_title, editor_helpfile, "grep_list_callback" )
# disable close while searching
set_dialog_item( grep_dhTemp, IDB_CLOSE_BTN, DAC_DISABLE )
begin_dialog( grep_dhTemp )
do
{
grep_found_match = grep_found_match + perform_grep( grep_str,
grep_dhTemp,
grep_numSearchCount,
grep_allBuffers )
} while ( grep_allBuffers && next_buffer("", 0, 1) != sentinel)
}
## grep()
#
# given a string and an optional number or string matches, search for all
# occurrences of the string in the current buffer and display a list of the
# strings in a menu.
#
global function grep( str, numSearchCount, allBuffers)
{
local dlg_title
local sentinel
local found_string = 0
local grep_string = ""
local occurrences
local success_str
init_grep()
if ( !str )
str = prompt_history( "SEARCH", "GREP String: ", "", -1, 1, "grep_dialog" );
if ( !str )
return;
add_prompt_history( "SEARCH", str );
numSearchCount += 0; # convert string to int
if ( numSearchCount <= 0 )
numSearchCount = MAX_SEARCH
grep_string = str
init_grep_call( str, numSearchCount, allBuffers )
save_foreground_context()
if ( process_background(DISABLE_FOREGROUND, function_id("background_grep")) )
success_str = "Search complete, "
else
{
success_str = "Search interrupted, "
restore_position( TRUE )
}
restore_foreground_contexts()
# can't stop once we're done
set_dialog_item( grep_dhTemp, DI_STOP_BUTTON, DAC_DISABLE )
# but we can close it
set_dialog_item( grep_dhTemp, IDB_CLOSE_BTN, DAC_ENABLE )
# if there is anything in the list, then we've found something
if ( !(occurrences = query_dialog_item(grep_dhTemp, IDL_LIST, DAC_COUNT_ITEMS)) )
{
grep_string = ""
warning( "No matching strings found." )
# if it hasn't already been deleted
if ( valid_list_dialog( grep_dhTemp ) )
delete_list_dialog( grep_dhTemp )
}
else
set_dialog_window( grep_dhTemp,
DWC_STATUSBARTEXT,
success_str occurrences " occurrences found." )
return grep_found_match
}
local foreground_context[]
global function save_foreground_context()
{
foreground_context[ current_buffer ].first = window_first
foreground_context[ current_buffer ].line = current_line
foreground_context[ current_buffer ].buffer = current_buffer
}
local function restore_foreground_contexts()
{
local old_cur_buf = current_buffer
local buffer
for ( buffer in foreground_context )
{
current_buffer = foreground_context[ buffer ].buffer
current_line = foreground_context[ buffer ].first
scroll_window_top()
current_line = foreground_context[ buffer ].line
}
delete foreground_context
current_buffer = old_cur_buf
}
local function perform_grep(str, dhTemp, numCount, displayFilename)
{
local current_column
local current_data
local sflags = or(search_flags, SEARCH_ONCE_PER_LINE+SEARCH_FORWARD)
local found_string = 0
# turn off wrap search so that we only search the file once
sflags = and(sflags, not(SEARCH_WRAPS) )
# turn off advance so that we can find the string if it starts at 1,1
sflags = and(sflags, not(SEARCH_ADVANCE) )
save_position();
if (and(search_flags, SEARCH_BLOCK) && selection_type())
goto_mark(selection_mark_top())
else
goto_buffer_top();
while ( numCount-- && search( str, sflags) )
{
found_string = 1
# position cursor at beginning of line so that read_buffer gets the
# whole line
#
goto_bol()
if ( displayFilename )
current_data = sprintf( "%s - %5d : %s", buffer_filename, current_line, read_buffer(200) )
else
current_data = sprintf( "%5d : %s", current_line, read_buffer(200) )
add_item_to_list( dhTemp, current_data, \
buffer_filename, current_line, 0, -1)
# kludge since search once per line doesn't work
goto_eol()
# turn advance back on so we find next string
sflags = or( sflags, SEARCH_ADVANCE )
set_dialog_window( dhTemp,
DWC_STATUSBARTEXT,
sprintf( "Searching file %s...(%d%%)",
buffer_filename,
current_line * 100 / buffer_last_line) );
} # end while
restore_position(1);
return found_string
}
global function grep_auto( numSearchCount, allBuffers )
{
grep( symbol_under_cursor(), numSearchCount, allBuffers )
}
function wgrep( str, numSearchCount)
{
local saveFlags
if (!str)
str = prompt_history("SEARCH", "All buffers GREP String: ", "", -1, 1, "wgrep_dialog" );
if (!str)
return;
saveFlags = search_flags
search_flags = and(saveFlags, not(SEARCH_BLOCK))
grep( str, numSearchCount, 1)
search_flags = saveFlags
}
global END_BACKGROUND = 0
global ENABLE_FOREGROUND = 1
global DISABLE_FOREGROUND = 2
global STOP_BACKGROUND = 3
global IN_BACKGROUND = 4
global BACKGROUND_FOREGROUND_SUSPEND = 5
global BACKGROUND_FOREGROUND_RESUME = 6
local lastGrepPath = "*.*"
# used by fgrep
local grep_sys_buffer = 0
local grep_arrayOfFiles[]
# used by grep
local grep_numSearchCount
local grep_allBuffers
# shared by fgrep, grep
local grep_dhTemp
local grep_dlg_title
local grep_found_match
local grep_str
local function init_grep()
{
grep_sys_buffer = 0
grep_found_match = FALSE
}
local function init_fgrep_call( str, dlg_title, arrayOfFiles )
{
grep_str = str
grep_dlg_title = dlg_title
grep_arrayOfFiles = arrayOfFiles
}
global function background_fgrep()
{
local i
local fn
local fileToOpen
# local orig_buffer = current_buffer
local file_list[]
grep_dhTemp = create_list_dialog( grep_dlg_title, editor_helpfile, "grep_list_callback" )
# disable close button while searching
set_dialog_item( grep_dhTemp, IDB_CLOSE_BTN, DAC_DISABLE )
begin_list_dialog( grep_dhTemp )
for ( i in grep_arrayOfFiles )
{
fileToOpen = grep_arrayOfFiles[i]
# Read the files into an array first. This section shouldn't be
# interrupted by a foreground findfirst, so suspend foreground
# processing for the duration.
process_background( BACKGROUND_FOREGROUND_SUSPEND )
fn = findfirst( fileToOpen )
while ( fn )
{
file_list[ fn ] = TRUE
fn = findnext()
}
process_background( BACKGROUND_FOREGROUND_RESUME )
for ( fn in file_list )
{
fn = tolower( buildpath(path_path(fileToOpen) fn) )
set_dialog_window( grep_dhTemp, DWC_STATUSBARTEXT, "Searching file " fn "..." )
# open the buffer as a system buffer
if ( filemode( fn ) == -1 )
continue
grep_sys_buffer = current_buffer = create_buffer( fn,
fn,
BUFFER_SYSTEM +
BUFFER_NO_UNDO )
grep_found_match = grep_found_match + perform_grep( grep_str,
grep_dhTemp,
MAX_SEARCH,
1 )
# current_buffer = orig_buffer
delete_buffer(grep_sys_buffer)
grep_sys_buffer = 0
}
# clear list for next subdir
delete file_list
# parse the drive:directory from the filename and store in
# last path.
#
lastGrepPath = path_path(fileToOpen) "*.*"
}
}
global function fgrep(str, infileToGrep, recursive)
{
local ret
local saveFlags
local dlg_title
local InitialPath
local listOfFiles
local arrayOfFiles
local occurrences
local success_str
init_grep()
if (!str)
str = prompt_history("SEARCH", "File GREP String: ", "", -1, 1, "fgrep_dialog" )
if (!str)
return;
# if infileToOpen is specified use it otherwise use
# use the last path that was used.
listOfFiles = infileToGrep
if ( !listOfFiles )
listOfFiles = open_dialog( lastGrepPath, "Select files to GREP" )
if ( listOfFiles )
{
add_prompt_history("SEARCH", str)
add_prompt_history("SRCHFILES", infileToGrep)
saveFlags = search_flags
search_flags = and( saveFlags, not(SEARCH_BLOCK) )
dlg_title = "Find All List - File(s) " infileToGrep " ( \"" str "\" )"
if ( recursive )
arrayOfFiles = get_recursive_dir_list( listOfFiles )
else
split( listOfFiles, arrayOfFiles, ";" )
init_fgrep_call( str, dlg_title, arrayOfFiles )
save_foreground_context()
if ( process_background(DISABLE_FOREGROUND, function_id("background_fgrep")) )
success_str = "Search complete, "
else
success_str = "Search interrupted, "
if ( grep_sys_buffer )
delete_buffer( grep_sys_buffer )
restore_foreground_contexts()
# can't stop once we're done
set_dialog_item( grep_dhTemp, DI_STOP_BUTTON, DAC_DISABLE )
# but we can close it.
set_dialog_item( grep_dhTemp, IDB_CLOSE_BTN, DAC_ENABLE )
if ( message_level < 1 )
message(" ")
# if there is anything in the list, then we've found something
if ( !(occurrences = query_dialog_item(grep_dhTemp, IDL_LIST, DAC_COUNT_ITEMS)) )
{
warning( "No matching strings found." )
# if it hasn't already been deleted
if ( valid_list_dialog( grep_dhTemp ) )
delete_list_dialog( grep_dhTemp );
}
else
set_dialog_window( grep_dhTemp,
DWC_STATUSBARTEXT,
success_str occurrences " occurrences found." )
search_flags = saveFlags
}
}
# add all of the subdirectories of dirToProcess to the dirsToProcess array
local function get_dirs( dirToProcess, dirsToProcess )
{
local spec
local subdir
local root_dir
local path
local mode
path = dirToProcess
# if its a dir, look in, not at, that dir
mode = filemode( path )
if ( mode != -1 && and(mode, _SUBDIR) )
{
spec = "*"
path = path "/" spec
}
else
{
spec = path_fname( path ) path_ext( path )
if ( !spec )
spec = "*"
}
# don't include starting dir
#dirsToProcess[ path ] = TRUE
root_dir = path_path( path )
subdir = findfirst( root_dir "*", ONLY_SUBDIR )
while ( subdir )
{
if ( subdir != "." && subdir != ".." )
dirsToProcess[ root_dir subdir "/" spec ] = TRUE
subdir = findnext()
}
}
global function get_recursive_dir_list( infileToGrep )
{
local paths[]
local index
local dirsToProcess[]
local dirsProcessed[]
local dir_list[]
local dir
local count
local i
split( infileToGrep, paths, ";" )
# form a list of the directories to recurse through
for ( index in paths )
dirsToProcess[ paths[index] ] = TRUE
# process all of the directories, adding each subdirectory
# to the list of directories to process as we go
while ( dirsToProcess )
{
count = 0
for ( dir in dirsToProcess )
{
count++
get_dirs( dir, dirsToProcess )
dirsProcessed[ dir ] = TRUE
delete dirsToProcess[ dir ]
}
if ( !count )
break
}
# put the result in an array indexed by number
i = 1
for ( dir in dirsProcessed )
dir_list[ i++ ] = dir
return dir_list
}