-- Copyright 1996-2000 Robelle Solutions Technology Inc. -- Version 1.02 September 7, 2000 name QSLUtilListAll group "Ro&belle" command "&List All"; property QSLScriptVersion = "ListAll 1.02 - Copyright Robelle Solutions Technology 2000"; -- This script allows a user to search for strings, regexp or patterns in one or more -- files. This script uses the FindAll document method. -- -- Script menu commands are created allowing the search to occur -- recursively (optional) for a particular string. The options are: -- (a) the current file only -- (b) the current file and include files. -- Include statements are $include, !include, #include or .include files -- -- The script has entry points implemented as On Command blocks: -- -- String: search current file with a simple string -- Regexp: search current file with a regular expression -- Pattern: search current file with a pattern -- About: displays a short text in the Script Control Panel explaining basic -- operations -- -- -- Convert a number to a right-justified string. -- The maximum output length is specified in the MaxLength parameter. -- Default is 9. -- If the value contains implied decimals, specify the number of decimals -- with the NumDecimals parameter. Default is 0. -- If NumDecimals is not 0, the values are aligned on the decimal point. -- sub RightJustify (InputNumber, MaxLength, NumDecimals); if typeof(MaxLength) = qedit.typeundefined then MaxLength = 9; endif if typeof(NumDecimals) = qedit.typeundefined then NumDecimals = 0; endif if NumDecimals >= MaxLength then error ("(RIGHTJUSTIFY) The number of decimals must be less than the maximum length"); endif blanks = " "; resultString = blanks; if NumDecimals > 0 then InputNumber = InputNumber / (10**NumDecimals); endif tempString = string(InputNumber); decimals = pos(tempString, "."); -- Zero is no decimal point found if decimals > 0 then decimals = length(tempString) - decimals; endif if decimals < NumDecimals then -- Fill in with enough trailing spaces after decimals tempString = tempString + blanks[1:(NumDecimals - decimals)]; if decimals = 0 then -- Must fill in for the missing decimal point too tempString = tempString + " "; endif endif resultString[(maxLength + 1) - length(tempString)] = tempString; return resultString; endsub -- -- Display script banner -- sub DisplayScriptInfo(FileH, IncludeFile, SearchString, SearchType, OutputFile); writelog QSLScriptVersion; if SearchType = 1 then searchDescription = "STRING"; else if SearchType = 2 then searchDescription = "REGEXP"; else searchDescription = "PATTERN"; endif endif descriptionLine = {}; if SearchString <> "" then descriptionLine = descriptionLine + ("Searching " + fileH.Title + " for " + searchDescription + " '" + SearchString +"'"); else descriptionLine = descriptionLine + ("Listing all Include filenames"); endif if IncludeFile = "" then descriptionLine = descriptionLine + "Searching CURRENT file only!"; else descriptionLine = descriptionLine + (IncludeFile + " files ARE searched!"); endif if FileH.IsOnHost then descriptionLine = descriptionLine + "Actual line numbers are displayed"; else descriptionLine = descriptionLine + "Relative line numbers are displayed"; endif descriptionLine = descriptionLine + ""; OutputFile.insert(descriptionLine); repeat for msgLine in descriptionLine writelog(msgLine); endrepeat endsub -- -- Use the selection as the search string. If there is no selection, -- prompt the user for one -- sub GetSearchString(FileH, SearchType) ; searchStr = ""; If length(FileH.selection) = 2 then -- Some text selected theSelection = FileH.selection; startOnLine = theSelection.start.line; endOnLine = theSelection.end.line; if startOnLine = endOnLine then -- All on the same line searchStr = FileH.getselectedtext()[1]; endif endif if SearchType = 1 then searchDescription = "STRING"; else if SearchType = 2 then searchDescription = "REGEXP"; else searchDescription = "PATTERN"; endif endif promptMsg = "Enter a " + searchDescription + " to search for. Clear the field to get a list of Include filenames."; if SearchType <> 1 or length(searchStr) = 0 then r = dialog( promptMsg, 1, searchStr ); okToGoOn = r.button ; if okToGoOn = 1 then searchStr = r.enteredText; else -- Cancel button stop; endif ; endif ; Return searchStr ; endsub ; sub PrintFileHeader(FileH, ResultFilename, ResultLines, OutputFile); if ResultFilename = "" then fileHeader = FileH.Title; else if FileH.IsOnHost then fileHeader = FileH.connectionName + ":"; else fileHeader = ""; endif fileHeader = fileHeader + ResultFilename; endif fileHeader = fileHeader + ": " + string(length(ResultLines)) + " line(s) found **"; OutputFile.Insert(fileHeader); OutputFile.Insert({"",""}); -- Skip to next line endsub -- -- Start processing the search request for the $Include searches -- Receives a value for the type of search requested. -- sub ListAll(FileH, IncludeOption, SearchString, SearchType, OutputFile) startTime = datetime(); logMessage = {}; logMessage = logMessage + ("Started at " + startTime.fmtshortdatetime()); logMessage = logMessage + "" + ""; -- Two blank lines OutputFile.Insert(logMessage); writelog(logMessage[1]); -- Only text so it does not appear in braces if typeof(OutputFile) = qedit.typeundefined then OutputFile = newfile(); endif if SearchType = 1 then findresult = fileH.FindAll(string: SearchString, searchreferenced: IncludeOption, ignorecase: true); else if SearchType = 2 then findresult = fileH.FindAll(regexp: SearchString, searchreferenced: IncludeOption, ignorecase: true); else -- Pattern match findresult = fileH.FindAll(pattern: SearchString, searchreferenced: IncludeOption, ignorecase: true); endif endif fileErrorFlag = false; repeat for fileResult in findresult if fileResult.ErrorCode <> 0 then writelog ("Unable to open " + fileResult.Filename); writelog ("Error # " + string(fileResult.ErrorCode)); writelog ("Message:" + fileResult.ErrorString); fileErrorFlag = true; else if length(fileResult.Lines) <> 0 then PrintFileHeader(FileH, fileResult.Filename, fileResult.Lines, OutputFile); repeat for lineDescriptor in fileResult.Lines foundLine = ""; if fileH.IsOnHost then -- Display absolute line number foundLine = foundLine + RightJustify(lineDescriptor.LineNumber, 9, 3); else -- Display relative line number foundLine = RightJustify(lineDescriptor.Line, 8); endif foundLine = foundLine + ": " + lineDescriptor.text; OutputFile.Insert(foundLine); OutputFile.Insert({"",""}); -- Skip to next line endrepeat else writelog ("No lines found in " + fileResult.Filename); endif endif endrepeat endTime = datetime(); logMessage = {}; logMessage = logMessage + "" + ("Completed at " + endTime.fmtshortdatetime()); writelog(logMessage[2]); -- Only text so it does not appear in braces if fileErrorFlag then logMessage = logMessage + "" + "NOTE: Some files could not be opened." + " Use Script | Control Panel to see the error messages."; endif logMessage = logMessage + ""; OutputFile.Insert(logMessage); endsub ; -- -- Interpret command line entered from the Script menu -- and invoke the appropriate procedure. -- sub ProcessListRequest (SearchType, IncludeFile) if typeof(SearchType) <> qedit.typefloat or SearchType < 1 or SearchType > 3 then result = dialog("SearchType must be one of: 1 (String), 2 (Regexp) or 3 (Pattern)"); stop; endif if typeof(IncludeFile) = qedit.typeundefined then IncludeFile = ""; endif fileH = qedit.activefile; if not exists(fileH) then result = dialog("Please open your desired document before attempting a search,"); else saveFilename = string(fileH.Title) ; searchString = GetSearchString(fileH, SearchType) ; outputFile = newfile(); DisplayScriptInfo(fileH, IncludeFile, searchString, SearchType, outputFile); if IncludeFile = "" then includeOption = 0; else if IncludeFile = "Include" then includeOption = 1; else if IncludeFile = "Use" then includeOption = 2; else if IncludeFile = "Copy" then includeOption = 3; else result = dialog("IncludeOption must be blank, 'Include', 'Use' or 'Copy'"); stop; endif endif endif endif ListAll(fileH, includeOption, searchString, SearchType, outputFile); endif if IncludeFile = "" then writelog ("ListAll is DONE!"); else writelog ("List" + IncludeFile + " is DONE!"); endif endsub on command "&String" ProcessListRequest(1, ""); endon on command "&Regexp" ProcessListRequest(2, ""); endon on command "&Pattern" ProcessListRequest(3, ""); endon on command "About..." qedit.showscp = true; writelog QSLScriptVersion ; writelog " " ; writelog "This script will perform a search on the current file. "; writelog "All lines found to contain the search string will be "; writelog "displayed in a new local file."; writelog " " ; writelog "ListAll will use selected text, if any, as the search string."; writelog "If no text is highlighted, then ListAll will prompt you for"; writelog "a search string." ; writelog " "; endon ;