Script to ask for number and find Number and Number +- 10

Script to ask for number and find Number and Number +- 10

19
Basic UserBasic User
19

    Oct 01, 2010#1

    I thought I'd ask, since I have not played with UE scripts yet. In my case I am learning canvas, and doing a project where the file is 2,000 lines with tons of things like
    ctx.moveTo(253,223);
    ctx.lineTo(244,232);
    ctx.lineTo(229,215);
    ctx.lineTo(234,205);

    Using a mouse to track pixels with a regular mouse, well, its easy to be off by a few. So to find something in 2000 lines where values for
    x,y range from 0 - 600 can be problematic when you are drawing over 500 paths. I trying to replicate a piece of modern art (Kandinsky) with canvas. If you have something, please post it.
    Whenever I have to decide between two evils, I always choose the one I haven't tried before. -Mae West

    6,686585
    Grand MasterGrand Master
    6,686585

      Oct 02, 2010#2

      Here is the commented script according to your requirements. It lists all found values in the output window with full name of active file and line number. So you can double click on such a line to position the cursor to that line or use the keys CTRL+SHIFT+DOWN ARROW and CTRL+SHIFT+UP ARROW to navigate through the lines with a found value without leaving the edit area of the file.

      Code: Select all

      if (UltraEdit.document.length > 0) {      // Is any file open?
      
         // Ask user for the value (range) to search for.
         var sValue = UltraEdit.getString("Enter the value to search for:",1);
         var nValue = parseInt(sValue,10);      // Convert the value string to an integer.
         var nMaxVal = nValue + 10;             // Add 10 to get maximum value.
         nValue -= 10;                          // Subtract 10 to get minimum value.
         if (nValue < 0) nValue = 0;            // No negative values are possible.
         var nValCount = nMaxVal - nValue + 1;  // Calculate total number of values to search for.
         var sExpression = "\\<(";              // "\<(" and ")\>" are needed to find only complete values.
         while (1) {                            // Build the regular expression search string.
            sExpression += nValue.toString();   // Append current value as string to the expression.
            nValCount--;                        // Subtract the number of values to append.
            if (!nValCount) break;              // No more value, exit loop.
            sExpression += '|';                 // Append character for OR expression.
            nValue++;                           // Increase the value by 1.
         }
         sExpression += ")\\>"
      
         // Remember current cursor position in active file.
         var nLineNum = UltraEdit.activeDocument.currentLineNum;
         var nColumnNum = UltraEdit.activeDocument.currentColumnNum;
         if (typeof(UltraEdit.activeDocumentIdx) == "undefined") nColumnNum++;
      
         UltraEdit.activeDocument.top();        // Go to top of file.
         UltraEdit.perlReOn();                  // Activate the Perl regular expression engine.
         // Define the parameters for a Perl regular expression search with matching only whole
         // words. The search is also case-sensitive because a case-sensitive search is faster.
         UltraEdit.activeDocument.findReplace.mode=0;
         UltraEdit.activeDocument.findReplace.matchCase=true;
         UltraEdit.activeDocument.findReplace.matchWord=false;
         UltraEdit.activeDocument.findReplace.regExp=true;
         UltraEdit.activeDocument.findReplace.searchDown=true;
         UltraEdit.activeDocument.findReplace.searchInColumn=false;
      
         // Prepare the output window for the results of the search and
         // write into first line of output window what is searched for.
         UltraEdit.outputWindow.showStatus=false;
         UltraEdit.outputWindow.clear();
         if (UltraEdit.outputWindow.visible == false) UltraEdit.outputWindow.showWindow(true);
         UltraEdit.outputWindow.write("Searching for: " + sExpression + "\n");
      
         // Search in the active file for one of the values in the regular expression.
         // Output the found values with full name of file and line number in the output window.
         var nFoundValues = 0;
         while (UltraEdit.activeDocument.findReplace.find(sExpression)) {
            var nFoundLineNum = UltraEdit.activeDocument.currentLineNum;
            var sFoundValue = UltraEdit.activeDocument.selection;
            UltraEdit.outputWindow.write(UltraEdit.activeDocument.path + "(" + nFoundLineNum + "): " + sFoundValue);
            nFoundValues++;
         }
      
         // Print a summary to the output window and position the cursor back to initial position.
         if (!nFoundValues) UltraEdit.outputWindow.write("Could not find any value.");
         else if (nFoundValues == 1) UltraEdit.outputWindow.write("\nFound only the above value.");
         else UltraEdit.outputWindow.write("\nFound in total " + nFoundValues + " values.");
         UltraEdit.activeDocument.gotoLine(nLineNum,nColumnNum);
      }
      Best regards from an UC/UE/UES for Windows user from Austria

      19
      Basic UserBasic User
      19

        Oct 02, 2010#3

        Perfect.
        Whenever I have to decide between two evils, I always choose the one I haven't tried before. -Mae West