Getting the number and jump to a line

Help with writing and running scripts

Getting the number and jump to a line

Postby ArnyEinstein » Tue May 08, 2007 9:04 am

Hi
I am trying to write a script that captures a number and a file name written in a line, then opens this file and jumps to the line with the captured number.
I have tried a few things but I am a dummy with respect to Java. Could anyone help me out?

Thanks
User avatar
ArnyEinstein
Newbie
 
Posts: 9
Joined: Mon May 07, 2007 11:00 pm

Re: Getting the number and jump to a line

Postby Mofi » Tue May 08, 2007 10:35 am

And what exactly is your problem? To get the number and file name from the line of the current file? Or to open the file? Or to jump to the line?

Have you searched or simply looked on the existing threads in the Script forum? Honestly!

What about forum topics Get current line and col from Script and Selecting text? They are not exactly what you want, but they should help you to find the solution.
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4066
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: Getting the number and jump to a line

Postby ArnyEinstein » Tue May 08, 2007 12:07 pm

Yep, I looked at them, but that didn't solve my problem. Here is the code for getting the number and using it to go to the line. But this is not working:

var rexCodeTypes = "[0-9]";
var rexCodeTypesF = "[/]";


UltraEdit.activeDocument.findReplace.regExp = true;
UltraEdit.activeDocument.findReplace.find(rexCodeTypes);

var intLine = UltraEdit.activeDocument.selectWord;
UltraEdit.open('filexyz');
UltraEdit.activeDocument.gotoLine(intLine,5);
User avatar
ArnyEinstein
Newbie
 
Posts: 9
Joined: Mon May 07, 2007 11:00 pm

Re: Getting the number and jump to a line

Postby Mofi » Tue May 08, 2007 2:38 pm

First specify always the regular expression engine before using a regular expression find/replace. See UE Regular Expressions Problem why this is important. Well, your very simple regular expression search string will work for all search engines.

Second, function selectWord selects the current word, but does not return anything as far as I know. You have to use

var intLine = UltraEdit.activeDocument.selection;

Well, I have not really written a script, only a few lines for testing. I just have read whole help page about the script commands and created the tag list group for "UE/UES Script Commands" based on the help and some tests for some commands, but not all commands.

By the way: If you use [0-9]+ you will find always the whole line number and don't need to use selectWord before using selection (I guess, not tested).
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4066
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: Getting the number and jump to a line

Postby ArnyEinstein » Tue May 08, 2007 3:02 pm

The problem is that UE doesn't seem to accept
UltraEdit.activeDocument.gotoLine(intLine,5);

this works, but this is not what I want:
UltraEdit.activeDocument.gotoLine(10,5);

Greetings from Switzerland
User avatar
ArnyEinstein
Newbie
 
Posts: 9
Joined: Mon May 07, 2007 11:00 pm

Re: Getting the number and jump to a line

Postby Bego » Tue May 08, 2007 6:05 pm

Hi dude,

well, there is sth. to learn here. Try this txt-file as your ONLY open file (file0):
213 123 test 12
213 123 test 12
213 123 test 12
213 123 test 12
213 123 test 12
213 123 test 12
213 123 test 12
213 123 test 12
213 123 test 12
213 123 test 12


Now let run this macro:
Code: Select all
var l = 5;
UltraEdit.activeDocument.top();
UltraEdit.activeDocument.findReplace.regExp = true;
UltraEdit.activeDocument.findReplace.find("t");  //it will match in 1st line

var intLine = UltraEdit.activeDocument.selectWord;
UltraEdit.activeDocument.gotoLine(l,0);  //Will go to line 5, same column. OK !
UltraEdit.document[0].write("Line " + String(intLine));


uups.
There is a line like this:
213 123 Line function selectWord() { [native code]}Line 12


intline seems to hold sth. different, sth. like "YES, true, the function is available (also posted sw. else here Sam ??? and Mofi above. ))

Now correct the macro and use selectWord()
You will now get a true. The function is there. OK. But no place to jump to !!! ;-)

Now write
Code: Select all
selectABCnotThere()


You'll get an "undefined".

So, better jump to correct integer-values. See also for "getCol" to get the current column-position, here in this forum. I hope this helped quite well.

viewtopic.php?t=4430

Gruezi, Bego
User avatar
Bego
Master
Master
 
Posts: 357
Joined: Wed Nov 24, 2004 12:00 am
Location: Germany

Re: Getting the number and jump to a line

Postby Jaretin » Wed May 09, 2007 5:04 am

The Problem is, that UltraEdit.activeDocument.selection returns a string, but you need an integer for UltraEdit.activeDocument.gotoLine.

use the following code to get intLine
Code: Select all
var line = UltraEdit.activeDocument.selection;
var intLine = parseInt( line );
User avatar
Jaretin
Basic User
Basic User
 
Posts: 19
Joined: Sun Mar 25, 2007 11:00 pm

Re: Getting the number and jump to a line

Postby ArnyEinstein » Wed May 09, 2007 6:28 am

Thanks! That was where I was looking for!
User avatar
ArnyEinstein
Newbie
 
Posts: 9
Joined: Mon May 07, 2007 11:00 pm

Re: Getting the number and jump to a line

Postby ArnyEinstein » Wed May 09, 2007 8:12 am

One last question: now I can jump to a line, but now I want to open the name of a file I copied:

I have the following line in my listing-file

**** error on line 28 in file c:\models\kli.gms

I want to copy the line number (which functions now with the help of all of you) and then open the file mentioned in this line.

I tried, but this exceeds my humble knowledge of java scripting.

All help is greatly aprreciated.

Greetings from the Swiss mountains
User avatar
ArnyEinstein
Newbie
 
Posts: 9
Joined: Mon May 07, 2007 11:00 pm

Re: Getting the number and jump to a line

Postby jorrasdk » Wed May 09, 2007 8:39 am

How about this:
Code: Select all
UltraEdit.ueReOn(); /* Let's use UE's own regexp syntax */
UltraEdit.activeDocument.findReplace.regExp = true;
UltraEdit.activeDocument.top();

var regexpFind = "error on line [0-9]+ in file *$";

if(UltraEdit.activeDocument.findReplace.find(regexpFind)) {
   var lineandfile = UltraEdit.activeDocument.selection;
   
   var words = lineandfile.split(" ");
   var lineno = parseInt(words[3]); // points to lineno in array
   
   var path = lineandfile.substr(lineandfile.indexOf("in file ")+8);

   UltraEdit.open(path);
   UltraEdit.activeDocument.gotoLine(lineno,0);   
}
User avatar
jorrasdk
Master
Master
 
Posts: 275
Joined: Mon Mar 19, 2007 11:00 pm
Location: Denmark


Return to Scripts