How to set default prompt value?

How to set default prompt value?

MikeW

    Dec 31, 2018#1

    I recently needed to insert line numbers every X number of lines for a poetry book. I found a couple scripts here that I modified and it works great. Thank y'all for those snippets I used.

    I prompt for the number of lines to skip using:

    Code: Select all

    var stepLines = UltraEdit.getValue("Place Line Numbers Every X Number of Lines",1);
    The prompt uses a zero to initialize the prompt. Which then requires the zero removed and a value entered. 

    My question is there a means to provide a default value in the prompt?

    Thank you, Mike

    6,616548
    Grand MasterGrand Master
    6,616548

      Jan 01, 2019#2

      It is not possible to define a default value preset in the prompt.

      In your case with 0 being an invalid value, you can use as next line if (!stepLines) stepLines = 50; to define a default value if the user hits on prompt just RETURN or ENTER.

      You can write this in the prompt, for example with var stepLines = UltraEdit.getValue("Place line numbers every X number of lines.\nDefault with number 0 is every 50 lines.\nPlease enter the number you want.",1);
      Best regards from an UC/UE/UES for Windows user from Austria

      MikeW
      MikeW

        Jan 01, 2019#3

        Thank you, Mofi. 

        Works well now with the if statement.

        This is the finished code if anyone finds it useful.

        Code: Select all

        // Set stepLines inital value by prompting
        var stepLines = UltraEdit.getValue("Place Line Numbers Every X Number of Lines.\nDefault is every 5 lines.\nPlease enter the number you want.",1);
        if (!stepLines) stepLines = 5 // Set default here if prompt is set to zero
        var gotoLine = stepLines + 0;
        UltraEdit.activeDocument.top();
        while (! UltraEdit.activeDocument.isEof()) {
           UltraEdit.activeDocument.gotoLine(gotoLine,0);
        // Increment the line jump value
           gotoLine = UltraEdit.activeDocument.currentLineNum + stepLines + 1;
           UltraEdit.activeDocument.write("##" + "^p"); }
           UltraEdit.activeDocument.top();
        // Replace ## + linebreak with ##
        if (! UltraEdit.activeDocument.isEof()) {
           UltraEdit.ueReOn();
           UltraEdit.activeDocument.findReplace.replaceAll=true;
           UltraEdit.activeDocument.findReplace.replaceInAllOpen=false;
           UltraEdit.activeDocument.findReplace.replace("##^p","##"); }
        // Replace ## placeholder with line Number and tab
        {  UltraEdit.activeDocument.top();
           while (UltraEdit.activeDocument.findReplace.find("##")) {
           var sLineNum = UltraEdit.activeDocument.currentLineNum.toString(10);
           UltraEdit.activeDocument.write(sLineNum + "^t"); }
           UltraEdit.activeDocument.top(); }
        Thank you and best regards, Mike

        6,616548
        Grand MasterGrand Master
        6,616548

          Jan 01, 2019#4

          I suggest the following script for this task:

          Code: Select all

          if (UltraEdit.document.length > 0)  // Is any file opened?
          {
             // Define environment for this script.
             UltraEdit.insertMode();
             if (typeof(UltraEdit.columnModeOff) == "function") UltraEdit.columnModeOff();
             else if (typeof(UltraEdit.activeDocument.columnModeOff) == "function") UltraEdit.activeDocument.columnModeOff();
          
             var nStepLines = UltraEdit.getValue("Place Line Numbers Every X Number of Lines.\nDefault is every 5 lines.\nPlease enter the number you want.",1);
             if (!nStepLines) nStepLines = 5; // Set default here if prompt is set to zero
          
             // Move caret to bottom of the active file to get number of lines.
             UltraEdit.activeDocument.bottom();
             var nLineCount = UltraEdit.activeDocument.currentLineNum;
             // Has the last line in file a line termination?
             // Yes, subtract the line number by one to get correct line count value.
             if (UltraEdit.activeDocument.isColNum(1)) nLineCount--;
          
             // Create alignment string by converting line count number being
             // maximum line number to a decimal number string and replacing all
             // characters by character 0 (or by space character). The characters
             // from the alignment string are inserted left to current line number
             // to insert all line numbers with same amount of characters
             // depending on line number of last line in file.
             var sAlign = nLineCount.toString(10).replace(/./g,"0");
          
             var nLineNumber = nStepLines;
             while (nLineNumber <= nLineCount)
             {
                UltraEdit.activeDocument.gotoLine(nLineNumber,1);
                var sLineNumber = nLineNumber.toString(10);
                if (sLineNumber.length < sAlign.length)
                {
                   sLineNumber = sAlign.substr(sLineNumber.length) + sLineNumber;
                }
                UltraEdit.activeDocument.write(sLineNumber+"\t");
                nLineNumber += nStepLines;
             }
             UltraEdit.activeDocument.top();
          }
          
          Please note that 0 for column number in UltraEdit.activeDocument.gotoLine function call means current column and not first column in line. And UltraEdit.activeDocument.isEof() is critical on using it as exit condition for a loop because this function returns only true if the caret really is set at end of file which is often not the case on using a find/replace or on using caret moving commands when last line of file has no line termination.
          Best regards from an UC/UE/UES for Windows user from Austria

          MikeW
          MikeW

            Jan 01, 2019#5

            Thank you again. Much more efficient than how I cobbled together pieces of the scripts I found.

            fwiw, the first UltraEdit.activeDocument.top(); resolves the column number issue, I think. At least the script ran properly. But it was inefficient in several thousand lines. I much appreciate your script.

            Easy to modify if zero padding is not desired (which for my needs, it isn't).

            May I ask a question about a particular line?

            In this line:

            Code: Select all

            var sAlign = nLineCount.toString(10).replace(/./g,"0");
            The last portion has .replace(/./g,"0")

            But it doesn't appear to actually do anything. That is, I can remove it and whether I am padding with zeros or not, the results seem the same. I do understand what that code/line portion does, or rather, should do.

            I'm attempting to understand and this is the only line portion I fail at thus far.

            Thank you.

            6,616548
            Grand MasterGrand Master
            6,616548

              Jan 01, 2019#6

              Here is an optimized version of the script without right aligning the line numbers with zeros or spaces.

              Code: Select all

              if (UltraEdit.document.length > 0)  // Is any file opened?
              {
                 // Define environment for this script.
                 UltraEdit.insertMode();
                 if (typeof(UltraEdit.columnModeOff) == "function") UltraEdit.columnModeOff();
                 else if (typeof(UltraEdit.activeDocument.columnModeOff) == "function") UltraEdit.activeDocument.columnModeOff();
              
                 var nStepLines = UltraEdit.getValue("Place Line Numbers Every X Number of Lines.\nDefault is every 5 lines.\nPlease enter the number you want.",1);
                 if (!nStepLines) nStepLines = 5; // Set default here if prompt is set to zero
              
                 var nLineNumber = nStepLines;
                 while (true)
                 {
                    UltraEdit.activeDocument.gotoLine(nLineNumber,1);
                    if (UltraEdit.activeDocument.currentLineNum != nLineNumber) break;
                    if (UltraEdit.activeDocument.isEof()) break;
                    UltraEdit.activeDocument.write(nLineNumber+"\t");
                    nLineNumber += nStepLines;
                 }
                 UltraEdit.activeDocument.top();
              }
              
              Both IF conditions inside the loop are necessary to avoid an endless running loop in some cases and avoid inserting a line number after last line ending at end of file and avoid inserting a line number at beginning of last line in file which is not a multiple of nStepLines.

              The line you don't understand first converts the integer line number of last line in file like 1842 into a string "1842" and replaces all digits in this string by 0 or a space. So depending on number of last line in file, the string assigned to variable sAlign has one, two, three, four, ... zeros or spaces. If the line number of current line converted to a string is shorter than the string of sAlign, the last characters of this string depending on length of current line number string are used to right align the current line number. In other words on a file with 1842 lines, sAlign is after this line a string variable with string value 0000 and for example for current line being 50, the last two 0 of sAlign are copied to a new string concatenated with string "50" to build the string "0050" to write into the file.
              Best regards from an UC/UE/UES for Windows user from Austria

              MikeW
              MikeW

                Jan 01, 2019#7

                I am very grateful, Mofi.

                Mike