Removing all spaces on selected text and inserting a string at end of each line

Removing all spaces on selected text and inserting a string at end of each line

4
NewbieNewbie
4

    Jan 08, 2015#1

    Could I have script to trim spaces and replace end of the line with * (or any character) for selected text lines only.

    Input

    Code: Select all

    one two   blah
    th ree
    A b  bb
    Output

    Code: Select all

    onetwoblah*
    three*
    Abbb*

    6,681583
    Grand MasterGrand Master
    6,681583

      Jan 09, 2015#2

      Here is this little script.

      Code: Select all

      if (UltraEdit.document.length > 0)  // Is any file opened?
      {
         // Is there anything selecting in active file?
         if (UltraEdit.activeDocument.isSel())
         {
            // And is the active file not opened in hex editing mode?
            if (!UltraEdit.activeDocument.hexMode)
            {
               // The column mode property is a property of the UltraEdit object in
               // UltraEdit for Windows and UEStudio, but a property of the document
               // object in UltraEdit for Linux and for Mac.
               var bColumnMode = false;
               if (typeof(UltraEdit.columnMode) == "boolean") bColumnMode = UltraEdit.columnMode;
               else if (typeof(UltraEdit.activeDocument.columnMode) == "boolean") bColumnMode = UltraEdit.activeDocument.columnMode;
      
               // And is column editing mode not active?
               if (!bColumnMode)
               {
                  UltraEdit.insertMode();
                  // Get selected text and remove all spaces and tabs.
                  var sTextNoSpaces = UltraEdit.activeDocument.selection.replace(/[ \t]/g,"");
                  // Overwrite selected text with * inserted before every line termination.
                  UltraEdit.activeDocument.write(sTextNoSpaces.replace(/(\r?\n|\r)/g,"*$1"));
               }
            }
         }
      }
      Replace (\r?\n|\r)/g,"*$1" by just \r?\n|\r/g,"*" if you really want to replace line terminations by * instead of inserting an asterisk before every line termination.
      Best regards from an UC/UE/UES for Windows user from Austria

      4
      NewbieNewbie
      4

        Jan 09, 2015#3

        Works!!!!

        Could I ask is it possible to trigger an input dialog box for user to enter a fixed text and then script to insert that text at the end of every single line?

        6,681583
        Grand MasterGrand Master
        6,681583

          Jan 09, 2015#4

          Here is an improved version of code with giving the user of the script the possibility to define the text to append on each line.

          And this script now appends also the user entered text to end of selected text if the selected text does not end with either carriage return or line-feed, for example if file does not end with line termination, or the line termination of last line of selected block was not selected, too

          Code: Select all

          if (UltraEdit.document.length > 0)  // Is any file opened?
          {
             // Is there anything selecting in active file?
             if (UltraEdit.activeDocument.isSel())
             {
                // And is the active file not opened in hex editing mode?
                if (!UltraEdit.activeDocument.hexMode)
                {
                   // The column mode property is a property of the UltraEdit object in
                   // UltraEdit for Windows and UEStudio, but a property of the document
                   // object in UltraEdit for Linux and for Mac.
                   var bColumnMode = false;
                   if (typeof(UltraEdit.columnMode) == "boolean") bColumnMode = UltraEdit.columnMode;
                   else if (typeof(UltraEdit.activeDocument.columnMode) == "boolean") bColumnMode = UltraEdit.activeDocument.columnMode;
          
                   // And is column editing mode not active?
                   if (!bColumnMode)
                   {
                      UltraEdit.insertMode();
                      // Get selected text and remove all spaces and tabs.
                      var sReformattedText = UltraEdit.activeDocument.selection.replace(/[ \t]/g,"");
                      // Ask user for text to append at end of each line.
                      var sUserText = UltraEdit.getString("Please enter text to append on each line:",1);
                      if (sUserText.length)   // Has the user entered anything?
                      {
                         // Insert user entered text before every line termination (DOS/UNIX/MAC).
                         sReformattedText = sReformattedText.replace(/(\r?\n|\r)/g,sUserText+"$1");
                         // If selected text does not end with a line terminator,
                         // append the entered text also to end of reformatted text.
                         if (sReformattedText.search(/[\r\n]$/) < 0) sReformattedText += sUserText;
                      }
                      // Overwrite selected text with reformatted text.
                      UltraEdit.activeDocument.write(sReformattedText);
                   }
                }
             }
          }
          
          Best regards from an UC/UE/UES for Windows user from Austria

          4
          NewbieNewbie
          4

            Jan 09, 2015#5

            Excellent.

            Just last bit. I have to do similar thing but in column mode as I wanted to insert fixed number of spaces and then text.

            For example:

            Input

            Code: Select all

            One Two Three
            Four Five
            Output

            Code: Select all

            OneTwoThree                                           XYZ PQR
            FourFive                                              XYZ PQR

            6,681583
            Grand MasterGrand Master
            6,681583

              Jan 10, 2015#6

              For this new script some more information are necessary to define column position for user text at end of each line.
              1. Is the column fixed at where the user text should be inserted?
              2. Or should the user define the column for user text on script execution?
              3. Or should the user text be inserted at column depending on longest line after removing all spaces and tabs?
              In case of second or third variant, is there a minimal column which should not be fallen below on inserting user text.
              Best regards from an UC/UE/UES for Windows user from Austria

              4
              NewbieNewbie
              4

                Jan 14, 2015#7

                Option 3 in your list..

                Thanks

                6,681583
                Grand MasterGrand Master
                6,681583

                  Jan 15, 2015#8

                  Here is the script modified once more to append the user text on all lines at same column depending on longest line of selected block.

                  Code: Select all

                  if (UltraEdit.document.length > 0)  // Is any file opened?
                  {
                     // Is there anything selecting in active file?
                     if (UltraEdit.activeDocument.isSel())
                     {
                        // And is the active file not opened in hex editing mode?
                        if (!UltraEdit.activeDocument.hexMode)
                        {
                           // The column mode property is a property of the UltraEdit object in
                           // UltraEdit for Windows and UEStudio, but a property of the document
                           // object in UltraEdit for Linux and for Mac.
                           var bColumnMode = false;
                           if (typeof(UltraEdit.columnMode) == "boolean") bColumnMode = UltraEdit.columnMode;
                           else if (typeof(UltraEdit.activeDocument.columnMode) == "boolean") bColumnMode = UltraEdit.activeDocument.columnMode;
                  
                           // And is column editing mode not active?
                           if (!bColumnMode)
                           {
                              UltraEdit.insertMode();
                              // Get selected text and remove all spaces and tabs.
                              var sReformattedText = UltraEdit.activeDocument.selection.replace(/[ \t]/g,"");
                              // Ask user for text to append at end of each line.
                              var sUserText = UltraEdit.getString("Please enter text to append on each line:",1);
                              if (sUserText.length)   // Has the user entered anything?
                              {
                                 // Determine line terminator type for splitting block into lines.
                                 if (UltraEdit.activeDocument.lineTerminator < 1) var sLineTerm = "\r\n";     // DOS
                                 else if (UltraEdit.activeDocument.lineTerminator == 1) var sLineTerm = "\n"; // UNIX
                                 else var sLineTerm = "\r";                                                   // MAC
                  
                                 var asLines = sReformattedText.split(sLineTerm);
                  
                                 // If selected block ends with a line termination, the last
                                 // string in array of lines is an empty string which must be
                                 // ignored on appending the alignment spaces and the user text.
                                 var nLineCount = asLines.length;
                                 if (asLines[asLines.length-1].length == 0) nLineCount--;
                  
                                 // Find length of longest line.
                                 var nMaxLength = 0;
                                 for (var nLine = 0; nLine < nLineCount; nLine++)
                                 {
                                    if (asLines[nLine].length > nMaxLength)
                                    {
                                       nMaxLength = asLines[nLine].length;
                                    }
                                 }
                  
                                 // Build a string with only space as long as longest line.
                                 var sSpaces = "";
                                 for (var nSpaceCount = 0; nSpaceCount < nMaxLength; nSpaceCount++) sSpaces += " ";
                  
                                 // Append to each line the alignment spaces and the user text.
                                 for (nLine = 0; nLine < nLineCount; nLine++)
                                 {
                                    asLines[nLine] += sSpaces.substr(0,nMaxLength-asLines[nLine].length) + sUserText;
                                 }
                  
                                 // Join the lines back to a block.
                                 sReformattedText = asLines.join(sLineTerm);
                              }
                              // Overwrite selected text with reformatted text.
                              UltraEdit.activeDocument.write(sReformattedText);
                           }
                        }
                     }
                  }
                  
                  Increase value of variable nMaxLength with nMaxLength++; after the loop determining maximum line length if you want also on longest line a space between line contents and appended user text.
                  Best regards from an UC/UE/UES for Windows user from Austria