Add counting numbers at beginning of lines in selected text

Add counting numbers at beginning of lines in selected text

1581
Power UserPower User
1581

    Jan 25, 2018#1

    I have a simple text:

    Code: Select all

    Jim
    John
    Sue
    Henry
    Tom
    Mike
    I want
    - to select some lines (e.g. John - Tom)
    - define the starting number (e.g. 3280)
    - define the counting interval (e.g. 2)
    - define the suffix (e.g. ": ")
    - press OK
    and get this result:

    Code: Select all

    Jim
    3280: John
    3282: Sue
    3284: Henry
    3286: Tom
    Mike
    
    
    Any known scripts going to this direction?
    UE 26.20.0.74 German / Win 10 x 64 Pro

    6,602547
    Grand MasterGrand Master
    6,602547

      Jan 25, 2018#2

      What's wrong with following procedure?
      1. Enable column editing mode with Alt+C.
      2. Select at column 1 with Shift+Down/Up Arrow or with mouse the four lines to modify.
      3. Click in menu Column on Insert Number (on using traditional menus and which I execute with a hotkey assigned to this command in key mapping configuration dialog).
      4. Enter 3280 as first number, TAB, 2 as increment and press RETURN.
      5. Select at column 4 again the four lines and press colon key on keyboard.
      6. Disable column editing mode with Alt+C.
      Well, a scripting solution which works on a selection in normal editing mode and prompting the user for the two integer values and the suffix string would be of course faster to execute and could be coded to produce just one undo step on doing the line modifications in memory. It is also not really difficult to code this little script when it is not necessary to write it for a selection of hundred thousands of selected lines.
      Best regards from an UC/UE/UES for Windows user from Austria

      1581
      Power UserPower User
      1581

        Jan 25, 2018#3

        Mofi wrote:What's wrong with following procedure?..
        Absolutely nothing - one more standard command I overlooked for years 😳

        Thanks Mofi
        UE 26.20.0.74 German / Win 10 x 64 Pro

        6,602547
        Grand MasterGrand Master
        6,602547

          Jan 26, 2018#4

          Writing the small script for this task was quite easy.

          Code: Select all

          if (UltraEdit.document.length > 0)  // Is any file opened?
          {
             // And is something selecting in active file?
             if (UltraEdit.activeDocument.isSel())
             {
                // Define environment for this script.
                UltraEdit.insertMode();
                if (typeof(UltraEdit.columnModeOff) == "function") UltraEdit.columnModeOff();
                else if (typeof(UltraEdit.activeDocument.columnModeOff) == "function") UltraEdit.activeDocument.columnModeOff();
          
                // Determine line termination type from first line in selection.
                var sLineTerm = UltraEdit.activeDocument.selection.replace(/^[^\r\n]*(\r?\n|\r)[\s\S]*$/,"$1");
          
                // Is there at least one line with line ending selected?
                if (sLineTerm != UltraEdit.activeDocument.selection)
                {
                   // Get all selected lines into an array of strings.
                   var asLines = UltraEdit.activeDocument.selection.split(sLineTerm);
          
                   var nLineCount = asLines.length;
                   // Do not process the last string in array if being an empty string
                   // because of selection ends at column 1 of line not being selected
                   // anymore, i.e. the last selected line is selected with its line
                   // ending (CR+LF or just LF or just CR).
                   if (!asLines[asLines.length-1].length) nLineCount--;
          
                   // Prompt the user for
                   //   * first number which must be 0 or greater,
                   //   * increment which must be a non-zero value,
                   //   * suffix which can be any string even an empty string.
                   var nNumber = UltraEdit.getValue("Please enter first number:",1);
                   do
                   {
                      var nIncrement = UltraEdit.getValue("Please enter non-zero increment:",1);
                   }
                   while (!nIncrement);
                   var sSuffix = UltraEdit.getString("Please enter suffix:",1);
          
                   // Determine maximum number depending on first number, increment
                   // and number of lines to modify, convert this decimal number to
                   // a string and replace all digits by the character 0 being later
                   // used to insert the number on all selected lines with leading
                   // zero(s) according to maximum inserted number.
                   var nMaxNumber = nNumber + (nIncrement * (nLineCount-1));
                   var sZeroDigits = nMaxNumber.toString(10);
                   var sZeroDigits = sZeroDigits.replace(/./g,"0");
          
                   // Insert the number and the suffix at begin of each line in array.
                   for (var nLine = 0; nLine < nLineCount; nLine++, nNumber += nIncrement)
                   {
                      var sNumber = nNumber.toString(10);
                      var sNumber = sZeroDigits.substr(sNumber.length) + sNumber;
                      asLines[nLine] = sNumber + sSuffix + asLines[nLine];
                   }
          
                   // Overwrite the selection with the modified lines.
                   UltraEdit.activeDocument.write(asLines.join(sLineTerm));
                }
             }
          }
          
          Negative numbers cannot be entered like in Insert Number dialog because UltraEdit.getValue does not allow entering character -.
          UltraEdit for Windows v24.00 or a later version is required on using this script on a selection with Unicode characters.
          Best regards from an UC/UE/UES for Windows user from Austria

          1581
          Power UserPower User
          1581

            Jan 26, 2018#5

            👏👏👏👏👏👏👏👏👏👏

              Jul 31, 2019#6

              Just a small remark on script posted above:
              If the selection does not start at the beginning of the first line, the first number is inserted at the current beginning of the selection and not at the beginning of the first line.

              But thanks again - just now I need it again.

              Peter
              UE 26.20.0.74 German / Win 10 x 64 Pro

              6,602547
              Grand MasterGrand Master
              6,602547

                Aug 02, 2019#7

                Here is a better script for this task with following advantages in comparison to the script posted above:
                1. It supports ASCII/ANSI and Unicode encoded files.
                  Note: UltraEdit for Windows v24.00 or UEStudio v17.00 is needed at least for inserting a Unicode suffix string in addition to the numbers into a Unicode encoded file.
                2. The selection must not start at beginning of a line. A selection can begin everywhere within a line and end also everywhere within another line. There must be just selected at least one line termination and one more character.
                The script can be used with UltraEdit for Windows v13.10 up to currently latest UE v26.10 and UEStudio v6.30 up to currently latest UES v19.10. It produces also just one undo record as the script posted above.

                Code: Select all

                if (UltraEdit.document.length > 0)  // Is any file opened?
                {
                   // And is something selecting in active file?
                   if (UltraEdit.activeDocument.isSel())
                   {
                      // Define environment for this script.
                      UltraEdit.insertMode();
                      if (typeof(UltraEdit.columnModeOff) == "function") UltraEdit.columnModeOff();
                      else if (typeof(UltraEdit.activeDocument.columnModeOff) == "function") UltraEdit.activeDocument.columnModeOff();
                
                      // Determine line termination type from first line in selection.
                      var sLineTerm = UltraEdit.activeDocument.selection.replace(/^[^\r\n]*(\r?\n|\r)[\s\S]*$/,"$1");
                
                      // Is there at least one line with line ending selected?
                      if (sLineTerm != UltraEdit.activeDocument.selection)
                      {
                         // Get all selected lines into an array of strings.
                         var asLines = UltraEdit.activeDocument.selection.split(sLineTerm);
                
                         var nLineCount = asLines.length;
                         // Do not process the last string in array if being an empty string
                         // because of selection ends at column 1 of line not being selected
                         // anymore, i.e. the last selected line is selected with its line
                         // ending (CR+LF or just LF or just CR).
                         if (!asLines[asLines.length-1].length) nLineCount--;
                
                         // Move caret to start of first selected line.
                         UltraEdit.activeDocument.gotoLine(UltraEdit.activeDocument.currentLineNum,1);
                
                         // Prompt the user for
                         //   * first number which must be 0 or greater,
                         //   * increment which must be a non-zero value,
                         //   * suffix which can be any string even an empty string.
                         var nNumber = UltraEdit.getValue("Please enter first number:",1);
                         do
                         {
                            var nIncrement = UltraEdit.getValue("Please enter non-zero increment:",1);
                         }
                         while (!nIncrement);
                         var sSuffix = UltraEdit.getString("Please enter suffix:",1);
                
                         // Determine maximum number depending on first number, increment
                         // and number of lines to modify, convert this decimal number to
                         // a string and replace all digits by the character 0 being later
                         // used to insert the number on all selected lines with leading
                         // zero(s) according to maximum inserted number.
                         var nMaxNumber = nNumber + (nIncrement * (nLineCount-1));
                         var sZeroDigits = nMaxNumber.toString(10);
                         var sZeroDigits = sZeroDigits.replace(/./g,"0");
                
                         // Create in user clipboard 9 the block with number and suffix
                         // at start of each selected line pasted next in column mode.
                         UltraEdit.selectClipboard(9);
                
                         // Does this version of UE/UES support direct access to clipboard?
                         if (typeof(UltraEdit.clipboardContent) == "string")
                         {
                            UltraEdit.clearClipboard();
                            for (var nLine = 0; nLine < nLineCount; nLine++, nNumber += nIncrement)
                            {
                               var sNumber = nNumber.toString(10);
                               var sNumber = sZeroDigits.substr(sNumber.length) + sNumber;
                               UltraEdit.clipboardContent += sNumber + sSuffix + "\r\n";
                            }
                            if (typeof(UltraEdit.columnModeOn) == "function") UltraEdit.columnModeOn();
                            else if (typeof(UltraEdit.activeDocument.columnModeOn) == "function") UltraEdit.activeDocument.columnModeOn();
                            UltraEdit.activeDocument.paste();
                         }
                         else  // Workaround for UE for Windows < v14.20 and UES < v9.00
                         {     // with no support for direct access of clipboard content.
                            for (var nDocIndex = 0; nDocIndex < UltraEdit.document.length; nDocIndex++)
                            {
                               if (UltraEdit.document[nDocIndex].path == UltraEdit.activeDocument.path) break;
                            }
                            UltraEdit.newFile();
                            UltraEdit.activeDocument.unicodeToASCII();
                            UltraEdit.activeDocument.unixMacToDos();
                            for (var nLine = 0; nLine < nLineCount; nLine++, nNumber += nIncrement)
                            {
                               var sNumber = nNumber.toString(10);
                               var sNumber = sZeroDigits.substr(sNumber.length) + sNumber;
                               UltraEdit.activeDocument.write(sNumber + sSuffix + "\r\n");
                            }
                            UltraEdit.activeDocument.selectAll();
                            UltraEdit.activeDocument.copy();
                            UltraEdit.closeFile(UltraEdit.activeDocument.path,2);
                            if (typeof(UltraEdit.columnModeOn) == "function") UltraEdit.columnModeOn();
                            else if (typeof(UltraEdit.activeDocument.columnModeOn) == "function") UltraEdit.activeDocument.columnModeOn();
                            UltraEdit.document[nDocIndex].paste();
                         }
                
                         if (typeof(UltraEdit.columnModeOff) == "function") UltraEdit.columnModeOff();
                         else if (typeof(UltraEdit.activeDocument.columnModeOff) == "function") UltraEdit.activeDocument.columnModeOff();
                         UltraEdit.clearClipboard();
                         UltraEdit.selectClipboard(0);
                      }
                   }
                }
                
                This script was tested by me with UltraEdit for Windows versions 13.10a+2, 14.20.1.1008, 22.20.0.49 and 26.10.0.72 on Windows XP and Windows 7 on ANSI and Unicode encoded files with DOS, UNIX and MAC line endings.
                Best regards from an UC/UE/UES for Windows user from Austria

                1581
                Power UserPower User
                1581

                  Aug 02, 2019#8

                  Great Mofi, thanks! 👍
                  UE 26.20.0.74 German / Win 10 x 64 Pro