Get current line and col from Script

Help with writing and running scripts

Get current line and col from Script

Postby HisRoyalRedness » Wed Mar 14, 2007 1:28 pm

Hi there,

Anyone know how to get the current line and column no from a script? I'm doing a find from the top of the document, and if nothing is found, I'd like to go back to the point from which I started. Maybe there's a better way then recording line and column before starting, like setting a bookamrk? Anyone know how to do this?
User avatar
HisRoyalRedness
Newbie
 
Posts: 6
Joined: Sun Mar 04, 2007 12:00 am

Re: Get current line and col from Script

Postby Bego » Wed Mar 14, 2007 1:54 pm

see online help.
togglebookmark / gotobookmark.

Or type sth. like "#myuniquemarkersign#", do your action and then make a search and replace to reposition and remove the marker.

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

Re: Get current line and col from Script

Postby HisRoyalRedness » Wed Mar 14, 2007 2:05 pm

Thanks, that will work as a second option. First prize is the get the current line number. Anyone?? The insertion of some string to search for is a bit messy...
User avatar
HisRoyalRedness
Newbie
 
Posts: 6
Joined: Sun Mar 04, 2007 12:00 am

Re: Get current line and col from Script

Postby danne » Wed Mar 14, 2007 3:48 pm

There are currently nothing of the sort available, it's been requested tho (I suggest you add your request as well so they know more like to have this feature) support@idmcomp.com

You have all the commands in the help file on a single page (Getting started -> Scripting commands)
User avatar
danne
Basic User
Basic User
 
Posts: 36
Joined: Mon Feb 07, 2005 12:00 am

Re: Get current line and col from Script

Postby HisRoyalRedness » Thu Mar 15, 2007 6:13 am

Thanks, I checked through the list, but was hoping there were a few 'undocumented' features I didn't know about. Oh well...

I'll send of the feature request right away. Thanks for the replies...
User avatar
HisRoyalRedness
Newbie
 
Posts: 6
Joined: Sun Mar 04, 2007 12:00 am

Re: Get current line and col from Script

Postby Bego » Fri Mar 16, 2007 8:53 am

Well, with an (not really nice) trick we can get the current column:

Code: Select all
function getCol() {
  var i = 0;
  while (true) {
    if (UltraEdit.activeDocument.isColNum(i)) {
       return i;
    }
    i++;
  }
  return i;
}

var iCol = 0;
iCol = getCol();
UltraEdit.activeDocument.write(String(iCol));
UltraEdit.activeDocument.write("Fertig");


VERY fast ;-)

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

Re: Get current line and col from Script

Postby Bego » Fri Mar 16, 2007 9:30 am

OK OK, This one is faster:

Code: Select all
function getCol() {
  //get the current Column.
  //speed-optimized. Hop to the right in steps. Then, when being bigger, hop back one by one.
  //Kind of "binary-tree" for fools ;-)
  var i = 1;
  var iStep = 10;
  while (true) {
    if (UltraEdit.activeDocument.isColNumGt(i)) {
       //still too much on the left side.
       i = i + iStep;
    }
    else
    {
      //hopped too far. go a little bit back
      for (var j = 0; j < iStep; j++) {
        //UltraEdit.document[1].write(String(j));
        if (UltraEdit.activeDocument.isColNum(i-j)) {
          //match !
          //UltraEdit.document[1].write("Match mit " + String(j));
          return (i-j);
        }
      }
    };
  }
  return i;
}

var iCol = 0;
iCol = getCol();
UltraEdit.activeDocument.write(String(iCol));
UltraEdit.activeDocument.write("Fertig");


Is this useful ?

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

Re: Get current line and col from Script

Postby Guest » Fri Mar 16, 2007 11:33 am

youd be best off starting at say 40 and going up/down by 20
then when you over/understep reduce the 20 to 10 and repeat and keep reducing until step size is 1, and buy the time its 1 you should have found the column.
Guest
 

Re: Get current line and col from Script

Postby HisRoyalRedness » Fri Mar 16, 2007 11:41 am

I like that! Sure, it's not the prettiest way of doing it, but it's the best so far in my very humble opinion. Thanks!

