Get current line and col from Script

Get current line and col from Script

6

    Mar 14, 2007#1

    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?

    344
    MasterMaster
    344

      Mar 14, 2007#2

      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
      Normally using all newest english version incl. each hotfix. Win 10 64 bit

      6

        Mar 14, 2007#3

        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...

        36
        Basic UserBasic User
        36

          Mar 14, 2007#4

          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) [email protected]

          You have all the commands in the help file on a single page (Getting started -> Scripting commands)

          6

            Mar 15, 2007#5

            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...

            344
            MasterMaster
            344

              Mar 16, 2007#6

              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

                Mar 16, 2007#7

                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
                Normally using all newest english version incl. each hotfix. Win 10 64 bit

                Guest
                Guest

                  Mar 16, 2007#8

                  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.

                  6

                    Mar 16, 2007#9

                    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.

                    344
                    MasterMaster
                    344

                      Mar 16, 2007#10

                      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
                      Normally using all newest english version incl. each hotfix. Win 10 64 bit

                      6

                        Mar 16, 2007#11

                        Oops! Spoke to soon :oops: The last post was a column search, not a line search...

                        8
                        NewbieNewbie
                        8

                          Mar 24, 2007#12

                          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

                          21
                          Basic UserBasic User
                          21

                            Apr 02, 2007#13

                            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.

                            344
                            MasterMaster
                            344

                              Apr 02, 2007#14

                              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
                              Normally using all newest english version incl. each hotfix. Win 10 64 bit

                              21
                              Basic UserBasic User
                              21

                                Apr 03, 2007#15

                                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);