Can the line number automatically be inserted in the code?

Can the line number automatically be inserted in the code?

3
NewbieNewbie
3

    Nov 03, 2010#1

    Hello,

    is it somehow possible with UltraEdit to get the current linenumber somehow automatically into the source code?

    I would like to use that scenario, when I write entries into a logfile, for example something like this in a JSP-File.

    System.out.println("\033[1m anmeldung.jsp: Line 273 \033[0m : "+statement);

    It's obvious that "Line 273" is only valid as long as I don't add new lines before, if I do that I always have to change the line number manually (and that's annoying as I have usually lots of those lines).

    So is something like

    System.out.println("\033[1m anmeldung.jsp: Line $$UE_Variable_for_linenumber$$ \033[0m : "+statement);

    possible? UE would have to replace that variable with the real number while saving for example.

    Thanks a lot
    Thomas

    6,604548
    Grand MasterGrand Master
    6,604548

      Nov 03, 2010#2

      C/C++/C# programmers would do that with the help of the preprocessor. I don't know if JSP files are precompiled and a preprocessor exists.

      There would be a very fast solution by inserting at column 1 an auto-increment number using command Insert Number with a separator column, next use a tagged regular expression replace to replace all the line numbers in content of file and finally delete the separator and the line number columns at start of every line. But this solution does not work for multiple line number references in one line and the undo steps recorded for that fast "update line numbers" method would not be fine when editing.

      Therefore I post here a script which makes the line number updates on execution line reference by line reference and shows you in a message box how many line numbers were found and how many of them were updated. Of course without this additional information the script would be a little bit faster.

      Code: Select all

      if (UltraEdit.document.length > 0) { // Is any file opened?
      
         // Define the environment for the script.
         UltraEdit.insertMode();
         if (typeof(UltraEdit.columnModeOff) == "function") UltraEdit.columnModeOff();
         else if (typeof(UltraEdit.activeDocument.columnModeOff) == "function") UltraEdit.activeDocument.columnModeOff();
         UltraEdit.activeDocument.hexOff();
         UltraEdit.ueReOn();
      
         // Remember the current cursor position to restore it at end of script.
         var nCurLine = UltraEdit.activeDocument.currentLineNum;
         var nCurColumn = UltraEdit.activeDocument.currentColumnNum;
         if (typeof(UltraEdit.activeDocumentIdx) == "undefined") nCurColumn++;
      
         // Go to top of the file and define the needed options for the searches.
         UltraEdit.activeDocument.top();
         UltraEdit.activeDocument.findReplace.mode=0;
         UltraEdit.activeDocument.findReplace.matchCase=false;
         UltraEdit.activeDocument.findReplace.matchWord=false;
         UltraEdit.activeDocument.findReplace.regExp=true;
         UltraEdit.activeDocument.findReplace.searchDown=true;
         UltraEdit.activeDocument.findReplace.searchInColumn=false;
      
         // Define the variables used in the following loop.
         var nLineRefChanged = 0;
         var nLineRefCount = 0;
         var sFoundLineNum = "";
         var sActiveLineNum = "";
         var sFoundLineRef = "";
      
         while (UltraEdit.activeDocument.findReplace.find("Line [0-9]+") ) {
      
            nLineRefCount++; // Found one more line reference.
            // Get current line number from selected line reference string.
            // and compare it with the line number of active line.
            sFoundLineRef = UltraEdit.activeDocument.selection;
            sFoundLineNum = sFoundLineRef.substring(5);
            sActiveLineNum = UltraEdit.activeDocument.currentLineNum.toString(10);
            if (sFoundLineNum != sActiveLineNum) {
               nLineRefChanged++; // The line number in this reference must be updated.
               UltraEdit.activeDocument.write(sFoundLineRef.substr(0,5)+sActiveLineNum);
            }
         }
         UltraEdit.activeDocument.gotoLine(nCurLine,nCurColumn);
         sFoundLineRef = nLineRefCount == 1 ? "" : "s";
         UltraEdit.messageBox(nLineRefChanged+" of "+nLineRefCount+" line reference"+sFoundLineRef+" updated.","Update Line Reference Result");
      }
      As you can see the script searches for all occurrences of word line in any case followed by a single space followed by an integer number. You can define your own rules. The script uses the UltraEdit regular expression engine, but you can use also the Unix or Perl regexp engine by changing the command UltraEdit.ueReOn(). The search string as is would work with all 3 engines.
      Best regards from an UC/UE/UES for Windows user from Austria

      3
      NewbieNewbie
      3

        Nov 08, 2010#3

        thank you so much, works like a charm :D

        Best
        Thomas

        1581
        Power UserPower User
        1581

          Nov 10, 2014#4

          Exactly 4 years later - many thanks also from me to Mofi: cool feature 8)

          Peter
          UE 26.20.0.74 German / Win 10 x 64 Pro