The files I'm working with are quite large though, so I think I'll start with a large number (say, 10000) and then either double or halve it until the current line is found.
User avatar
HisRoyalRedness
Newbie
 
Posts: 6
Joined: Sun Mar 04, 2007 12:00 am

Re: Get current line and col from Script

Postby Bego » Fri Mar 16, 2007 11:58 am

Yes, but implementing this (http://en.wikipedia.org/wiki/Binary_search_algorithm) would not really be faster in files (lines) usually beeing not longer than 100 chars.
Maybe somebody else wants to implement it...
And: On a binary search you need an upper limit eg. 256 or 512 chars.
Mine works also with 1234 chars.

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

Re: Get current line and col from Script

Postby HisRoyalRedness » Fri Mar 16, 2007 11:59 am

Oops! Spoke to soon :oops: The last post was a column search, not a line search...
User avatar
HisRoyalRedness
Newbie
 
Posts: 6
Joined: Sun Mar 04, 2007 12:00 am

Re: Get current line and col from Script

Postby Klaatu » Sat Mar 24, 2007 4:52 am

I like this idea, and wanted to add a CurCol property to the activeDocument object, but it doesn't seem to work. Anyone know what's wrong with the code below? I'm fairly new to JavaScript, so it's probably something I'm not understanding.
Code: Select all
function getCol() {
  //get the current Column.
  //speed-optimized. Hop to the right in steps. Then, when being bigger, hop back one by one.
  //Kind of "binary-tree" for fools ;-)
  var i = 1;
  var iStep = 10;
  while (true) {
    if (this.isColNumGt(i)) {
       //still too much on the left side.
       i += iStep;
    }
    else
    {
      //hopped too far. go a little bit back
      for (var j = 0; j < iStep; j++) {
        if (this.isColNum(i-j)) {
          return (i-j);
        }
      }
    };
  }
  return i;
}

UltraEdit.activeDocument.prototype.CurCol = getCol;


TIA
User avatar
Klaatu
Newbie
 
Posts: 5
Joined: Thu Feb 24, 2005 12:00 am
Location: Ceti-Alpha 5

Re: Get current line and col from Script

Postby Jaretin » Mon Apr 02, 2007 3:07 pm

Here a Script to get line and column.
It is not good to use with huge documents and a selected text will be lost, but just an idea.

Code: Select all
var endOfLine = "\r\n";
UltraEdit.activeDocument.selectToTop();
myLines = UltraEdit.activeDocument.selection.split(endOfLine);
line = myLines.length;
column = myLines[myLines.length -1].length + 1;
UltraEdit.getString("Line: " + line + " Column: " + column, 1);
UltraEdit.activeDocument.gotoLine(line, column);


Greetings from Hamburg.
User avatar
Jaretin
Basic User
Basic User
 
Posts: 19
Joined: Sun Mar 25, 2007 11:00 pm

Re: Get current line and col from Script

Postby Bego » Mon Apr 02, 2007 6:18 pm

Very good.
Tried it on a 1500 lines 70k TXT-doc and it was still fast.
Should do the job for me UNTIL HOPEFULLY IDM IMPLEMENTS IT.

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

Re: Get current line and col from Script

Postby Jaretin » Tue Apr 03, 2007 9:50 am

If you have tabs in your document you shoul have to use this script:

Code: Select all
var endOfLine = "\r\n";
var tab = "\t";
var spacesForTab = 2;

var column = 1;
var line;
UltraEdit.activeDocument.selectToTop();
myLines = UltraEdit.activeDocument.selection.split(endOfLine);
line = myLines.length;
tabsInLastLine = myLines[myLines.length -1].split(tab).length - 1;
column += tabsInLastLine * ( spacesForTab - tab.length ) + myLines[myLines.length -1].length;
myLines = null;
UltraEdit.activeDocument.gotoLine(line, column);
UltraEdit.getString("Line: " + line + " Column: " + column, 1);
User avatar
Jaretin
Basic User
Basic User
 
Posts: 19
Joined: Sun Mar 25, 2007 11:00 pm


Return to Scripts