Tapatalk

How to copy text with line numbers?

How to copy text with line numbers?

2
NewbieNewbie
2

    Jan 21, 2015#1

    I need to include sections of program code in a document, and include the line numbers. I'm sure this must be possible with UE, but I cannot figure out how to do this (I'd like to avoid having to code my own macro). Including screen shots is a potential solution, but it's not elegant.
    Suggestions appreciated.

    6,685587
    Grand MasterGrand Master
    6,685587

      Jan 21, 2015#2

      There is no built-in command to copy a selection with line numbers.

      But a very simple UltraEdit script can be used to copy a selection with line numbers into active clipboard.

      Code: Select all

      // Is any file opened with a selection in normal text editing mode?
      if (UltraEdit.document.length > 0)
      {
         if (UltraEdit.activeDocument.isSel())
         {
            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;
      
               if (!bColumnMode)
               {
                  var sLineTerm = "\n";
                  if (typeof(UltraEdit.activeDocument.lineTerminator) == "number")
                  {
                     // Determine line terminator type of active file.
                     if (UltraEdit.activeDocument.lineTerminator < 1) sLineTerm = "\r\n";
                     else if (UltraEdit.activeDocument.lineTerminator == 2) sLineTerm = "\r";
                  }
      
                  // Load current selection into memory as list of lines.
                  var asLines = UltraEdit.activeDocument.selection.split(sLineTerm);
      
                  // Get line number of first selected line.
                  var nLineNumber = UltraEdit.activeDocument.currentLineNum;
      
                  // Does the selection end with a line termination?
                  var nLineCount = asLines.length;
                  if (!asLines[nLineCount-1].length) nLineCount--;
      
                  // Determine line number of last selected line.
                  var nLastLineNumber = nLineNumber + nLineCount;
      
                  // Convert the number of last line into a decimal string.
                  var sLastLineNumber = nLastLineNumber.toString(10);
      
                  // Replace every character of this string by a zero. It could
                  // be also another character like a space. The resulting
                  // string is used for right alignment of the line numbers.
                  var sAlignment = sLastLineNumber.replace(/./g,"0");
      
                  // Define the string being used as separator between line
                  // number and the contents of the line, e.g. tab character.
                  var sSeparator = "\t";
      
                  // Insert the line number at beginning of each line.
                  for(var nLineIndex = 0; nLineIndex < nLineCount; nLineNumber++, nLineIndex++)
                  {
                     // Convert line number into a decimal string.
                     var sLineNumber = nLineNumber.toString(10);
      
                     // Get the right number of characters from alignment string.
                     var sAlignedNumber = sAlignment.substr(0,sAlignment.length-sLineNumber.length);
      
                     // Append the current line number as decimal string.
                     sAlignedNumber += sLineNumber;
      
                     // Append the separator string if the line is not empty.
                     if (asLines[nLineIndex].length) sAlignedNumber += sSeparator;
      
                     asLines[nLineIndex] = sAlignedNumber + asLines[nLineIndex];
                  }
      
                  // Copy reformatted bock to currently active clipboard.
                  UltraEdit.clipboardContent = asLines.join(sLineTerm);
               }
            }
         }
      }
      
      The advantages of using a script:
      • The string separating the line number from line contents can be customized, see variable sSeparator.
      • Left or right alignment of the line numbers is customizable via appropriate script code.
        This script produces the line numbers right aligned.
      • A right alignment can be done with any character.
        This script uses preceding zeros, but can be changed to spaces easily, see variable sAlignment.
      • It is customizable by script code if empty lines should have also a line number.
        This script gives also empty lines a line number.
      • It is customizable by script code if empty lines should have only the line number or also the separator string.
        This script omits the tab character after line number for empty lines.
      The disadvantages of using a script:
      • It might not work for lines with Unicode characters (without extra code).
        (I have not yet tested the script on a file with Unicode text.)
      • It will definitely not work for very large selections over millions of lines.
      For usage with Copy as HTML or Copy as RTF a separate script would be necessary which gets HTML or RTF block from active clipboard, inserts the line numbers according to current selection at appropriate positions within HTML/RTF code, and writes back the HTML/RTF formatted block now with line numbers to active clipboard.
      Best regards from an UC/UE/UES for Windows user from Austria

      2
      NewbieNewbie
      2

        Jan 22, 2015#3

        Thanks!