Selection only returns first character

Selection only returns first character

4
NewbieNewbie
4

    Jul 31, 2007#1

    Hello everybody!

    The script below is supposed to do the following: read a line number and a tag number. Then it should print all tags in that specific line until the tag number is reached.

    Code: Select all

    var i_LineNum = UltraEdit.getValue("Goto line number?", 1);
    var i_TagNum = UltraEdit.getValue("Goto tag number?", 1);
    
    UltraEdit.ueReOn();
    
    with (UltraEdit.activeDocument) {
    	gotoLine(i_LineNum);
    	for (i = 0; i < i_TagNum; i++) {
    		findReplace.regExp = true;
    		findReplace.find("<*>");
    		UltraEdit.outputWindow.write(selection);
    	}
    	findReplace.regExp = false;
    }
    
    The loop is working correctly as well as the text selection in the document window. However, the output window only displays the first character of the selection, i.e. the "<".

    My first attempt was using matchBrace() instead of findReplace. I also put startSelect() and endSelect() around the find command. The result stays the same.

    What am I missing here?

    TIA for your help!
    Uli
    Using UE 13.10 on WinXP Pro SP 2

    6,682583
    Grand MasterGrand Master
    6,682583

      Jul 31, 2007#2

      What about using UltraEdit.activeDocument.selection instead of selection?
      Best regards from an UC/UE/UES for Windows user from Austria

      4
      NewbieNewbie
      4

        Jul 31, 2007#3

        Hi Mofi,

        I tried that but I doesn't change anything. So, what now?

        Anyway, I thought that through the with-command it was specified that the selection should be taken from the active document.

        Best,
        Uli
        Using UE 13.10 on WinXP Pro SP 2

        6,682583
        Grand MasterGrand Master
        6,682583

          Jul 31, 2007#4

          Sorry, my mistake. I have not seen the with command. Your code is absolutely right. However, I have now tried following:

          Code: Select all

          UltraEdit.ueReOn();
          
          /*
          <Tag1> <Tag2>
          */
          
          with (UltraEdit.activeDocument) {
             top();
             findReplace.regExp = true;
             findReplace.find("<*>");
             UltraEdit.outputWindow.write(selection);
             findReplace.regExp = false;
          }
          and it writes <Tag1> into the output window. I have executed this test script with UltraEdit v13.10a+1. And also your script worked.

          If you really use v13.10 update and try again.
          Best regards from an UC/UE/UES for Windows user from Austria

          4
          NewbieNewbie
          4

            Jul 31, 2007#5

            Hi Mofi,

            I tried your script and everything works fine.

            That gave me the idea that my problem may be related to the file encoding. My files are Unicode and then the script does not work no matter if it is UTF-16 or UTF-8. If I convert them to ASCII then the script works.

            I would appreciate if you could double check that.

            Best,
            Uli
            Using UE 13.10 on WinXP Pro SP 2

            6,682583
            Grand MasterGrand Master
            6,682583

              Jul 31, 2007#6

              That's the problem. Strings are NULL terminated and a Unicode string contains lots of NULLs (for ASCII characters every second byte). So you need a Unicode to ASCII conversion. So your script works currently for ASCII/ANSI files, but not for a Unicode file. I have verified it.
              Best regards from an UC/UE/UES for Windows user from Austria

              2
              NewbieNewbie
              2

                Aug 14, 2007#7

                man, i've got thru exactly the same problem,
                yet there's 1 more thing to consider if you planning your scripts run more than once on the same document.

                Since the UltraEdit.activeDocument.unicodeToASCII() function can be
                invoked repeatedly with scripting feature, not likely on Uedit App menu,
                you'll get the characters messed up eventually.


                Here's the solution.
                just call a dummy ASCIIToUnicode() function in a pair.

                Code: Select all

                UltraEdit.activeDocument.ASCIIToUnicode();
                UltraEdit.activeDocument.unicodeToASCII();
                It's a bit weird that there's no read-only status variable for files' encoding =$

                4
                NewbieNewbie
                4

                  Aug 14, 2007#8

                  Thanks a lot for the tip. That's the trick I was looking for!
                  Using UE 13.10 on WinXP Pro SP 2

                  6,682583
                  Grand MasterGrand Master
                  6,682583

                    Aug 14, 2007#9

                    in0de wrote:It's a bit weird that there's no read-only status variable for files' encoding =$
                    I have sent following per email to IDM on Mon, 04 Jun 2007:
                    Subject: UE1310B2 - Request for additional script document properties

                    UltraEdit v13.10 Beta 2 has some very important new document properties. But in my point of view the 2 most important document properties are not available for script programers. Since years and after writing dozens of macros for other users the main problem for macros and probably also scripts is the file format (encoding) and the line termination format.

                    So I would like to see following document object scripting properties:

                    var file_encoding = UltraEdit.activeDocument.format;

                    or

                    var file_encoding = UltraEdit.activeDocument.encoding;

                    Return values:

                    0 ... ASCII/ANSI
                    1 ... EBCDIC
                    2 ... UTF-8
                    3 ... UTF-16 LE
                    4 ... UTF-16 BE
                    5 ... UTF-16 ASCII Escaped Unicode

                    Files in the last 4 formats are always opened by UltraEdit in UTF-16 LE format (Unicode) and so I would use in scripts often simply

                    if (file_encoding >= 2) { ... }

                    because often the specific Unicode format is not important, only if the file is an ASCII/ANSI file or a Unicode file. Well, with these return values and for most users like I which have never seen an EBCDIC file I could use in the scripts also

                    if (file_encoding)
                    {
                    // excecute Unicode specific commands
                    }
                    else
                    {
                    // excecute plain text specific commands
                    }


                    The second property should be something like:

                    var lineterm = UltraEdit.activeDocument.lineTermination;

                    or

                    var lineterm = UltraEdit.activeDocument.lineEnding;

                    Return values:

                    0 ... UNIX
                    1 ... MAC
                    2 ... DOS
                    3 ... UNIX but converted temporarily to DOS
                    4 ... MAC but converted temporarily to DOS

                    or if the last 2 items cannot be returned:

                    0 ... DOS
                    1 ... UNIX
                    2 ... MAC
                    If you want that too, sent also a feature request email to IDM support. The more users require it, the higher the priority for the IDM developers to code it.
                    Best regards from an UC/UE/UES for Windows user from Austria

                    262
                    MasterMaster
                    262

                      Aug 14, 2007#10

                      I have also this moment requested these properties. Good suggestion.

                      I have a workaround I use every now and then for the line ending stuff. I often need to append line endings to new lines and need to now if these should be DOS or UNIX. So I use this simple function in the top of my script:

                      Code: Select all

                      var CRLF = getActiveDocumentLineTerm();
                      
                      /* write readable line endings to output window */
                      UltraEdit.outputWindow.write("Line terminators in file: "+CRLF.replace(/\r/,"CR").replace(/\n/,"LF"));
                      
                      function getActiveDocumentLineTerm() {
                      	/* Returns line terminators of active file as string */
                      	UltraEdit.activeDocument.top();
                      	UltraEdit.activeDocument.selectLine(); /* select line always captures line terminators (if any) */
                      
                      	return UltraEdit.activeDocument.selection.replace(/^.*([\r\n]*)$/g, "$1");
                      }
                      also a variant of this could come close to Mofis suggested property:

                      Code: Select all

                      var linetermType = activeDocumentLineTerm();
                      var lineterms = ["DOS", "UNIX", "MAC"];
                      UltraEdit.outputWindow.write("File type: "+lineterms[linetermType]);
                      
                      function activeDocumentLineTerm() {
                      	/* Returns code for line terminators of active file:
                      	-1 = Unable to resolve line terminators for file
                      	0 = DOS
                      	1 = UNIX
                      	2 = MAC
                      	*/
                      	UltraEdit.activeDocument.top();
                      	UltraEdit.activeDocument.selectLine(); 
                      
                      	return UltraEdit.activeDocument.selection.replace(/^.*([\r\n]*)$/g, function($1,$2) {
                      		switch ($2) {
                      			case "\r\n": return 0; /* DOS */
                      			case "\n": return 1; /* UNIX */
                      			case "\r":  return 2; /* MAC */
                      			default : return -1;
                      		}
                      	});
                      }
                      Warning: This doesn't work with UTF files because activeDocument.selection chokes on the 00-escaped strings.

                      1

                        Aug 31, 2007#11

                        Wouldn't a better solution be to have the <selection> property always convert the document's text to the JavaScript standard of the Unicode codepage? Thus, it would avoid all of this explicit codepage handling.

                        The opposite conversion should occur when the script puts characters into the document -- It should convert it from the script's Unicode to the document's codepage.

                        So, you could look at the initial problem (that <selection> was only returning a single character) is not due to embedded NUL characters -- It's due to a missing conversion from the document's codepage to JavaScript's Unicode.

                        If you add this conversion into UltraEdit, then every script will work with every codepage, and no explicit conversion will ever be necessary.

                        4
                        NewbieNewbie
                        4

                          Mar 10, 2008#12

                          ReadJohn317 is absolutely right.

                          I have tried to get the following script to work without success:


                          *PLEASE* fix this fast so we can apply scripts to UTF-8 files.

                          Having to manually switch from UTF-8 to ASCII and back is a real pain in the ass :cry:

                          262
                          MasterMaster
                          262

                            Mar 10, 2008#13

                            tulpe wrote:*PLEASE* fix this fast so we can apply scripts to UTF-8 files.
                            Hello tulpe. We - the users of UE - cannot fix this for you. This is a user-to-user forum. Write to IDM directly. See more at the top of the page.

                            4
                            NewbieNewbie
                            4

                              Mar 10, 2008#14

                              But.. you.. I mean.. whoops :)