Can the line number automatically be inserted in the code?

Help with writing and running scripts

Can the line number automatically be inserted in the code?

Postby thofei » Wed Nov 03, 2010 8:46 am

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
thofei
Newbie
 
Posts: 3
Joined: Wed Nov 03, 2010 8:33 am

Re: Can the line number automatically be inserted in the code?

Postby Mofi » Wed Nov 03, 2010 1:49 pm

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 separater column, next use a tagged regular expression replace to replace all the line numbers in content of file and finally delete the separater 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();
   UltraEdit.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.
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4051
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: Can the line number automatically be inserted in the code?

Postby thofei » Mon Nov 08, 2010 11:43 am

thank you so much, works like a charm :D

Best
Thomas
thofei
Newbie
 
Posts: 3
Joined: Wed Nov 03, 2010 8:33 am


Return to Scripts