Behavior of .selectWord() or is it the cursor position?

Help with writing and running scripts

Behavior of .selectWord() or is it the cursor position?

Postby danne » Wed Apr 09, 2008 6:57 am

Has anyone had selectWord act strangely since the last update (I am not really sure if that's when it started, can't go back and check tho).

Anyways, I made a script for checking the text at the cursor to see if it matches any of my predefined texts (had an old macro for this before, but it's ways faster with JS).

ie, like this (cut down version, but it's basically it, only removed unimportant function definitions):

Code: Select all
setUpEnv();

var word = UltraEdit.activeDocument.selectWord();
word = UltraEdit.activeDocument.selection;

switch (word)
{
    case "dvc":
        UltraEdit.activeDocument.deleteText();
        UltraEdit.activeDocument.insertTemplate(24);
        break;
    case "dvi":
        UltraEdit.activeDocument.deleteText();
        UltraEdit.activeDocument.insertTemplate(28);
        break;
    case "dvl":
        UltraEdit.activeDocument.deleteText();
        UltraEdit.activeDocument.insertTemplate(29);
        break;

    .
    .
    .
    .
   

    default:
        var ColNum = UltraEdit.activeDocument.currentColumnNum;
        if (typeof(UltraEdit.activeDocumentIdx) == "undefined") ColNum++;
        UltraEdit.activeDocument.gotoLine(UltraEdit.activeDocument.currentLineNum, ColNum); // reset selection
}

function setUpEnv()
{
    UltraEdit.insertMode();
    UltraEdit.columnModeOff();
    UltraEdit.activeDocument.hexOff();
    UltraEdit.ueReOn();
}


It just checks the text at the cursor and inputs the correct 'snippet' (template in this case) instead of the text. I invoke the script by pressing shift + space.

Now, here's the thing(s):
sometimes it seems the cursor position gets completely off, especially in conjunction with undo, results in selectWord to show contents I have no idea where it found (often chars with a value < 10 (00-0F) )

[$replace$] doesn't seem to work as it used to when inserting from JS (the indentation is completely ignored, which I think wasn't the case previously).

The first I solved with doing this before invoking selectWord();
UltraEdit.activeDocument.gotoLine(0,UltraEdit.activeDocument.currentColumnNum);

The second problem I have no idea as to how to get around, anyone has any idea? (I have a version without templates where I fix the indentation right in the script, but it'd be tedious to do for all templates)
User avatar
danne
Basic User
Basic User
 
Posts: 36
Joined: Mon Feb 07, 2005 12:00 am

Re: Behavior of .selectWord() or is it the cursor position?

Postby Mofi » Thu Apr 10, 2008 2:47 am

First correct

var word = UltraEdit.activeDocument.selectWord();
word = UltraEdit.activeDocument.selection;


to

UltraEdit.activeDocument.selectWord();
var word = UltraEdit.activeDocument.selection;


Second if you use UltraEdit.activeDocument.deleteText(); to delete the current selection [$replace$] in the template will never work because nothing is selected.

I have not run a test script to verify if these changes solve your problem.
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4066
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: Behavior of .selectWord() or is it the cursor position?

Postby danne » Thu Apr 10, 2008 8:44 am

Mofi wrote:First correct

var word = UltraEdit.activeDocument.selectWord();
word = UltraEdit.activeDocument.selection;


to

UltraEdit.activeDocument.selectWord();
var word = UltraEdit.activeDocument.selection;



Sorry, that's what I have written, seems I messed up while cleaning out the code before pasting to the forum. (That change shouldn't make a difference tho, haven't tried the code in red tho).

Mofi wrote:Second if you use UltraEdit.activeDocument.deleteText(); to delete the current selection [$replace$] in the template will never work because nothing is selected.

I have not run a test script to verify if these changes solve your problem.


After reading your second comment, I fixed the problem. The wanted effect of [$replace$] was reached by doing the following:

Code: Select all
    case "mes":
        UltraEdit.activeDocument.deleteToEndOfLine();
        UltraEdit.activeDocument.selectWord();
        UltraEdit.activeDocument.insertTemplate(33);
        break;


Thanks for the input Mofi!

Still have to use the workaround to get .selectWord() to always do what it should :|
User avatar
danne
Basic User
Basic User
 
Posts: 36
Joined: Mon Feb 07, 2005 12:00 am


Return to Scripts