Find in file output into a variable

Find in file output into a variable

4
NewbieNewbie
4

    Mar 21, 2008#1

    hi,

    i'm wondering if it's some how possible using UltraEdit.frInFiles.find(StrLookFor); to have the result in a var instead of the output window or an active file.

    thanks for your help.

    6,686585
    Grand MasterGrand Master
    6,686585

      Mar 21, 2008#2

      No, not directly. But you can copy the entire output window or edit window content into a clipboard, open a temporary new ASCII file (not Unicode!), paste it there, select it and use the command to get the current selection into a variable.
      Best regards from an UC/UE/UES for Windows user from Austria

      4
      NewbieNewbie
      4

        Mar 25, 2008#3

        Thanks for the answer mofi.

        Here is a script i'm trying to make up. It's supposed to look for function definition in a working directory full of source files.
        Like you are writing your code and use a function defined in an other file but exported and you need to see the code of this function.( to refresh your memory :))
        You just click on the function named where it's called in your code and this script open the file that has your function code in it.
        (i've used your script GetListOfFiles),

        This script almost works, but i'm having 2 problems :
        - sometimes the function gotoline doesn't work properly. Like i found a function definition in file, but when i tried to find the definition of a 2nd function the scripts just doesn't got to the line where it's supposed to go, though it has the right line saved in the var lineFunction.
        - 2nd point is that i doesn't really like the fact that i have a small tab open to store my list of results from the find in files but i guess i can't make it be " unseen" just using UE scripting.

        If anybody would like to give me some tips or improvement i'll be thankfull.

        Code: Select all

        Principal();
        
        function Principal()
        {
        	//This script requires UltraEdit v13.10 or UEStudio v6.30 or any later.
        		//var init
        		var findStr;
        
        		//Clear the output window, make it visible and disable status information.
        		UltraEdit.outputWindow.showStatus=false;
        		UltraEdit.outputWindow.clear();
        		if (UltraEdit.outputWindow.visible == false) {
          			UltraEdit.outputWindow.showWindow(true);
        		}
        
        		//Get the line where the cursor is.
        		UltraEdit.activeDocument.startSelect(); 
        		UltraEdit.activeDocument.key("END"); 
        		var findStr = UltraEdit.activeDocument.selection;
        		UltraEdit.activeDocument.endSelect(); 
        		// Remember current regular expression engine.
        		var RegexEngine = UltraEdit.regexMode;
        		/* A regular expression engine must be defined or the find
        		for the last line in the Unicode results could fail. */
        		UltraEdit.ueReOn();
        			
        		findStr = findStr.replace(/\(.*\)/,"");
        		findStr = findStr.replace(/ .*/,"");
        		findStr = findStr.replace(/;/,"");
        		findStr = findStr.replace(/\,/,"");
        		findStr = "function " + findStr + "(";
        			
        		switch (RegexEngine) {   // Restore original regular expression engine.
              			case 1:  UltraEdit.unixReOn(); break;
              			case 2:  UltraEdit.perlReOn(); break;
              			default: UltraEdit.ueReOn();   break;
           }
        var TxtDir = "";	
        		GetListOfFiles(0, TxtDir, "*.psl","",findStr);		
        		// selection into var for last active tab
        		UltraEdit.activeDocument.top();
        		UltraEdit.activeDocument.key("DOWN ARROW");
         		UltraEdit.activeDocument.key("DOWN ARROW");
        		UltraEdit.activeDocument.startSelect();
        		UltraEdit.activeDocument.key("END");
            UltraEdit.activeDocument.copy();
        var wrkTimestampText = UltraEdit.activeDocument.selection;
        		 UltraEdit.activeDocument.endSelect(); 												
        var pslFilePath = wrkTimestampText.replace(/\(.*\)/,"");					
        var lineFunction = wrkTimestampText.replace(/\).*/,"");									
        lineFunction = lineFunction.replace(/.*\(/,"");
        
        ResultsDocTitle = "** Find Results ** " ; 	
        	for (var i = 0; i < UltraEdit.document.length; i++)
              {
                 if (UltraEdit.document[i].path == ResultsDocTitle) {
                    UltraEdit.closeFile(UltraEdit.document[i].path,2);
                    break;
                 }
        		}
        
        //lineFunction =  parseInt( lineFunction );
          lineFunction =  parseInt( lineFunction );
        //UltraEdit.outputWindow.write(lineFunction);
        	UltraEdit.open(pslFilePath);
        	UltraEdit.activeDocument.top();
        	UltraEdit.activeDocument.gotoLine(lineFunction, 0);
        	
         } 
          
        /*** GetListOfFiles *******************************************************/
        This function is copyright by Mofi for free usage by UE/UES users. The
        author cannot be responsible for any damage caused by this script. You
        use it at your own risk.
        
        function GetListOfFiles (FileList, Directory, FileType, SubDirs , StrLookFor) {
           var SummaryInfo = "Search complete, found";
           var ResultsDocTitle = "** Find Results ** ";  // Note the space at end!
           var OutputType = (typeof(DebugMessage) == "number") ? DebugMessage : 2;
        
           if (typeof(FileList) != "value" || FileList < 0 || FileList > 4) FileList = 0;
        
           if (FileList == 0) {  // Search in a specified directory?
              // If no directory specified, use current working directory.
              if (typeof(Directory) != "string" || Directory == "" ) Directory = ".\\";
              // Append a backslash if it is missing at end of the directory string.
              else if (Directory.match(/\\$/) == null) Directory += "\\";
              // Search for all files if no file type is specified.
              if (typeof(FileType) != "string" || FileType == "") FileType = "*";
              if (typeof(SubDirs) != "boolean") SubDirs = false;
           } else {
              Directory = "";    // For the list of open, favorite, project
              FileType = "";     // or solution files the other 3 parameters
              SubDirs = false;   // have always the same default values.
           }
        
           // Remember current regular expression engine.
           var RegexEngine = UltraEdit.regexMode;
           /* A regular expression engine must be defined or the find
              for the last line in the Unicode results could fail. */
           UltraEdit.ueReOn();
        
           /* Run a Find In Files with an empty search string to get the
              list of files stored in the specified directory in an edit
              window and delete the last line with the summary info. */
           UltraEdit.frInFiles.directoryStart=Directory;
           UltraEdit.frInFiles.filesToSearch=FileList;
           UltraEdit.frInFiles.matchCase=false;
           UltraEdit.frInFiles.matchWord=false;
           UltraEdit.frInFiles.regExp=true;
           UltraEdit.frInFiles.searchInFilesTypes=FileType;
           UltraEdit.frInFiles.searchSubs=SubDirs;
           UltraEdit.frInFiles.unicodeSearch=false;
           UltraEdit.frInFiles.useOutputWindow=false;
        	 UltraEdit.frInFiles.find(StrLookFor);
        
        //UltraEdit.frInFiles.find("*");
        //   var ListCreated = false;
           if (UltraEdit.activeDocument.path == ResultsDocTitle) ListCreated = true;
           else {
              for (var i = 0; i < UltraEdit.document.length; i++)
              {
                 if (UltraEdit.document[i].path == ResultsDocTitle) {
                    UltraEdit.document[i].setActive();
                    ListCreated = true;
                    break;
                 }
              }
           }
           if (ListCreated == true) {
              // Search for the summary info at bottom of the results.
              UltraEdit.activeDocument.findReplace.searchDown=false;
              UltraEdit.activeDocument.findReplace.matchCase=true;
              UltraEdit.activeDocument.findReplace.matchWord=false;
              UltraEdit.activeDocument.findReplace.regExp=false;
        		  UltraEdit.activeDocument.findReplace.find(SummaryInfo);
              ListCreated = UltraEdit.activeDocument.isFound();
           }
           UltraEdit.activeDocument.findReplace.searchDown=true;
           switch (RegexEngine) {   // Restore original regular expression engine.
              case 1:  UltraEdit.unixReOn(); break;
              case 2:  UltraEdit.perlReOn(); break;
              default: UltraEdit.ueReOn();   break;
           }
           /* Check now if the Find above has had success finding the last line in
              the active document which should contain the Find In Files results. */
           if (ListCreated == false) {
              if (OutputType == 2) {
                 UltraEdit.messageBox("There is a problem with frInFiles command or one of the strings in the variables\n\"SummaryInfo\" or \"ResultsDocTitle\" is not adapted to your version of UE/UES!","GetListOfFiles Error");
              } else if (OutputType == 1) {
                 if (UltraEdit.outputWindow.visible == false) UltraEdit.outputWindow.showWindow(true);
                 UltraEdit.outputWindow.write("GetListOfFiles: There is a problem with frInFiles command or one of the strings in the variables");
                 UltraEdit.outputWindow.write("                \"SummaryInfo\" or \"ResultsDocTitle\" is not adapted to your version of UE/UES!");
              }
              return false;
           }
            UltraEdit.activeDocument.deleteLine();
           UltraEdit.activeDocument.top();
           UltraEdit.activeDocument.key("RIGHT ARROW");
           if (UltraEdit.activeDocument.currentPos > 1) UltraEdit.activeDocument.unicodeToASCII();
           else UltraEdit.activeDocument.top();
        
           // If top of file is also end of file, no files were found.
           if (UltraEdit.activeDocument.isEof()) {
              if (OutputType == 2) {
                 UltraEdit.messageBox("No file "+FileType+" was found in directory\n\n"+Directory,"GetListOfFiles Error");
              } else if (OutputType == 1) {
                 if (UltraEdit.outputWindow.visible == false) UltraEdit.outputWindow.showWindow(true);
                 UltraEdit.outputWindow.write("GetListOfFiles: No file "+FileType+" was found in directory "+Directory);
              }
              UltraEdit.closeFile(UltraEdit.activeDocument.path,2);
              return false;
           }
           
           return true;
        }

        6,686585
        Grand MasterGrand Master
        6,686585

          Mar 25, 2008#4

          Hm, why do you write a script for something which can be already done without a script?

          With the function list open in UltraEdit you can right click on the function list and switch the display of functions from current file to all files in a project. You only have to set up a project or workspace which is not very difficult. Also with a project you can use Ctags to create the symbol database. Ctags supports not only function names, it supports also other symbols like global variables and constants, defines, etc. And Ctags can be also highly customized - you can define your own search strings for the symbols. With a Ctags database you can use "Find Symbol" to go to the definition of a symbol.

          If I have understand your description correct your script is a replacement for the built-in function list support and a sub function of what Ctags offers for users which use it.
          Best regards from an UC/UE/UES for Windows user from Austria

          4
          NewbieNewbie
          4

            Mar 25, 2008#5

            well, i got like 270/280 function in my project and using the function listing won't be ergonomic.
            So, having some script allowing me just to select the function in a text and get the right file open on the right line would be so much nice.

            I've tried Ctags, but i always get the couldn't find TAG and code is written a language that is not defined in UE.

            Thx

            6,686585
            Grand MasterGrand Master
            6,686585

              Mar 26, 2008#6

              UltraEdit is a general text editor. No language is supported directly. Every language supported by default is supported because of various default definitions which can be modified by the users. The same is true for Ctags. It supports various languages by predefined built-in settings. But Ctags can be customized to support every language.

              However, back to your script. After a quick look on the lines

              UltraEdit.open(pslFilePath);
              UltraEdit.activeDocument.top();
              UltraEdit.activeDocument.gotoLine(lineFunction, 0);

              I have following suggestions:

              1. Delete the useless line with command top().
              2. Use a loop to search in the array of open documents if the file you want to open is not already open.

              If the file is already open, use the setActive() command instead of the open command. Maybe with those changes your problem with gotoLine not executed correct is solved.
              Best regards from an UC/UE/UES for Windows user from Austria

              4
              NewbieNewbie
              4

                Apr 02, 2008#7

                Thanks for your advice.
                The scripts is almost finished.
                I'l post it when it's done 100%.

                16
                Basic UserBasic User
                16

                  Apr 17, 2008#8

                  I would very much be interested in your script if you finish it. It's one of the main features I miss from Eclipse. The list 'for all Project files' options is no solution for me (at all). Different from mofi I do find that setting up a project/workspace is a annoying thing to do, especially because you can't use filters to filter out folders you do not want to use (SVN f.e.), and because the CTAG's option is totally unclear for me and I never got it to work. But most importantly because most of my projects are webbased, meaning a mixture of CSS, JS, PHP, html and all kind of other types of files. For PHP and JS I generate the function names, resulting in a looong list of functions, for html files I list important tags and for CSS pretty much every style in the file. So way to many functions. For my current project it takes more than 3 minutes to build it up even. So if you get this working to your satisfaction it would be great if you posted it.